Skip to main content

Fast evaluation of multivariate normal distribution

Project description

Fast evaluation of multivariate normal distributions

This package provides a fast way to evaluate the pdf and cdf of a standardized multivariate normal distribution. Currently, it only contains code for the bivariate normal distribution.

The implementation in this package is based on the following references:

  1. Drezner, Zvi, and George O. Wesolowsky. "On the computation of the bivariate normal integral." Journal of Statistical Computation and Simulation 35.1-2 (1990): 101-107.
  2. Genz, Alan, and Frank Bretz. "Computation of multivariate normal and t probabilities." Lecture Notes in Statistics 195 (2009)

Simply put, the method comes down to an interpolation specifically tailored to the multivariate normal distribution. Although it is an approximation, the method is near to exact and fast. The implementation in Scipy is based on the same methodology, see here and [here](https://github.com/scipy/scipy/blob/v1.13.0/scipy/stats/_qmvnt.py.

With scalar input, the speed is comparable to the Scipy implementation. The Scipy implemantation, however, is slow for vector valued input. This packages containes a vectorized implementation which is signficantly faster albeit still slower than a native C implementation such as the one in the approxcdf package. The advantage of this package is that it is pure Python and does not require any (build) dependencies.

Rough findings for speed improvements compared to the Scipy implementation.

Module Correlation Speed improvement single Speed improvement vectorized
fastnorm abs < 0.925 >1000 x faster than Scipy 10 x faster than Scipy
approxcdf abs < 0.925 >1000 x faster than Scipy 10 x faster than Scipy
fastnorm abs > 0.925 >1000 x faster than Scipy 3 x faster than Scipy
approxcdf abs > 0.925 >1000 x faster than Scipy 10 x faster than Scipy

These finds are based on an average of 100 runs and can be reproduced by running the example.py script.

Basic example

import fastnorm as fn
correl = 0.5

x=[1,1]
fn.bivar_norm_pdf(x, correl)
fn.bivar_norm_cdf(x, correl)

x=[[1,1],[2,2]]
fn.bivar_norm_pdf(x, correl)
fn.bivar_norm_cdf(x, correl)

Installation

To install from PyPI:

pip install fastnorm

To install the latest development version from github:

pip install git+https://github.com/mvds314/fastnorm.git

Development

For development purposes, clone the repo:

git clone https://github.com/mvds314/fastnorm.git

Then navigate to the folder containing setup.py and run

pip install -e .

to install the package in edit mode.

Run unittests with pytest.

Roadmap

  • Add support for the trivariate and quadrivariate normal distribution.
  • Add support for the higher dimensional normal distribution.
  • Maybe extend to the multivariate t-distribution.

Related software

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

fastnorm-26.3.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

fastnorm-26.3.1-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file fastnorm-26.3.1.tar.gz.

File metadata

  • Download URL: fastnorm-26.3.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for fastnorm-26.3.1.tar.gz
Algorithm Hash digest
SHA256 3f910b6436e09117e88863981976686c5a360a26240f0521f4c9abaebf3aa6e2
MD5 7931434f4e58e09f8a037d88b24b4f9e
BLAKE2b-256 2d845c7f0bbd5ccd17128af2e1fe042bb983b90a048edce71aa466f19aa94b66

See more details on using hashes here.

File details

Details for the file fastnorm-26.3.1-py3-none-any.whl.

File metadata

  • Download URL: fastnorm-26.3.1-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for fastnorm-26.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 18ae59ee09867955c79dfc7bef82f9b5837e5f17f7a2fc30044606d92dac6e13
MD5 ca8a1c6603fe5f74e3f3443ffd52d5a6
BLAKE2b-256 8ab66f6f468339ffbc5eac0ddef2bfdf1293dedef00073f06eab7d534857d84a

See more details on using hashes here.

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