scip.stats.truncnorm - 使用数组进行loc和scale时出错

时间:2016-03-07 15:27:09

标签: python scipy normal-distribution

我想假设正态分布扰乱一组点。我正在使用scipy.stats.truncnorm,因为我需要确保被扰动的点始终是正的。这是一个MWE:

import numpy as np
from scipy.stats import truncnorm

# Generate points to perturb
N = 100000
z = np.random.rand(N)
sigmaz = (z+1.0)*0.03

# Set limits for truncnorm
a = (0.0-z)/sigmaz
b = np.ones_like(z)*np.inf

# Set size -- want to sample once for each point
size = tuple(np.ones(len(z)))
print truncnorm.rvs(a=a,b=b,loc=z,scale=sigmaz,size=size)

但是,我收到以下错误:

Traceback (most recent call last):
  File "./test.py", line 17, in <module>
    print truncnorm.rvs(a=a,b=b,loc=z,scale=sigmaz,size=size)
  File "/share/modules/install_dir/anaconda/lib/python2.7/site-packages/scipy/stats/_distn_infrastructure.py", line 818, in rvs
    cond = logical_and(self._argcheck(*args), (scale >= 0))
  File "/share/modules/install_dir/anaconda/lib/python2.7/site-packages/scipy/stats/_continuous_distns.py", line 3796, in _argcheck
    if self.a > 0:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

那么有人知道如何解决这个错误并为均值和西格玛指定数组,每个数组都有自己不同的边界值a,b?

或者有人知道在python中采用另一种方法来避免手动循环吗?

非常感谢您提供的任何帮助!

1 个答案:

答案 0 :(得分:1)

这是一个已知的错误。截断的正态分布不接受类似于数组的loc和scale。大多数发行版都有,但不是这个。