Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sequential_importance_sampling-0.1.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file sequential_importance_sampling-0.1.0.tar.gz.

File metadata

File hashes

Hashes for sequential_importance_sampling-0.1.0.tar.gz
Algorithm Hash digest
SHA256 10d1dc8adc6e1deb210665184f663ecd959951012e9a1bf6a7ae53aafa32fa4f
MD5 7f18ad68bdccb86f1a89e2d4c976723d
BLAKE2b-256 56519f98cd674cf5c22faf042bd55a6d5c858381c766425be1bfe6efbb748d48

See more details on using hashes here.

File details

Details for the file sequential_importance_sampling-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sequential_importance_sampling-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ed86740ca005cac7c34595340958c7ad4d7bdb5be31403f286d051488a8c083e
MD5 20f96f56116b01926ad8dab50d4e46b5
BLAKE2b-256 dbbea8de1205ad36fc45d2b1830a2a32a5a67b60633be7f618607b501dbbd621

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page