Add PyMC3 sampler
I have started an attempt to add PyMC3 as a sampler option.
At the moment this can be used with the Uniform
prior, for which I have added a new pymc3
method, and the GaussianLikelihood
for which I have added a pymc3_likelihood
method. I can convert the other prior classes to also have pymc3
methods too.
At the moment I've just got PyMC3 to run with the default NUTS sampler, but can change the _run_external_sampler
method to take in other allowed sampler keyword args.
I've tested this with a linear regression example and it works!
Merge request reports
Activity
This is great! I have a couple of questions, really just catching up.
-
What is your reason for adding the
pymc3
specific things to the likelihood and prior modules and not adding them as methods of thePymc3
class? My initial thought would be that if we can put them in the sampler class it will keep it nicely self contained. -
Is there a fundamental reason why this needs a uniform prior? If so could you use the rescaling we have and set the prior that the sampler sees to always be essentially
Uniform(0, 1)
?
-
added 1 commit
- 3d862da7 - Move setting of PyMC3 prior into Prior class
Hi @colm.talbot,
What is your reason for adding the pymc3 specific things to the likelihood and prior modules and not adding them as methods of the Pymc3 class? My initial thought would be that if we can put them in the sampler class it will keep it nicely self contained.
It may be possible to put everything in the
Pymc3
class, but I was trying to keep prior-y things with the priors and likelihood-y things with the likelihood.Is there a fundamental reason why this needs a uniform prior? If so could you use the rescaling we have and set the prior that the sampler sees to always be essentially Uniform(0, 1)?
The uniform prior was just the first one I thought to test out, but any of the other prior functions could also be used (when I've added the
pymc3_prior
method to them).added 1 commit
- 3346003e - prior.py: add pymc3_prior method to Gaussian prior
added 1 commit
- 7d5cdb3b - prior.py: add pymc3_prior method to Gaussian prior
added 1 commit
- 7c16ca73 - Remove use_pymc3 arguments from Prior class as not required
I've moved the setting of the PyMC3 priors from the
Prior
class into thePymc3
sampler class. If you required a custom prior that is not one of the defined PyMC3 distributions then the specific prior class will have to have apymc3_prior
method defined, but otherwise no specific method is required. Although the class will have to have attributes that are the same names as the arguments required for that specific PyMC3 distribution.@matthew-pitkin I have to agree with @colm.talbot here. We don't want the all the pymc3 related things in the general Prior classes. We should rather build an interface, i.e. like a bunch of conversion functions, that take in tupak priors and convert them into pymc3 priors.
added 50 commits
-
c86b472d...b9c37fdf - 42 commits from branch
Monash:master
- 5cd6307e - Adding PyMC3 sampler:
- 3cd7e775 - Add working example of using the PyMC3 sampler
- 176e2c29 - prior.py: do not need to add new sample method
- 80b5b49e - linear_regression_pymc3.py: use draws kwarg
- d7606585 - Move setting of PyMC3 prior into Prior class
- f750c5a6 - prior.py: add pymc3_prior method to Gaussian prior
- ffd9726e - Remove use_pymc3 arguments from Prior class as not required
- 40742974 - Move setting of PyMC3 priors:
Toggle commit list-
c86b472d...b9c37fdf - 42 commits from branch
added 1 commit
- aa2bc23f - likelihood.py: add pymc3_likelihood method for PoissonLikelihood class
added 1 commit
- b4e43ab5 - Start adding custom PyMC3 priors for distributions that are not in PyMC3
@colm.talbot @moritz.huebner in b4e43ab5 I've started along the way of adding conversion functions in the
Pymc3
class for the current tupak Priors. Let me know what you think of the way I've set it out - there may be a neater way for the mappings.added 1 commit
- 404f2e2b - sampler.py: add mapping of tupak PowerLaw prior to PyMC3 prior
Hi Matt,
Wow this looks awesome. Does this mean we'd be able to access the NUTS sampler? I'm 100% for having access to pymc3.
Looking through the history, I echo Colm and Moritz in that we want, as much as possible, to avoid sampler-specifics in other parts of the code base. I see now that it looks like there is an added
pymc3_likelihood
in the Gaussian and Poisson likelihoods. Is there anyway to get around doing this for every likelihood?If there isn't then I'd suggest we instead subclass
GaussianLikelihood
toGaussianLikelihoodForPyMC3
or something. Only because, we'd really like people to see theGaussianLikehood
in its simplest form (which can be used by dynesty etc).@gregory.ashton Yes, we can now access the NUTS sampler (although, as a major caveat, it will take some [not sure how much] work to make the GW likelihood function work with PyMC3)!
I can create a subclass likelihood for PyMC3. Would it be worth trying to make this as invisible to the user as possible, i.e., if the user defines a
GaussianLikelihood
by wants to use PyMC3 as the sampler, then thePymc3
_run_external_sampler
just internally converts to the appropriate PyMC3 likelihood class? I'll change it to this for now.I can create a subclass likelihood for PyMC3. Would it be worth trying to make this as invisible to the user as possible, i.e., if the user defines a GaussianLikelihood by wants to use PyMC3 as the sampler, then the Pymc3 _run_external_sampler just internally converts to the appropriate PyMC3 likelihood class? I'll change it to this for now.
Actually, I'll entirely move the PyMC3 likelihood into the sampler class for the pre-defined tupak likelihood. I'll just have mappings between the tupak
GaussianLikelihood
andPoissonLikelihood
and their equivalent PyMC3 functions like I've done for the priors.