gpt4 book ai didi

python-2.7 - 使用 numpy 从偏态正态分布生成 N 个随机数

转载 作者:行者123 更新时间:2023-12-04 05:06:28 25 4
gpt4 key购买 nike

我需要 python 中的一个函数来从偏斜正态分布返回 N 个随机数。偏斜需要作为一个参数。

例如我目前的用途是
x = numpy.random.randn(1000)
理想的功能是例如
x = randn_skew(1000, skew=0.7)
解决方案需要符合:python version 2.7, numpy v.1.9

类似的答案在这里:skew normal distribution in scipy但是,这会生成 PDF 而不是随机数。

最佳答案

我首先生成 PDF 曲线以供引用:

NUM_SAMPLES = 100000
SKEW_PARAMS = [-3, 0]

def skew_norm_pdf(x,e=0,w=1,a=0):
# adapated from:
# http://stackoverflow.com/questions/5884768/skew-normal-distribution-in-scipy
t = (x-e) / w
return 2.0 * w * stats.norm.pdf(t) * stats.norm.cdf(a*t)

# generate the skew normal PDF for reference:
location = 0.0
scale = 1.0
x = np.linspace(-5,5,100)

plt.subplots(figsize=(12,4))
for alpha_skew in SKEW_PARAMS:
p = skew_norm_pdf(x,location,scale,alpha_skew)
# n.b. note that alpha is a parameter that controls skew, but the 'skewness'
# as measured will be different. see the wikipedia page:
# https://en.wikipedia.org/wiki/Skew_normal_distribution
plt.plot(x,p)

PDFs of skew normal distributions

接下来我找到了一个从偏斜正态分布中采样随机数的VB实现,并将其转换为python:
# literal adaption from:
# http://stackoverflow.com/questions/4643285/how-to-generate-random-numbers-that-follow-skew-normal-distribution-in-matlab
# original at:
# http://www.ozgrid.com/forum/showthread.php?t=108175
def rand_skew_norm(fAlpha, fLocation, fScale):
sigma = fAlpha / np.sqrt(1.0 + fAlpha**2)

afRN = np.random.randn(2)
u0 = afRN[0]
v = afRN[1]
u1 = sigma*u0 + np.sqrt(1.0 -sigma**2) * v

if u0 >= 0:
return u1*fScale + fLocation
return (-u1)*fScale + fLocation

def randn_skew(N, skew=0.0):
return [rand_skew_norm(skew, 0, 1) for x in range(N)]

# lets check they at least visually match the PDF:
plt.subplots(figsize=(12,4))
for alpha_skew in SKEW_PARAMS:
p = randn_skew(NUM_SAMPLES, alpha_skew)
sns.distplot(p)

histograms from skew normal distributions as generated

然后写了一个快速版本(没有进行大量测试)似乎是正确的:
def randn_skew_fast(N, alpha=0.0, loc=0.0, scale=1.0):
sigma = alpha / np.sqrt(1.0 + alpha**2)
u0 = np.random.randn(N)
v = np.random.randn(N)
u1 = (sigma*u0 + np.sqrt(1.0 - sigma**2)*v) * scale
u1[u0 < 0] *= -1
u1 = u1 + loc
return u1

# lets check again
plt.subplots(figsize=(12,4))
for alpha_skew in SKEW_PARAMS:
p = randn_skew_fast(NUM_SAMPLES, alpha_skew)
sns.distplot(p)

histograms from skew normal distributions as generated by the faster method

关于python-2.7 - 使用 numpy 从偏态正态分布生成 N 个随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36200913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com