# Can I sample from an unnormalized target distribution by using stan?

I have some simple question. I am stan beginner.

It seems to use stan, we need to specify full Bayesian model.
But often time in Bayesian modelling, we may only aim to sample from some target density, possibly, a proportional part of a posterior distribution. Can I also sample from the target density in this case?

Yes, provided that the proportionality is a constant. For more see Sections 3.3 and 4.1 of https://betanalpha.github.io/assets/case_studies/stan_intro.html.

Thanks a lot for the answer. But it seems that it is a conceptual explanation.
Can you give me some “specific stan code example for this situation?”

Unfortunately the situation is not particularly well defined so I’m not sure what kind specifics you might need.

In general

``````model {
// Anything
}
``````

and

``````model {
// Anything
target += constant;
}
``````

define exactly the same target distribution.

For example one can specify a distribution represented by a normal density for the parameter x and constants mu and sigma with

``````model {
target += -0.5 * square((x - mu) / sigma)) - log(sigma) - log(sqrt(2 * pi());
}
``````

or

``````model {
target += -0.5 * square((x - mu) / sigma)) - log(sigma);
}
``````

or

``````model {
target += -0.5 * square((x - mu) / sigma));
}
``````

or even

``````model {
target += -0.5 * square((x - mu) / sigma)) + 10;
}
``````

Not knowing the right normalization doesn’t matter because it doesn’t effect the probabilistic computation.

3 Likes

Thanks a lot!!

That means that: suppose I want to simply sample from univariate Gaussian with mean mu and std sigma that are considered to be constant:

Then using
data {
real mu; // mean
real<lower=0> sigma; // std
}
model {
target += exp( - (1/(2*(sigma^2))*( x - mu )^2 )
}

or
data {
real mu; // mean
real<lower=0> sigma; // std
}
model {
x ~ normal(mu, sigma)
}

should give the same answer, right??

I suspect that as long as it’s a sufficient statistics for your target density; anything goes!

Yes, although to verify I recommend that you run both and compare their outputs.