Skip to main content

Fast Generation of Correlated Non-Normal Random Variates

Project description

Fast generation of COrrelated Non-NOrmal RAndom Variates (CONNORAV)

PyPI Python Build Coverage License: MIT

Specify your distributions in terms of mean, standard deviation, skew, kurtosis and a correlation matrix. CONNORAV can generate random variates fitting these distribution descriptions in a fast and accurate manner.

Requirements

  • Python >= 3.8
  • NumPy:
    • For Python < 3.10: >=1.22,<2.0
    • For Python >= 3.10: >=1.26 (2.x supported)
  • SciPy:
    • For Python < 3.10: >=1.10,<1.12
    • For Python >= 3.10: >=1.13

Motivation

Fitting a distribution to mean, standard deviation, skew and kurtosis is a surprisingly tricky proposition, which is a little surprising since these are the most common descriptors used when describing non-normal distributions. CONNORAV achieves this using the optimization techinique described by Tuenter (2001) to fit these statistics to an analytical Johnson SU distribution.

  • We match the first four moments to a Johnson SU distribution using:
  • H. J. H. Tuenter (2001), “An algorithm to determine the parameters of SU-curves in the Johnson system of probability distributions by moment matching.”

While it is true that not all distributions can be described using mean, standard deviation, skew and kurtosis alone, a lot of data resembles these shapes and the statistics are very easy to measure.

Once the distributions have been specified, non-normal correlated random variates can be generated via the copula trick. This is extremely useful for monte-carlo analysis and risk assessment.

Examples

Generate a distribution and test the random number generator.

In [1]: from connorav import MSSKDistribution

In [2]: dist = MSSKDistribution(2.5,1.3,-0.3,2.0)

Alternatively you can construct using an array or tuple

In [2]: dist = MSSKDistribution([2.5,1.3,-0.3,2.0])

In [3]: dist.mean()
Out[3]: 2.4999999999942295

In [4]: dist.std()
Out[4]: 1.3000000000127732

In [5]: samples = dist.rvs(100000)

In [6]: samples.mean()
Out[6]: 2.4958419823009592

In [7]: samples.std()
Out[7]: 1.2991039572289937

In [8]: from scipy.stats import skew, kurtosis

In [9]: skew(samples)
Out[9]: -0.2879332050448415

In [10]: kurtosis(samples)
Out[10]: 1.926596584590544

Lets generate some numbers.

In [1]: import numpy

In [2]: from connorav import CorrelatedNonNormalRandomVariates

In [3]: num_samples = 200000

In [4]: # The desired covariance matrix.

In [5]: correlations = numpy.array([
   ...:         [  1.00,  0.60,  0.30],
   ...:         [  0.60,  1.00,  0.50],
   ...:         [  0.30,  0.50,  1.00]
   ...:     ])


In [7]: moments = numpy.array([[0.0,1.0,0.0,0.0],[14.0,0.9,0.8,1.8],[7.9,0.3,0.2,1.6]])


In [8]: rv_generator = CorrelatedNonNormalRandomVariates(moments,correlations,num_samples)
Normal

In [9]: rv = rv_generator.rv

In [11]: x = numpy.corrcoef(rv)

In [12]: print x
[[ 1.          0.58994313  0.29320107]
 [ 0.58994313  1.          0.48837508]
 [ 0.29320107  0.48837508  1.        ]]


In [13]: from scipy.stats import skew, kurtosis

In [14]: for i in range(3):
   ....:         print "Moments",i
   ....:         print rv[i,:].mean()
   ....:         print rv[i,:].std()
   ....:         print skew(rv[i,:])
   ....:         print kurtosis(rv[i,:])
   ....:     
Moments 0
0.00153746238126
0.999078801649
-0.0016863652429
-0.0135121492172
Moments 1
13.9980416496
0.894414387863
0.805404037664
1.8027042337
Moments 2
7.90077062606
0.29955446882
0.215294195427
1.61036003402

=======

Created by Christopher Bates

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

connorav-0.2.1.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

connorav-0.2.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file connorav-0.2.1.tar.gz.

File metadata

  • Download URL: connorav-0.2.1.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for connorav-0.2.1.tar.gz
Algorithm Hash digest
SHA256 4673e07a894872738d27c42ade78c00d778156b7d2ac55f7050985a57e3481d6
MD5 d55d4e44bbbe1b2891974b5e110f6bab
BLAKE2b-256 a814ac7b98201ca85ccf51a6c94704f68e890a45f52f04a33e081d0b178d00f7

See more details on using hashes here.

File details

Details for the file connorav-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: connorav-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for connorav-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d5025fb4531b9b34428e5794ff6d069b0ed49e53cdfe22197fd93c32c9b0618c
MD5 6407a14399db7810f8c233b7c9ec3afd
BLAKE2b-256 80d039c6d9b0637b06ba2738fb9e37936ccdec9744b7dc3d7662adf49fabddfd

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