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.2

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.2
File Size Uploaded Provenance Actions
product_monte_carlo-0.2.2.tar.gz 16.7 kB With provenance Details for product_monte_carlo-0.2.2.tar.gz

Built distribution (wheel)

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

Total release size: 34.9 kB

Release files / product_monte_carlo-0.2.2.tar.gz

Download URL product_monte_carlo-0.2.2.tar.gz
Size 16.7 kB
Tags Source
SHA-256 checksum
How to use checksums
56403ead660468e3f631caefa99a6d029f334c4a33c1e45c5ee48b0da9864293
BLAKE2b-256 checksum
How to use checksums
b77548c7194278343b47a0af8dd89a6980342774feed45cf8b73f1ce1df2cc9f
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.2-py3-none-any.whl

Download URL product_monte_carlo-0.2.2-py3-none-any.whl
Size 18.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e664d8bad43c116877b78def0b66b5c0828bc7e3380959fe73643f082e39937e
BLAKE2b-256 checksum
How to use checksums
63faae431e7b4d3c1c773c24ec7c7af2ac2595d4da52d55dd93fd8b85cf2e129
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.2

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.2.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