Skip to content
Snippets Groups Projects
Commit 8ce03519 authored by Sylvia Biscoveanu's avatar Sylvia Biscoveanu
Browse files

Merge branch 'pymc3_to_pymc4' into 'master'

Switch PyMC3 to PyMC (which is the new name starting from PyMC v4.0.0)

See merge request !1117
parents 8927f8ba 44267c50
No related branches found
No related tags found
1 merge request!1117Switch PyMC3 to PyMC (which is the new name starting from PyMC v4.0.0)
Pipeline #451249 passed
......@@ -21,7 +21,7 @@ from .nestle import Nestle
from .polychord import PyPolyChord
from .ptemcee import Ptemcee
from .ptmcmc import PTMCMCSampler
from .pymc3 import Pymc3
from .pymc import Pymc
from .pymultinest import Pymultinest
from .ultranest import Ultranest
from .zeus import Zeus
......@@ -38,7 +38,7 @@ IMPLEMENTED_SAMPLERS = {
"nestle": Nestle,
"ptemcee": Ptemcee,
"ptmcmcsampler": PTMCMCSampler,
"pymc3": Pymc3,
"pymc": Pymc,
"pymultinest": Pymultinest,
"pypolychord": PyPolyChord,
"ultranest": Ultranest,
......
This diff is collapsed.
......@@ -69,7 +69,7 @@ MCMC samplers
- bilby-mcmc :code:`bilby.bilby_mcmc.sampler.Bilby_MCMC`
- emcee :code:`bilby.core.sampler.emcee.Emcee`
- ptemcee :code:`bilby.core.sampler.ptemcee.Ptemcee`
- pymc3 :code:`bilby.core.sampler.pymc3.Pymc3`
- pymc :code:`bilby.core.sampler.pymc.Pymc`
- zeus :code:`bilby.core.sampler.zeus.Zeus`
......
......@@ -11,7 +11,7 @@ import numpy as np
from bilby.core.likelihood import GaussianLikelihood
# A few simple setup steps
label = "linear_regression_pymc3"
label = "linear_regression_pymc"
outdir = "outdir"
bilby.utils.check_directory_exists_and_if_not_mkdir(outdir)
......@@ -58,7 +58,7 @@ priors["c"] = bilby.core.prior.Uniform(-2, 2, "c")
result = bilby.run_sampler(
likelihood=likelihood,
priors=priors,
sampler="pymc3",
sampler="pymc",
injection_parameters=injection_parameters,
outdir=outdir,
draws=2000,
......
......@@ -11,10 +11,10 @@ would give equivalent results as using the pre-defined 'Gaussian Likelihood'
import bilby
import matplotlib.pyplot as plt
import numpy as np
import pymc3 as pm
import pymc as pm
# A few simple setup steps
label = "linear_regression_pymc3_custom_likelihood"
label = "linear_regression_pymc_custom_likelihood"
outdir = "outdir"
bilby.utils.check_directory_exists_and_if_not_mkdir(outdir)
......@@ -50,7 +50,7 @@ fig.savefig("{}/{}_data.png".format(outdir, label))
# Parameter estimation: we now define a Gaussian Likelihood class relevant for
# our model.
class GaussianLikelihoodPyMC3(bilby.core.likelihood.GaussianLikelihood):
class GaussianLikelihoodPyMC(bilby.core.likelihood.GaussianLikelihood):
def __init__(self, x, y, sigma, func):
"""
A general Gaussian likelihood - the parameters are inferred from the
......@@ -68,45 +68,44 @@ class GaussianLikelihoodPyMC3(bilby.core.likelihood.GaussianLikelihood):
will require a prior and will be sampled over (unless a fixed
value is given).
"""
super(GaussianLikelihoodPyMC3, self).__init__(x=x, y=y, func=func, sigma=sigma)
super(GaussianLikelihoodPyMC, self).__init__(x=x, y=y, func=func, sigma=sigma)
def log_likelihood(self, sampler=None):
"""
Parameters
----------
sampler: :class:`bilby.core.sampler.Pymc3`
sampler: :class:`bilby.core.sampler.Pymc`
A Sampler object must be passed containing the prior distributions
and PyMC3 :class:`~pymc3.Model` to use as a context manager.
If this is not passed, the super class is called and the regular
likelihood is evaluated.
"""
from bilby.core.sampler import Pymc3
from bilby.core.sampler import Pymc
if not isinstance(sampler, Pymc3):
print(sampler, type(sampler))
return super(GaussianLikelihoodPyMC3, self).log_likelihood()
if not isinstance(sampler, Pymc):
return super(GaussianLikelihoodPyMC, self).log_likelihood()
if not hasattr(sampler, "pymc3_model"):
raise AttributeError("Sampler has not PyMC3 model attribute")
if not hasattr(sampler, "pymc_model"):
raise AttributeError("Sampler has not PyMC model attribute")
with sampler.pymc3_model:
mdist = sampler.pymc3_priors["m"]
cdist = sampler.pymc3_priors["c"]
with sampler.pymc_model:
mdist = sampler.pymc_priors["m"]
cdist = sampler.pymc_priors["c"]
mu = model(time, mdist, cdist)
# set the likelihood distribution
pm.Normal("likelihood", mu=mu, sd=self.sigma, observed=self.y)
pm.Normal("likelihood", mu=mu, sigma=self.sigma, observed=self.y)
# Now lets instantiate a version of our GaussianLikelihood, giving it
# the time, data and signal model
likelihood = GaussianLikelihoodPyMC3(time, data, sigma, model)
likelihood = GaussianLikelihoodPyMC(time, data, sigma, model)
# Define a custom prior for one of the parameter for use with PyMC3
class PyMC3UniformPrior(bilby.core.prior.Uniform):
# Define a custom prior for one of the parameter for use with PyMC
class PyMCUniformPrior(bilby.core.prior.Uniform):
def __init__(self, minimum, maximum, name=None, latex_label=None):
"""
Uniform prior with bounds (should be equivalent to bilby.prior.Uniform)
......@@ -124,10 +123,10 @@ class PyMC3UniformPrior(bilby.core.prior.Uniform):
float or array to be passed to the superclass.
"""
from bilby.core.sampler import Pymc3
from bilby.core.sampler import Pymc
if not isinstance(sampler, Pymc3):
return super(PyMC3UniformPrior, self).ln_prob(sampler)
if not isinstance(sampler, Pymc):
return super(PyMCUniformPrior, self).ln_prob(sampler)
return pm.Uniform(self.name, lower=self.minimum, upper=self.maximum)
......@@ -136,13 +135,13 @@ class PyMC3UniformPrior(bilby.core.prior.Uniform):
# We make a prior
priors = dict()
priors["m"] = bilby.core.prior.Uniform(0, 5, "m")
priors["c"] = PyMC3UniformPrior(-2, 2, "c")
priors["c"] = PyMCUniformPrior(-2, 2, "c")
# And run sampler
result = bilby.run_sampler(
likelihood=likelihood,
priors=priors,
sampler="pymc3",
sampler="pymc",
draws=1000,
tune=1000,
discard_tuned_samples=True,
......
......@@ -3,7 +3,7 @@ dynesty
emcee
nestle
ptemcee
pymc3>=3.6
pymc>=4.0.0
pymultinest
kombine
ultranest>=3.0.0
......
import unittest
from unittest.mock import MagicMock
import pytest
import bilby
@pytest.mark.xfail(
raises=AttributeError,
reason="Dependency issue with pymc3 causes attribute error on import",
)
class TestPyMC3(unittest.TestCase):
class TestPyMC(unittest.TestCase):
def setUp(self):
self.likelihood = MagicMock()
self.priors = bilby.core.prior.PriorDict(
dict(a=bilby.core.prior.Uniform(0, 1), b=bilby.core.prior.Uniform(0, 1))
)
self.sampler = bilby.core.sampler.Pymc3(
self.sampler = bilby.core.sampler.Pymc(
self.likelihood,
self.priors,
outdir="outdir",
......@@ -37,7 +31,7 @@ class TestPyMC3(unittest.TestCase):
step=None,
init="auto",
n_init=200000,
start=None,
initvals=None,
trace=None,
chain_idx=0,
chains=2,
......@@ -61,7 +55,7 @@ class TestPyMC3(unittest.TestCase):
step=None,
init="auto",
n_init=200000,
start=None,
initvals=None,
trace=None,
chain_idx=0,
chains=2,
......
......@@ -54,7 +54,7 @@ _sampler_kwargs = dict(
frac_threshold=0.5,
),
PTMCMCSampler=dict(Niter=101, burn=2, isave=100),
# pymc3=dict(draws=50, tune=50, n_init=250), removed until testing issue can be resolved
pymc=dict(draws=50, tune=50, n_init=250),
pymultinest=dict(nlive=100),
pypolychord=dict(nlive=100),
ultranest=dict(nlive=100, temporary_directory=False),
......@@ -65,7 +65,7 @@ sampler_imports = dict(
dynamic_dynesty="dynesty"
)
no_pool_test = ["dnest4", "pymultinest", "nestle", "ptmcmcsampler", "pypolychord", "ultranest"]
no_pool_test = ["dnest4", "pymultinest", "nestle", "ptmcmcsampler", "pypolychord", "ultranest", "pymc"]
def slow_func(x, m, c):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment