Skip to content

Commit 98889df

Browse files
colintwiecki
authored andcommitted
Unskip some tests
1 parent 4157f0f commit 98889df

File tree

2 files changed

+21
-74
lines changed

2 files changed

+21
-74
lines changed

pymc3/distributions/continuous.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,12 +926,20 @@ def __init__(self, alpha, beta=1, *args, **kwargs):
926926
super(InverseGamma, self).__init__(*args, **kwargs)
927927
self.alpha = alpha
928928
self.beta = beta
929-
self.mean = (alpha > 1) * beta / (alpha - 1.) or np.inf
929+
self.mean = self._calculate_mean()
930930
self.mode = beta / (alpha + 1.)
931931
self.variance = tt.switch(tt.gt(alpha, 2),
932932
(beta**2) / (alpha * (alpha - 1.)**2),
933933
np.inf)
934934

935+
def _calculate_mean(self):
936+
m = self.beta / (self.alpha - 1.)
937+
try:
938+
return (self.alpha > 1) * m or np.inf
939+
except ValueError: # alpha is an array
940+
m[self.alpha <= 1] = np.inf
941+
return m
942+
935943
def random(self, point=None, size=None, repeat=None):
936944
alpha, beta = draw_values([self.alpha, self.beta],
937945
point=point)

pymc3/tests/test_distributions_random.py

Lines changed: 12 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -127,124 +127,94 @@ def test_random_sample_returns_nd_array(self):
127127
class ScalarParameterShape(SeededTest):
128128

129129
def check(self, dist, **kwargs):
130-
nr.seed(20090425)
131130
test_cases = [(None, (1,)), (5, (5,)), ((4, 5), (4, 5))]
132131
check_dist((dist, kwargs), test_cases)
133132

134133
def test_normal(self):
135-
nr.seed(20090425)
136134
self.check(Normal, mu=0., tau=1.)
137135

138136
def test_uniform(self):
139-
nr.seed(20090425)
140137
self.check(Uniform, lower=0., upper=1.)
141138

142139
def test_half_normal(self):
143-
nr.seed(20090425)
144140
self.check(HalfNormal, tau=1.)
145141

146142
def test_wald(self):
147-
nr.seed(20090425)
148143
self.check(Wald, mu=1., lam=1., alpha=0.)
149144

150145
def test_beta(self):
151-
nr.seed(20090425)
152146
self.check(Beta, alpha=1., beta=1.)
153147

154148
def test_exponential(self):
155-
nr.seed(20090425)
156149
self.check(Exponential, lam=1.)
157150

158151
def test_laplace(self):
159-
nr.seed(20090425)
160152
self.check(Laplace, mu=1., b=1)
161153

162154
def test_lognormal(self):
163-
nr.seed(20090425)
164155
self.check(Lognormal, mu=1., tau=1.)
165156

166157
def test_student_t(self):
167-
nr.seed(20090425)
168158
self.check(StudentT, nu=5, mu=0., lam=1.)
169159

170160
def test_pareto(self):
171-
nr.seed(20090425)
172161
self.check(Pareto, alpha=0.5, m=1.)
173162

174163
def test_cauchy(self):
175-
nr.seed(20090425)
176164
self.check(Cauchy, alpha=1., beta=1.)
177165

178166
def test_half_cauchy(self):
179-
nr.seed(20090425)
180167
self.check(HalfCauchy, beta=1.)
181168

182169
def test_gamma(self):
183-
nr.seed(20090425)
184170
self.check(Gamma, alpha=1., beta=1.)
185171

186172
def test_inverse_gamma(self):
187-
nr.seed(20090425)
188173
self.check(InverseGamma, alpha=0.5, beta=0.5)
189174

190175
def test_chi_squared(self):
191-
nr.seed(20090425)
192176
self.check(ChiSquared, nu=2)
193177

194178
def test_weibull(self):
195-
nr.seed(20090425)
196179
self.check(Weibull, alpha=1., beta=1.)
197180

198181
def test_ex_gaussian(self):
199-
nr.seed(20090425)
200182
self.check(ExGaussian, mu=0., sigma=1., nu=1.)
201183

202184
def test_vonmises(self):
203-
nr.seed(20090425)
204185
self.check(VonMises, mu=0., kappa=1.)
205186

206187
def test_binomial(self):
207-
nr.seed(20090425)
208188
self.check(Binomial, n=5, p=0.5)
209189

210190
def test_beta_binomial(self):
211-
nr.seed(20090425)
212191
self.check(BetaBinomial, alpha=1., beta=1., n=1)
213192

214193
def test_bernoulli(self):
215-
nr.seed(20090425)
216194
self.check(Bernoulli, p=0.5)
217195

218196
def test_poisson(self):
219-
nr.seed(20090425)
220197
self.check(Poisson, mu=1.)
221198

222199
def test_negative_binomial(self):
223-
nr.seed(20090425)
224200
self.check(NegativeBinomial, mu=1., alpha=1.)
225201

226202
def test_constant_dist(self):
227-
nr.seed(20090425)
228203
self.check(ConstantDist, c=3)
229204

230205
def test_zero_inflated_poisson(self):
231-
nr.seed(20090425)
232206
self.check(ZeroInflatedPoisson, theta=1, psi=0.3)
233207

234208
def test_zero_inflated_negative_binomial(self):
235-
nr.seed(20090425)
236209
self.check(ZeroInflatedNegativeBinomial, mu=1., alpha=1., psi=0.3)
237210

238211
def test_discrete_uniform(self):
239-
nr.seed(20090425)
240212
self.check(DiscreteUniform, lower=0., upper=10)
241213

242214
def test_geometric(self):
243-
nr.seed(20090425)
244215
self.check(Geometric, p=0.5)
245216

246217
def test_categorical(self):
247-
nr.seed(20090425)
248218
self.check(Categorical, p=np.array([0.2, 0.3, 0.5]))
249219

250220

@@ -404,9 +374,6 @@ def test_gamma(self):
404374
self.check(Gamma, alpha=self.ones, beta=self.ones)
405375

406376
def test_inverse_gamma(self):
407-
# InverseGamma fails due to calculation of self.mean in __init__
408-
raise SkipTest(
409-
'InverseGamma fails due to calculation of self.mean in __init__')
410377
self.check(InverseGamma, alpha=self.ones / 2, beta=self.ones / 2)
411378

412379
def test_chi_squared(self):
@@ -456,9 +423,6 @@ def test_geometric(self):
456423
self.check(Geometric, p=self.ones / 2)
457424

458425
def test_categorical(self):
459-
# Categorical cannot be initialised with >1D probabilities
460-
# raise SkipTest(
461-
# 'Categorical cannot be initialised with >1D probabilities')
462426
self.check(Categorical, p=self.ones / len(self.ones))
463427

464428

@@ -523,9 +487,6 @@ def test_gamma(self):
523487
self.check(Gamma, alpha=self.ones, beta=self.ones)
524488

525489
def test_inverse_gamma(self):
526-
# InverseGamma fails due to calculation of self.mean in __init__
527-
raise SkipTest(
528-
'InverseGamma fails due to calculation of self.mean in __init__')
529490
self.check(InverseGamma, alpha=self.ones / 2, beta=self.ones / 2)
530491

531492
def test_chi_squared(self):
@@ -574,9 +535,6 @@ def test_geometric(self):
574535
self.check(Geometric, p=self.ones / 2)
575536

576537
def test_categorical(self):
577-
# Categorical cannot be initialised with >1D probabilities
578-
raise SkipTest(
579-
'Categorical cannot be initialised with >1D probabilities')
580538
self.check(Categorical, p=self.ones / self.n)
581539

582540

@@ -635,13 +593,11 @@ def ref_rand(size, lam):
635593
def test_laplace(self):
636594
def ref_rand(size, mu, b):
637595
return st.laplace.rvs(mu, b, size=size)
638-
639596
pymc3_random(Laplace, {'mu': R, 'b': Rplus}, ref_rand=ref_rand)
640597

641598
def test_lognormal(self):
642599
def ref_rand(size, mu, tau):
643600
return np.exp(mu + (tau ** -0.5) * st.norm.rvs(loc=0., scale=1., size=size))
644-
645601
pymc3_random(Lognormal, {'mu': R, 'tau': Rplusbig}, ref_rand=ref_rand)
646602

647603
def test_student_t(self):
@@ -660,16 +616,15 @@ def ref_rand(size, beta):
660616
return st.halfcauchy.rvs(scale=beta, size=size)
661617
pymc3_random(HalfCauchy, {'beta': Rplusbig}, ref_rand=ref_rand)
662618

663-
def test_gamma(self):
619+
def test_gamma_alpha_beta(self):
664620
def ref_rand(size, alpha, beta):
665621
return st.gamma.rvs(alpha, scale=1. / beta, size=size)
666-
pymc3_random(Gamma, {'alpha': Rplusbig,
667-
'beta': Rplusbig}, ref_rand=ref_rand)
622+
pymc3_random(Gamma, {'alpha': Rplusbig, 'beta': Rplusbig}, ref_rand=ref_rand)
668623

624+
def test_gamma_mu_sd(self):
669625
def ref_rand(size, mu, sd):
670626
return st.gamma.rvs(mu**2 / sd**2, scale=sd ** 2 / mu, size=size)
671-
pymc3_random(
672-
Gamma, {'mu': Rplusbig, 'sd': Rplusbig}, ref_rand=ref_rand)
627+
pymc3_random(Gamma, {'mu': Rplusbig, 'sd': Rplusbig}, ref_rand=ref_rand)
673628

674629
def test_inverse_gamma(self):
675630
def ref_rand(size, alpha, beta):
@@ -705,8 +660,7 @@ def test_binomial(self):
705660
Binomial, {'n': Nat, 'p': Unit}, ref_rand=st.binom.rvs)
706661

707662
def test_beta_binomial(self):
708-
pymc3_random_discrete(BetaBinomial,
709-
{'n': Nat, 'alpha': Rplus, 'beta': Rplus},
663+
pymc3_random_discrete(BetaBinomial, {'n': Nat, 'alpha': Rplus, 'beta': Rplus},
710664
ref_rand=self._beta_bin)
711665

712666
def _beta_bin(self, n, alpha, beta, size=None):
@@ -717,31 +671,16 @@ def test_bernoulli(self):
717671
ref_rand=lambda size, p=None: st.bernoulli.rvs(p, size=size))
718672

719673
def test_poisson(self):
720-
pymc3_random_discrete(Poisson, {'mu': Rplusbig},
721-
# Test always fails with larger sample sizes.
722-
size=500,
723-
ref_rand=st.poisson.rvs)
724-
725-
def poisson_gamma_random(alpha, mu, size):
726-
g = st.gamma.rvs(alpha, scale=alpha / mu, size=size)
727-
g[g == 0] = np.finfo(float).eps
728-
return st.poisson.rvs(g)
674+
pymc3_random_discrete(Poisson, {'mu': Rplusbig}, size=500, ref_rand=st.poisson.rvs)
729675

730676
def test_negative_binomial(self):
731-
# TODO: fix this so test passes
732-
# pymc3_random_discrete(NegativeBinomial, {'mu':Rplusbig, 'alpha':Rplusbig},
733-
# size=1000,
734-
# ref_rand=lambda size, mu=None,
735-
# alpha=None: poisson_gamma_random(alpha, mu, size))
736-
raise SkipTest(
737-
'NegativeBinomial test always fails for unknown reason.')
677+
def ref_rand(size, alpha, mu):
678+
return st.nbinom.rvs(alpha, alpha / (mu + alpha), size=size)
679+
pymc3_random_discrete(NegativeBinomial, {'mu': Rplusbig, 'alpha': Rplusbig},
680+
size=100, fails=50, ref_rand=ref_rand)
738681

739682
def test_geometric(self):
740-
pymc3_random_discrete(Geometric, {'p': Unit},
741-
# Test always fails with larger sample sizes.
742-
size=500,
743-
fails=50, # Be a bit more generous.
744-
ref_rand=nr.geometric)
683+
pymc3_random_discrete(Geometric, {'p': Unit}, size=500, fails=50, ref_rand=nr.geometric)
745684

746685
def test_discrete_uniform(self):
747686
def ref_rand(size, lower, upper):
@@ -810,5 +749,5 @@ def test_wishart(self):
810749
# st.wishart(V, df=n, size=size))
811750

812751
def test_lkj(self):
813-
# To do: generate random numbers.
752+
# TODO: generate random numbers.
814753
raise SkipTest('LJK random sampling not implemented yet.')

0 commit comments

Comments
 (0)