Sequential importance sampling for contingency tables with fixed margins
Project description
Sequential Importance Sampling for Contingency Tables
Numba-accelerated sequential importance sampling (SIS) for uniformly sampling two-way contingency tables with fixed row and column margins, following Chen, Diaconis, Holmes, and Liu (2005, JASA).
Installation
pip install sequential-importance-sampling
For development (editable install from source):
pip install -e .
Usage
import numpy as np
from sequential_importance_sampling import sample_tables
# Row and column margins from Chen et al. (2005)
row_sums = np.array([10, 62, 13, 11, 39])
col_sums = np.array([65, 25, 45])
# Draw 150,000 importance-weighted tables
tables, logq = sample_tables(row_sums, col_sums, num_samples=150_000, rng_seed=44042)
# Estimate the number of tables with these margins
# True value: 239,382,173 (Diaconis and Gangolli, 1995)
print("Estimate:", np.exp(-logq).mean())
Batch dimensions are supported for sampling many independent two-way tables in parallel. If row_sums has shape (R, d1, d2, ...) and col_sums has shape (C, d1, d2, ...), each combination of batch indices defines an independent R×C table problem. The output tables will have shape (num_samples, d1, d2, ..., R, C) with batch dims before table dims for C-contiguous access. Note that this does not extend to multi-way (3+) contingency tables with additional margin constraints — each batch element is a separate two-way table.
API
| Function | Description |
|---|---|
sample_tables |
Main entry point — sample batches of tables with arbitrary batch dimensions |
sample_table_sis |
Sample a single contingency table via column-wise SIS |
sample_tables returns (tables, logq) where logq contains the log importance weights. Pass parallel=False to disable numba parallelism.
Tests
Run the Diaconis-Gangolli counting verification:
python tests/test_diaconis_gangolli.py
True count: 239,382,173
SIS estimate (n=150k): 239,413,201
Coefficient of variation: 0.9512
Effective sample size: 78,750
Run the Holmes-Jones example from the Chen et al. paper:
python tests/test_holmes_jones.py
Reference (Chen et al.): 3.383e16
SIS estimate (n=1M): 3.382e+16
log10 estimate: 16.5291
log10 reference: 16.5293
Coefficient of variation: 1.0537
Effective sample size: 473,875
References
- Chen, Y., Diaconis, P., Holmes, S. P., & Liu, J. S. (2005). Sequential Monte Carlo methods for statistical analysis of tables. Journal of the American Statistical Association, 100(469), 109-120.
- Diaconis, P., & Gangolli, A. (1995). Rectangular arrays with fixed margins. In Discrete Probability and Algorithms (pp. 15-41). Springer.
Project details
Release history Release notifications | RSS feed
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 sequential_importance_sampling-0.1.1.tar.gz.
File metadata
- Download URL: sequential_importance_sampling-0.1.1.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e85240520f65f7fab72de360bddb9ec73dd644d2d1e4f8a4c919019b6143e851
|
|
| MD5 |
78676554473511aebf19b7057f943b5a
|
|
| BLAKE2b-256 |
212ea4fac8f6a1ec8896869b38cabed5233f84bdac6e812b27faa9576233c9f1
|
File details
Details for the file sequential_importance_sampling-0.1.1-py3-none-any.whl.
File metadata
- Download URL: sequential_importance_sampling-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
28ec6d822b928f1946198fbbfce6c3a7ffbe2e282dbb8bd1a554e95617d91c11
|
|
| MD5 |
89d4126001fa77e4fa6dbbc68f83e7a8
|
|
| BLAKE2b-256 |
23eb2eb1fb4f1980e1d9e9e1cc6dc211aefcc89c473d42b38660ec856f3e412c
|