Skip to main content

Product Monte Carlo (PMC)

PMC estimates expectations under a product of densities. To see how, consider the following setup.

Suppose you can draw samples independently from $K$ distributions on $\mathcal X\subseteq\mathbb R^p$:

$$ X_k \overset{\text{ind.}}{\sim} \pi_k, \qquad k=1,\ldots,K. $$

We define the product distribution $\pi^\star$ as follows:

$$ \pi^\star(x) = \frac{\prod_{k=1}^{K}\pi_k(x)} {\int_{\mathcal X} \prod_{k=1}^{K}\pi_k(u)\thinspace\mathrm{d}u} \propto \prod_{k=1}^{K}\pi_k(x), \qquad \forall x \in \mathcal X. $$

Given a function $h:\mathcal X\to\mathbb R$, we wish to compute its expectation under $\pi^\star$:

$$ \mathbb E_{X\sim\pi^\star}[h(X)] = \int_{\mathcal X}h(x)\thinspace\pi^\star(x)\thinspace\mathrm{d}x. $$

We call a function $c:\mathcal X^K\to\mathcal X$ a consensus function if

$$ c(x,\ldots,x)=x, \qquad \forall\text{ }x\in\mathcal X. $$

We call a function $d:\mathcal X^K\to\mathbb R^m$ a discrepancy function if, for all $x_1,\ldots,x_K\in\mathcal X$,

$$ d(x_1,\ldots,x_K)=\mathbf 0 \quad\Longleftrightarrow\quad x_1=\cdots=x_K. $$

Let $C=c(X_1,\ldots,X_K)$ and $D=d(X_1,\ldots,X_K)$. Then, under suitable regularity conditions, we have that

$$ \mathbb E_{X\sim\pi^\star}[h(X)] = \mathbb E[h(C)\mid D=\mathbf 0_m]. $$

PMC then works as follows:

  1. Draw $S$ independent samples per distribution.
  2. For each set of $K$ draws, compute its consensus $C$ and discrepancy $D$.
  3. Fit a regressor to predict $h(C)$ from $D$.
  4. Estimate the target expectation by evaluating the fitted regressor at $D=\mathbf 0_m$.

Install

Python

To install the Python package from PyPI, run:

pip install product-monte-carlo

R

To install the R package from GitHub, run:

remotes::install_github("PietroCarlotti/PMC", subdir = "src/r")

Example

Python

The example below shows how to estimate an expectation with PMC in Python.

# Import PyTorch and PMC classes
import torch
from pmc import AnchoredDifferences, Mean, OLS, PMC

# Draw S = 1,000 samples from K = 2 univariate standard Gaussian distributions (p = 1)
samples = torch.randn(1_000, 2, 1)

# Configure PMC to estimate the mean under the product distribution
pmc_estimator = PMC(
    consensus=Mean(),
    discrepancy=AnchoredDifferences(),
    h=lambda x: x,
    regressor=OLS(),
)

# Compute and print the resulting estimate
pmc_estimate = pmc_estimator.estimate(samples)
print(pmc_estimate)

R

The same expectation can be estimated with PMC in R.

# Load PMC
library(ProductMonteCarlo)

# Draw S = 1,000 samples from K = 2 univariate standard Gaussian distributions (p = 1)
samples <- array(rnorm(1000 * 2), dim = c(1000, 2, 1))

# Estimate the mean under the product distribution
pmc_estimate <- pmc(samples)

# Print the resulting estimate
print(pmc_estimate)

Metadata

Release files for product-monte-carlo 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for product-monte-carlo 0.2.1
File Size Uploaded Provenance Actions
product_monte_carlo-0.2.1.tar.gz 15.2 kB With provenance Details for product_monte_carlo-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for product-monte-carlo 0.2.1
File Interpreter ABI Platform Actions
product_monte_carlo-0.2.1-py3-none-any.whl Python 3 none any Details for product_monte_carlo-0.2.1-py3-none-any.whl

Total release size: 31.6 kB

Release files / product_monte_carlo-0.2.1.tar.gz

Download URL product_monte_carlo-0.2.1.tar.gz
Size 15.2 kB
Tags Source
SHA-256 checksum
How to use checksums
1e989cf59691e6b3e5807716eb370a9616007d57d0019535b2b8497214eb7029
BLAKE2b-256 checksum
How to use checksums
0ce132927c51817c3607cde846819915e3fb953b5031167f852bc8ba01e2d183
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / product_monte_carlo-0.2.1-py3-none-any.whl

Download URL product_monte_carlo-0.2.1-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
116b65d09972b891b573ce5a65af87e4b50b04aa308574a7201e657c28862c8c
BLAKE2b-256 checksum
How to use checksums
b184e43b29f7b34b41849950bb01dfc42cf17483c40c06e97b16cbf048a7ba8f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Security for product-monte-carlo 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for product_monte_carlo-0.2.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page