Skip to main content

Statistical distributions as native Polars expressions — PDF, CDF, PPF, SF, and sampling for 20+ distributions, powered by Rust.

Project description

polars-sdist

Statistical distributions as native Polars expressions. Built with Rust and PyO3 for zero-copy, vectorized computation over Polars DataFrames.

Install

pip install polars-sdist

Quick start

import polars as pl
from polars_sdist import SdistNamespace as sdist

df = pl.DataFrame({"x": [-1.96, 0.0, 1.96]})

# CDF, PDF, PPF, SF, ln_pdf — all as expressions
df.with_columns(
    cdf=sdist(pl.col("x")).normal_cdf(mu=0, sigma=1),
    pdf=sdist(pl.col("x")).normal_pdf(mu=0, sigma=1),
    sf=sdist(pl.col("x")).normal_sf(mu=0, sigma=1),
)

Inverse CDF (PPF)

df = pl.DataFrame({"p": [0.025, 0.5, 0.975]})
df.with_columns(
    z=sdist(pl.col("p")).normal_ppf(mu=0, sigma=1),
)

Discrete distributions

df = pl.DataFrame({"k": [0, 1, 2, 3, 4, 5]}).cast({"k": pl.Float64})
df.with_columns(
    pmf=sdist(pl.col("k")).poisson_pmf(lambda_=3.0),
    cdf=sdist(pl.col("k")).poisson_cdf(lambda_=3.0),
)

Random sampling (fixed parameters)

import polars_sdist

s = polars_sdist.sample_normal(n=10_000, mu=0, sigma=1, seed=42)
s = polars_sdist.sample_beta(n=10_000, alpha=2, beta=5, seed=42)

Random sampling (column parameters)

df = pl.DataFrame({
    "mu": [0.0, 10.0, -10.0],
    "sigma": [1.0, 0.5, 2.0],
})
df.with_columns(
    sample=sdist(pl.col("mu")).sample_normal(sigma=pl.col("sigma"), seed=42),
)

Supported distributions

Continuous

Distribution Parameters PDF CDF PPF SF ln_pdf
Normal mu, sigma x x x x x
Log-Normal mu, sigma x x x x x
Beta alpha, beta x x x x x
Gamma shape, rate x x x x x
Exponential lambda_ x x x x x
Cauchy location, scale x x x x x
Chi-Squared df x x x x x
Student's t df x x x x x
Fisher-Snedecor (F) d1, d2 x x x x x
Gumbel location, scale x x x x x
Inverse Gamma shape, scale x x x x x
Laplace location, scale x x x x x
Pareto shape, scale x x x x x
Triangular min, max, mode x x x x x
Uniform a, b x x x x x
Weibull shape, scale x x x x x

Discrete

Distribution Parameters PMF CDF PPF SF
Bernoulli p x x x x
Binomial n, p x x x x
Geometric p x x x x
Hypergeometric pop_size, success_states, draws x x x x
Negative Binomial r, p x x x x
Poisson lambda_ x x x x
Discrete Uniform a, b x x x x

Sampling-only

These distributions support random sampling but not PDF/CDF:

PERT, Skew-Normal, Inverse Gaussian, Frechet, Zeta, Zipf

Performance

Benchmarked end-to-end against scipy/numpy on 1M elements. Median of 5 runs after warmup.

Distribution Operation polars-sdist scipy Speedup
normal pdf 8.6 ms 36.8 ms 4.3x
normal ppf 20.9 ms 48.4 ms 2.3x
normal sample 3.6 ms 7.6 ms 2.1x
normal cdf 23.3 ms 37.2 ms 1.6x
lognormal pdf 17.1 ms 63.8 ms 3.7x
lognormal sample 7.2 ms 12.7 ms 1.8x
lognormal ppf 27.8 ms 42.3 ms 1.5x
lognormal cdf 32.5 ms 41.4 ms 1.3x
binomial pmf 32.0 ms 86.2 ms 2.7x
binomial sample 20.1 ms 30.5 ms 1.5x
chi-squared sample 10.0 ms 16.7 ms 1.7x
chi-squared pdf 49.2 ms 60.9 ms 1.2x
chi-squared cdf 90.3 ms 110.9 ms 1.2x
beta ppf 517.8 ms 601.1 ms 1.2x
beta pdf 80.3 ms 89.5 ms 1.1x

polars-sdist is faster in 15 of 19 benchmarks. Areas where scipy is faster (beta CDF/sample, chi-squared PPF, binomial CDF) use highly optimized C/Fortran special-function implementations.

Run it yourself:

pip install scipy
python benchmarks/bench_distributions.py --sizes 1000000

License

MIT

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

polars_sdist-0.1.0.tar.gz (36.6 kB view details)

Uploaded Source

Built Distributions

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

polars_sdist-0.1.0-cp39-abi3-win_amd64.whl (4.6 MB view details)

Uploaded CPython 3.9+Windows x86-64

polars_sdist-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.0 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

polars_sdist-0.1.0-cp39-abi3-macosx_11_0_arm64.whl (3.7 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

polars_sdist-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

polars_sdist-0.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.4 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

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

File metadata

  • Download URL: polars_sdist-0.1.0.tar.gz
  • Upload date:
  • Size: 36.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for polars_sdist-0.1.0.tar.gz
Algorithm Hash digest
SHA256 917ed8a2303bb044b1d588c43caf9d1291ccf9b067141719e042407e07140797
MD5 1165e2c196c6f5c669d92c65206f5b11
BLAKE2b-256 f0deddb243a365392a083c671dec2ad3108951ce2a5c708fafa9f6ea4540740a

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_sdist-0.1.0.tar.gz:

Publisher: release.yml on mattbuck85/polars-sdist

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_sdist-0.1.0-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: polars_sdist-0.1.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 4.6 MB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for polars_sdist-0.1.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 9cd64ec33a94c81303cead15d664e869c3d810d0713d324cb366262ff1f94550
MD5 94c2e25e3318e1f83ac5435b9c46d53a
BLAKE2b-256 aeb389fa465f022fbcf718d6fe5edb6c470ce69207ab867f4c3a9489da9df04e

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_sdist-0.1.0-cp39-abi3-win_amd64.whl:

Publisher: release.yml on mattbuck85/polars-sdist

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_sdist-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for polars_sdist-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a1b7a57d471303147b1b932b62f05b07171dc0467933eef16dac81e80debecd8
MD5 207ce3a87b065108e5a90ba8e6ea01bd
BLAKE2b-256 73f37da1c99ee2333114f9685be230ba4f2ed8601cdac6f099df9b94315e72ad

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_sdist-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on mattbuck85/polars-sdist

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_sdist-0.1.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_sdist-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0fcd8cc900f177a89d91fd859c117aa6979327e4e574fb91b30415cc10d5a249
MD5 6ccfe15aac2e31fac1b56b1b11aaa394
BLAKE2b-256 d0680117a1919bec9a17bca65a918d14a46ee7a712abda952d381643cb25cee2

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_sdist-0.1.0-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on mattbuck85/polars-sdist

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_sdist-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for polars_sdist-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 220905f5cdec3e4510b74f6fe5079dbb289a281312ea32d19a2ee46f9aa42501
MD5 8a757ff50036f7ad8a8544db6d9f95aa
BLAKE2b-256 05853a3a2f781908c7ae9b2bcfc0bf325102367e867b4e1459549e990a51fcc4

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_sdist-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl:

Publisher: release.yml on mattbuck85/polars-sdist

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_sdist-0.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for polars_sdist-0.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d4ce4f4e10c7b26a172db2257fe053f35e8427c7455ada6ae9300e0045ca42fa
MD5 7f58b8d2546b4077448941cee0d8880f
BLAKE2b-256 aa9fd93c5844e73dce4b9d69fb16c1f1609bc3f069931957547dd9713fb13255

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_sdist-0.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on mattbuck85/polars-sdist

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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