Skip to main content

ghq

Gauss-Hermite quadrature in JAX

Installed easily from PyPI:

pip install ghq

Univariate Gaussian integrals

Gauss-Hermite quadrature is a method for numerically integrating functions of the form:

$$ \int_{-\infty}^{\infty} f(x) \mathbf{N}(x \mid \mu, \sigma^2) dx \approx \sum_{i=1}^\mathsf{degree} w_i f(x_i), $$

where $\lbrace x_i, w_i \rbrace_{i=1}^\mathsf{degree}$ are chosen deterministically. The integral approximation can be executed easily with ghq:

ghq.univariate(f, mu, sigma, degree=32)

where f: Callable[[float], Array] is a JAX vectorisable function and degree is an optional argument controlling the number of function evalutions, increasing degree increases the accuracy of the integral. (Note that the output of f can be multivariate but the input is univariate).

Univariate unbounded integrals

More generally, we can use an importance-sampling-like approach to integrate functions of the form:

$$ \int_{-\infty}^{\infty} f(x) dx = \int_{-\infty}^{\infty} \frac{f(x)}{\mathbf{N}(x \mid \mu, \sigma^2)} \mathbf{N}(x \mid \mu, \sigma^2) dx \approx \sum_{i=1}^\mathsf{degree} w_i \frac{f(x_i)}{\mathbf{N}(x_i \mid \mu, \sigma^2)}, $$

and with ghq:

ghq.univariate_importance(f, mu, sigma, degree=32)

Univariate half-bounded integrals

Consider lower-bounded integrals of the form:

$$ \int_{a}^{\infty} f(x) dx =\int_{-\infty}^{\infty} f(a + e^y) e^y dy \approx \sum_{i=1}^\mathsf{degree} w_i \frac{f(a + e^{y_i}) e^{y_i}}{\mathbf{N}(y_i \mid \mu, \sigma^2)}, $$

where we use the transformation $y = \log(x - a)$ to map the lower-bounded integral to an unbounded integral. This can be approximated with ghq:

ghq.univariate_importance(f, mu, sigma, degree=32, lower=a)

or for upper-bounded integrals over $[-\infty, b)$ using transformation $y = \log(b - x)$:

ghq.univariate_importance(f, mu, sigma, degree=32, upper=b)

Univariate bounded integrals

For doubly-bounded integrals in $[a, b)$ we have

$$ \int_{a}^{b} f(x) dx = \int_{-\infty}^{\infty} f\left(a + (b-a)\text{logit}^{-1}(y)\right) (b-a) \text{logit}^{-1}(y) \left(1-\text{logit}^{-1}(y)\right) dy, $$

where we use the transfomation $y=\text{logit}(\frac{x-a}{b-a})$ with $\text{logit}(u)=\log\frac{u}{1-u}$ and $\text{logit}^{-1}(v) = \frac{1}{1+e^{-v}}$.

In ghq we have:

ghq.univariate_importance(f, mu, sigma, degree=32, lower=a, upper=b).

The Stan reference manual provides an excellent reference for transformations of variables.

Multivariate Gaussian integrals

$$ \int f(x) \mathbf{N}(x \mid \mu, \Sigma) dx, $$

in ghq is:

ghq.multivariate(f, mu, Sigma, degree=32)

where f: Callable[[Array], Array] is a function that takes a multivariate input. Beware though that multivariate Gauss-Hermite quadrature has complexity $O(\text{degree}^d)$ where $d$ is the dimension of the integral, so it is not feasible for high-dimensional integrals.

Multivariate unbounded integrals

For generic multivariate integrals

$$ \int f(x) dx = \int \frac{f(x)}{\mathbf{N}(x \mid \mu, \Sigma)} \mathbf{N}(x \mid \mu, \Sigma) dx, $$

in ghq is:

ghq.multivariate_importance(f, mu, Sigma, degree=32)

Although as above this is not feasible for high-dimensional integrals due to $O(\text{degree}^d)$ cost. Additionally only expect good performance when the domain of $f$ is approximately the same as the domain of high probability mass for the Gaussian distribution.

Multivariate bounded integrals

Coming soon...

Citation

If you use ghq in your research, please cite it using the following BibTeX entry:

@software{ghq,
  author = {Duffield, Samuel},
  title = {ghq: Gauss-Hermite quadrature in JAX},
  year = {2024},
  url = {https://github.com/SamDuffield/ghq}
}

Metadata

Release files for ghq 0.0.5

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

Source distribution (sdist)

Source distribution for ghq 0.0.5
File Size Uploaded
ghq-0.0.5.tar.gz 9.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ghq 0.0.5
File Interpreter ABI Platform
ghq-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 19.5 kB

Release files / ghq-0.0.5.tar.gz

Download URL ghq-0.0.5.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
50577b99b8f13b13c22c59255937b2477e2fcda585bbde5ca880cce8fa2af42c
BLAKE2b-256 checksum
How to use checksums
58b4569d9a7e1ed178c93a5b79efd0ec1115470f2ff442891291fd5f8e175842
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.10

Release files / ghq-0.0.5-py3-none-any.whl

Download URL ghq-0.0.5-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
890b349114ffdb4d007ab92aa1ef3273c807a594f790b154fb0c37aaab57cb3b
BLAKE2b-256 checksum
How to use checksums
001fd453609183edb791296049a424bf4a56a147ec3e3e7a04ccdbd5295d196b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.10

Release history Release notifications | RSS feed

This release

0.0.5 This release

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

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