Skip to content

Commit 3f098c3

Browse files
lucianopaztwiecki
authored andcommitted
Fix for #3225. Made Triangular c attribute be handled consistently with scipy.stats. Added test and updated example code. (#3253)
1 parent b1ee243 commit 3f098c3

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
### Maintenance
1515

16+
- Fixed Triangular distribution `c` attribute handling in `random` and updated sample codes for consistency (#3225)
1617
- Renamed `sample_ppc()` and `sample_ppc_w()` to `sample_posterior_predictive()` and `sample_posterior_predictive_w()`, respectively.
1718
- Refactor SMC and properly compute marginal likelihood (#3124)
1819

pymc3/distributions/continuous.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3262,13 +3262,15 @@ class Triangular(BoundedContinuous):
32623262
plt.style.use('seaborn-darkgrid')
32633263
x = np.linspace(-2, 10, 500)
32643264
lowers = [0., -1, 2]
3265-
cs = [0.5, 0.5, 0.75]
3266-
uppers = [4., 2, 6]
3267-
for lower, c_, upper_ in zip(lowers, cs, uppers):
3268-
pdf = st.triang.pdf(x, loc=lower, c=c_, scale=upper_)
3265+
cs = [2., 0., 6.5]
3266+
uppers = [4., 1, 8]
3267+
for lower, c, upper in zip(lowers, cs, uppers):
3268+
scale = upper - lower
3269+
c_ = (c - lower) / scale
3270+
pdf = st.triang.pdf(x, loc=lower, c=c_, scale=scale)
32693271
plt.plot(x, pdf, label='lower = {}, c = {}, upper = {}'.format(lower,
3270-
lower + upper_ * c_,
3271-
lower + upper_))
3272+
c,
3273+
upper))
32723274
plt.xlabel('x', fontsize=12)
32733275
plt.ylabel('f(x)', fontsize=12)
32743276
plt.legend(loc=1)
@@ -3318,7 +3320,9 @@ def random(self, point=None, size=None):
33183320
"""
33193321
c, lower, upper = draw_values([self.c, self.lower, self.upper],
33203322
point=point, size=size)
3321-
return generate_samples(stats.triang.rvs, c=c-lower, loc=lower, scale=upper-lower,
3323+
scale = upper - lower
3324+
c_ = (c - lower) / scale
3325+
return generate_samples(stats.triang.rvs, c=c_, loc=lower, scale=scale,
33223326
size=size, dist_shape=self.shape, random_state=None)
33233327

33243328
def logp(self, value):

pymc3/tests/test_distributions_random.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pymc3.distributions.distribution import draw_values
1414
from .helpers import SeededTest
1515
from .test_distributions import (
16-
build_model, Domain, product, R, Rplus, Rplusbig, Rplusdunif,
16+
build_model, Domain, product, R, Rplus, Rplusbig, Runif, Rplusdunif,
1717
Unit, Nat, NatSmall, I, Simplex, Vector, PdMatrix,
1818
PdMatrixChol, PdMatrixCholUpper, RealMatrix, RandomPdMatrix
1919
)
@@ -518,6 +518,13 @@ def ref_rand(size, mu, kappa):
518518
return st.vonmises.rvs(size=size, loc=mu, kappa=kappa)
519519
pymc3_random(pm.VonMises, {'mu': R, 'kappa': Rplus}, ref_rand=ref_rand)
520520

521+
def test_triangular(self):
522+
def ref_rand(size, lower, upper, c):
523+
scale = upper - lower
524+
c_ = (c - lower) / scale
525+
return st.triang.rvs(size=size, loc=lower, scale=scale, c=c_)
526+
pymc3_random(pm.Triangular, {'lower': Runif, 'upper': Runif + 3, 'c': Runif + 1}, ref_rand=ref_rand)
527+
521528
def test_flat(self):
522529
with pm.Model():
523530
f = pm.Flat('f')

0 commit comments

Comments
 (0)