Monte Carlo Post-analysis package for global sensitivity analysis and integration
Project description
MCPost: Monte Carlo Post-analysis Package
MCPost is a comprehensive Python package for post-analysis of Monte Carlo samples, providing tools for global sensitivity analysis (GSA) and Monte Carlo integration with modern packaging standards and extensive documentation.
Features
Global Sensitivity Analysis
- Multiple sensitivity metrics: Mutual Information, Distance Correlation, Permutation Importance
- Gaussian Process surrogates with Automatic Relevance Determination (ARD)
- Sobol' indices for variance-based sensitivity analysis
- Partial Dependence Plots for interpretable results
- Robust preprocessing with automatic constant column detection
Monte Carlo Integration
- Standard Monte Carlo integration with importance sampling
- Automatic integration with adaptive sampling strategies
- Flexible PDF specification for target and sampling distributions
Modern Package Features
- Type hints and comprehensive documentation
- Modular design with clean public APIs
- Optional dependencies for visualization and development
- Extensive testing with property-based tests
- Performance optimizations for large datasets
Installation
MCPost supports multiple installation methods to suit different use cases:
Basic Installation
For core functionality (GSA and integration without plotting):
pip install MC-post
Installation from Source
For the latest development version:
pip install git+https://github.com/zzhang0123/mcpost.git
Development Installation
For contributors and developers:
# Clone the repository
git clone https://github.com/mcpost/mcpost.git
cd mcpost
# Install in development mode with all dependencies
pip install -e .[dev]
# Run tests to verify installation
pytest
Quick Start
Global Sensitivity Analysis
MCPost provides comprehensive GSA capabilities with multiple sensitivity metrics:
import numpy as np
from mcpost import gsa_pipeline
# Define a simple test function
def polynomial_function(X):
"""
Simple polynomial: f(x1, x2, x3) = x1^2 + 2*x2 + 0.1*x3
We expect x2 to be most influential, x1 moderately influential,
and x3 to have minimal influence.
"""
x1, x2, x3 = X[:, 0], X[:, 1], X[:, 2]
return x1**2 + 2*x2 + 0.1*x3
# Generate parameter samples
n_samples = 1000
X = np.random.uniform(-1, 1, (n_samples, 3)) # 3 parameters in [-1, 1]
# Evaluate function
y = polynomial_function(X)
Y = y.reshape(-1, 1) # GSA expects 2D array
# Run comprehensive GSA analysis
# Run GSA analysis
param_names = ["x1", "x2", "x3"]
feature_names = ["polynomial"]
print("Running GSA analysis...")
results = gsa_pipeline(
X, Y,
param_names=param_names,
feature_names=feature_names,
scaler="minmax",
enable_sobol=True,
enable_gp=True,
enable_perm=True,
make_pdp=False, # Skip PDPs for this simple example
N_sobol=2048
)
# Display results
sensitivity_table = results["results"]["polynomial"]["table"]
print("\nSensitivity Analysis Results:")
print(sensitivity_table)
Monte Carlo Integration
Integration with Custom Distributions
# Define integration problem: E[x^2] where x ~ N(0,1)
# Analytical solution: 1.0
def integrand(theta):
"""Function to integrate: f(x) = x^2"""
return theta[:, 0]**2
def target_pdf(theta):
"""Standard normal PDF"""
return np.exp(-0.5 * theta[:, 0]**2) / np.sqrt(2 * np.pi)
print("Integration Problem: E[X^2] where X ~ N(0,1)")
print("Analytical solution: 1.0")
print()
# Method 1: Standard Monte Carlo
n_samples = 5000
theta_samples = np.random.normal(0, 1, (n_samples, 1))
f_values = integrand(theta_samples)
mc_result = monte_carlo_integral(theta_samples, f_values, target_pdf)
print("Standard Monte Carlo:")
print(f" Integral estimate: {mc_result['integral'][0]:.6f}")
print(f" Uncertainty: {mc_result['uncertainty'][0]:.6f}")
print(f" Effective sample size: {mc_result['effective_sample_size']:.0f}")
print(f" Error: {abs(mc_result['integral'][0] - 1.0):.6f}")
Documentation and Resources
Complete Documentation
- Getting Started Tutorial: Your first MCPost analysis
- GSA Deep Dive: Advanced sensitivity analysis
- Extension Guide: Creating custom methods
Quick References
Learning Resources
- Getting Started Tutorial: Your first MCPost analysis
- GSA Deep Dive: Advanced sensitivity analysis
Example Applications
- Climate Modeling: GSA for climate model parameters
- Integration Comparison: Monte Carlo integration examples
Requirements
Core Dependencies
- Python 3.8+
- NumPy >= 1.20.0, <2.4.0
- Pandas >= 1.3.0
- Scikit-learn >= 1.0.0
- SciPy >= 1.7.0
- dcor >= 0.5.0
- SALib >= 1.4.0
Optional Dependencies
- Visualization: matplotlib >= 3.5.0
- Development: pytest, hypothesis, black, mypy
- Documentation: sphinx, jupyter, nbsphinx
Development Setup
git clone https://github.com/zzhang0123/mcpost.git
cd mcpost
pip install -e .[dev]
pytest
Testing
MCPost includes a comprehensive test suite:
# Run all tests
pytest tests/
# Run specific test categories
pytest tests/test_gsa/ # GSA functionality tests
pytest tests/test_integration/ # Integration tests
pytest tests/test_utils/ # Utility tests
# Run property-based tests
pytest tests/ -k "property"
# Run with coverage
pytest tests/ --cov=mcpost --cov-report=html
License
This project is licensed under the MIT License - see the LICENSE file for details.
Citation
If you use MCPost in your research, please cite:
@software{mcpost,
title={MCPost: Monte Carlo Post-analysis Package},
author={MCPost Contributors},
url={https://zh-zhang.com/mcpost/},
version={0.1.3},
year={2026}
}
Acknowledgments
MCPost builds upon several excellent open-source libraries:
- Scikit-learn for machine learning algorithms
- SALib for Sobol' sensitivity analysis
- dcor for distance correlation
- SciPy for scientific computing
- NumPy for numerical computing
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mc_post-0.1.4.tar.gz.
File metadata
- Download URL: mc_post-0.1.4.tar.gz
- Upload date:
- Size: 154.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
83c04d6cd2101520adacc0cd8b877d185344c1e2404f76a5ea7db122431ad4db
|
|
| MD5 |
f98a9085aa234919672ebf5f2b91c6f3
|
|
| BLAKE2b-256 |
0b81a83ec317ced6d5f417cd28ada663726e3a1c902841b8ff43a3f03a9debbf
|
Provenance
The following attestation bundles were made for mc_post-0.1.4.tar.gz:
Publisher:
release.yml on zzhang0123/mcpost
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mc_post-0.1.4.tar.gz -
Subject digest:
83c04d6cd2101520adacc0cd8b877d185344c1e2404f76a5ea7db122431ad4db - Sigstore transparency entry: 798615276
- Sigstore integration time:
-
Permalink:
zzhang0123/mcpost@d28ec4b0d98a234cdbcb4b9cb1ca4ed59f7a4a89 -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/zzhang0123
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d28ec4b0d98a234cdbcb4b9cb1ca4ed59f7a4a89 -
Trigger Event:
push
-
Statement type:
File details
Details for the file mc_post-0.1.4-py3-none-any.whl.
File metadata
- Download URL: mc_post-0.1.4-py3-none-any.whl
- Upload date:
- Size: 54.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c9577f08562f00a2fc46bc2ddead77f4eb2c89ddc7ec8abb69d0f97ddc4a68b
|
|
| MD5 |
23184ef959f06d090a7d22b33957f12a
|
|
| BLAKE2b-256 |
e65cf68726f4ec432c2a295901ef25e9cbbc88bf16cfbdf1c7e472ef82569a13
|
Provenance
The following attestation bundles were made for mc_post-0.1.4-py3-none-any.whl:
Publisher:
release.yml on zzhang0123/mcpost
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mc_post-0.1.4-py3-none-any.whl -
Subject digest:
7c9577f08562f00a2fc46bc2ddead77f4eb2c89ddc7ec8abb69d0f97ddc4a68b - Sigstore transparency entry: 798615278
- Sigstore integration time:
-
Permalink:
zzhang0123/mcpost@d28ec4b0d98a234cdbcb4b9cb1ca4ed59f7a4a89 -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/zzhang0123
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d28ec4b0d98a234cdbcb4b9cb1ca4ed59f7a4a89 -
Trigger Event:
push
-
Statement type: