Skip to content

Commit

Permalink
Run black on distributions/ and */__init__.py (#4139)
Browse files Browse the repository at this point in the history
* Run black on distributions/ and */__init__.py

* Run black on step_methods/

* Revert two files

* Update pymc3/distributions/bound.py

Co-authored-by: Marco Gorelli <m.e.gorelli@gmail.com>

* Revert blackened metropolis.py

* Revert blackened files

* Fix error in quadpotential.py

Co-authored-by: Marco Gorelli <m.e.gorelli@gmail.com>
Co-authored-by: Ali Septiandri <ali.septiandri@revolut.com>
  • Loading branch information
3 people authored Sep 29, 2020
1 parent 77fe82a commit 0bd2d65
Show file tree
Hide file tree
Showing 21 changed files with 347 additions and 415 deletions.
151 changes: 76 additions & 75 deletions pymc3/distributions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,78 +100,79 @@

from .bound import Bound

__all__ = ['Uniform',
'Flat',
'HalfFlat',
'TruncatedNormal',
'Normal',
'Beta',
'Kumaraswamy',
'Exponential',
'Laplace',
'StudentT',
'Cauchy',
'HalfCauchy',
'Gamma',
'Weibull',
'Bound',
'Lognormal',
'HalfStudentT',
'ChiSquared',
'HalfNormal',
'Wald',
'Pareto',
'InverseGamma',
'ExGaussian',
'VonMises',
'Binomial',
'BetaBinomial',
'Bernoulli',
'Poisson',
'NegativeBinomial',
'ConstantDist',
'Constant',
'ZeroInflatedPoisson',
'ZeroInflatedNegativeBinomial',
'ZeroInflatedBinomial',
'DiscreteUniform',
'Geometric',
'Categorical',
'OrderedLogistic',
'DensityDist',
'Distribution',
'Continuous',
'Discrete',
'NoDistribution',
'TensorType',
'MvNormal',
'MatrixNormal',
'KroneckerNormal',
'MvStudentT',
'Dirichlet',
'Multinomial',
'Wishart',
'WishartBartlett',
'LKJCholeskyCov',
'LKJCorr',
'AR1',
'AR',
'GaussianRandomWalk',
'MvGaussianRandomWalk',
'MvStudentTRandomWalk',
'GARCH11',
'SkewNormal',
'Mixture',
'NormalMixture',
'Triangular',
'DiscreteWeibull',
'Gumbel',
'Logistic',
'LogitNormal',
'Interpolated',
'Bound',
'Rice',
'Moyal',
'Simulator',
'fast_sample_posterior_predictive'
]
__all__ = [
"Uniform",
"Flat",
"HalfFlat",
"TruncatedNormal",
"Normal",
"Beta",
"Kumaraswamy",
"Exponential",
"Laplace",
"StudentT",
"Cauchy",
"HalfCauchy",
"Gamma",
"Weibull",
"Bound",
"Lognormal",
"HalfStudentT",
"ChiSquared",
"HalfNormal",
"Wald",
"Pareto",
"InverseGamma",
"ExGaussian",
"VonMises",
"Binomial",
"BetaBinomial",
"Bernoulli",
"Poisson",
"NegativeBinomial",
"ConstantDist",
"Constant",
"ZeroInflatedPoisson",
"ZeroInflatedNegativeBinomial",
"ZeroInflatedBinomial",
"DiscreteUniform",
"Geometric",
"Categorical",
"OrderedLogistic",
"DensityDist",
"Distribution",
"Continuous",
"Discrete",
"NoDistribution",
"TensorType",
"MvNormal",
"MatrixNormal",
"KroneckerNormal",
"MvStudentT",
"Dirichlet",
"Multinomial",
"Wishart",
"WishartBartlett",
"LKJCholeskyCov",
"LKJCorr",
"AR1",
"AR",
"GaussianRandomWalk",
"MvGaussianRandomWalk",
"MvStudentTRandomWalk",
"GARCH11",
"SkewNormal",
"Mixture",
"NormalMixture",
"Triangular",
"DiscreteWeibull",
"Gumbel",
"Logistic",
"LogitNormal",
"Interpolated",
"Bound",
"Rice",
"Moyal",
"Simulator",
"fast_sample_posterior_predictive",
]
41 changes: 10 additions & 31 deletions pymc3/distributions/bound.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,13 @@ def _random(self, lower, upper, point=None, size=None):
upper = np.asarray(upper)
if lower.size > 1 or upper.size > 1:
raise ValueError(
"Drawing samples from distributions with "
"array-valued bounds is not supported."
"Drawing samples from distributions with array-valued bounds is not supported."
)
total_size = np.prod(size).astype(np.int)
samples = []
s = 0
while s < total_size:
sample = np.atleast_1d(
self._wrapped.random(point=point, size=total_size)
).flatten()
sample = np.atleast_1d(self._wrapped.random(point=point, size=total_size)).flatten()

select = sample[np.logical_and(sample >= lower, sample <= upper)]
samples.append(select)
Expand Down Expand Up @@ -128,7 +125,7 @@ def random(self, point=None, size=None):
upper,
dist_shape=self.shape,
size=size,
not_broadcast_kwargs={'point': point},
not_broadcast_kwargs={"point": point},
)
elif self.lower is not None:
lower = draw_values([self.lower], point=point, size=size)
Expand All @@ -138,7 +135,7 @@ def random(self, point=None, size=None):
np.inf,
dist_shape=self.shape,
size=size,
not_broadcast_kwargs={'point': point},
not_broadcast_kwargs={"point": point},
)
else:
upper = draw_values([self.upper], point=point, size=size)
Expand All @@ -148,7 +145,7 @@ def random(self, point=None, size=None):
upper,
dist_shape=self.shape,
size=size,
not_broadcast_kwargs={'point': point},
not_broadcast_kwargs={"point": point},
)


Expand All @@ -168,9 +165,7 @@ def __init__(self, distribution, lower, upper, transform="infer", *args, **kwarg
if lower is not None:
default = lower + 1

super().__init__(
distribution, lower, upper, default, *args, transform=transform, **kwargs
)
super().__init__(distribution, lower, upper, default, *args, transform=transform, **kwargs)


class _ContinuousBounded(_Bounded, Continuous):
Expand Down Expand Up @@ -215,9 +210,7 @@ def __init__(self, distribution, lower, upper, transform="infer", *args, **kwarg
else:
default = None

super().__init__(
distribution, lower, upper, default, *args, transform=transform, **kwargs
)
super().__init__(distribution, lower, upper, default, *args, transform=transform, **kwargs)


class Bound:
Expand Down Expand Up @@ -283,23 +276,11 @@ def __call__(self, name, *args, **kwargs):
transform = kwargs.pop("transform", "infer")
if issubclass(self.distribution, Continuous):
return _ContinuousBounded(
name,
self.distribution,
self.lower,
self.upper,
transform,
*args,
**kwargs
name, self.distribution, self.lower, self.upper, transform, *args, **kwargs
)
elif issubclass(self.distribution, Discrete):
return _DiscreteBounded(
name,
self.distribution,
self.lower,
self.upper,
transform,
*args,
**kwargs
name, self.distribution, self.lower, self.upper, transform, *args, **kwargs
)
else:
raise ValueError("Distribution is neither continuous nor discrete.")
Expand All @@ -311,8 +292,6 @@ def dist(self, *args, **kwargs):
)

elif issubclass(self.distribution, Discrete):
return _DiscreteBounded.dist(
self.distribution, self.lower, self.upper, *args, **kwargs
)
return _DiscreteBounded.dist(self.distribution, self.lower, self.upper, *args, **kwargs)
else:
raise ValueError("Distribution is neither continuous nor discrete.")
12 changes: 3 additions & 9 deletions pymc3/distributions/shape_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ def _check_shape_type(shape):
raise TypeError(f"Value {s} is not a valid integer")
out.append(o)
except Exception:
raise TypeError(
f"Supplied value {shape} does not represent a valid shape"
)
raise TypeError(f"Supplied value {shape} does not represent a valid shape")
return tuple(out)


Expand Down Expand Up @@ -172,10 +170,7 @@ def broadcast_dist_samples_shape(shapes, size=None):
shapes = [_check_shape_type(s) for s in shapes]
_size = to_tuple(size)
# samples shapes without the size prepend
sp_shapes = [
s[len(_size) :] if _size == s[: min([len(_size), len(s)])] else s
for s in shapes
]
sp_shapes = [s[len(_size) :] if _size == s[: min([len(_size), len(s)])] else s for s in shapes]
try:
broadcast_shape = shapes_broadcasting(*sp_shapes, raise_exception=True)
except ValueError:
Expand Down Expand Up @@ -277,8 +272,7 @@ def get_broadcastable_dist_samples(
out_shape = broadcast_dist_samples_shape(p_shapes, size=size)
# samples shapes without the size prepend
sp_shapes = [
s[len(_size) :] if _size == s[: min([len(_size), len(s)])] else s
for s in p_shapes
s[len(_size) :] if _size == s[: min([len(_size), len(s)])] else s for s in p_shapes
]
broadcast_shape = shapes_broadcasting(*sp_shapes, raise_exception=True)
broadcastable_samples = []
Expand Down
1 change: 0 additions & 1 deletion pymc3/distributions/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ def _str_repr(self, name=None, dist=None, formatting="plain"):
return f"{name} ~ Simulator({function}({params}), {distance}, {sum_stat})"



def identity(x):
"""Identity function, used as a summary statistics."""
return x
Expand Down
38 changes: 25 additions & 13 deletions pymc3/distributions/special.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
from theano.scalar.basic_scipy import GammaLn, Psi
from theano import scalar

__all__ = ['gammaln', 'multigammaln', 'psi', 'log_i0']
__all__ = ["gammaln", "multigammaln", "psi", "log_i0"]

scalar_gammaln = GammaLn(scalar.upgrade_to_float, name='scalar_gammaln')
gammaln = tt.Elemwise(scalar_gammaln, name='gammaln')
scalar_gammaln = GammaLn(scalar.upgrade_to_float, name="scalar_gammaln")
gammaln = tt.Elemwise(scalar_gammaln, name="gammaln")


def multigammaln(a, p):
Expand All @@ -33,21 +33,33 @@ def multigammaln(a, p):
degrees of freedom. p > 0
"""
i = tt.arange(1, p + 1)
return (p * (p - 1) * tt.log(np.pi) / 4.
+ tt.sum(gammaln(a + (1. - i) / 2.), axis=0))
return p * (p - 1) * tt.log(np.pi) / 4.0 + tt.sum(gammaln(a + (1.0 - i) / 2.0), axis=0)


def log_i0(x):
"""
Calculates the logarithm of the 0 order modified Bessel function of the first kind""
"""
return tt.switch(tt.lt(x, 5), tt.log1p(x**2. / 4. + x**4. / 64. + x**6. / 2304.
+ x**8. / 147456. + x**10. / 14745600.
+ x**12. / 2123366400.),
x - 0.5 * tt.log(2. * np.pi * x) + tt.log1p(1. / (8. * x)
+ 9. / (128. * x**2.) + 225. / (3072. * x**3.)
+ 11025. / (98304. * x**4.)))
return tt.switch(
tt.lt(x, 5),
tt.log1p(
x ** 2.0 / 4.0
+ x ** 4.0 / 64.0
+ x ** 6.0 / 2304.0
+ x ** 8.0 / 147456.0
+ x ** 10.0 / 14745600.0
+ x ** 12.0 / 2123366400.0
),
x
- 0.5 * tt.log(2.0 * np.pi * x)
+ tt.log1p(
1.0 / (8.0 * x)
+ 9.0 / (128.0 * x ** 2.0)
+ 225.0 / (3072.0 * x ** 3.0)
+ 11025.0 / (98304.0 * x ** 4.0)
),
)


scalar_psi = Psi(scalar.upgrade_to_float, name='scalar_psi')
psi = tt.Elemwise(scalar_psi, name='psi')
scalar_psi = Psi(scalar.upgrade_to_float, name="scalar_psi")
psi = tt.Elemwise(scalar_psi, name="psi")
Loading

0 comments on commit 0bd2d65

Please sign in to comment.