-
-
Notifications
You must be signed in to change notification settings - Fork 70
Implement BetaNegativeBinomial distribution #258
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
Merged
ricardoV94
merged 15 commits into
pymc-devs:main
from
williambdean:add_beta_negative_binomial
Nov 10, 2023
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
fc148ca
implement and some tests that mirror GeneralizedPoisson
williambdean 43093dc
run precommit
williambdean 0f93212
write as CustomDist
williambdean e41f46b
add to docstring
williambdean 61f4b26
add figure
williambdean 24fb9c1
Update pymc_experimental/distributions/discrete.py
williambdean 7553770
Update pymc_experimental/tests/distributions/test_discrete.py
williambdean a8c2388
remove imports
williambdean 0f9c706
Merge branch 'add_beta_negative_binomial' of https://github.com/wd606…
williambdean c8925a4
Merge branch 'main' into add_beta_negative_binomial
williambdean b9f52b6
run precommit
williambdean a9b1ab6
reorder docstring
williambdean 06aa4d3
run on all files
williambdean d4f46a6
broadcast parameter shapes
williambdean 6fdc4bf
add to all
williambdean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
|
||
import numpy as np | ||
import pymc as pm | ||
from pymc.distributions.dist_math import check_parameters, factln, logpow | ||
from pymc.distributions.dist_math import betaln, check_parameters, factln, logpow | ||
from pymc.distributions.shape_utils import rv_size_is_none | ||
from pytensor import tensor as pt | ||
from pytensor.tensor.random.op import RandomVariable | ||
|
@@ -173,6 +173,125 @@ def logp(value, mu, lam): | |
) | ||
|
||
|
||
class BetaNegativeBinomial: | ||
R""" | ||
Beta Negative Binomial distribution. | ||
|
||
The pmf of this distribution is | ||
|
||
.. math:: | ||
|
||
f(x \mid \alpha, \beta, r) = \frac{B(r + x, \alpha + \beta)}{B(r, \alpha)} \frac{\Gamma(x + \beta)}{x! \Gamma(\beta)} | ||
|
||
where :math:`B` is the Beta function and :math:`\Gamma` is the Gamma function. | ||
|
||
For more information, see https://en.wikipedia.org/wiki/Beta_negative_binomial_distribution. | ||
|
||
.. plot:: | ||
:context: close-figs | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
from scipy.special import betaln, gammaln | ||
def factln(x): | ||
return gammaln(x + 1) | ||
def logp(x, alpha, beta, r): | ||
return ( | ||
betaln(r + x, alpha + beta) | ||
- betaln(r, alpha) | ||
+ gammaln(x + beta) | ||
- factln(x) | ||
- gammaln(beta) | ||
) | ||
plt.style.use('arviz-darkgrid') | ||
x = np.arange(0, 25) | ||
params = [ | ||
(1, 1, 1), | ||
(1, 1, 10), | ||
(1, 10, 1), | ||
(1, 10, 10), | ||
(10, 10, 10), | ||
] | ||
for alpha, beta, r in params: | ||
pmf = np.exp(logp(x, alpha, beta, r)) | ||
plt.plot(x, pmf, "-o", label=r'$alpha$ = {}, $beta$ = {}, $r$ = {}'.format(alpha, beta, r)) | ||
plt.xlabel('x', fontsize=12) | ||
plt.ylabel('f(x)', fontsize=12) | ||
plt.legend(loc=1) | ||
plt.show() | ||
|
||
======== ====================================== | ||
Support :math:`x \in \mathbb{N}_0` | ||
Mean :math:`{\begin{cases}{\frac {r\beta }{\alpha -1}}&{\text{if}}\ \alpha >1\\\infty &{\text{otherwise}}\ \end{cases}}` | ||
Variance :math:`{\displaystyle {\begin{cases}{\frac {r\beta (r+\alpha -1)(\beta +\alpha -1)}{(\alpha -2){(\alpha -1)}^{2}}}&{\text{if}}\ \alpha >2\\\infty &{\text{otherwise}}\ \end{cases}}}` | ||
======== ====================================== | ||
|
||
Parameters | ||
---------- | ||
alpha : tensor_like of float | ||
shape of the beta distribution (alpha > 0). | ||
beta : tensor_like of float | ||
shape of the beta distribution (beta > 0). | ||
r : tensor_like of float | ||
number of successes until the experiment is stopped (integer but can be extended to real) | ||
""" | ||
|
||
@staticmethod | ||
def beta_negative_binomial_dist(alpha, beta, r, size): | ||
if rv_size_is_none(size): | ||
alpha, beta, r = pt.broadcast_arrays(alpha, beta, r) | ||
|
||
p = pm.Beta.dist(alpha, beta, size=size) | ||
return pm.NegativeBinomial.dist(p, r, size=size) | ||
|
||
@staticmethod | ||
def beta_negative_binomial_logp(value, alpha, beta, r): | ||
res = ( | ||
betaln(r + value, alpha + beta) | ||
- betaln(r, alpha) | ||
+ pt.gammaln(value + beta) | ||
- factln(value) | ||
- pt.gammaln(beta) | ||
) | ||
res = pt.switch( | ||
pt.lt(value, 0), | ||
-np.inf, | ||
res, | ||
) | ||
|
||
return check_parameters( | ||
res, | ||
alpha > 0, | ||
beta > 0, | ||
r > 0, | ||
msg="alpha > 0, beta > 0, r > 0", | ||
) | ||
|
||
def __new__(cls, name, alpha, beta, r, **kwargs): | ||
return pm.CustomDist( | ||
name, | ||
alpha, | ||
beta, | ||
r, | ||
dist=cls.beta_negative_binomial_dist, | ||
logp=cls.beta_negative_binomial_logp, | ||
class_name="BetaNegativeBinomial", | ||
**kwargs, | ||
) | ||
|
||
@classmethod | ||
def dist(cls, alpha, beta, r, **kwargs): | ||
return pm.CustomDist.dist( | ||
alpha, | ||
beta, | ||
r, | ||
dist=cls.beta_negative_binomial_dist, | ||
logp=cls.beta_negative_binomial_logp, | ||
class_name="BetaNegativeBinomial", | ||
**kwargs, | ||
) | ||
|
||
|
||
class Skellam: | ||
R""" | ||
Skellam distribution. | ||
|
@@ -228,6 +347,9 @@ class Skellam: | |
|
||
@staticmethod | ||
def skellam_dist(mu1, mu2, size): | ||
if rv_size_is_none(size): | ||
mu1, mu2 = pt.broadcast_arrays(mu1, mu2) | ||
|
||
Comment on lines
+350
to
+352
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return pm.Poisson.dist(mu=mu1, size=size) - pm.Poisson.dist(mu=mu2, size=size) | ||
|
||
@staticmethod | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.