Metadata-Version: 2.4
Name: product-monte-carlo
Version: 0.1.2
Summary: Product Monte Carlo for distributed and federated inference
Author: Pietro Carlotti
License-Expression: MIT
Project-URL: Homepage, https://github.com/PietroCarlotti/PMC_AISTATS
Project-URL: Source, https://github.com/PietroCarlotti/PMC_AISTATS
Keywords: Bayesian inference,distributed inference,federated learning,Monte Carlo,PyTorch
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch<3,>=2.1
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: ruff; extra == "test"
Dynamic: license-file

# Product Monte Carlo (PMC)

PMC estimates expectations under a product of densities. 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\limits_{k=1}^{K}\pi_k(x)}
{\int_{\mathcal X}\prod\limits_{k=1}^{K}\pi_k(u)\,du}
\propto
\prod\limits_{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 the product distribution $\pi^\star$:

$$
\mathbb E_{X\sim\pi^\star}[h(X)]
=
\int_{\mathcal X}h(x)\,\pi^\star(x)\,\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\; x\in\mathcal X.
$$

We call a function $d:\mathcal X^K\to\mathbb R^m$ a **discrepancy function** if

$$
d(x_1,\ldots,x_K)=\mathbf 0
\quad\Longleftrightarrow\quad
x_1=\cdots=x_K,
\qquad \forall\; (x_1,\ldots,x_K)\in\mathcal X^K.
$$

Then, under suitable regularity conditions, we have that

$$
\mathbb E_{X\sim\pi^\star}[h(X)]
=
\mathbb E\!\left[
h\!\left(c(X_1,\ldots,X_K)\right)
\;\middle|\;
d(X_1,\ldots,X_K)=\mathbf 0_m
\right].
$$

**PMC then works as follows:**

1. Draw $S$ independent samples from each of the $K$ distributions.
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

```bash
pip install product-monte-carlo
```

## Quick example

```python
import torch

from pmc import AnchoredDifferences, Mean, OLS, PMC

# Draw 1,000 samples from each of two one-dimensional components.
samples = torch.randn(1_000, 2, 1)

# Estimate the mean under their product density.
estimator = PMC(
    consensus=Mean(),
    discrepancy=AnchoredDifferences(),
    h=lambda x: x,
    regressor=OLS(),
)
estimate = estimator.estimate(samples)
print(estimate)
```
