I'm studying Bayesian statistics and I'm trying to estimate the mean of a normal distribution given a normal prior and data which are normally distributed. I have formulas to compute the posterior distribution analitically but I want to solve it by simulation and see if results coincide.
The prior for the mean is N(160, 10). My data are 120 samples stored in a column of a Pandas dataframe and they follow a normal distribution N(256, 2301). Using formulas, my posterior distribution for the mean should be N(195, 6.57).
However when plotting the trace results do not coincide. Here is the code:
with pm.Model() as model:
mu = pm.Normal('parameters', mu=160, sd=50)
observed_data = pm.Normal(
'observed_data', mu=mu, observed=df['Cholestoral'])
with model:
# Sample from the posterior
trace = pm.sample(draws=300, chains=2, tune=300,
discard_tuned_samples=True)
If I plot the posterior this way, I get a normal with mean=256 and very little variance. If I add to the observed_data the variance of my data, 2301, I get as a posterior a normal with mean=160.
I don't understand what's wrong with my code. Can anyone help?