Skip to content

Fix for #3225 Triangular c attribute #3253

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
merged 2 commits into from
Nov 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

### Maintenance

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

Expand Down
18 changes: 11 additions & 7 deletions pymc3/distributions/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -3262,13 +3262,15 @@ class Triangular(BoundedContinuous):
plt.style.use('seaborn-darkgrid')
x = np.linspace(-2, 10, 500)
lowers = [0., -1, 2]
cs = [0.5, 0.5, 0.75]
uppers = [4., 2, 6]
for lower, c_, upper_ in zip(lowers, cs, uppers):
pdf = st.triang.pdf(x, loc=lower, c=c_, scale=upper_)
cs = [2., 0., 6.5]
uppers = [4., 1, 8]
for lower, c, upper in zip(lowers, cs, uppers):
scale = upper - lower
c_ = (c - lower) / scale
pdf = st.triang.pdf(x, loc=lower, c=c_, scale=scale)
plt.plot(x, pdf, label='lower = {}, c = {}, upper = {}'.format(lower,
lower + upper_ * c_,
lower + upper_))
c,
upper))
plt.xlabel('x', fontsize=12)
plt.ylabel('f(x)', fontsize=12)
plt.legend(loc=1)
Expand Down Expand Up @@ -3318,7 +3320,9 @@ def random(self, point=None, size=None):
"""
c, lower, upper = draw_values([self.c, self.lower, self.upper],
point=point, size=size)
return generate_samples(stats.triang.rvs, c=c-lower, loc=lower, scale=upper-lower,
scale = upper - lower
c_ = (c - lower) / scale
return generate_samples(stats.triang.rvs, c=c_, loc=lower, scale=scale,
size=size, dist_shape=self.shape, random_state=None)

def logp(self, value):
Expand Down
9 changes: 8 additions & 1 deletion pymc3/tests/test_distributions_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pymc3.distributions.distribution import draw_values
from .helpers import SeededTest
from .test_distributions import (
build_model, Domain, product, R, Rplus, Rplusbig, Rplusdunif,
build_model, Domain, product, R, Rplus, Rplusbig, Runif, Rplusdunif,
Unit, Nat, NatSmall, I, Simplex, Vector, PdMatrix,
PdMatrixChol, PdMatrixCholUpper, RealMatrix, RandomPdMatrix
)
Expand Down Expand Up @@ -518,6 +518,13 @@ def ref_rand(size, mu, kappa):
return st.vonmises.rvs(size=size, loc=mu, kappa=kappa)
pymc3_random(pm.VonMises, {'mu': R, 'kappa': Rplus}, ref_rand=ref_rand)

def test_triangular(self):
def ref_rand(size, lower, upper, c):
scale = upper - lower
c_ = (c - lower) / scale
return st.triang.rvs(size=size, loc=lower, scale=scale, c=c_)
pymc3_random(pm.Triangular, {'lower': Runif, 'upper': Runif + 3, 'c': Runif + 1}, ref_rand=ref_rand)

def test_flat(self):
with pm.Model():
f = pm.Flat('f')
Expand Down