Skip to main content

Statistics, pdf, cdf, inverse cdf and random numbers of the generalized chi-square distribution

Project description

gx2

gx2 — Generalized chi-square distribution

gx2 computes the statistics, characteristic function, pdf, cdf, inverse cdf and random numbers of the generalized chi-square distribution.

A generalized chi-square variable is a weighted sum of independent non-central chi-square variables plus a normal variable — equivalently, the quadratic form of a normal random vector. It is parametrized by:

parameter meaning
w weights of the non-central chi-square terms
k their degrees of freedom
lambda_ their non-centralities (named lambda_ because lambda is a Python keyword)
s scale (standard deviation) of the added normal term
m constant offset

Installation

pip install gx2

Requires numpy, scipy and mpmath. matplotlib is optional, for plotting in the getting-started notebook.

To install from a local clone instead:

pip install .
# or, for development (editable install with test/plot extras):
pip install -e ".[plot,test]"

Getting started

import gx2

w, k, lambda_, s, m = [1, -5, 2], [1, 2, 3], [2, 3, 7], 0, 5

gx2.stat(w, k, lambda_, s, m)          # mean and variance
gx2.cdf(25, w, k, lambda_, s, m)       # cdf at x = 25
gx2.pdf(25, w, k, lambda_, s, m)       # pdf at x = 25
gx2.inv(0.9, w, k, lambda_, s, m)      # 90th percentile
gx2.rnd(w, k, lambda_, s, m, size=5)   # random numbers

Open GettingStarted.ipynb for an interactive tour with worked examples and plots. For any function, see its full documentation with help(gx2.cdf) (or gx2.cdf? in Jupyter).

Public functions

function purpose
stat(w, k, lambda_, s, m) mean and variance
char(t, w, k, lambda_, s, m) characteristic function
rnd(w, k, lambda_, s, m, size=, method=) random numbers
cdf(x, w, k, lambda_, s, m, side=, method=, ...) cdf
pdf(x, w, k, lambda_, s, m, side=, method=, ...) pdf
inv(p, w, k, lambda_, s, m, side=, method=, ...) inverse cdf
gx2_to_norm_quad_params(w, k, lambda_, s, m) gx2 → quadratic-form coefficients of a standard normal
norm_quad_to_gx2_params(mu, v, quad, merge=) quadratic form of a normal → gx2 parameters

The individual computation routines (imhof, ruben, ifft, pearson, tail, ellipse, cdf_ray, pdf_ray, …) and numerical helpers (log_sum_exp, signed_log_sum_exp, phi_ray, …) are also exposed.

Computation methods for cdf / pdf

method='auto' (default) picks a good method for the given parameters. You can also force one:

method notes
'imhof' Imhof–Davies numerical integration (precision='basic' or 'vpa')
'ray' ray-trace method (precision='basic', 'log' or 'vpa'; tune with n_rays, force_mc)
'ifft' inverse-FFT method; x='full' returns the cdf/pdf over a spanning grid
'ruben' Ruben's series — requires all w the same sign and s=0
'tail' infinite-tail approximation
'pearson' Pearson's 3-moment approximation
'ellipse' ellipse approximation near a finite tail — requires all w the same sign and s=0

Usage notes

  • cdf and pdf return just the probability/density by default. Pass full_output=True (auto-enabled for x='full') to also receive the error estimate and, for x='full', the grid of x-values.
  • In the far tails, probabilities can fall below double precision (~1e-308). The 'tail', 'ellipse' and 'ray' methods then return the base-10 logarithm of such values (a negative number); inv likewise accepts a negative p as a log10 probability. precision='log' (the ray default) is the easiest way to reach this regime.
  • The 'ray' method runs on the CPU with NumPy and batches automatically over rays to bound memory. precision='vpa' uses mpmath and returns mpmath.mpf objects for sub-realmin values.

Author and citation

Abhranil Das, Center for Perceptual Systems, The University of Texas at Austin. Bugs / comments / questions / suggestions to abhranil.das@utexas.edu.

This is the Python port of the MATLAB toolbox. If you use this code, please cite:

License

MIT — see LICENSE.

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

gx2-1.0.0.tar.gz (23.9 kB view details)

Uploaded Source

Built Distribution

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

gx2-1.0.0-py3-none-any.whl (23.9 kB view details)

Uploaded Python 3

File details

Details for the file gx2-1.0.0.tar.gz.

File metadata

  • Download URL: gx2-1.0.0.tar.gz
  • Upload date:
  • Size: 23.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for gx2-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6e03d2b57a26585caecbcbc9f97c221652016050c0501ef91ecdcc8a88feccd0
MD5 4a9f3cf9d7f7519801c3f51165310ac9
BLAKE2b-256 3683a746f774c13cfa46368a0e5c863329d368f59b81f615f0e2e67e2059454d

See more details on using hashes here.

Provenance

The following attestation bundles were made for gx2-1.0.0.tar.gz:

Publisher: publish.yml on abhranildas/gx2-py

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

File details

Details for the file gx2-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: gx2-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 23.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for gx2-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 acc400b4922efdd27981d1844f42093d16bb7760e4111fb072ac4eaa1bb36c2e
MD5 fedd997504b2c23a702317c189c61ee9
BLAKE2b-256 2dc549241e248d53239a0686d7674cf2e9620abb08593472e8a9489781ebccb4

See more details on using hashes here.

Provenance

The following attestation bundles were made for gx2-1.0.0-py3-none-any.whl:

Publisher: publish.yml on abhranildas/gx2-py

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