Skip to content

Commit eebed5e

Browse files
committed
Replace custom incomplete_beta with aesara.tensor.betainc
Closes pymc-devs#4420
1 parent a3ee747 commit eebed5e

File tree

5 files changed

+37
-220
lines changed

5 files changed

+37
-220
lines changed

RELEASE-NOTES.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- The `Distribution` keyword argument `testval` has been deprecated in favor of `initval`.
99
- `pm.sample` now returns results as `InferenceData` instead of `MultiTrace` by default (see [#4744](https://github.com/pymc-devs/pymc3/pull/4744)).
1010
- `pm.sample_prior_predictive` no longer returns transformed variable values by default. Pass them by name in `var_names` if you want to obtain these draws (see [4769](https://github.com/pymc-devs/pymc3/pull/4769)).
11-
...
11+
- ...
1212

1313
### New Features
1414
- The `CAR` distribution has been added to allow for use of conditional autoregressions which often are used in spatial and network models.
@@ -20,12 +20,14 @@
2020
- Add `logcdf` method to Kumaraswamy distribution (see [#4706](https://github.com/pymc-devs/pymc3/pull/4706)).
2121
- The `OrderedMultinomial` distribution has been added for use on ordinal data which are _aggregated_ by trial, like multinomial observations, whereas `OrderedLogistic` only accepts ordinal data in a _disaggregated_ format, like categorical
2222
observations (see [#4773](https://github.com/pymc-devs/pymc3/pull/4773)).
23+
- ...
2324

2425
### Maintenance
2526
- Remove float128 dtype support (see [#4514](https://github.com/pymc-devs/pymc3/pull/4514)).
2627
- Logp method of `Uniform` and `DiscreteUniform` no longer depends on `pymc3.distributions.dist_math.bound` for proper evaluation (see [#4541](https://github.com/pymc-devs/pymc3/pull/4541)).
2728
- `Model.RV_dims` and `Model.coords` are now read-only properties. To modify the `coords` dictionary use `Model.add_coord`. Also `dims` or coordinate values that are `None` will be auto-completed (see [#4625](https://github.com/pymc-devs/pymc3/pull/4625)).
2829
- The length of `dims` in the model is now tracked symbolically through `Model.dim_lengths` (see [#4625](https://github.com/pymc-devs/pymc3/pull/4625)).
30+
- The `incomplete_beta` function in `pymc3.distributions.dist_math` was replaced by `aesara.tensor.betainc` (see [4736](https://github.com/pymc-devs/pymc3/pull/4736)).
2931
- ...
3032

3133
## PyMC3 3.11.2 (14 March 2021)

pymc3/distributions/continuous.py

+8-18
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
bound,
6060
clipped_beta_rvs,
6161
i0e,
62-
incomplete_beta,
6362
log_i0,
6463
log_normal,
6564
logpow,
@@ -1246,23 +1245,19 @@ def logcdf(value, alpha, beta):
12461245
12471246
Parameters
12481247
----------
1249-
value: numeric
1250-
Value(s) for which log CDF is calculated.
1248+
value: numeric or np.ndarray or aesara.tensor
1249+
Value(s) for which log CDF is calculated. If the log CDF for multiple
1250+
values are desired the values must be provided in a numpy array or Aesara tensor.
12511251
12521252
Returns
12531253
-------
12541254
TensorVariable
12551255
"""
1256-
# incomplete_beta function can only handle scalar values (see #4342)
1257-
if np.ndim(value):
1258-
raise TypeError(
1259-
f"Beta.logcdf expects a scalar value but received a {np.ndim(value)}-dimensional object."
1260-
)
12611256

12621257
return bound(
12631258
at.switch(
12641259
at.lt(value, 1),
1265-
at.log(incomplete_beta(alpha, beta, value)),
1260+
at.log(at.betainc(alpha, beta, value)),
12661261
0,
12671262
),
12681263
0 <= value,
@@ -1915,27 +1910,22 @@ def logcdf(value, nu, mu, sigma):
19151910
19161911
Parameters
19171912
----------
1918-
value: numeric
1919-
Value(s) for which log CDF is calculated.
1913+
value: numeric or np.ndarray or aesara.tensor
1914+
Value(s) for which log CDF is calculated. If the log CDF for multiple
1915+
values are desired the values must be provided in a numpy array or Aesara tensor.
19201916
19211917
Returns
19221918
-------
19231919
TensorVariable
19241920
"""
1925-
# incomplete_beta function can only handle scalar values (see #4342)
1926-
if np.ndim(value):
1927-
raise TypeError(
1928-
f"StudentT.logcdf expects a scalar value but received a {np.ndim(value)}-dimensional object."
1929-
)
1930-
19311921
lam, sigma = get_tau_sigma(sigma=sigma)
19321922

19331923
t = (value - mu) / sigma
19341924
sqrt_t2_nu = at.sqrt(t ** 2 + nu)
19351925
x = (t + sqrt_t2_nu) / (2.0 * sqrt_t2_nu)
19361926

19371927
return bound(
1938-
at.log(incomplete_beta(nu / 2.0, nu / 2.0, x)),
1928+
at.log(at.betainc(nu / 2.0, nu / 2.0, x)),
19391929
0 < nu,
19401930
0 < sigma,
19411931
0 < lam,

pymc3/distributions/discrete.py

+14-35
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
binomln,
3636
bound,
3737
factln,
38-
incomplete_beta,
3938
log_diff_normal_cdf,
4039
logpow,
4140
normal_lccdf,
@@ -145,25 +144,20 @@ def logcdf(value, n, p):
145144
146145
Parameters
147146
----------
148-
value: numeric
149-
Value for which log CDF is calculated.
147+
value: numeric or np.ndarray or aesara.tensor
148+
Value(s) for which log CDF is calculated. If the log CDF for multiple
149+
values are desired the values must be provided in a numpy array or Aesara tensor.
150150
151151
Returns
152152
-------
153153
TensorVariable
154154
"""
155-
# incomplete_beta function can only handle scalar values (see #4342)
156-
if np.ndim(value):
157-
raise TypeError(
158-
f"Binomial.logcdf expects a scalar value but received a {np.ndim(value)}-dimensional object."
159-
)
160-
161155
value = at.floor(value)
162156

163157
return bound(
164158
at.switch(
165159
at.lt(value, n),
166-
at.log(incomplete_beta(n - value, value + 1, 1 - p)),
160+
at.log(at.betainc(n - value, value + 1, 1 - p)),
167161
0,
168162
),
169163
0 <= value,
@@ -732,21 +726,16 @@ def logcdf(value, n, p):
732726
733727
Parameters
734728
----------
735-
value: numeric
736-
Value for which log CDF is calculated.
729+
value: numeric or np.ndarray or aesara.tensor
730+
Value(s) for which log CDF is calculated. If the log CDF for multiple
731+
values are desired the values must be provided in a numpy array or Aesara tensor.
737732
738733
Returns
739734
-------
740735
TensorVariable
741736
"""
742-
# incomplete_beta function can only handle scalar values (see #4342)
743-
if np.ndim(value):
744-
raise TypeError(
745-
f"NegativeBinomial.logcdf expects a scalar value but received a {np.ndim(value)}-dimensional object."
746-
)
747-
748737
return bound(
749-
at.log(incomplete_beta(n, at.floor(value) + 1, p)),
738+
at.log(at.betainc(n, at.floor(value) + 1, p)),
750739
0 <= value,
751740
0 < n,
752741
0 <= p,
@@ -1461,20 +1450,15 @@ def logcdf(value, psi, n, p):
14611450
14621451
Parameters
14631452
----------
1464-
value: numeric
1465-
Value for which log CDF is calculated.
1453+
value: numeric or np.ndarray or aesara.tensor
1454+
Value(s) for which log CDF is calculated. If the log CDF for multiple
1455+
values are desired the values must be provided in a numpy array or Aesara tensor.
14661456
14671457
Returns
14681458
-------
14691459
TensorVariable
14701460
"""
14711461

1472-
# logcdf can only handle scalar values due to limitation in Binomial.logcdf
1473-
if np.ndim(value):
1474-
raise TypeError(
1475-
f"ZeroInflatedBinomial.logcdf expects a scalar value but received a {np.ndim(value)}-dimensional object."
1476-
)
1477-
14781462
return bound(
14791463
logaddexp(at.log1p(-psi), at.log(psi) + _logcdf(binomial, value, {}, n, p)),
14801464
0 <= value,
@@ -1616,19 +1600,14 @@ def logcdf(value, psi, n, p):
16161600
16171601
Parameters
16181602
----------
1619-
value: numeric
1620-
Value for which log CDF is calculated.
1603+
value: numeric or np.ndarray or aesara.tensor
1604+
Value(s) for which log CDF is calculated. If the log CDF for multiple
1605+
values are desired the values must be provided in a numpy array or Aesara tensor.
16211606
16221607
Returns
16231608
-------
16241609
TensorVariable
16251610
"""
1626-
# logcdf can only handle scalar values due to limitation in NegativeBinomial.logcdf
1627-
if np.ndim(value):
1628-
raise TypeError(
1629-
f"ZeroInflatedNegativeBinomial.logcdf expects a scalar value but received a {np.ndim(value)}-dimensional object."
1630-
)
1631-
16321611
return bound(
16331612
logaddexp(at.log1p(-psi), at.log(psi) + _logcdf(nbinom, value, {}, n, p)),
16341613
0 <= value,

0 commit comments

Comments
 (0)