3 min read

Bayesian Normal Model

Inference for unknown mean and known variance

For a sampling model Y|μ,σ2iidN(μ,σ2) we wish to make inference on μ assuming σ2 is known. The first step will be to derive an expression for π(μ|Y=y,σ2) which is proportional to π(μ|σ2)π(y|μ,σ2). To following prior structure will be adopted:

Y|μ,σ2N(μ,σ2)

μN(μ0,σ02)

Now we can derive an expression for π(μ|Y=y,σ2)π(μ|σ2)π(y|μ,σ2).

π(μ|σ2)π(y|μ,σ2)e12σ02(μμo)2e12σ2(yμ)2

=e12(1σ02(μ22μμ0+μ02)+1σ2(y22μy+μ2))

Now let’s just work with the 1σ02(μ22μμ0+μ02)+1σ2(y22μy+μ2) by itself.

1σ02(μ22μμ0+μ02)+1σ2(y22μy+μ2)=μ2σ022μμ0σ02+μ02σ02+y2σ22μyσ2+μ2σ2

=μ2(1σ02+1σ2)2μ(μ0σ02+yσ2)+μ02σ02+y2σ2

To get this final expression into a quadratic form we can let a=1σ02+1σ2, b=μ0σ02+yσ2, and c=μ02σ02+y2σ2. In proportionality the c term will drop from the exponent. Now we have

π(μ|σ2,y)e12(aμ22bμ)=e12a(μ22bμ/a+b2/a2)+b2/2a

=e12a(μb/a)2=e12(μb/a1/a)2

Which is recognizable as the kernel of a N(b/a,1/a)=N(μn=μ0σ02+yσ21σ02+1σ2,σn2=11σ02+1σ2).

Inference for unknown mean and unknown variance

It is more reasonable to expect that in an actual data analysis if we are trying to estimate the mean of a normal population then we likely need to estimate the variance as well. The following parameterization is used by Hoff in A First Course in Bayesian Statistical Methods.

1/σ2gamma(shape=ν02,rate=ν0σ022)

μ|σ2N(μ0,σ2κ0)

Y1,...,Yn|μ,σ2iidN(μ,σ2)

The posterior joint distribution of μ,σ2 after observing the data Y1,...,Yn is given by the following product.

π(μ,σ2|y1,...,yn)=π(μ|σ2,y1,...,yn)π(σ2|y1,...,yn)

This first term π(μ|σ2,y1,...,yn) is the conditional posterior distribution of μ. Since it is conditional on σ2 (i.e. σ2 is “known”) it can be derived with a calculation identical to the one given in the previous section. Generalizing to a sample of size n instead of size 1 and substituting σ2/κn for σ02 we get that

μ|y1,...,yn,σ2N(μn=κ0μ0+ny¯κn,σ2κn)

where κn=κ0+n. Hoff’s parameterization lends itself to a nice interpretation of μ0 and the mean of κ0 prior observations and μn as the mean of κ0 prior observations and n new observations all together. Also, the variance of μ|y1,...,yn,σ2 is σ2/κn.

Now to get the posterior distribution of σ2 after observing y1,...,yn we need to integrate over all possible values of μ according to

π(σ2|y1,...,yn)π(σ2)π(y1,...,yn|σ2)

=π(σ2)μπ(y1,...,yn|μ,σ2)π(μ|σ2)dμ

=π(σ2)μ(12πσ2)n/2e(yiμ)22σ2(12πσ2/κ0)1/2e(μμ0)22σ2/κ0dμ

Eventually, we get that σ2|y1,...,ynIG(νn/2,νnσn2/2) or alternatively 1/σ2|y1,...,ynGamma(νn/2,νnσn2/2), where

νn=ν0+n

σn2=1νn[ν0σ02+(n1)s2+κ0nκn(y¯μ0)2]

We get another nice interpretation here of ν0 as the prior sample size with corresponding sample variance σ02. Also s2 is the sample variance and (n1)s2 is the sample sum of squares.

normR

# devtools::install_github("carter-allen/normR")
library(normR)

Monte Carlo Sampling

Example from Hoff pg. 76.

# parameters for the normal prior
mu_0 <- 1.9
kappa_0 <- 1

# parameters for the gamma prior
sigsq_0 <- 0.010
nu_0 <- 1

# some data 
ys <- c(1.64,1.70,1.72,1.74,1.82,1.82,1.82,1.90,2.08)
n <- length(ys)
y_bar <- mean(ys)
ssq <- var(ys)

# posterior parameters
kappa_n <- kappa_0 + n
nu_n <- nu_0 + n
mu_n <- mu_n(kappa_0,mu_0,n,y_bar)
sigsq_n <- sigsq_n(nu_0,sigsq_0,n,ssq,kappa_0,y_bar,mu_0)
mu_n
## [1] 1.814
sigsq_n
## [1] 0.015324
# posterior variance draws
sigsq_posterior_draw <- draw_sigsq_posterior(10000,nu_n,sigsq_n)
# posterior mean draws
mu_posterior_draw <- draw_mu_posterior(mu_n,sigsq_posterior_draw,kappa_n)
quantile(mu_posterior_draw,c(0.025,0.975))
##     2.5%    97.5% 
## 1.727323 1.899154
# plot posterior draws
normR::plot_monte_sim(sigsq_posterior_draw,mu_posterior_draw)

# plot posterior density estimate for sigma
normR::plot_posterior_density_estimate(sigsq_posterior_draw,
                                       plot_title = "Posterior density estimate of sigma")

# posterior density estimate of mu
normR::plot_posterior_density_estimate(mu_posterior_draw,
                                       plot_title = "Posterior density estimate of mu")

Shiny App

https://carter-allen.shinyapps.io/NormalModel/