Skip to content

Commit fffd959

Browse files
authored
Merge pull request #1495 from pymc-devs/constant
Renamed ConstantDist to Constant
2 parents 976393b + a70689e commit fffd959

File tree

7 files changed

+18
-48
lines changed

7 files changed

+18
-48
lines changed

pymc3/distributions/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from .discrete import Poisson
3232
from .discrete import NegativeBinomial
3333
from .discrete import ConstantDist
34+
from .discrete import Constant
3435
from .discrete import ZeroInflatedPoisson
3536
from .discrete import ZeroInflatedNegativeBinomial
3637
from .discrete import DiscreteUniform
@@ -94,6 +95,7 @@
9495
'Poisson',
9596
'NegativeBinomial',
9697
'ConstantDist',
98+
'Constant',
9799
'ZeroInflatedPoisson',
98100
'ZeroInflatedNegativeBinomial',
99101
'DiscreteUniform',

pymc3/distributions/discrete.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .distribution import Discrete, draw_values, generate_samples
1010

1111
__all__ = ['Binomial', 'BetaBinomial', 'Bernoulli', 'Poisson',
12-
'NegativeBinomial', 'ConstantDist', 'ZeroInflatedPoisson',
12+
'NegativeBinomial', 'ConstantDist', 'Constant', 'ZeroInflatedPoisson',
1313
'ZeroInflatedNegativeBinomial', 'DiscreteUniform', 'Geometric',
1414
'Categorical']
1515

@@ -413,7 +413,7 @@ def logp(self, value):
413413
sumto1)
414414

415415

416-
class ConstantDist(Discrete):
416+
class Constant(Discrete):
417417
"""
418418
Constant log-likelihood.
419419
@@ -424,7 +424,7 @@ class ConstantDist(Discrete):
424424
"""
425425

426426
def __init__(self, c, *args, **kwargs):
427-
super(ConstantDist, self).__init__(*args, **kwargs)
427+
super(Constant, self).__init__(*args, **kwargs)
428428
self.mean = self.median = self.mode = self.c = c
429429

430430
def random(self, point=None, size=None, repeat=None):
@@ -441,6 +441,11 @@ def logp(self, value):
441441
c = self.c
442442
return bound(0, tt.eq(value, c))
443443

444+
def ConstantDist(*args, **kwargs):
445+
warnings.warn("ConstantDist has been deprecated. In future, use Constant instead.",
446+
DeprecationWarning)
447+
return Constant(*args, **kwargs)
448+
444449

445450
class ZeroInflatedPoisson(Discrete):
446451
R"""

pymc3/step_methods/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,4 @@
1616

1717
from .slicer import Slice
1818

19-
from .nuts import NUTS
20-
21-
from .arraystep import Constant
19+
from .nuts import NUTS

pymc3/step_methods/arraystep.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from enum import IntEnum, unique
99

1010
__all__ = ['ArrayStep', 'ArrayStepShared', 'metrop_select', 'SamplerHist',
11-
'Competence', 'Constant']
11+
'Competence']
1212

1313

1414
@unique
@@ -163,30 +163,3 @@ def __init__(self):
163163
def acceptr(self):
164164
return np.minimum(np.exp(self.metrops), 1)
165165

166-
167-
class Constant(ArrayStep):
168-
"""
169-
Dummy sampler that returns the current value at every iteration. Useful for
170-
fixing parameters at a particular value.
171-
172-
Parameters
173-
----------
174-
vars : list
175-
List of variables for sampler.
176-
model : PyMC Model
177-
Optional model for sampling step. Defaults to None (taken from context).
178-
"""
179-
180-
def __init__(self, vars, model=None, **kwargs):
181-
182-
model = modelcontext(model)
183-
184-
self.model = model
185-
186-
vars = inputvars(vars)
187-
188-
super(Constant, self).__init__(vars, [model.fastlogp], **kwargs)
189-
190-
def astep(self, q0, logp):
191-
192-
return q0

pymc3/tests/test_distributions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from ..blocking import DictToVarBijection, DictToArrayBijection, ArrayOrdering
88
from ..distributions import (DensityDist, Categorical, Multinomial, VonMises, Dirichlet,
99
MvStudentT, MvNormal, ZeroInflatedPoisson,
10-
ZeroInflatedNegativeBinomial, ConstantDist, Poisson, Bernoulli, Beta,
10+
ZeroInflatedNegativeBinomial, ConstantDist, Constant, Poisson, Bernoulli, Beta,
1111
BetaBinomial, HalfStudentT, StudentT, Weibull, Pareto, InverseGamma,
1212
Gamma, Cauchy, HalfCauchy, Lognormal, Laplace, NegativeBinomial,
1313
Geometric, Exponential, ExGaussian, Normal, Flat, LKJCorr, Wald,
@@ -479,7 +479,7 @@ def test_poisson(self):
479479
lambda value, mu: sp.poisson.logpmf(value, mu))
480480

481481
def test_constantdist(self):
482-
self.pymc3_matches_scipy(ConstantDist, I, {'c': I},
482+
self.pymc3_matches_scipy(Constant, I, {'c': I},
483483
lambda value, c: np.log(c == value))
484484

485485
def test_zeroinflatedpoisson(self):

pymc3/tests/test_distributions_random.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ class TestNegativeBinomial(BaseTestCases.BaseTestCase):
281281
params = {'mu': 1., 'alpha': 1.}
282282

283283

284-
class TestConstantDist(BaseTestCases.BaseTestCase):
285-
distribution = pm.ConstantDist
284+
class TestConstant(BaseTestCases.BaseTestCase):
285+
distribution = pm.Constant
286286
params = {'c': 3}
287287

288288

@@ -471,7 +471,7 @@ def ref_rand(size, p):
471471
def test_constant_dist(self):
472472
def ref_rand(size, c):
473473
return c * np.ones(size, dtype=int)
474-
pymc3_random_discrete(pm.ConstantDist, {'c': I}, ref_rand=ref_rand)
474+
pymc3_random_discrete(pm.Constant, {'c': I}, ref_rand=ref_rand)
475475

476476
def test_mv_normal(self):
477477
def ref_rand(size, mu, cov):

pymc3/tests/test_step.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pymc3.sampling import assign_step_methods, sample
66
from pymc3.model import Model
77
from pymc3.step_methods import (NUTS, BinaryGibbsMetropolis, CategoricalGibbsMetropolis,
8-
Metropolis, Constant, Slice, CompoundStep,
8+
Metropolis, Slice, CompoundStep,
99
MultivariateNormalProposal, HamiltonianMC)
1010
from pymc3.distributions import Binomial, Normal, Bernoulli, Categorical
1111
from numpy.testing import assert_almost_equal
@@ -68,14 +68,6 @@ def test_step_categorical(self):
6868
self.check_stat(check, trace)
6969

7070

71-
def test_constant_step(self):
72-
with Model():
73-
x = Normal('x', 0, 1)
74-
start = {'x': -1}
75-
tr = sample(10, step=Constant([x]), start=start)
76-
assert_almost_equal(tr['x'], start['x'], decimal=10)
77-
78-
7971
class TestCompoundStep(unittest.TestCase):
8072
samplers = (Metropolis, Slice, HamiltonianMC, NUTS)
8173

0 commit comments

Comments
 (0)