Bayesian Inference and computational methods – Practical 1 Solutions including R code for implementing these 1. Simulating random sample of size 1000 from N(0, 1) x = runif(1000, 0, 1) y = runif(1000, 0, 1) z = sqrt(-2.0*log(x))*cos(2.0*pi*y) hist(z) 2. Converting random sample of N(0, 1) to N(10, 10) z10 = sqrt(10)*z + 10.0 hist(z10) Here z refers to the sample generated in question 1. 3. Uniform(-1, 1) x = 2.0*runif(1000, 0, 1) - 1 4. Simulating Exp(2) by inversion of c.d.f. z = runif(1000, 0, 1) #random sample from U(0, 1) x = -0.5*log(1 - z) #transformation by inversion of c.d.f. [Note x = -0.5*log(z) also gives sample Exp(2) since 1-z and z are both i.i.d. U(0, 1)] 5. Simulating 1000 draws from Gamma(3, 2). This can be done by adding together 3 independent Exp(2) random variables. Therefore z = -0.5*(log(runif(1000, 0, 1)) + log(runif(1000, 0, 1)) + log(runif(1000, 0, 1))) or z = -0.5*(log(runif(1000, 0, 1)*runif(1000, 0, 1)*runif(1000, 0, 1))) do the job. 6. Code for simulating from Poisson() using Poisson process with rate 1. Algorithm counts number of events occurring within time window of length . for(i in 1:1000) { k=0 sum = 0.0 while(sum < lam) { sum = sum – log(runif(1, 0, 1)) k = k+1 } z[i] = k-1 } Code for inversion of cdf for Poisson distribution. for(i in 1:1000){ k=0 q = runif(1, 0, 1) while(ppois(k, lam) < q) k = k+1 z[i] = k } 7. Simulate r.s. of size 1000 from Bin(12, 0.5) Various possibilities exist. One way to do it is to simply add together independent Bernoulli(0.5) distributions. for(i in 1:1000) { sum = 0 for(j in 1:12) sum = sum + trunc(runif(1, 0, 1)+0.5) z[i] = sum } 8. Simulating throwing 10 fair dice for(i in 1:n) { sum = 0 for(j in 1:10) sum = sum + trunc(6*runif(1, 0, 1)) + 1 z[i] = sum } 9. Simply use recipe from question 1 to generate 2 independent i.i.d. samples of size 1000 from N(0, 1), then use formulae from hint on question sheet. 10. Rejection sampling from Beta(2, 2) using q(x) = 1, p(x) = 6x(1-x). Note that p/q < 3/2. It follows that acceptance probability is 4x(1-x) (check this). k=1 while(k<1001) { u = runif(1, 0, 1) if(runif(1, 0, 1) < 4*u*(1 - u)) { z[k] = u k = k+1 } } Inversion of c.d.f. for this case requires solving a cubic equation as described in solutions to Chapter 4, Q. 3. To obtain sample from Beta(2.2, 2.2) you can use the z generated above and apply rejection sampling to it. Now q(x) corresponds to the Beta(2.2) density. It follows that B(2, 2) px x 0.2 (1 x) 0.2 K 0.25 0.2 c q x B2.2, 2.2 Therefore the acceptance ratio for rejection sampler (p/(cq)) is given by 40.2x0.2(1-x)0.2 We can obtain our random sample as: newz = c(1:1000) const = 4^0.2 k=1 for(i in 1:1000) { if(runif(1, 0, 1) < const*z[i]^0.2*(1 - z[i])^0.2) { newz[k] = z[i] k = k+1 } } On competing this loop k-1 tells us how many samples were generated. Note that k-1 will (almost certainly) be less than 1000 since some of the z's will be rejected.