{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# 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 ﬁgures indicate the ﬁt
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 2

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

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online