Alex Merson
Alex Merson

Reputation: 687

scip.stats.truncnorm -- error using arrays for loc and scale

I am wanting to perturb a set of points assuming a normal distribution. I am using scipy.stats.truncnorm as I need to ensure that the perturbed points are always positive. Here is a 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)

However, I am getting the following error:

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()

So does anybody know how to get around this error and specify arrays for the mean and sigma, each with their own different value for the bounds a,b?

Or does anybody know of another way to do this in python that avoids manual loops?

Thanks a lot for any help you can provide!

Upvotes: 0

Views: 276

Answers (1)

ev-br
ev-br

Reputation: 26030

This is a known bug. Truncated normal distribution does not accept array-like loc and scale. Most distributions do, but not this one.

Upvotes: 1

Related Questions