# Why does $\log p(\mu, \log \sigma \mid y)=\text{constant} -n\log\sigma-\frac{1}{2\sigma^2}\left((n-1)s^2+n(\overline{y}-\mu)^2\right)$?

I'm studying Bayesian data analysis and I came across to the following derivation in my study material:

Given normally distributed i.i.d samples $$y_1, y_2, \ldots, y_n$$, $$y_i\sim N(\mu, \sigma^2)$$ and a uniform prior on $$(\mu, \log\sigma)$$, i.e. $$p(\mu, \log\sigma) \propto 1$$ then the log-posterior $$\log p(\mu, \log \sigma \mid y)$$ is:

$$\log p(\mu, \log \sigma \mid y)=\text{constant} -n\log\sigma-\frac{1}{2\sigma^2}\left((n-1)s^2+n(\overline{y}-\mu)^2\right),$$

where $$s^2$$ is the sample variance $$s^2=\frac{1}{n-1}\sum_{i=1}^n(y_i-\overline{y})^2$$ and $$\overline{y}=\frac{1}{n}\sum_{i=1}^n y_i$$ is the sample average.

My question is: how do we arrive in the log-posterior shown above? Can you give the main steps explicitly?

This problem is from Gelman's et al. Bayesian Data analysis book.

In the usual way of writing the posterior up to some unknown normalising constant we have $$p(\mu, \log \sigma \mid \mathbf{y} ) \propto p(\mathbf{y} \mid \mu, \log \sigma)p(\mu, \log \sigma), \tag{1}$$ then letting $$p(\mu, \log \sigma) \propto 1$$ be an improper prior $$(1)$$ becomes $$p(\mu, \log \sigma \mid \mathbf{y} ) \propto p(\mathbf{y}\mid\mu, \log \sigma) \cdot 1 \qquad \text{or} \qquad p(\mu,\log \sigma \mid \mathbf{y} ) = \frac{1}{Z}p(\mathbf{y} \mid \mu, \log \sigma),$$ for some unknown normalising constant, which may not actually exist and is something that must be checked for after arriving at a candidate form for the posterior. Now taking the logarithm of both sides we have \begin{align*} \log p(\mu,\log \sigma\mid\mathbf{y} ) &= \log \left(\frac{1}{(2\pi )^{n/2} \sigma^n}e^{-\frac{\sum_i (y_i-\mu)^2}{2\sigma^2}}\right)+ \log(1) + \text{constant} \\ &=-\frac{1}{2\sigma^2}\sum_i\left(y_i - \mu\right)^2-n\log\sigma + \mbox{constant} \tag{2}. \end{align*} Now the following is a common decomposition \begin{align*} \sum_i (y_i -\mu)^2 &= \sum_i(y_i-\bar{y} +\bar{y}-\mu)^2 \\ &=\sum_i (y_i - \bar{y})^2+\sum_i(\bar{y}-\mu)^2+2\underbrace{\sum_i(y_i-\bar{y})(\bar{y}-\mu)}_{=0} \\ &=(n-1)s^2+n(\bar{y}-\mu)^2. \tag{3} \end{align*} Then putting $$(3)$$ into $$(2)$$ gives the result. You can then if you like check this does indeed normalise
• I mean you converted the likelihood $p(y|\mu, \sigma^2)$ to $p(y|\mu,\log\sigma)$ (which is the same as the posterior in this case)? Commented Oct 1, 2017 at 18:27