How to create a distribution with a given value, variance, skew and excess in Python?

random.gauss (mu, sigma)

Above is a function that allows you to randomly draw a number from a normal distribution with a given value and dispersion. But how can we extract values ​​from the normal distribution, determined not only by the first two moments?

sort of:

random.gauss (mu, sigma, skew, excess)

+6
source share
2 answers

How about using scipy? You can select a distribution from continuous distributions in the scipy.stats library .

The generalized gamma function has non-zero skew and kurtosis, but you will have a little work to figure out which parameters to use to indicate the distribution to get a specific average, variance, skew and kurtosis. Here is the code to get you started.

import scipy.stats import matplotlib.pyplot as plt distribution = scipy.stats.norm(loc=100,scale=5) sample = distribution.rvs(size=10000) plt.hist(sample) plt.show() print distribution.stats('mvsk') 

This displays a histogram of a sample of 10,000 elements from the normal distribution with an average of 100 and a variance of 25 and prints the distribution statistics:

(array(100.0), array(25.0), array(0.0), array(0.0))

Replacing the normal distribution with a generalized gamma distribution,

 distribution = scipy.stats.gengamma(100, 70, loc=50, scale=10) 

you get statistics [mean, variance, skew, excess]] (array(60.67925117494595), array(0.00023388203873597746), array(-0.09588807605341435), array(-0.028177799805207737)) .

+4
source

Try using this:

http://statsmodels.sourceforge.net/devel/generated/statsmodels.sandbox.distributions.extras.pdf_mvsk.html#statsmodels.sandbox.distributions.extras.pdf_mvsk

Returns the advanced Gaussian PDF function, given the list of the 1st, 2nd moment and skew, and Fisher (excess) excess.

Parameters: mvsk: list mu, mc2, skew, kurt

Looks nice. There is a link to the source on this page.

Oh, and here is another StackOverflow question that pointed me there: Apply kurtosis to distribution in python

+1
source

Source: https://habr.com/ru/post/956776/


All Articles