Metadata-Version: 2.5
Name: saddlepoint-expansions
Version: 0.2.1
Summary: Saddlepoint expansions of any order for multivariate tail probabilities and densities, from the cumulant generating function
Project-URL: Repository, https://github.com/s-broda/saddlepoint-expansions
Project-URL: Documentation, https://s-broda.github.io/saddlepoint-expansions/
Author: Simon A. Broda, Raymond Kan
License: MIT License
        
        Copyright (c) 2026 Simon A. Broda and Raymond Kan
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: Taylor-mode automatic differentiation,asymptotic expansion,orthant probability,saddlepoint approximation,tail probability
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Mathematics
Requires-Python: >=3.10
Requires-Dist: numpy>=1.24
Requires-Dist: scipy>=1.10
Provides-Extra: docs
Requires-Dist: furo; extra == 'docs'
Requires-Dist: myst-parser; extra == 'docs'
Requires-Dist: sphinx>=7; extra == 'docs'
Provides-Extra: test
Requires-Dist: pytest>=7; extra == 'test'
Description-Content-Type: text/markdown

# saddlepoint-expansions

[![CI](https://github.com/s-broda/saddlepoint-expansions/actions/workflows/ci.yml/badge.svg)](https://github.com/s-broda/saddlepoint-expansions/actions/workflows/ci.yml)
[![Documentation](https://img.shields.io/badge/docs-latest-blue.svg)](https://s-broda.github.io/saddlepoint-expansions/)

Saddlepoint expansions of any order for tail probabilities and densities of random vectors,
from the cumulant generating function alone. Python port of the Julia package
[SaddlepointExpansions.jl](https://github.com/s-broda/SaddlepointExpansions.jl).

The package implements the expansions of Broda and Kan, *Uniform saddlepoint expansions for
multivariate tail probabilities*: the orthant probability `P(X ≥ x)` of a random vector in any
dimension (first-order term: Kolassa 2003), its density, and the density and distribution
function of a ratio, with the univariate expansions of Daniels and of Hauschildt and Robinson as
the one-dimensional case; and those of Bao, Broda and Kan, *Inversion formulas and saddlepoint
expansions for Studentized means*, for the density and distribution function of `X/sqrt(Y)`
(`studentized_density`, `studentized_tailprob`, `studentized_quantile`). The expansion of order `M` has relative error `O(n^-M)`, uniformly for
saddlepoints in the closed positive orthant; saddlepoints with negative components are handled
by inclusion-exclusion.

The cumulant generating function is supplied as an ordinary Python function written with numpy.
All its derivatives are generated by Taylor-mode automatic differentiation, with a small
truncated-polynomial algebra included in the package (no dependency beyond numpy and scipy),
directly in the whitened coordinates in which the expansions are evaluated.

```python
import numpy as np
from saddlepoint_expansions import CGF, sample_mean, tailprob, density

# X = (U0 + U1, U0 + U2), U_i independent unit exponentials; the mean of n = 5 observations
K = CGF(lambda t: -np.log(1 - t[0]) - np.log(1 - t[1]) - np.log(1 - t[0] - t[1]), 2,
        domain=lambda t: t[0] < 1 and t[1] < 1 and t[0] + t[1] < 1)
Kn = sample_mean(K, 5)

tailprob(Kn, [3.0, 3.0], order=4)["F"]      # P(X̄₁ ≥ 3, X̄₂ ≥ 3), orders 1 to 4; exact 0.02215196
density(Kn, [3.0, 3.0], order=4)["f"]       # the joint density
tailprob(Kn, [3.0, 3.0], lower=True)["F"]   # P(X̄₁ ≤ 3, X̄₂ ≤ 3)
```

A library of cumulant generating functions (normal, gamma, noncentral chi-squared, quadratic
forms in normal variables, Wishart diagonals, sums of exponentials) and transformations
(reflection, marginals, linear combinations, sample means, independent sums) is included, and
the lower-level derivative interface (`spa_bi`, `spa_mv`, `spa_orthant`, ...) remains available
for models with closed-form derivatives. A `CGF` can also carry its Taylor polynomials in closed
form (`quadratic_forms_cgf` does, by the trace recursion of the paper), and `ratio_density` and
`ratio_tailprob` handle vectors of ratios.

## Installation

```
pip install git+https://github.com/s-broda/saddlepoint-expansions
```

Python 3.10 or later, numpy and scipy.

## Differences from the Julia package

Everything is in double precision. The Julia package also runs in arbitrary precision
(`BigFloat`), uses an exact recursion for the orthant integrals in that case, and repeats
densities in three or more dimensions, and the Studentized-mean density, in higher precision when
the terms cancel strongly; here the cancellation indicator (`info["kappa_pdf"]`, `info["kappa"]`)
is reported instead. The
Julia package is the reference implementation: the test suite of this package compares every
function with values computed by it (`tests/reference_values.csv`, written by
`test/reference_values.jl` of the Julia package and shared with the MATLAB counterpart).

## Documentation and tests

```
pip install -e ".[docs,test]"
sphinx-build -b html docs docs/_build/html
pytest
```

## Citation

> Broda, S. A. and Kan, R. Uniform saddlepoint expansions for multivariate tail probabilities.

and, for the Studentized-mean functions,

> Bao, Y., Broda, S. A. and Kan, R. Inversion formulas and saddlepoint expansions for Studentized means.

## License

MIT; see `LICENSE`.
