# Generating data from the posterior distribution

Let $$p(D \mid \mu,\sigma^2) \sim \mathcal{N}(\mu,\sigma^2)$$ where $$D=(x_1\ldots x_n)$$ is my data. I imposed a normal prior on the mean as $$\pi(\mu) \sim \mathcal{N}(\mu_0,\sigma_0^2)$$ Using Bayes, I know that the posterior is $$p(\mu \mid D) \propto \mathcal{N}(\mu_n,\sigma_n^2)$$ where $$\mu_n$$ is a convex combination between the ML estimate of $$\mu$$ and the initial belief $$\mu_0$$, that is $$\mu_n = w\mu_{ML} + (1-w)\mu_0 \tag{1}$$ $$\sigma_n^{-2} = \frac{n}{\sigma^2} + \frac{1}{\sigma_0^2}\tag{2}$$ Using any random normal generator (R,MATLAB for ex), I could generate $$D$$ using $$\mu,\sigma^2$$.

Question: If possible, how do I generate $$\mu \mid D$$ without $$\mu_n,\sigma_n$$, i.e. only using the evidence $$D$$? I just want to compare the empirical posterior moments, $$\hat{\mu}_n,\hat{\sigma}_n^2$$, to the true ones in equations $$(1,2)$$.

Example: In this question, there is a posterior histogram (evaluated empirically). How do you do that ?

• I do not understand the question: $\mu_n$ is sufficient for this posterior, so "knowing" $D$ is equivalent to knowing $\mu_n$. May 16, 2019 at 13:48
• “Using only $D$” or also the priors?
– Tim
Oct 3, 2021 at 6:32

It appears that $$\sigma$$ is known in your case. Then, just use the R command rnorm to generate a random sample from a normal distribution with the corresponding parameters

rnorm(n, mu = mun, sigma = sigman)


where mun and sigman are defined by your equations.

https://stat.ethz.ch/R-manual/R-devel/library/stats/html/Normal.html

If you don't know what the posterior parameters are, just check the Conjugate Prior wiki:

https://en.wikipedia.org/wiki/Conjugate_prior#When_likelihood_function_is_a_continuous_distribution

• this is not my question.. My question is how to generate $\mu \mid D$ .. that is using the data not $\mu_n,\sigma_n$. May 16, 2019 at 10:04
• @user21312048 Once you have the data D, and you need to have data :), just calculate the parameters of the posterior distribution, and then simulate from $\mu \mid D$, which is just a normal distribution. If you want to simulate the data $D$ too, you need to ask that too :) May 16, 2019 at 10:06
• you did not read my question, where i clearly mention "Using any random normal generator (R,MATLAB for ex), I could generate $D$ (and possibly the prior) using $\mu,\sigma^2$." Now, using only the data $D$, how to generate $\mu$ ? May 16, 2019 at 10:09