Skip to main content

A package for performing uncertainty quantification using a parallelized sequential Monte Carlo sampler.

Project description

SMCPy - Sequential Monte Carlo with Python

Build

Description

SMCPy is an open-source package for performing uncertainty quantification using a parallelized sequential Monte Carlo sampler.

Key Features

  • Alternative to Markov chain Monte Carlo for Bayesian inference problems
  • Unbiased estimation of marginal likelihood for Bayesian model selection
  • Parallelization through either numpy vectorization or mpi4py

Quick Start

Installation

To install SMCPy, use pip.

pip install smcpy

Overview

To operate the code, the user supplies a computational model built in Python 3.6+, defines prior distributions for each of the model parameters to be estimated, and provides data to be used for probabilistic model calibration. SMC sampling of the parameter posterior distribution can then be conducted with ease through instantiation of a sampler class and a call to the sample() method.

The two primary sampling algorithms implemented in this package are MPI-enabled versions of those presented in the following articles, respectively:

Nguyen, Thi Le Thu, et al. "Efficient sequential Monte-Carlo samplers for Bayesian inference." IEEE Transactions on Signal Processing 64.5 (2015): 1305-1319. Link to Article | BibTeX Reference

Buchholz, Alexander, Nicolas Chopin, and Pierre E. Jacob. "Adaptive tuning of hamiltonian monte carlo within sequential monte carlo." Bayesian Analysis 1.1 (2021): 1-27. Link to Article | BibTeX Reference

The first is a simple likelihood tempering approach in which the tempering sequence is fixed and user-specified (FixedPhiSampler). The second is an adaptive approach that chooses the tempering steps based on a target effective sample size (AdaptiveSampler).

This software was funded by and developed under the High Performance Computing Incubator (HPCI) at NASA Langley Research Center.

Example Usage

import numpy as np

from scipy.stats import uniform

from spring_mass_model import SpringMassModel
from smcpy.utils.plotter import plot_pairwise
from smcpy import AdaptiveSampler, VectorMCMC, VectorMCMCKernel


# Load data
std_dev = 0.5
displacement_data = np.genfromtxt('noisy_data.txt')

# Define prior distributions & MCMC kernel
priors = [uniform(0, 10), uniform(0, 10)]
vector_mcmc = VectorMCMC(model.evaluate, displacement_data, priors, std_dev)
mcmc_kernel = VectorMCMCKernel(vector_mcmc, param_order=('K', 'g'))

# SMC sampling
smc = AdaptiveSampler(mcmc_kernel)
step_list, mll_list = smc.sample(num_particles=500, num_mcmc_samples=5, target_ess=0.8)

# Display results
print(f'parameter means = {step_list[-1].compute_mean()}')
plot_pairwise(step_list[-1].params, step_list[-1].weights, save=True,
              param_labels=['K', 'g'])

The above code produces probabilistic estimates of K, the spring stiffness divided by mass, and g, the gravitational constant on some unknown planet. These estimates are in the form of weighted particles and can be visualized by plotting the pairwise weights as shown below. The mean of each parameter is marked by the dashed red line. The true values for this example were K = 1.67 and g = 4.62. More details can be found in the spring mass example. To run this model in parallel using MPI, the MCMC kernel just needs to be built with the ParallelVectorMCMC class in place of VectorMCMC. More details can be found in the MPI example.

Pairwise plot

To run this model in parallel using MPI, the MCMC kernel just needs to be built with the ParallelVectorMCMC class in place of VectorMCMC. More details can be found in the MPI example (smcpy/examples/mpi_example/).

Tests

Clone the repo and move into the package directory:

git clone https://github.com/nasa/SMCPy.git
cd SMCPy

Install requirements necessary to use SMCPy:

pip install -r requirements.txt

Optionally, if you'd like to use the MPI-enabled parallel sampler, install the associated requirements:

pip install -r requirements_optional.txt

Add SMCPy to your Python path. For example:

export PYTHONPATH="$PYTHONPATH:/path/to/smcpy"

Run the tests to ensure proper installation:

pytest tests

Contributing

  1. Fork (https://github.com/nasa/SMCPy/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a Pull Request

Development

NASA Langley Research Center
Hampton, Virginia

This software was funded by and developed under the High Performance Computing Incubator (HPCI) at NASA Langley Research Center.

Authors

  • Patrick Leser
  • Michael Wang

License

Notices: Copyright 2018 United States Government as represented by the Administrator of the National Aeronautics and Space Administration. No copyright is claimed in the United States under Title 17, U.S. Code. All Other Rights Reserved.

Disclaimers No Warranty: THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE. THIS AGREEMENT DOES NOT, IN ANY MANNER, CONSTITUTE AN ENDORSEMENT BY GOVERNMENT AGENCY OR ANY PRIOR RECIPIENT OF ANY RESULTS, RESULTING DESIGNS, HARDWARE, SOFTWARE PRODUCTS OR ANY OTHER APPLICATIONS RESULTING FROM USE OF THE SUBJECT SOFTWARE. FURTHER, GOVERNMENT AGENCY DISCLAIMS ALL WARRANTIES AND LIABILITIES REGARDING THIRD-PARTY SOFTWARE, IF PRESENT IN THE ORIGINAL SOFTWARE, AND DISTRIBUTES IT "AS IS."

Waiver and Indemnity: RECIPIENT AGREES TO WAIVE ANY AND ALL CLAIMS AGAINST THE UNITED STATES GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT. IF RECIPIENT'S USE OF THE SUBJECT SOFTWARE RESULTS IN ANY LIABILITIES, DEMANDS, DAMAGES, EXPENSES OR LOSSES ARISING FROM SUCH USE, INCLUDING ANY DAMAGES FROM PRODUCTS BASED ON, OR RESULTING FROM, RECIPIENT'S USE OF THE SUBJECT SOFTWARE, RECIPIENT SHALL INDEMNIFY AND HOLD HARMLESS THE UNITED STATES GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT, TO THE EXTENT PERMITTED BY LAW. RECIPIENT'S SOLE REMEDY FOR ANY SUCH MATTER SHALL BE THE IMMEDIATE, UNILATERAL TERMINATION OF THIS AGREEMENT.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smcpy-0.1.7.tar.gz (36.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

smcpy-0.1.7-py3-none-any.whl (45.5 kB view details)

Uploaded Python 3

File details

Details for the file smcpy-0.1.7.tar.gz.

File metadata

  • Download URL: smcpy-0.1.7.tar.gz
  • Upload date:
  • Size: 36.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for smcpy-0.1.7.tar.gz
Algorithm Hash digest
SHA256 b47d49f4ad279af1ff05e4d9d73cf0443e5149c4ab498f7b0c903817f37b0357
MD5 e4f700fbb2085ba36d439cb3f0fe1c4a
BLAKE2b-256 8d468dec470205635523d506dad1edcb94f46005e1b246099c011e3613fdbfa0

See more details on using hashes here.

Provenance

The following attestation bundles were made for smcpy-0.1.7.tar.gz:

Publisher: pypi.yml on nasa/SMCPy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file smcpy-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: smcpy-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 45.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for smcpy-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 665f4dfd7af56cb286022d3e769b262372f44588905f543135be291a0d9dcdaa
MD5 c9fbaedb6dce0142cc9dd4f1921f98b4
BLAKE2b-256 6537a7df16362dd8795721a32020046f62f42d540049a78454d25b45b9fad705

See more details on using hashes here.

Provenance

The following attestation bundles were made for smcpy-0.1.7-py3-none-any.whl:

Publisher: pypi.yml on nasa/SMCPy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page