PyApprox provides flexible and efficient tools for high-dimensional approximation, uncertainty quantification, and decision-making under uncertainty. It implements methods addressing various issues surrounding high-dimensional parameter spaces and limited evaluations of expensive simulation models, with the goal of facilitating simulation-aided knowledge discovery, prediction, and design.
Documentation | Tutorials | Paper
Tools are provided for:
- Surrogate modeling — polynomial chaos expansions (least squares, compressive sensing, interpolation), Gaussian process regression (single- and multi-output, DAG-structured), low-rank tensor decompositions (function trains), and sparse grid interpolation
- Multi-fidelity estimation — approximate control variates (ACV), multi-level Monte Carlo (MLMC), multi-fidelity Monte Carlo (MFMC), MLBLUE, and group ACV
- Bayesian experimental design — KL-based and goal-oriented optimal experimental design with gradient-based optimization
- Bayesian inference — MCMC sampling and conjugate posterior analysis
- Sensitivity analysis — Sobol indices, Morris screening, and surrogate-based sensitivity
- Probability and risk — random variable transformations, risk measures, and random field representations (KLE)
- PDE solvers — collocation and Galerkin finite-element methods for advection-diffusion-reaction, Helmholtz, Stokes, elasticity, and more
- Optimization — implicit function differentiation, adjoint methods, and design under uncertainty
All code is fully typed, supports dual backends (NumPy and PyTorch), and preserves PyTorch autograd computation graphs for automatic differentiation.
import numpy as np
from pyapprox.util.backends.numpy import NumpyBkd
from pyapprox.interface.functions.fromcallable.function import FunctionFromCallable
from pyapprox.probability import UniformMarginal, IndependentJoint
from pyapprox.surrogates.sparsegrids import create_basis_factories
from pyapprox.surrogates.sparsegrids.isotropic_fitter import IsotropicSparseGridFitter
from pyapprox.surrogates.sparsegrids.subspace_factory import TensorProductSubspaceFactory
from pyapprox.surrogates.affine.indices import LinearGrowthRule
bkd = NumpyBkd()
# Define a 2D function using the FunctionProtocol
def target(samples):
x, y = samples[0], samples[1]
return bkd.reshape(x**3 + x*y + y**2, (1, -1))
func = FunctionFromCallable(1, 2, target, bkd)
# Build a sparse grid surrogate
marginals = [UniformMarginal(-1.0, 1.0, bkd) for _ in range(2)]
joint = IndependentJoint(marginals, bkd)
factories = create_basis_factories(joint.marginals(), bkd, "gauss")
growth = LinearGrowthRule(scale=1, shift=1)
tp_factory = TensorProductSubspaceFactory(bkd, factories, growth)
fitter = IsotropicSparseGridFitter(bkd, tp_factory, level=3)
samples = fitter.get_samples()
result = fitter.fit(func(samples))
surrogate = result.surrogate
# Evaluate surrogate at new points
test_pts = joint.rvs(100)
approx_values = surrogate(test_pts)- Python >= 3.10
- NumPy >= 2.0, SciPy >= 1.11, PyTorch >= 2.5
- matplotlib, sympy, networkx
git clone https://github.com/sandialabs/pyapprox.git
cd pyapprox
pip install -e ".[test]"pip install -e ".[fem]" # Finite element (scikit-fem)
pip install -e ".[umbridge]" # UMBridge model interface
pip install -e ".[numba]" # Numba JIT acceleration
pip install -e ".[parallel]" # Parallel execution (joblib, mpire)
pip install -e ".[cvxpy]" # Convex optimization
pip install -e ".[test]" # Testing tools
pip install -e ".[lint]" # Linting (mypy, ruff)
pip install -e ".[docs]" # Documentation (quarto)
pip install -e ".[all]" # Everythingconda env create -f environment.yml
conda activate pyapprox
pip install -e .pytest pyapprox -v --tb=shortThe -v flag enables verbose output and --tb=short abbreviates tracebacks on failures for readability.
Some tests are marked as slow and are skipped by default. To include them:
PYAPPROX_RUN_SLOW=1 pytest pyapprox -v --tb=short # include slow tests (>5s)
PYAPPROX_RUN_SLOWER=1 pytest pyapprox -v --tb=short # include slower tests (>30s)
PYAPPROX_RUN_SLOWEST=1 pytest pyapprox -v --tb=short # include all testsTo run only a specific tier (skipping fast and other tiers), combine the environment variable with -m:
PYAPPROX_RUN_SLOW=1 pytest pyapprox -v --tb=short -m slow # only @slow_test
PYAPPROX_RUN_SLOWER=1 pytest pyapprox -v --tb=short -m slower # only @slower_test
PYAPPROX_RUN_SLOWEST=1 pytest pyapprox -v --tb=short -m slowest # only @slowest_testTo run tests in parallel (requires pytest-xdist):
pytest pyapprox -v --tb=short -n auto # use all CPUs
pytest pyapprox -v --tb=short -n 4 # use 4 workersThe tutorial site is built with Quarto. Install it, then:
cd tutorials
./build.sh # build with cached results (freeze)
./build.sh --execute # force re-execute all code
./build.sh --no-execute # skip execution, use cache only
./build.sh -j auto # parallel execution (auto-detect CPUs)
./build.sh -j 4 # parallel execution with 4 workers
./build.sh --notebooks # also generate downloadable .ipynb files
./build.sh --serve # start local server after build
./build.sh --skip=pacv_usage # skip a specific tutorialOutput is written to tutorials/library/_site/.
ruff check pyapprox/ # style and import checks
mypy pyapprox/ # static type checkingContributions are welcome. Please:
- Fork the repository and create a feature branch
- Ensure all tests pass (including slow):
PYAPPROX_RUN_SLOWEST=1 pytest pyapprox -v --tb=short - Ensure no lint errors:
ruff check pyapprox/ - Submit a pull request
If you use PyApprox in your research, please cite:
@article{JAKEMAN2023105825,
title = {PyApprox: A software package for sensitivity analysis, Bayesian inference,
optimal experimental design, and multi-fidelity uncertainty quantification
and surrogate modeling},
author = {J.D. Jakeman},
journal = {Environmental Modelling \& Software},
volume = {170},
pages = {105825},
year = {2023},
doi = {10.1016/j.envsoft.2023.105825}
}PyApprox is licensed under the MIT License.
This research was developed with funding from the Defense Advanced Research Projects Agency (DARPA), the U.S. Department of Energy Office of Science Advanced Scientific Computing Research (ASCR) program, and the Sandia National Laboratories Laboratory Directed Research and Development (LDRD) program. The views, opinions and/or findings expressed are those of the author and should not be interpreted as representing the official views or policies of the Department of Defense or the U.S. Government.