Problem with variance for Normal conjugate distributions

Hi, it is my first topic in this forum.

I’m trying to calculate the posterior mean with likelihood and prior distribution conjugate.
i have generated the data from N(23,10), and so i know the true standard deviation of the generator process. this sample is generated with R

i have set the prior distribution of theta like N(0,100) and the likelihood like N(\theta,10). the stan script is here.

the theory to calculate the posterior variance said that

$$\sigma_{pos}=( \frac{1}{\sigma_0}+ \frac{N}{\sigma} )^{-1}$$

i have done a script in R to calculate this, but the variance of generated sample theta is different by the theoretical variance calculated with formula

Can anyone help me to understand why the variance are different?
thanks for your help

Variance.R (1.3 KB)
Modello.Stan (198 Bytes)

Great idea to validate this kind of thing. Unofortunately, there’s a bug in your model. You have

for(i in 1:N) Y ~ normal(theta,sigma);

which counts every data point N times. What you want is either

for (n in 1:N) Y[n] ~ normal(theta, sigma);

or more succintly and efficiently,

Y ~ normal(theta, sigma);

This is one of the programming errors we hope to catch with our new lint mode, as it’s suprisingly common.

@Bob_Carpenter thank you very much for your help, i have correct my error, and seem that the program works fine.
This is my github link for this project.

Thanks for reporting back! I’ll mark it as solved.