Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unnormalized distributions to the language #63

Closed
VMatthijs opened this issue Feb 25, 2019 · 16 comments
Closed

Add unnormalized distributions to the language #63

VMatthijs opened this issue Feb 25, 2019 · 16 comments
Labels
feature New feature or request

Comments

@VMatthijs
Copy link
Member

VMatthijs commented Feb 25, 2019

Based on discussions with @seantalts and @bob-carpenter :

For each distribution, e.g. poisson_lpmf, make the compiler accept the function name poisson_unnormalized_lpmf as well.
This should code gen to poisson_lpmf<true>.
This allows us to treat
x ~ poisson(42);
as syntactic sugar for
target += poisson_unnormalized_lpmf(x| 42);.

This is related to issue stan-dev/stan#1299 .

Also make sure that algebraic optimizations like bernoulli_lpmf(x|inv_logit(theta))->bernoulli_logit_lpmf(x|theta) and automatic vectorization optimizations apply to these unnormalized distributions as well.

@VMatthijs VMatthijs added the feature New feature or request label Feb 25, 2019
@seantalts
Copy link
Member

just hitting this now - should this actually be inverted? We have a propto__ type parameter to log_prob and it seems like sometimes explicitly want to set it to false rather than sometimes explicitly setting it to true (false is the expensive, more accurate case). So perhaps poisson_normalized_lpmf? Or what about poisson_lpmf_normalized?

@bob-carpenter
Copy link
Contributor

bob-carpenter commented May 31, 2019

_lpmf should remain the suffix to keep the name mangling rules and doc simple.

I think we should support marked cases and leave the unmarked cases the same. So the following two groups would contain equivalent statements, leading off with our existing forms.

y ~ foo(theta);

y ~ foo_propto(theta);
target += foo_propto_lpdf(y | theta);
target += foo_lpdf(y | theta);

y ~ foo_normalized(theta);
target += foo_normalized_lpdf(y | theta);

You can think of the first statement in each of these two groups as syntactic sugar and the ~ statements can now be fully defined as syntactic sugar for target +=.

I could be convinced that propto should be replaced with unnormalized, but it's a handful to type.

@seantalts
Copy link
Member

Oh, for supporting these for users? Sure, makes sense.

I'm having trouble parsing the two groups. Are you saying y ~ foo(theta) = target += foo_lpdf(y | theta) and so on, line 1 = line 1, line 2= line 2, line 3 = line 3? Or also that line 1 = line 2 = line 3?

@seantalts
Copy link
Member

@VMatthijs likes "_unnormalized" and Bob likes "_propto." Unfortunately I don't really care - propto is nice in that it's what we use in code gen

seantalts added a commit that referenced this issue May 31, 2019
Fix #63, which fixed low_dim_gauss_mix
@VMatthijs
Copy link
Member Author

I don't really care either. Let's go with _propto.

@VMatthijs VMatthijs reopened this May 31, 2019
@VMatthijs
Copy link
Member Author

I don't think these have been exposed to the language yet. They're in the backend now, but the type checker should still be made to accept them.

@seantalts
Copy link
Member

Yep, sorry about that.

@bob-carpenter
Copy link
Contributor

bob-carpenter commented May 31, 2019 via email

@seantalts
Copy link
Member

Agreed! I'll write up a discourse post.

@seantalts
Copy link
Member

@VMatthijs
Copy link
Member Author

VMatthijs commented Jun 3, 2019

Just a reminder to ourselves that whatever ends up being implemented should be documented in the wiki. (And then in the manual when we are ready to release.)

@seantalts
Copy link
Member

Does anyone remember why we wanted to expose this to the end users in the language? Was there a motivating use-case for when you needed to use target += foo but didn't want it to be normalized?

@VMatthijs
Copy link
Member Author

@bob-carpenter , weren't you concerned about the normalizing constant of a gamma distribution being expensive to compute or something like that?

@bob-carpenter
Copy link
Contributor

bob-carpenter commented Jun 3, 2019 via email

@seantalts
Copy link
Member

Also, if we want to reduce sampling statements to target increment statements, then we need this flexibility to be able to actually implement the sampling statements as they're currently implemented (unnormalized, that is).

We don't actually need this exposed to end users to do that, right?

Yes, in general there's a lot of expensive computation that's avoided if we can do unnormalized/propto computations. But then in some contexts, like mixtures, we need the normalized forms.

Agreed, but are there cases where you can't use ~ but need to get the propto=true version?

In other words, are there specific motivating examples for exposing this to end users of the Stan language?

@rok-cesnovar
Copy link
Member

Superseed by #253 and #541. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants