<- 1
a <- 1
b <- c(rep(0,2),rep(1,6),rep(2,7),rep(3,16),rep(4,11),rep(5,6),rep(6,2))
y <- sum(y)
s_n <- length(y)
n
<- seq(0,5,by = 0.01)
theta <- sapply(theta,function(thet){
ll prod(dpois(y,thet))
})#scalling likelihood to be visible in plot
<- max(dgamma(theta,a+s_n,b+n))/max(ll)
M plot(theta,dgamma(theta,a+s_n,b+n),type = 'l',col = 'red',lwd = 2,
xlab = expression(theta),
ylab = 'Density')
lines(theta,dgamma(theta,a,b),col = 'blue',lwd = 2)
lines(theta,ll*M,col = 'orange',lwd = 2)
legend('topright',legend = c('Posterior',' Gamma Prior','Scaled Joint-Likelihood'),col = c('red','blue','orange'),lwd = 2)
12 Exercises
12.0.1 Exercise 1
Poisson model has mass function
\[ f_Y(y;\theta) = \frac{\theta^yexp\{-\theta\}}{y!} \]
for \(y \in \mathbb{Z}^+\)
Explore the posterior density \(\pi_n(\theta)\) using two priors
\(Gamma(\alpha_0,\beta_0)\)
prior determined by the assumption that \(\phi = log\theta \sim Normal(\eta_0,\tau_0^2)\) a priori. In other words, that the prior is a log-normal distribution
We have the following observations
y | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
Count | 2 | 6 | 7 | 16 | 11 | 6 | 2 |
Assuming that the likelihood is independent given \(\theta\) (de Finetti), multiplying the joint likelihood by the Gamma prior gives us a Gamma posterior with observation-dependent parameters (due to gamma-poisson conjugacy - see Conjugate Distributions).
\[ \pi_n(\theta) \sim Gamma(\alpha_0 + s_n,\beta_0 + n) \\ s_n = \sum^n_{i=1}y_i \]
We can now plot the posterior over all possible \(\theta \in \Theta\).
<- 0
eta <- 10
tau #Can treat the joint-likelihood as a gamma dist with a = sn and b = n to avoid need for individual observations
<- function(eta,tau,theta,sn,n){
lnorm_pois <- dgamma(theta,sn,n)*dnorm(log(theta),eta,tau)
calc return(calc)
}<- integrate(lnorm_pois,eta = eta,tau =tau,sn = s_n,n = n,lower = 0, upper = 10)
norm_const
<- max(lnorm_pois(eta,tau,theta,s_n,n)/norm_const$value)/max(ll)
M <- max(lnorm_pois(eta,tau,theta,s_n,n))/max(dnorm(log(theta),eta,tau))
S plot(theta,lnorm_pois(eta,tau,theta,s_n,n)/norm_const$value,type = 'l',col = 'red',lwd = 2,
xlab = expression(theta),
ylab = 'Density')
lines(theta,dnorm(log(theta),eta,tau)*S,col = 'blue',lwd = 2)
lines(theta,ll*M,col = 'orange',lwd = 2)
legend('topright',legend = c('Posterior',' Log-Norm Prior','Scaled Joint-Likelihood'),col = c('red','blue','orange'),lwd = 2)