Let $X \overset{iid}{\sim}N(\mu, \sigma^2)$ assuming $\sigma^2$ is known and the [[prior]] distribution on $\mu$ is the [[normal distribution]]. The [[posterior]] will also be a normal distribution.
$
\mu \sim N \Big(\mu_0, \frac{\sigma^2}{M} \Big) \to \mu \sim N \Big(\frac{n \bar x + M \mu_0}{n + M}, \frac{\sigma^2}{n + M} \Big)
$
When both the mean and variance are unknown, use the procedure in [[estimating the mean and variance of normally distributed data]].
> [!Tip]
> A good estimate for M is the ratio of the data variance to the known population variance.
> ```R
> m = sig**2 / sig0**2
> post_mean = (n * xbar + m * mu0) / (n + m)
> post_var = sig**2 / (n + m)
> ```
**Prior**
$\pi(\mu) \propto \text{exp} \Big \{-\frac{M}{2 \sigma^2} (\mu - \mu_0)^2 \Big \} \propto \text{exp} \Big \{-\frac{M\mu^2 - 2 M \mu_0 \mu}{2 \sigma^2} \Big \}$
Note constants of proportionality are dropped.
**Likelihood**
$
f(x | \mu) \propto \text{exp} \Big \{-\frac{1}{2 \sigma^2} \sum (x_i - \mu)^2 \Big \} \propto \text{exp} \Big \{-\frac{n\mu^2 - 2\mu n \bar x}{2 \sigma^2} \Big \}
$
Note constants of proportionality are dropped.
**Posterior**
$
\pi(\mu | x) \propto \text{exp} \Big \{-\frac{(n+M)\mu^2 - (2 n \bar x - 2 M \mu_0) \mu}{2 \sigma^2} \Big \} \propto \text{exp} \Big \{-\frac{(n+M)}{2 \sigma^2} \Big (\mu - \frac{n \bar x + M \mu_0}{n + M} \Big )^2 \Big \}
$
Here we use [[completing the square]] to factor where $a = n + M$, $b = -2 n \bar x - 2 M \mu_0$ and $c=0$.
The posterior probability distribution can be thought of as a weighted average between the prior estimate for $\mu_0$ and the posterior prediction $\hat \mu$ where we can think of $M$ as the prior sample size.
$\hat \mu = (1 - w_n)\mu_0 + w_n \bar x$
where
$w_n = \frac{n}{n+M}$