Giter Club home page Giter Club logo

pocomc's Introduction

logo

pocoMC is a Python implementation of the Preconditioned Monte Carlo method for accelerated Bayesian inference

License: GPL v3 Documentation Status

Getting started

Brief introduction

pocoMC utilises a Normalising Flow in order to precondition the target distribution by removing any correlations between its parameters. The code then generates posterior samples, that can be used for parameter estimation, using a powerful adaptive Sequential Monte Carlo algorithm manifesting a sampling effiency that can be orders of magnitude higher than without precondition. Furthermore, pocoMC also provides an unbiased estimate of the model evidence that can be used for the task of Bayesian model comparison.

Documentation

Read the docs at pocomc.readthedocs.io for more information, examples and tutorials.

Installation

To install pocomc using pip run:

pip install pocomc

or, to install from source:

git clone https://github.com/minaskar/pocomc.git
cd pocomc
python setup.py install

Basic example

For instance, if you wanted to draw samples from a 10-dimensional Rosenbrock distribution with a uniform prior, you would do something like:

import pocomc as pc
import numpy as np
from scipy.stats import uniform

n_dim = 10  # Number of dimensions

prior = pc.Prior(n_dim*[uniform(-10.0, 20.0)]) # U(-10,10)

def log_likelihood(x):
    return -np.sum(10.0*(x[:,::2]**2.0 - x[:,1::2])**2.0 \
            + (x[:,::2] - 1.0)**2.0, axis=1)

sampler = pc.Sampler(
    prior=prior,
    likelihood=log_likelihood,
    vectorize=True,
)
sampler.run()

samples, weights, logl, logp = sampler.posterior() # Weighted posterior samples

logz, logz_err = sampler.evidence() # Bayesian model evidence estimate and uncertainty

Attribution & Citation

Please cite the following papers if you found this code useful in your research:

@article{karamanis2022accelerating,
    title={Accelerating astronomical and cosmological inference with preconditioned Monte Carlo},
    author={Karamanis, Minas and Beutler, Florian and Peacock, John A and Nabergoj, David and Seljak, Uro{\v{s}}},
    journal={Monthly Notices of the Royal Astronomical Society},
    volume={516},
    number={2},
    pages={1644--1653},
    year={2022},
    publisher={Oxford University Press}
}

@article{karamanis2022pocomc,
    title={pocoMC: A Python package for accelerated Bayesian inference in astronomy and cosmology},
    author={Karamanis, Minas and Nabergoj, David and Beutler, Florian and Peacock, John A and Seljak, Uros},
    journal={arXiv preprint arXiv:2207.05660},
    year={2022}
}

Licence

Copyright 2022-Now Minas Karamanis and contributors.

pocoMC is free software made available under the GPL-3.0 License. For details see the LICENSE file.

pocomc's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

pocomc's Issues

Feature request: backups/restarts

Being able to restart jobs which terminate prematurely would be extremely useful for challenging analyses, especially on HPC clusters which impose maximum time limits.
To be generally useful, restarting should work regardless of what sort of pool was used (number of cores, MPI vs. multiprocessing, etc.).

Re: Summary checkpoint in Joss review

The authors claim

  1. pocoMC can speed up the sampling procedure by orders of magnitude when compared to alternatives such as nested sampling.
  2. Parallelisation to computing clusters manifests linear scaling.

Regarding 1., although I do expect a speed-up coming from the reparametrization using normalizing flow, it would be better to see a demonstration of the speed-up to some degree. I know wall time demonstration is a bit annoying due to many other factors in optimizing a program, so a reduction in the total number of calls of the likelihood + benchmarking of normalizing flow would be apperciated.

Regarding 2., I don't see this parallelisation benchmark anywhere in the doc. Can the author provide a small example to show the scaling relation to some moderate number of cores (e.g. 32-128)?

#openjournals/joss-reviews#4634

self.logw infinite

I'm running an inference where my log_like function could return -np.inf even for parameters within the prior. I'm encountering this error message:

~/anaconda3/envs/python3/lib/python3.8/site-packages/pocomc/sampler.py in _resample(self, u, x, J, L, P)
615
616 assert np.all(~np.isnan(self.logw))
--> 617 assert np.all(np.isfinite(self.logw))
618 assert np.all(~np.isnan(w))
619 assert np.all(np.isfinite(w))

AssertionError:

The progress bar is: Iter: 1it [00:00, 264.16it/s, beta=1.6e-10, calls=768, ESS=0.949, logZ=-.0674, accept=0.234, N=0, scale=1]

I read the code and my impression is that is error message is entirely expected? First, self.L is initiated as -np.inf where only the in-prior samples are replaced by the likelihood:
self.L = np.full((len(self.x),), -np.inf)
self.L[finite_prior_mask] = self._log_like(self.x[finite_prior_mask])

And later self.logw is calculated as
self.logw = logw_prev + self.L * (beta - beta_prev)

Maybe pocomc requires log_like to be finite when log_prior is finite?

Sigma calculation error: AttributeError: 'float' object has no attribute 'item'

Hello I have been using the pococmc package to sample parameters of an ODE-based system. Occasionally, an error arises in my runs: AttributeError: 'float' object has no attribute 'item'. This happens at different times depending on which seed I use, and sometimes doesn't happen at all. It seems like sigma is a Tensor object but is somehow incorrectly being typed as a float? Any ideas how to fix this?

Error trace example:

  sampler.run(p_0, ess, gamma, int(n_dim), int(1000*n_dim), progress=True)
  File "/opt/miniconda3/envs/pyPoco/lib/python3.10/site-packages/pocomc/sampler.py", line 405, in run
    self.u, self.x, self.J, self.L, self.P = self._mutate(
  File "/opt/miniconda3/envs/pyPoco/lib/python3.10/site-packages/pocomc/sampler.py", line 532, in _mutate
    results = preconditioned_metropolis(
  File "/opt/miniconda3/envs/pyPoco/lib/python3.10/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "/opt/miniconda3/envs/pyPoco/lib/python3.10/site-packages/pocomc/mcmc.py", line 167, in preconditioned_metropolis
    scale=sigma.item() / (2.38 / np.sqrt(n_dim)),
AttributeError: 'float' object has no attribute 'item'

Warning in running pytest

I got these two minor warnings when I am running pytest. Please consider adjusting the testing scripts and code to remove the warnings.

platform linux -- Python 3.9.12, pytest-7.1.2, pluggy-1.0.0
rootdir: /mnt/home/wwong/Software/PublicCode/pocomc
collected 33 items                                                                                                                     

tests/test_flow.py ..............                                                                                                [ 42%]
tests/test_pearson.py .                                                                                                          [ 45%]
tests/test_plotting.py ...                                                                                                       [ 54%]
tests/test_sampler.py .....                                                                                                      [ 69%]
tests/test_scaler.py .....                                                                                                       [ 84%]
tests/test_state.py ....                                                                                                         [ 96%]
tests/test_tools.py .                                                                                                            [100%]

=========================================================== warnings summary ===========================================================
pocomc/scaler.py:1
  /mnt/home/wwong/Software/PublicCode/pocomc/pocomc/scaler.py:1: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
    import imp

tests/test_flow.py::FlowTestCase::test_logprob_float64
  /mnt/home/wwong/Software/PublicCode/pocomc/pocomc/tools.py:239: UserWarning: Float64 data is currently unsupported, casting to Float32. Output will also have type Float32.
    warnings.warn(f"Float64 data is currently unsupported, casting to Float32. Output will also have type Float32.")

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=================================================== 33 passed, 2 warnings in 58.36s 
====================================================

#openjournals/joss-reviews#4634

Feature request: derived parameters

Really excited to try out your sampler - I'm writing the CosmoSIS interface now.

One thing that would help would be being able to save derived parameters, so e.g. we can make sigma_8 - Omega_m plots after sampling in A_s without further processing. Not sure how easy that is here - it might clash with your vectorization tests.

Paramters EXCEED BOUND

Thanks for your sampler and I am excited to use it. But sometime particles could walk exceed prior bounds then rising an error. It seems like the issue of transformation between parameters.

C:\Users\Loyal.Aujust\AppData\Local\Programs\Python\Python38\lib\site-packages\pocomc\scaler.py:386: RuntimeWarning: invalid value encountered in logaddexp p = np.exp(-np.logaddexp(0, -u[:, self.mask_both])) C:\Users\Loyal.Aujust\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\linalg\linalg.py:2099: RuntimeWarning: invalid value encountered in slogdet sign, logdet = _umath_linalg.slogdet(a, signature=signature)

What is the possible reason for this issue? thanks for helping!

Potential issue in `pocomc.flow`

Hello @minaskar, thanks for using Zuko in pocoMC! I hope you like it ;)

I quickly checked how you were using it, and noticed a potential issue with the following.

self.transform = self.flow().transform

Here, self.transform is a torch.distributions.Transform created with the current weights of self.flow. This means that updating the parameters of the flow (self.flow) may not affect self.transform. Instead, it would be safer to make self.transform a property that returns self.flow().transform without storing it, such that they are always tied.

@property
def transform(self):
    return self.flow().transform

or, equivalently,

@property
def transform(self):
    return self.flow.transform()

In addition, here,

pocomc/pocomc/flow.py

Lines 89 to 91 in 61d7e0f

x = self.transform.inv(u)
logdetj = self.transform.log_abs_det_jacobian(x, None)
return x, -logdetj

you can use the call_and_ladj method on the reverse transformation.

x, logdetj = self.transform.inv.call_and_ladj(u)

And, here,

pocomc/pocomc/flow.py

Lines 121 to 123 in 61d7e0f

u = torch.randn(size, self.n_dim)
x = self.transform.inv(u)
return x, self.flow().log_prob(x)

you can use the rsample_and_log_prob method of the flow, which is faster than inv + log_prob.

x, log_p = self.flow().rsample_and_log_prob((size,))

Tell me if you have any issues.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.