Skip to content

Commit

Permalink
Merge 16b00c5 into 8c87f73
Browse files Browse the repository at this point in the history
  • Loading branch information
twiecki committed Jan 14, 2017
2 parents 8c87f73 + 16b00c5 commit 2b67a91
Show file tree
Hide file tree
Showing 46 changed files with 2,127 additions and 924 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ env:
- PYTHON_VERSION=2.7 TESTCMD=" -vv --with-timer --with-coverage --cover-package=pymc3 -e test_examples -e test_distributions"
- PYTHON_VERSION=2.7 TESTCMD=" -vv --with-timer --with-coverage --cover-package=pymc3 pymc3.tests.test_distributions pymc3.tests.test_examples"
- PYTHON_VERSION=2.7 TESTCMD=" -vv --with-timer --with-coverage --cover-package=pymc3 pymc3.tests.test_distributions_random"
- PYTHON_VERSION=3.5 TESTCMD=" -vv --with-timer --with-coverage --cover-package=pymc3 -e test_examples -e test_distributions"
- PYTHON_VERSION=3.5 TESTCMD=" -vv --with-timer --with-coverage --cover-package=pymc3 pymc3.tests.test_distributions pymc3.tests.test_examples"
- PYTHON_VERSION=3.5 TESTCMD=" -vv --with-timer --with-coverage --cover-package=pymc3 pymc3.tests.test_distributions_random"
- PYTHON_VERSION=3.6 TESTCMD=" -vv --with-timer --with-coverage --cover-package=pymc3 -e test_examples -e test_distributions"
- PYTHON_VERSION=3.6 TESTCMD=" -vv --with-timer --with-coverage --cover-package=pymc3 pymc3.tests.test_distributions pymc3.tests.test_examples"
- PYTHON_VERSION=3.6 TESTCMD=" -vv --with-timer --with-coverage --cover-package=pymc3 pymc3.tests.test_distributions_random"
script:
- . ./scripts/test.sh $TESTCMD

Expand Down
7 changes: 3 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Features
Getting started
---------------

- The `PyMC3 tutorial <http://pymc-devs.github.io/pymc3/notebooks/getting_started.html>`__
- The `PyMC3 tutorial <http://pymc-devs.github.io/pymc3/notebooks/getting_started.html>`__
- `PyMC3 examples <http://pymc-devs.github.io/pymc3/examples.html>`__
and the `API reference <http://pymc-devs.github.io/pymc3/api.html>`__
- `Probabilistic Programming and Bayesian Methods for Hackers <https://github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers>`__
Expand Down Expand Up @@ -93,7 +93,7 @@ Another option is to clone the repository and install PyMC3 using
Dependencies
------------

PyMC3 is tested on Python 2.7 and 3.5 and depends on Theano, NumPy,
PyMC3 is tested on Python 2.7 and 3.6 and depends on Theano, NumPy,
SciPy, Pandas, and Matplotlib (see ``requirements.txt`` for version
information).

Expand Down Expand Up @@ -154,9 +154,8 @@ Sponsors
.. |Build Status| image:: https://travis-ci.org/pymc-devs/pymc3.png?branch=master
:target: https://travis-ci.org/pymc-devs/pymc3
.. |Coverage| image:: https://coveralls.io/repos/github/pymc-devs/pymc3/badge.svg?branch=master
:target: https://coveralls.io/github/pymc-devs/pymc3?branch=master
:target: https://coveralls.io/github/pymc-devs/pymc3?branch=master
.. |NumFOCUS| image:: http://www.numfocus.org/uploads/6/0/6/9/60696727/1457562110.png
:target: http://www.numfocus.org/
.. |Quantopian| image:: https://raw.githubusercontent.com/pymc-devs/pymc3/master/docs/quantopianlogo.jpg
:target: https://quantopian.com

121 changes: 56 additions & 65 deletions docs/source/notebooks/convolutional_vae_keras_advi.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pymc3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .blocking import *
from .distributions import *
from .external import *
from .math import logsumexp, logit, invlogit
from .model import *
from .stats import *
Expand Down
6 changes: 5 additions & 1 deletion pymc3/distributions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from .continuous import HalfCauchy
from .continuous import Gamma
from .continuous import Weibull
from .continuous import Bound
from .continuous import HalfStudentT
from .continuous import Lognormal
from .continuous import ChiSquared
Expand All @@ -38,6 +37,7 @@
from .discrete import Geometric
from .discrete import Categorical

from .distribution import Bound
from .distribution import DensityDist
from .distribution import Distribution
from .distribution import Continuous
Expand All @@ -60,6 +60,8 @@
from .timeseries import AR1
from .timeseries import GaussianRandomWalk
from .timeseries import GARCH11
from .timeseries import MvGaussianRandomWalk
from .timeseries import MvStudentTRandomWalk

from .transforms import transform
from .transforms import stick_breaking
Expand Down Expand Up @@ -116,6 +118,8 @@
'LKJCorr',
'AR1',
'GaussianRandomWalk',
'MvGaussianRandomWalk',
'MvStudentTRandomWalk',
'GARCH11',
'SkewNormal',
'Mixture',
Expand Down
114 changes: 3 additions & 111 deletions pymc3/distributions/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

from . import transforms
from .dist_math import bound, logpow, gammaln, betaln, std_cdf, i0, i1
from .distribution import Continuous, draw_values, generate_samples
from .distribution import Continuous, draw_values, generate_samples, Bound

__all__ = ['Uniform', 'Flat', 'Normal', 'Beta', 'Exponential', 'Laplace',
'StudentT', 'Cauchy', 'HalfCauchy', 'Gamma', 'Weibull',
'Bound', 'HalfStudentT', 'StudentTpos', 'Lognormal', 'ChiSquared',
'HalfStudentT', 'StudentTpos', 'Lognormal', 'ChiSquared',
'HalfNormal', 'Wald', 'Pareto', 'InverseGamma', 'ExGaussian',
'VonMises', 'SkewNormal']

Expand Down Expand Up @@ -147,7 +147,7 @@ def logp(self, value):
lower = self.lower
upper = self.upper
return bound(-tt.log(upper - lower),
value >= lower, value <= upper)
value >= lower, value <= upper)


class Flat(Continuous):
Expand Down Expand Up @@ -1094,114 +1094,6 @@ def logp(self, value):
value >= 0, alpha > 0, beta > 0)


class Bounded(Continuous):
R"""
An upper, lower or upper+lower bounded distribution
Parameters
----------
distribution : pymc3 distribution
Distribution to be transformed into a bounded distribution
lower : float (optional)
Lower bound of the distribution, set to -inf to disable.
upper : float (optional)
Upper bound of the distribibution, set to inf to disable.
tranform : 'infer' or object
If 'infer', infers the right transform to apply from the supplied bounds.
If transform object, has to supply .forward() and .backward() methods.
See pymc3.distributions.transforms for more information.
"""

def __init__(self, distribution, lower, upper, transform='infer', *args, **kwargs):
self.dist = distribution.dist(*args, **kwargs)
self.__dict__.update(self.dist.__dict__)
self.__dict__.update(locals())

if hasattr(self.dist, 'mode'):
self.mode = self.dist.mode

if transform == 'infer':
self.transform, self.testval = self._infer(lower, upper)

def _infer(self, lower, upper):
"""Infer proper transforms for the variable, and adjust test_value.
In particular, this deals with the case where lower or upper may be +/-inf, or an
`ndarray` or a `theano.tensor.TensorVariable`
"""
if isinstance(upper, tt.TensorVariable):
_upper = upper.tag.test_value
else:
_upper = upper
if isinstance(lower, tt.TensorVariable):
_lower = lower.tag.test_value
else:
_lower = lower

testval = self.dist.default()
if not self._isinf(_lower) and not self._isinf(_upper):
transform = transforms.interval(lower, upper)
if (testval <= _lower).any() or (testval >= _upper).any():
testval = 0.5 * (_upper + _lower)
elif not self._isinf(_lower) and self._isinf(_upper):
transform = transforms.lowerbound(lower)
if (testval <= _lower).any():
testval = lower + 1
elif self._isinf(_lower) and not self._isinf(_upper):
transform = transforms.upperbound(upper)
if (testval >= _upper).any():
testval = _upper - 1
else:
transform = None
return transform, testval

def _isinf(self, value):
"""Checks whether the value is +/-inf, or else returns 0 (even if an ndarray with
entries that are +/-inf)
"""
try:
return bool(np.isinf(value))
except ValueError:
return False

def _random(self, lower, upper, point=None, size=None):
samples = np.zeros(size).flatten()
i, n = 0, len(samples)
while i < len(samples):
sample = self.dist.random(point=point, size=n)
select = sample[np.logical_and(sample > lower, sample <= upper)]
samples[i:(i + len(select))] = select[:]
i += len(select)
n -= len(select)
if size is not None:
return np.reshape(samples, size)
else:
return samples

def random(self, point=None, size=None, repeat=None):
lower, upper = draw_values([self.lower, self.upper], point=point)
return generate_samples(self._random, lower, upper, point,
dist_shape=self.shape,
size=size)

def logp(self, value):
return bound(self.dist.logp(value),
value >= self.lower, value <= self.upper)


def Bound(distribution, lower=-np.inf, upper=np.inf):
class _BoundedDist(Bounded):
def __init__(self, *args, **kwargs):
first, args = args[0], args[1:]
super(self, _BoundedDist).__init__(first, distribution, lower, upper, *args, **kwargs)

@classmethod
def dist(cls, *args, **kwargs):
return Bounded.dist(distribution, lower, upper, *args, **kwargs)

return _BoundedDist


def StudentTpos(*args, **kwargs):
warnings.warn("StudentTpos has been deprecated. In future, use HalfStudentT instead.",
DeprecationWarning)
Expand Down
14 changes: 9 additions & 5 deletions pymc3/distributions/discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from scipy import stats

from .dist_math import bound, factln, binomln, betaln, logpow
from .distribution import Discrete, draw_values, generate_samples
from .distribution import Discrete, draw_values, generate_samples, reshape_sampled

__all__ = ['Binomial', 'BetaBinomial', 'Bernoulli', 'Poisson',
'NegativeBinomial', 'ConstantDist', 'Constant', 'ZeroInflatedPoisson',
Expand Down Expand Up @@ -250,7 +250,7 @@ def random(self, point=None, size=None, repeat=None):
dist_shape=self.shape,
size=size)
g[g == 0] = np.finfo(float).eps # Just in case
return stats.poisson.rvs(g)
return reshape_sampled(stats.poisson.rvs(g), size, self.shape)

def logp(self, value):
mu = self.mu
Expand Down Expand Up @@ -441,9 +441,11 @@ def logp(self, value):
c = self.c
return bound(0, tt.eq(value, c))


def ConstantDist(*args, **kwargs):
import warnings
warnings.warn("ConstantDist has been deprecated. In future, use Constant instead.",
DeprecationWarning)
DeprecationWarning)
return Constant(*args, **kwargs)


Expand Down Expand Up @@ -489,7 +491,8 @@ def random(self, point=None, size=None, repeat=None):
g = generate_samples(stats.poisson.rvs, theta,
dist_shape=self.shape,
size=size)
return g * (np.random.random(np.squeeze(g.shape)) < psi)
sampled = g * (np.random.random(np.squeeze(g.shape)) < psi)
return reshape_sampled(sampled, size, self.shape)

def logp(self, value):
return tt.switch(value > 0,
Expand Down Expand Up @@ -543,7 +546,8 @@ def random(self, point=None, size=None, repeat=None):
dist_shape=self.shape,
size=size)
g[g == 0] = np.finfo(float).eps # Just in case
return stats.poisson.rvs(g) * (np.random.random(np.squeeze(g.shape)) < psi)
sampled = stats.poisson.rvs(g) * (np.random.random(np.squeeze(g.shape)) < psi)
return reshape_sampled(sampled, size, self.shape)

def logp(self, value):
return tt.switch(value > 0,
Expand Down
Loading

0 comments on commit 2b67a91

Please sign in to comment.