【问题标题】:generate N random numbers from a skew normal distribution using numpy使用 numpy 从偏态正态分布生成 N 个随机数
【发布时间】:2016-07-12 02:04:24
【问题描述】:

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

例如我目前的用途是

x = numpy.random.randn(1000)

理想的功能是例如

x = randn_skew(1000, skew=0.7)

解决方案需要符合:python 2.7版本,numpy v.1.9

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

【问题讨论】:

  • 您提出了一个请求,但这是一个问答网站,那么您的问题是什么?我们将帮助您解决在编码时遇到的问题,但不仅仅是为您编写代码。
  • 你想生成服从分布的随机数?

标签: python-2.7 numpy random


【解决方案1】:
from scipy.stats import skewnorm
a=10
data= skewnorm.rvs(a, size=1000)

这里的a是一个参数,可以参考: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.skewnorm.html

【讨论】:

  • 这很好,但问题最初是针对numpy的。
【解决方案2】:

改编自 fGarch R 包中的 rsnorm 函数

def random_snorm(n, mean = 0, sd = 1, xi = 1.5):
    def random_snorm_aux(n, xi):
        weight = xi/(xi + 1/xi)
        z = numpy.random.uniform(-weight,1-weight,n)
        xi_ = xi**numpy.sign(z)
        random = -numpy.absolute(numpy.random.normal(0,1,n))/xi_ * numpy.sign(z)
        m1 = 2/numpy.sqrt(2 * numpy.pi)
        mu = m1 * (xi - 1/xi)
        sigma = numpy.sqrt((1 - m1**2) * (xi**2 + 1/xi**2) + 2 * m1**2 - 1)
        return (random - mu)/sigma

    return random_snorm_aux(n, xi) * sd + mean

【讨论】:

  • 当我尝试执行此操作时出现噪音 p = random_snorm(n, 5.61594709, 3.73888096, 1.62537967) x = linspace(-10, 100, n) plt.plot(x, p) 或我将如何检查结果?
【解决方案3】:

我首先生成 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)

接下来我找到了一个从偏态正态分布中采样随机数的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)

然后写了一个快速版本(未经大量测试)似乎是正确的:

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)

【讨论】:

    猜你喜欢
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 2014-11-06
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 2020-08-26
    • 2011-05-26
    相关资源
    最近更新 更多