Skip to content

Commit 16996b5

Browse files
souravsinghtwiecki
authored andcommitted
ENH Add Rice Distributions (#1819)
1 parent eb0025b commit 16996b5

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

pymc3/distributions/continuous.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
'StudentT', 'Cauchy', 'HalfCauchy', 'Gamma', 'Weibull',
2323
'HalfStudentT', 'StudentTpos', 'Lognormal', 'ChiSquared',
2424
'HalfNormal', 'Wald', 'Pareto', 'InverseGamma', 'ExGaussian',
25-
'VonMises', 'SkewNormal']
25+
'VonMises', 'SkewNormal','Rice']
2626

2727

2828
class PositiveContinuous(Continuous):
@@ -1347,3 +1347,50 @@ def logp(self, value):
13471347
tt.switch(tt.eq(value, c), tt.log(2 / (upper - lower)),
13481348
tt.switch(alltrue_elemwise([c < value, value <= upper]),
13491349
tt.log(2 * (upper - value) / ((upper - lower) * (upper - c))),np.inf)))
1350+
1351+
class Rice(Continuous):
1352+
R"""
1353+
Rice distribution.
1354+
1355+
.. math::
1356+
1357+
f(x\mid \nu ,\sigma )=
1358+
{\frac {x}{\sigma ^{2}}}\exp
1359+
\left({\frac {-(x^{2}+\nu ^{2})}{2\sigma ^{2}}}\right)I_{0}\left({\frac {x\nu }{\sigma ^{2}}}\right),
1360+
1361+
======== ==============================================================
1362+
Support :math:`x \in (0, +\infinity)`
1363+
Mean :math:`\sigma {\sqrt {\pi /2}}\,\,L_{{1/2}}(-\nu ^{2}/2\sigma ^{2})`
1364+
Variance :math:`2\sigma ^{2}+\nu ^{2}-{\frac {\pi \sigma ^{2}}{2}}L_{{1/2}}^{2}
1365+
\left({\frac {-\nu ^{2}}{2\sigma ^{2}}}\right)`
1366+
======== ==============================================================
1367+
1368+
1369+
Parameters
1370+
----------
1371+
nu : float
1372+
shape parameter.
1373+
sd : float
1374+
standard deviation.
1375+
1376+
"""
1377+
def __init__(self, nu=None, sd=None, *args, **kwargs):
1378+
super(Rice, self).__init__(*args, **kwargs)
1379+
self.nu = tt.as_tensor_variable(nu)
1380+
self.sd = tt.as_tensor_variable(sd)
1381+
self.mean = sd * np.sqrt(np.pi / 2) * tt.exp((-nu**2 / (2 * sd**2)) / 2) * ((1 - (-nu**2 / (2 * sd**2)))
1382+
* i0(-(-nu**2 / (2 * sd**2)) / 2) - (-nu**2 / (2 * sd**2)) * i1(-(-nu**2 / (2 * sd**2)) / 2))
1383+
self.variance = 2 * sd**2 + nu**2 - (np.pi * sd**2 / 2) * (tt.exp((-nu**2 / (2 * sd**2)) / 2) * ((1 - (-nu**2 / (
1384+
2 * sd**2))) * i0(-(-nu**2 / (2 * sd**2)) / 2) - (-nu**2 / (2 * sd**2)) * i1(-(-nu**2 / (2 * sd**2)) / 2)))**2
1385+
1386+
def random(self, point=None, size=None, repeat=None):
1387+
nu, sd = draw_values([self.nu, self.sd],
1388+
point=point)
1389+
return generate_samples(stats.rice.rvs, b=nu, scale=sd, loc=0,
1390+
dist_shape=self.shape, size=size)
1391+
1392+
def logp(self, value):
1393+
nu = self.nu
1394+
sd = self.sd
1395+
return bound(tt.log(value / (sd**2)*tt.exp(-(value**2 + nu**2) / (2 * sd**2))*i0(value * nu / (sd**2))),
1396+
nu>0)

pymc3/tests/test_distributions.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
InverseGamma, Gamma, Cauchy, HalfCauchy, Lognormal, Laplace,
1313
NegativeBinomial, Geometric, Exponential, ExGaussian, Normal,
1414
Flat, LKJCorr, Wald, ChiSquared, HalfNormal, DiscreteUniform,
15-
Bound, Uniform, Triangular, Binomial, SkewNormal, DiscreteWeibull)
15+
Bound, Uniform, Triangular, Binomial, SkewNormal, DiscreteWeibull,
16+
Rice)
17+
1618
from ..distributions import continuous, multivariate
1719
from nose_parameterized import parameterized
1820
from numpy import array, inf, log, exp
@@ -688,3 +690,7 @@ def test_vonmises(self):
688690
def test_multidimensional_beta_construction(self):
689691
with Model():
690692
Beta('beta', alpha=1., beta=1., shape=(10, 20))
693+
694+
def test_rice(self):
695+
self.pymc3_matches_scipy(Normal, R, {'nu': R, 'sd': Rplus},
696+
lambda value, mu, sd: sp.norm.logpdf(value, mu, sd))

0 commit comments

Comments
 (0)