Metadata-Version: 2.4
Name: product-monte-carlo
Version: 0.1.0
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)

Product Monte Carlo (PMC) estimates expectations under a density proportional to a product of component densities using only samples from the individual components. PMC constructs consensus and discrepancy variables from independent component draws, fits the conditional expectation of a target function given the discrepancy, and evaluates that regression at zero discrepancy. This supports density-evaluation-free aggregation in settings such as distributed Bayesian inference and simulation-based inference.

This repository contains the Python PMC implementation, R fusion baselines, and simulation and application pipelines.

## Installation

PMC requires Python 3.10 or newer. Install the released package with:

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

For development from a repository checkout, use `python3 -m pip install -e .` instead.

## Quick start

```python
import torch

from pmc import AnchoredDifferences, Mean, OLS, PMC

samples = torch.randn(1_000, 2, 1)
estimator = PMC(
    consensus=Mean(),
    discrepancy=AnchoredDifferences(),
    h=lambda consensus: consensus,
    regressor=OLS(),
)
estimate = estimator.estimate(samples)
```

## Research code

### R

The fusion baselines require R 4.4 or newer and the dependencies declared in [`src/fusion/DESCRIPTION`](src/fusion/DESCRIPTION). The package is loaded in place rather than installed globally. After installing those dependencies, verify the package with:

```bash
Rscript -e 'devtools::load_all("src/fusion")'
```

## Data

External data are stored under `data/raw/`, processed data under `data/processed/`, and application results under `data/results/`.

- **Respiratory:** a longitudinal clinical-trial dataset used for the privacy-restricted aggregation application.
- **HIGGS:** a large binary-classification dataset with 28 predictors used for the large-scale inference application.
- **MAGIC:** a binary-classification dataset with 10 measured features used for an additional large-scale inference experiment.

The shared download script fetches only HIGGS and respiratory:

```bash
bash src/data/fetch_raw_datasets.sh
```

MAGIC is downloaded and prepared separately:

```bash
python3 src/data/prepare_magic.py
```

## Reproducibility and navigation

- `experiments/outputs/` contains study-specific configurations and curated simulation results.
- `experiments/scripts/launcher.sh`, `launcher.py`, and `launcher.R` provide the shared experiment launchers.
- `src/applications/` contains the respiratory, HIGGS, MAGIC, and HSP90 application pipelines.

## HSP90 simulation-based inference

This experiment combines K = 10 noisy images of a protein. A neural posterior gives an image-level posterior for one shape parameter, and PMC combines the K image-level posteriors into a posterior for that parameter. The Oracle is MCMC on the product of the image-level posterior densities.

The image simulator is an external package that is **not** included in this repository and is not publicly available. Stage 1 (rendering and training) and the MCMC Oracle script therefore require access to the external image simulator, and its checkout path (`SIMULATOR_PATH` below) is passed as the first argument to those scripts. Every other stage runs without it, from the saved posterior draws in `data/results/hsp90/sbi/posterior_draws.npz`. The results are stored under `data/results/hsp90/`.

Use `requirements.txt` for every stage. Run all commands from the repository root.

1. Render images and train the neural posterior (needs the external image simulator). Writes `posterior_draws.npz` and the trained posterior to the output directory.

   ```bash
   PYTHONPATH=. python src/applications/run_hsp90_sbi.py \
     SIMULATOR_PATH \
     data/results/hsp90/sbi
   ```

2. Fit the PMC OLS, MLP, and GP estimators to the saved draws. Reads `posterior_draws.npz` and writes `pmc_ols_moments.json`, `pmc_mlp_moments.json`, and `pmc_gp_moments.json`. The `--device` option (`cpu` by default, or `cuda`) sets the device for the MLP and GP fits; OLS always runs on the CPU.

   ```bash
   PYTHONPATH=src python src/applications/run_hsp90_pmc.py \
     data/results/hsp90/sbi/posterior_draws.npz \
     data/results/hsp90/pmc \
     --device cpu
   ```

3. Compute the tail and KDE-product diagnostics. Reads `posterior_draws.npz` and `pmc_mlp_moments.json` and writes `tail_diagnostics.json` and `kde_product_check.json`.

   ```bash
   python src/applications/run_hsp90_diagnostics.py \
     data/results/hsp90/sbi/posterior_draws.npz \
     data/results/hsp90/pmc/pmc_mlp_moments.json \
     data/results/hsp90/diagnostics
   ```

4. Compute the Oracle moments by MCMC on the product of the image-level posteriors (needs the external image simulator). Reads the saved draws (`--draws`, default `data/results/hsp90/sbi/posterior_draws.npz`) and the trained posterior from stage 1, and writes `mcmc_moments.json`.

   ```bash
   PYTHONPATH=. python src/applications/run_hsp90_mcmc.py \
     SIMULATOR_PATH \
     data/results/hsp90/oracle
   ```

## Directory map

```text
.
├── src/
│   ├── pmc/                 # Python PMC package
│   ├── fusion/              # R fusion package
│   ├── data/                # External-data download and preparation
│   ├── models/              # Shared application models
│   └── applications/        # Real-data and simulation-based applications
├── experiments/
│   ├── scripts/             # Shared experiment launchers
│   └── outputs/             # Study configurations and curated results
└── data/                    # Raw, processed, and application data
```
