# Final - 1 Problem Assume that you can generate beta...

This preview shows pages 1–2. Sign up to view the full content.

1 Problem Assume that you can generate beta distribution (using R function rbeta), implement Ahrens and Dieter’s algorithm for generating binomial random variate by writing an R function. Test on various values of K and make a recommendation with respect to the language (R, that is) and to the computer you use. You can use R graphics to help you explain your choice of the value of K. 2 Solution First, I write code to generate a binomial random variable. > # Generate one replicate > ad.bin <- function(k=100, K=50, theta=0.5){ + if(K > k | k <= 0 | K <= 0) stop("Error: K > k or k < 0 or K < 0") + X=0 + while(k <= K) { + i=trunc(1+k*theta) + v <- rbeta(1, i, k+1-i) + if (theta < v){ + theta <- theta/v + k = i - 1 + } else { + X <- X + i + theta <- (theta-v)/(1-v) + k <- k - i + k=k+1 + }} + u <- runif(k) + return(X+length(u[u<theta])) + } Second, I write code to generate multiple binomial random variables. > # Generate several replicates > r.bin <- function(sampl.size, size, effi.par, prob){ + bin.rv<-c(rep(0,sampl.size)) + for (i in 1:sampl.size){ + bin.rv[i] <- ad.bin(size, effi.par, prob) + } + return(bin.rv) + } 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Third, I employ the functions to evaluate K by using graphs. The figures indicate the fit
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern