Skip to content

Commit ac9652e

Browse files
Add nu parametrization to beta distribution (#6344)
* Add (mu, nu) parametrization * add test beta logp * Add relationship between alpha, beta and nu * change test structure * remove white space accidently added in docstring * remove sample size from docstring --------- Co-authored-by: Michael Osthege <[email protected]>
1 parent b213cf3 commit ac9652e

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

pymc/distributions/continuous.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ class Beta(UnitContinuous):
10621062
======== ==============================================================
10631063
10641064
Beta distribution can be parameterized either in terms of alpha and
1065-
beta or mean and standard deviation. The link between the two
1065+
beta, mean and standard deviation or mean and sample size. The link between the three
10661066
parametrizations is given by
10671067
10681068
.. math::
@@ -1072,6 +1072,9 @@ class Beta(UnitContinuous):
10721072
10731073
\text{where } \kappa = \frac{\mu(1-\mu)}{\sigma^2} - 1
10741074
1075+
\alpha = \mu * \nu
1076+
\beta = (1 - \mu) * \nu
1077+
10751078
Parameters
10761079
----------
10771080
alpha : tensor_like of float, optional
@@ -1081,7 +1084,9 @@ class Beta(UnitContinuous):
10811084
mu : tensor_like of float, optional
10821085
Alternative mean (0 < ``mu`` < 1).
10831086
sigma : tensor_like of float, optional
1084-
Alternative standard deviation (1 < ``sigma`` < sqrt(``mu`` * (1 - ``mu``))).
1087+
Alternative standard deviation (0 < ``sigma`` < sqrt(``mu`` * (1 - ``mu``))).
1088+
nu : tensor_like of float, optional
1089+
Alternative "sample size" of a Beta distribution (``nu`` > 0).
10851090
10861091
Notes
10871092
-----
@@ -1092,8 +1097,8 @@ class Beta(UnitContinuous):
10921097
rv_op = pytensor.tensor.random.beta
10931098

10941099
@classmethod
1095-
def dist(cls, alpha=None, beta=None, mu=None, sigma=None, *args, **kwargs):
1096-
alpha, beta = cls.get_alpha_beta(alpha, beta, mu, sigma)
1100+
def dist(cls, alpha=None, beta=None, mu=None, sigma=None, nu=None, *args, **kwargs):
1101+
alpha, beta = cls.get_alpha_beta(alpha, beta, mu, sigma, nu)
10971102
alpha = at.as_tensor_variable(floatX(alpha))
10981103
beta = at.as_tensor_variable(floatX(beta))
10991104

@@ -1106,17 +1111,21 @@ def moment(rv, size, alpha, beta):
11061111
return mean
11071112

11081113
@classmethod
1109-
def get_alpha_beta(self, alpha=None, beta=None, mu=None, sigma=None):
1114+
def get_alpha_beta(self, alpha=None, beta=None, mu=None, sigma=None, nu=None):
11101115
if (alpha is not None) and (beta is not None):
11111116
pass
11121117
elif (mu is not None) and (sigma is not None):
11131118
kappa = mu * (1 - mu) / sigma**2 - 1
11141119
alpha = mu * kappa
11151120
beta = (1 - mu) * kappa
1121+
elif (mu is not None) and (nu is not None):
1122+
alpha = mu * nu
1123+
beta = (1 - mu) * nu
11161124
else:
11171125
raise ValueError(
11181126
"Incompatible parameterization. Either use alpha "
1119-
"and beta, or mu and sigma to specify distribution."
1127+
"and beta, mu and sigma or mu and nu to specify "
1128+
"distribution."
11201129
)
11211130

11221131
return alpha, beta

pymc/tests/distributions/test_continuous.py

+10
Original file line numberDiff line numberDiff line change
@@ -2026,6 +2026,16 @@ class TestBetaMuSigma(BaseTestDistributionRandom):
20262026
checks_to_run = ["check_pymc_params_match_rv_op"]
20272027

20282028

2029+
class TestBetaMuNu(BaseTestDistributionRandom):
2030+
pymc_dist = pm.Beta
2031+
pymc_dist_params = {"mu": 0.5, "nu": 3}
2032+
expected_alpha, expected_beta = pm.Beta.get_alpha_beta(
2033+
mu=pymc_dist_params["mu"], nu=pymc_dist_params["nu"]
2034+
)
2035+
expected_rv_op_params = {"alpha": expected_alpha, "beta": expected_beta}
2036+
checks_to_run = ["check_pymc_params_match_rv_op"]
2037+
2038+
20292039
class TestExponential(BaseTestDistributionRandom):
20302040
pymc_dist = pm.Exponential
20312041
pymc_dist_params = {"lam": 10.0}

0 commit comments

Comments
 (0)