如何在Python中生成具有给定均值,方差,偏斜和峰度的分布? [英] How to generate a distribution with a given mean, variance, skew and kurtosis in Python?
问题描述
random.gauss(mu,sigma)
random.gauss(mu, sigma)
Above是一项功能,可以根据给定的均值和方差从正态分布中随机抽取一个数字.但是,我们如何才能从仅由两个前时刻定义的正态分布中提取值呢?
Above is a function allowing to randomly draw a number from a normal distribution with a given mean and variance. But how can we draw values from a normal distribution defined by more than only the two first moments?
类似:
random.gauss(亩,西格玛,偏斜,峰度)
random.gauss(mu, sigma, skew, kurtosis)
推荐答案
如何使用scipy?您可以从scipy.stats中的连续分布中选择所需的分布库.
How about using scipy? You can pick the distribution you want from continuous distributions in the scipy.stats library.
广义伽玛函数具有非零的偏斜度和峰度,但是您需要做一些工作来弄清楚使用哪些参数来指定分布以获得特定的均值,方差,偏度和峰度.这是一些入门的代码.
The generalized gamma function has non-zero skew and kurtosis, but you'll have a little work to do to figure out what parameters to use to specify the distribution to get a particular mean, variance, skew and kurtosis. Here's some 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')
这将显示正态分布中平均值为100,方差为25的10,000个元素样本的直方图,并打印分布的统计信息:
This displays a histogram of a 10,000 element sample from a normal distribution with mean 100 and variance 25, and prints the distribution's statistics:
(array(100.0), array(25.0), array(0.0), array(0.0))
用广义伽玛分布代替正态分布,
Replacing the normal distribution with the generalized gamma distribution,
distribution = scipy.stats.gengamma(100, 70, loc=50, scale=10)
您会得到统计数据[均值,方差,偏斜,峰度]
(array(60.67925117494595), array(0.00023388203873597746), array(-0.09588807605341435), array(-0.028177799805207737))
.
you get the statistics [mean, variance, skew, kurtosis]
(array(60.67925117494595), array(0.00023388203873597746), array(-0.09588807605341435), array(-0.028177799805207737))
.
这篇关于如何在Python中生成具有给定均值,方差,偏斜和峰度的分布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!