language question

79 views
Skip to first unread message

Andrew Gelman

unread,
Sep 26, 2016, 3:46:12 PM9/26/16
to stan development mailing list
Hi, I was teaching and I told the students that, in Stan, the following line:

a ~ foo(b,c,d);

is the same as:

target += foo_lpdf(a|b,c,d);

Is this in general true, or are there examples (discrete distributions? others?) where it is not true?
I didn't want to give this as a general tip if it's not actually generally correct!

Thx
A


Daniel Lee

unread,
Sep 26, 2016, 4:13:09 PM9/26/16
to stan-dev mailing list
Hi Andrew,

The answer is almost. The answer below holds for all the univariate distributions, but not necessarily all the multivariate distributions.

Let's take the normal distribution as an example.

target += normal_lpdf(y | mu, sigma);

will always increment the target by:
- log(2 * pi()) - log(sigma) - (y - mu)^2 / (2 * sigma^2)

Always.

It's not so simple for:
y ~ normal(mu, sigma);

It depends on what y, mu, and sigma are. Here are the 8 cases:

parameter y, parameter mu, parameter sigma:
- log(sigma) - (y - mu)^2 / (2 * sigma^2)
// the constant term is dropped

parameter y, parameter mu, data sigma:
- (y - mu)^2 / (2 * sigma^2)

parameter y, data mu, parameter sigma:
- log(sigma) - (y - mu)^2 / (2 * sigma^2)

parameter y, data mu, data sigma:
- (y - mu)^2 / (2 * sigma^2)

data y, parameter mu, parameter sigma:
- log(sigma) - (y - mu)^2 / (2 * sigma^2)
// the constant term is dropped

data y, parameter mu, data sigma:
- (y - mu)^2 / (2 * sigma^2)

data y, data mu, parameter sigma:
- log(sigma) - (y - mu)^2 / (2 * sigma^2)

data y, data mu, data sigma:
0
// that's right. Nothing is computed


Right now, it's more efficient to write y ~ normal(mu, sigma) because we don't compute constants.


For user-defined distributions, it's all the same and will compute the full thing.


Daniel





A


--
You received this message because you are subscribed to the Google Groups "stan development mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrew Gelman

unread,
Sep 26, 2016, 4:30:56 PM9/26/16
to stan...@googlegroups.com
Ahhh, yes, I forgot about normalizing constants.
A
To unsubscribe from this group and stop receiving emails from it, send an email to stan-dev+u...@googlegroups.com.

Daniel Lee

unread,
Sep 26, 2016, 4:32:26 PM9/26/16
to stan-dev mailing list
We'll need a way to control this from the language at some point using _lpdf functions.


Daniel

Aki Vehtari

unread,
Sep 27, 2016, 4:27:51 AM9/27/16
to stan development mailing list, gel...@stat.columbia.edu
It can also be

target += foo_lpmf(a|b,c,d);

for discrete distributions.

Aki

Reply all
Reply to author
Forward
0 new messages