Problem 2.2

set.seed(123)
s <- 100000
Y.reg <- 563  # The number of home-runs over his career in regular season
n.reg <- 2820 # The number of games played over his career in regular season
theta.reg <- rbeta(s,Y.reg+1,n.reg-Y.reg+1) 
hist(theta.reg,breaks=100, xlab = expression(theta), ylab = "Posterior of regular season")

set.seed(123)
s <- 100000
Y.world <- 10 # The number of home-runs over his career in World series
n.world <- 27 # The number of games played over his career in World series
theta.world <- rbeta(s,Y.world+1,n.world-Y.world+1)
hist(theta.world,breaks = 100, xlab = expression(theta), ylab = "Posterior of world series")

mean(theta.reg<theta.world)
## [1] 0.98465

It concludes that Reggie Jackson performs better in the world series.


Problem 2.3

The likelihood is \(f(Y|\theta) = \theta exp(-\theta Y)\). In order to be a conjugate prior, the prior distribution should be one of parameter family following the same distribution of a posterior distribution. Therefore, a conjugate prior is \(\theta \sim {\sf Gamma}(a, b)\). The resulting posterior is, \(p(\theta |Y) \propto f(Y|\theta)\pi(\theta) \propto [\theta exp(-\theta Y)] * [\theta ^{a}exp(-\theta b)] \propto \theta^{(a+1)-1}exp[-\theta (Y+b)]\). Therefore, $p(|Y) Gamma(a+1,Y+b) $


Problem 2.5

Select a and b to get the median around 75 and the variance is \(a/b^{2}\) around 100, which is equivalent to \(a = 100*b^{2}\).

b_grid = seq(0.0001,1,0.0001)
median = qgamma(0.5,100*b_grid^2, b_grid)
b = b_grid[which.min(abs(median-75))]
a = 100*b^2
a
## [1] 56.91194
b
## [1] 0.7544

Plot the medians to find out the obtimal b value.

plot(b_grid,median)
abline(v=b)

With a and b selected above, plot all lamda’s to check the pattern.

lambda = rgamma(1000000,a,b)
hist(lambda, breaks=100)

The statistics of is below,

var(lambda) # variance of lambda
## [1] 99.87039
mean(lambda>75) # mean of lambda
## [1] 0.500278