Section 7 - Tutorial R and In-Class Questions R Flashcards
Q10 A researcher is studying survival for patients with a rare autoimmune disease. The probability of death due to the disease is given by π, which is assumed to have a
beta distribution with parameters 5 and 8.
In order to estimate π, the researcher analyses a sample of π = 20 people.
The Bayesian estimate for π under quadratic loss, given π₯ deaths in a sample size of π is:
π₯ + π
π + π + π
i) Obtain 1,000 simulations of the posterior probability of death, based on
1,000 samples each of 20 people. Hence, obtain an average empirical estimate for π under quadratic loss.
Use the function set.seed(31) so that your answer is reproducible.
The Bayesian estimate for π under all-or-nothing loss, given π₯ deaths in a sample
size of π is:
π₯ + π β 1
π + π + π β 2
ii) Obtain 1,000 simulations of the posterior probability of death, based on
1,000 samples each of 20 people. Hence, obtain an average empirical estimate for π under all-or-nothing loss.
Use the function set.seed(31) so that your answer is reproducible.
The Bayesian estimate for π under absolute loss, given π₯ deaths in a sample size of π is the median of the posterior distribution π΅ππ‘π(π₯ + π, π β π₯ + π).
iii) Obtain 1,000 simulations of the posterior probability of death, based on
1,000 samples each of 20 people. Hence, obtain an average empirical estimate for π under absolute loss.
Use the function set.seed(31) so that your answer is reproducible.
Q10
i) 0.39118
ii) 0.38416
iii) 0.38896
Or, using alternative method:
i) 0.38518
ii) 0.37777
iii) 0.38284
#######################################################################
####### Section 7 tutorial #######
#######################################################################
Question 10
define parameters
sample size n=20
# prior distribution beta(a,b) = beta(5,8)
n <- 20
a <- 5
b <- 8
(i) Quadratic loss
postmean <- rep(0,1000)
set.seed(31)
for (i in 1:1000)
{p <- rbeta(1,a,b)
x <- rbinom(1,n,p)
postmean[i] <- (x+a)/(a+b+n)}
mean(postmean)
(ii) 0-1 loss
postmode <- rep(0,1000)
set.seed(31)
for (i in 1:1000)
{p <- rbeta(1,a,b)
x <- rbinom(1,n,p)
postmode[i] <- (x+a-1)/(a+b+n-2)}
mean(postmode)
(ii) absolute loss
postmed <- rep(0,1000)
set.seed(31)
for (i in 1:1000)
{p <- rbeta(1,a,b)
x <- rbinom(1,n,p)
postmed[i] <- qbeta(0.5,x+a,n-x+b)}
mean(postmed)
###########################################################################
Alternative method
### using rbinom(n,1,p) and sum(x)
altpostmean <- rep(0,1000)
set.seed(31)
for (i in 1:1000)
{p <- rbeta(1,a,b)
x <- rbinom(n,1,p)
altpostmean[i] <- (sum(x)+a)/(a+b+n)}
mean(altpostmean)
(ii) 0-1 loss
altpostmode <- rep(0,1000)
set.seed(31)
for (i in 1:1000)
{p <- rbeta(1,a,b)
x <- rbinom(n,1,p)
altpostmode[i] <- (sum(x)+a-1)/(a+b+n-2)}
mean(altpostmode)
(ii) absolute loss
altpostmed <- rep(0,1000)
set.seed(31)
for (i in 1:1000)
{p <- rbeta(1,a,b)
x <- rbinom(n,1,p)
altpostmed[i] <- qbeta(0.5,sum(x)+a,n-sum(x)+b)}
mean(altpostmed)
Exam standard question: CS1 B April 2019 Q2
Q11 Consider the π = 30 independent and identically distributed observations
(π¦1, π¦2, β¦ , π¦π) given below from a random variable π with probability distribution
function π(π¦, π) = π^π¦*π^βπ/π¦! .
You can enter the π¦ values into R by using:
y=c(5,5,6,2,4,10,2,5,5,2,5,3,7,4,4,5,4,6,7,2,8,4,6,4,3,6,6,6,5,7)
By assuming a prior distribution proportional to πβπΌπ, we can show that the
posterior distribution of π is:
π(π|π¦1, π¦2, β¦ , π¦π) β π^βπ¦π *π^β(π+πΌ)π
We can observe that the posterior distribution of π is Gamma with parameters
π,π=1β π¦,π β 1 and π + πΌ.
i)
a) Plot the posterior probability density function of π for values of π in
the interval [3.2, 6.8] and assuming πΌ = 0.01.
Hint: Consult your FIN2017 notes for a reminder on plotting density
functions.
The range of values of π can be obtained in R by seq(3.2, 6.8,by=0.01).
b) Carry out a simulation of π = 5,000 posterior samples for the parameter π.
ii) Plot the histogram of the posterior distribution of π using your simulated
values.
iii) Calculate the mean, median and standard deviation of your simulated
values of π.
Two possible values for the true value of the parameter π are π = 15 and π = 5.
iv) Comment on these two values based on your answers in parts (ii) and (iii).
Q11
iii) Mean: 4.8988
Median: 4.8814
Standard deviation: 0.4065
Note, no seed set so answers will vary
15 is quite far away from the range of samples obtained for the posterior
distribution of ΞΈ. [1]
On the other hand 5 is more likely to be the true value. [1]
15 is very unlikely to be the case if there is no calculation error.
Exam standard question β IfoA Curriculum 2019 CS1 Sample Paper
Q12 A Bayesian credibility model is used to model annual claim numbers, denoted by π, for the coming year. These are assumed to have a Poisson distribution with mean π, where π itself is modelled by a gamma distribution with parameters πΌ = 100 and π½ = 1.
(i)
a) Implement π = 1000 Monte Carlo repetitions of a credibility analysis to estimate the distribution of the posterior mean of parameter π using the credibility factor π = 1/( π½ + 1), in the case where the number of past claims π₯ is known only for the last one year.
b) Provide the histogram of the 1000 Monte Carlo posterior mean estimates calculated in part (i)(a).
[15]
(ii)
a) Calculate the mean and variance of the Monte Carlo posterior mean estimates from part (i).
b) Compare the Monte Carlo mean and variance obtained in part (ii)(a) with those obtained from samples of size 1,000 drawn from a πΊππππ(πΌ + π₯, π½ + 1) distribution. Round your results to three decimal places.
[12]
(iii) Comment on your findings in parts (i) and (ii).
[3]
[Total 30]
#######################################################################
####### Section 7 tutorial #######
#######################################################################
Question 12 - IFoA Solution
(i)
#(a)
M <- 1000
alpha <- 100
beta <- 1
Z <- 1/(beta+1)
pm <- X <- numeric(M)
for(m in 1:M){
lam = rgamma(1, shape=alpha, rate=beta)
x = rpois(1, lam)
X[m] = x
pm[m] = Zx +(1-Z)alpha/beta
}
help(rgamma)
# (b)
hist(pm, main=βHistorgram of posterior meansβ,
xlab=βPosterior meanβ, ylab=βFrequencyβ)
(ii)
(a)
round(mean(pm),3)
round(var(pm),3)
(b)
mG <- numeric(M)
for(m in 1:M){
lam = rgamma(1, shape=alpha, rate=beta)
x = rpois(1, lam)
y = rgamma(1000, shape = alpha+x, rate = beta+1)
mG[m]=mean(y)
}
round(mean(mG), 3)
round(var(mG),3)
(ii) MC mean and variance of posterior mean estimates: 99.904, 51.167
MC mean and variance of posterior from Gamma samples: 99.900,
51.238
Note that this solution to part (ii) uses a new set of Monte Carlo
repetitions. This is not necessary, and full credit can be given for
combining parts (i) and (ii) in a single exercise. Clearly the precise
numerical values for the means and variances will differ from
implementation to implementation.
(iii) The similarity of the Monte Carlo estimates of the mean and variance and
those from the Gamma(Ξ± + π₯, Ξ² + 1) sample demonstrates that the
posterior Distribution for the Poisson/Gamma credibility model is the
Gamma(Ξ± + π₯, Ξ² + 1).