Normal likelihood via sufficient statistics

This code runs and seems to work fine, but is there a better way than writing the likelihood out directly? Thank you!

data {
int<lower=0> n;
real nybar;
real ny2bar;
}

parameters {
real mu;
real<lower=0> sigma;
}

model {
mu ~ normal(0,10000);
sigma ~ normal(0,10);

target += -ny2bar/(2 * sigma^2)+nybar * mu/(sigma^2) - n * mu^2/(2 * sigma^2)- n * log(sigma);
}

There is not enough explanation for your code, but based on the title of the post, maybe this section in Stan user’s guide is relevant?
https://mc-stan.org/docs/stan-users-guide/efficiency-tuning.html#normal-sufficient-statistics

2 Likes

Thanks for the answer and that is indeed a relevant page, and does give an example for the Poisson case …

my code is about the normal case where we can write the likelihood using the N, Nybar=sum(y) and Ny2bar=sum(y^2).

It feels like there should be a built in function to do this, but perhaps there is not.

It looks like we do have a normal_sufficient_lpdf in the Math library, but it just needs to be exposed to Stan

3 Likes

The link I provided was specifically for the subsection “Normal sufficient statistics”, and the Poisson section is after that.

That works when you use conjugate priors, but not in the non-conjugate case … when you still have sufficient statistics.