Skip to main content

cps-sampling

Conditional Poisson sampling (CPS) in Python. You give it inclusion probabilities π, and it gives you samples of exactly n = Σπ units in which unit i appears with probability π_i.

CPS is the maximum-entropy design with fixed sample size: Poisson sampling conditioned on the sample size. Its fixed size makes estimates less variable than Poisson sampling with the same π, which is why we use it for active statistical inference (Skalnes and Oberst, 2026).

Install

pip install git+https://github.com/oberst-lab/cps-sampling.git

Usage

import numpy as np
from cps_sampling import ConditionalPoisson

rng = np.random.default_rng(0)
uncertainty = rng.uniform(size=10_000)
pi = 500 * uncertainty / uncertainty.sum()      # inclusion probabilities summing to n = 500

design = ConditionalPoisson(pi=pi, n=500)        # slow step, done once: about 1 s for N = 25,000, n = 1,000
s = design.sample(seed=0)                        # indices of exactly 500 units; under 1 ms each

y = rng.normal(size=10_000)                      # only y[s] is used below
mean = np.sum(y[s] / pi[s]) / len(pi)            # Horvitz-Thompson mean
var = design.variance(y=y, s=s)                  # Deville's estimator, needs only pi
  • Sample size. You pass n, and ConditionalPoisson raises an error unless sum(π) = n.
  • Certainty units. Units with π_i = 1 are in every sample. The other units are clipped to [γ, 1 − γ], with γ = 1e-10 by default.
  • Caching. Building the design is the slow step. ConditionalPoisson(pi=pi, n=n, cache_dir="cache") saves it to disk, keyed by π, so a rerun with the same π is instant.
  • Second-order probabilities. design.joint_inclusion() returns the N × N matrix of π_ij (Matei and Tillé, 2005). It takes O(n N²) time, so use it only for moderate N.
  • Variance. design.variance(y=y, s=s, method=...) estimates the variance of the HT mean (divided by N²; pass N=1 for the total). method is "deville" (the default, recommended by Matei and Tillé), "syg" (Sen–Yates–Grundy) or "ht"; the last two build π_ij.

How it works

  1. Calibration. CPS with weights w_i = p_i / (1 − p_i) does not have inclusion probabilities p, so calibrate solves for the working probabilities p that give π, by the fixed point of Chen, Dempster and Liu (1994).
  2. Draw matrix. draw_matrix fills q[k, z] = P(unit k is drawn | z + 1 units are still needed from units k, …, N − 1) by a recursion over elementary symmetric polynomials, in log space for stability.
  3. Drawing. sample_from_matrix walks through the units once and includes unit k with probability q[k, units still needed]. The matrix is built once and reused for every draw.

Citation

If you use this package, please cite:

@inproceedings{skalnes2026fixed,
  title     = {Fixed-Size Active Statistical Inference},
  author    = {Skalnes, Erik and Oberst, Michael},
  booktitle = {Advances in Neural Information Processing Systems},
  year      = {2026}
}

References

  • Chen, Dempster and Liu (1994). Weighted finite population sampling to maximize entropy. Biometrika 81(3).
  • Matei and Tillé (2005). Evaluation of variance approximations and estimators in maximum entropy sampling with unequal probability and fixed sample size. Journal of Official Statistics 21(4).
  • Deville (1993). Estimation de la variance pour les enquêtes en deux phases. Manuscript, INSEE.

Metadata

Release files for cps-sampling 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cps-sampling 0.1.0
File Size Uploaded Provenance
cps_sampling-0.1.0.tar.gz 10.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for cps-sampling 0.1.0
File Interpreter ABI Platform
cps_sampling-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.0 kB

Release files / cps_sampling-0.1.0.tar.gz

Download URL cps_sampling-0.1.0.tar.gz
Size 10.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b3975253fecbf8739bf26d9a71028281a5765e3ac66f6c31aae54fde99e0e7e8
BLAKE2b-256 checksum
How to use checksums
2b35249531e03dfe2c4d90bbc4d08694467397fa11a5015c44ad69040b78eeff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / cps_sampling-0.1.0-py3-none-any.whl

Download URL cps_sampling-0.1.0-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a3e11751c804a15eb2ecd4f7b2a84c7efabfd0faf7e31f59be82a3ebd9956312
BLAKE2b-256 checksum
How to use checksums
6acc432f943801af1fc00d9c5ed13d4ffe7bb7ee4aba4fae5c295a20e4062d0e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for cps-sampling 0.1.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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