-
-
Notifications
You must be signed in to change notification settings - Fork 188
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
Feature/issue 3121 beta neg binomial rng #3126
Feature/issue 3121 beta neg binomial rng #3126
Conversation
Jenkins Console Log Machine informationNo LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focalCPU: G++: Clang: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two small changes then this is good to go!
for (size_t n = 0; n < size_p; ++n) { | ||
odds_ratio_p[n] = stan::math::exp(stan::math::log(p_vec.val(n)) | ||
- stan::math::log((1 - p_vec.val(n)))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can add #include <stan/math/prim/fun/log.hpp>
at the top to remove stan::math::log
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be log1m as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Will change it
template <typename T_r, typename T_alpha, typename T_beta, class RNG> | ||
inline typename VectorBuilder<true, int, T_r, T_alpha, T_beta>::type | ||
beta_neg_binomial_rng(const T_r &r, const T_alpha &alpha, const T_beta &beta, | ||
RNG &rng) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
template <typename T_r, typename T_alpha, typename T_beta, class RNG> | |
inline typename VectorBuilder<true, int, T_r, T_alpha, T_beta>::type | |
beta_neg_binomial_rng(const T_r &r, const T_alpha &alpha, const T_beta &beta, | |
RNG &rng) { | |
template <typename T_r, typename T_alpha, typename T_beta, typename RNG> | |
inline auto beta_neg_binomial_rng(const T_r& r, const T_alpha& alpha, const T_beta& beta, RNG& rng) { |
Jenkins Console Log Machine informationNo LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focalCPU: G++: Clang: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
Summary
With this PR the rng of beta negative binomial distribution are added.
See issue #3121
Expressions involved :
for$$Y\sim \text{BNB}(r,\alpha,\beta)$$ we have
where$\text{NB}(y~|~r,p) = \binom {y+r-1}{y} (1-p)^{y} p^{r}.$
In Stan, the negative binomial distribution is given by
so
Therefore, provided$r, \alpha, \beta$ , we generate $$p \sim \text{Beta}(\alpha ,\beta )$$ and then sample $Y \sim \text{NB}(r, \frac{p}{1-p})$ .
Tests
Test is written follow
test/unit/math/prim/prob/beta_binomial_test.cpp
Side Effects
Under more extreme paremeter combination that makes the tail of the distribution heavy,
neg_binomial_rng
might throwdomain_error
becauserng_from_gamma >= POISSON_MAX_RATE
. Seemath/stan/math/prim/prob/neg_binomial_rng.hpp
Line 60 in 4a812be
This is also a problem for
neg_binomial_rng
andpoisson_rng
. But due to their nature, this situation is relatively rare.The reason behind this is that the Poisson random number generator in Boost will overflow when it exceeds the type range. Stan uses 32-bit int, so there is an upper limit. The current code includes POISSON_MAX_RATE to prevent overflow.
Release notes
beta_neg_binomial_rng
is available if merged.Checklist
Copyright holder: (fill in copyright holder information)
The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
- Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
- Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
the basic tests are passing
./runTests.py test/unit
)make test-headers
)make test-math-dependencies
)make doxygen
)make cpplint
)the code is written in idiomatic C++ and changes are documented in the doxygen
the new changes are tested