Suppose the sample $(7,2,6,12,10,9)$ is well approximated by a normal distribution with mean $\mu$ and standard deviation $6$. Use the discrete prior distribution $(8,9,10,11)$ of possible values for $\mu$ all equally likely. Find the posterior distribution and the posterior standard deviation.

So I computed the posterior using R as follows

```
> sample = c(7,2,6,12,10,9)
> sample.var = 6^2/length(sample)
> mu = c(8,9,10,11)
> prior = rep(0.25,4)
> likelihood= dnorm(mean(sample),mean = mu, sd = sqrt(sample.var))
> posterior = (likelihood*prior)/(sum(likelihood*prior))
> posterior
[1] 0.3434827 0.2989415 0.2202344 0.1373414
> posterior.mean = sum(posterior*mu)
> posterior.mean
[1] 9.151435
```

So I have the posterior probability of the values for $\mu$, and I computed the posterior mean.

But Im not sure what exactly the posterior standard deviation is supposed to be.

I have the formula, $\sqrt{\frac{\sum(x_i-\mu)^2}{n}}$ My assumption was the posterior mean should be $\mu$ in this formula, but I'm not sure if the $x_i$ values should be the sample, or the prior values.

My guess is it is this value:

```
> posterior.sd = sqrt(sum((mu-posterior.mean)^2/4))
> posterior.sd
[1] 7.298286
```

## Best Answer

There are a couple of types of errors in your code. I wrote the code using nested loops, but good form would recommend using a member of the

applyfamily of functions instead. I also printed you a sanity graph. Your posterior mean is about 9.15 and your posterior standard deviation is about 1.04.One thing to remember is that your posterior mean has to be inside your supported set of possible parameter values. Likewise, you posterior standard deviation should be sensible given the structure of your mass function.

It is better code to write the posterior as a function and the likelihood as a function and to use apply functions over the data and the parameter space. However, as you did not do that, I did not assume that you knew how to do that. Apply functions can be a bit difficult to read if you haven't used them.