stan-dev / math

The Stan Math Library is a C++ template library for automatic differentiation of any order using forward, reverse, and mixed modes. It includes a range of built-in functions for probabilistic modeling, linear algebra, and equation solving.
https://mc-stan.org
BSD 3-Clause "New" or "Revised" License
737 stars 185 forks source link

Poisson-binomial lpmf, lcdf, lccdf, rng #869

Closed bob-carpenter closed 4 years ago

bob-carpenter commented 6 years ago

Summary:

Provide full implementation of Poisson-binomial for inclusion in Stan.

Description:

See the algorithm discussion on Discourse and Wikipedia article.

Analytic gradients would be a bonus, but a templated dynamic programming algorithm is a reasonable place to start.

shoshievass commented 6 years ago

If it's okay with you all, I'd like to take this on as my intro-to-stan-dev project. It might take a while in between job market prep so let me know if it's urgent + someone else would like to push on it.

bob-carpenter commented 6 years ago

Thanks, @shoshievass. I had also mentioned this issue to @rayleigh as something he might work on.

As I mentioned in writing up the issue, there are two ways to go here: (1) write a templated implementation that just follows the algorithm I wrote in Stan, or (2) write a version with analytic gradients for reverse mode. For (1), you could start with the output from the Stan compiler---you can look at the C++ class generated for the Stan code implementing the lpmf.

You could also do (1) and then later replace with (2). Either way, all the relevant tests will have to be written. The best thing to read on Stan's reverse-mode autodiff framework is the arXiv paper.

syclik commented 6 years ago

It’s all yours! If you need help, post on discourse.

On Thu, Jun 21 2018 at 4:42 PM, notifications@github.com wrote:

If it's okay with you all, I'd like to take this on as my intro-to-stan-dev project. It might take a while in between job market prep so let me know if it's urgent + someone else would like to push on it.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/stan-dev/math/issues/869#issuecomment-399237051, or mute the thread https://github.com/notifications/unsubscribe-auth/AAZ_F1Km4TY9FAF6Zxzip31Wv7a6K5l2ks5t_AVHgaJpZM4UJAH6 .

jake-wittman commented 4 years ago

Just curious - has any progress been made on this? I have a problem I'm working on that I think would be appropriate to model with a poisson-binomial distribution.

bob-carpenter commented 4 years ago

Not that I kow of. We just kicked off another project with @shoshievass, so maybe it'll come up again.

dirmeier commented 4 years ago

Hello, I would love to help contributing to Stan and found this issue as "good first issue". If you are interested I could have a look into this? Cheers, Simon

shoshievass commented 4 years ago

Totally fine on my end!

-Shosh On Jun 7, 2020, 12:05 PM -0700, Simon Dirmeier notifications@github.com, wrote:

Hello, I would love to help contributing to Stan and found this issue as "good first issue". If you are interested I could have a look into this? Cheers, Simon — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

dirmeier commented 4 years ago

Cool, I'll give it a try

andrjohns commented 4 years ago

Feel free to comment here if you have any questions about adding distributions/functions, always good to have more contributors on board!

dirmeier commented 4 years ago

Hey @andrjohns , thanks for the offer. I've started implementing the lpmf (here) following binomial_lpmf.hpp as a template. Sorry if this question is stupid, but I don't quit get the block within is_constant_all (here). Do I need to manually specify the gradients?

andrjohns commented 4 years ago

You don't have to specify the gradients if you don't want to (although it can help with performance). If you just write the function to return the right log-probability, Stan's autodiff will take care of figuring out the gradients (this is how the ordered probit distribution is coded, for example).

If you do want to calculate the gradients, you want to code it so that the calculations are only done in cases where the gradients are actually needed. The if (!is_constant_all<T_prob>::value) block is specifying that the gradients should only be calculated when the input variable is a type that contains gradient information (i.e., a var or fvar)

rok-cesnovar commented 4 years ago

Closed via https://github.com/stan-dev/math/pull/1938