Name | ghq JSON |
Version |
0.0.3
JSON |
| download |
home_page | |
Summary | Gauss-Hermite quadrature in JAX |
upload_time | 2024-02-10 16:17:43 |
maintainer | |
docs_url | None |
author | |
requires_python | ~=3.9 |
license | Apache-2.0 |
keywords |
jax
integration
quadrature
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# ghq
Gauss-Hermite quadrature in JAX
# Installation
```
pip install ghq
```
# Numerical Integration
## 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](https://en.wikipedia.org/wiki/Gauss%E2%80%93Hermite_quadrature). The integral approximation can be executed easily with `ghq`:
```python
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`:
```python
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`:
```python
ghq.univariate_importance(f, mu, sigma, degree=32, lower=a)
```
or for upper-bounded integrals over $[-\infty, b)$ using transformation $y = \log(b - x)$:
```python
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:
```python
ghq.univariate_importance(f, mu, sigma, degree=32, lower=a, upper=b).
```
The [Stan reference manual](https://mc-stan.org/docs/reference-manual/variable-transforms.html) provides an excellent reference for transformations of variables.
## Multivariate Gaussian integrals
$$
\int f(x) \mathbf{N}(x \mid \mu, \Sigma) dx,
$$
in `ghq` is:
```python
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
Coming soon...
Raw data
{
"_id": null,
"home_page": "",
"name": "ghq",
"maintainer": "",
"docs_url": null,
"requires_python": "~=3.9",
"maintainer_email": "",
"keywords": "jax,integration,quadrature",
"author": "",
"author_email": "Sam Duffield <s@mduffield.com>",
"download_url": "https://files.pythonhosted.org/packages/d1/0f/509edef5a2549e3bce46acd47e22a728a1e6b99baf235e5d36ab76adbf22/ghq-0.0.3.tar.gz",
"platform": null,
"description": "# ghq\n\nGauss-Hermite quadrature in JAX\n\n# Installation\n\n```\npip install ghq\n```\n\n# Numerical Integration\n\n\n## Univariate Gaussian integrals\nGauss-Hermite quadrature is a method for numerically integrating functions of the form:\n\n$$\n\\int_{-\\infty}^{\\infty} f(x) \\mathbf{N}(x \\mid \\mu, \\sigma^2) dx \\approx \\sum_{i=1}^\\mathsf{degree} w_i f(x_i),\n$$\n\nwhere $\\lbrace x_i, w_i \\rbrace_{i=1}^\\mathsf{degree}$ are chosen [deterministically](https://en.wikipedia.org/wiki/Gauss%E2%80%93Hermite_quadrature). The integral approximation can be executed easily with `ghq`:\n```python\nghq.univariate(f, mu, sigma, degree=32)\n```\nwhere `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).\n\n## Univariate unbounded integrals\nMore generally, we can use an importance-sampling-like approach to integrate functions of the form:\n\n$$\n\\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)},\n$$\n\nand with `ghq`:\n```python\nghq.univariate_importance(f, mu, sigma, degree=32)\n```\n\n## Univariate half-bounded integrals\nConsider lower-bounded integrals of the form:\n\n$$\n\\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)},\n$$\n\nwhere we use the transformation $y = \\log(x - a)$ to map the lower-bounded integral to an unbounded integral. This can be approximated with `ghq`:\n```python\nghq.univariate_importance(f, mu, sigma, degree=32, lower=a)\n```\nor for upper-bounded integrals over $[-\\infty, b)$ using transformation $y = \\log(b - x)$:\n```python\nghq.univariate_importance(f, mu, sigma, degree=32, upper=b)\n```\n\n## Univariate bounded integrals\nFor doubly-bounded integrals in $[a, b)$ we have\n\n$$\n\\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,\n$$\n\nwhere 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}}$.\n\nIn `ghq` we have:\n```python\nghq.univariate_importance(f, mu, sigma, degree=32, lower=a, upper=b).\n```\n\nThe [Stan reference manual](https://mc-stan.org/docs/reference-manual/variable-transforms.html) provides an excellent reference for transformations of variables.\n\n\n\n## Multivariate Gaussian integrals\n\n$$\n\\int f(x) \\mathbf{N}(x \\mid \\mu, \\Sigma) dx,\n$$\n\nin `ghq` is:\n```python\nghq.multivariate(f, mu, Sigma, degree=32)\n```\nwhere `f: Callable[[Array], Array]` is a function that takes a multivariate input. \nBeware though that multivariate Gauss-Hermite quadrature has complexity \n$O(\\text{degree}^d)$ where $d$ is the dimension of the integral, so it is not feasible \nfor high-dimensional integrals.\n\n\n## Multivariate unbounded integrals\n\nComing soon...\n\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "Gauss-Hermite quadrature in JAX",
"version": "0.0.3",
"project_urls": {
"Homepage": "https://github.com/SamDuffield/ghq",
"Repository": "https://github.com/SamDuffield/ghq"
},
"split_keywords": [
"jax",
"integration",
"quadrature"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f94f040172546a5f6517c5621c262f995b8c56446870b759937917b761ac64d3",
"md5": "d92040669195f0c04543375b63e29060",
"sha256": "2fcf530958cf937d8c8ca3cdf402efcd7fecb4dbd1d036134fed72bedc7ca7ba"
},
"downloads": -1,
"filename": "ghq-0.0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d92040669195f0c04543375b63e29060",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "~=3.9",
"size": 8819,
"upload_time": "2024-02-10T16:17:41",
"upload_time_iso_8601": "2024-02-10T16:17:41.531009Z",
"url": "https://files.pythonhosted.org/packages/f9/4f/040172546a5f6517c5621c262f995b8c56446870b759937917b761ac64d3/ghq-0.0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "d10f509edef5a2549e3bce46acd47e22a728a1e6b99baf235e5d36ab76adbf22",
"md5": "5a81f885e9b162271e9d1c2dcc1f967b",
"sha256": "380d9961ce92ec7ffc588ed20fedccb9b7072865ecd670f1c5e3740dd9fe9c2f"
},
"downloads": -1,
"filename": "ghq-0.0.3.tar.gz",
"has_sig": false,
"md5_digest": "5a81f885e9b162271e9d1c2dcc1f967b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "~=3.9",
"size": 8717,
"upload_time": "2024-02-10T16:17:43",
"upload_time_iso_8601": "2024-02-10T16:17:43.320003Z",
"url": "https://files.pythonhosted.org/packages/d1/0f/509edef5a2549e3bce46acd47e22a728a1e6b99baf235e5d36ab76adbf22/ghq-0.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-10 16:17:43",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "SamDuffield",
"github_project": "ghq",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "ghq"
}