SophieAlexVW
SophieAlexVW

Reputation: 11

Error using scipy.stats truncnorm.rvs(a,b,loc,scale) python

I'm trying to sample from the truncated normal distribution by using the truncnorm() function from the scipy stats package in python. However, I keep getting the following error:

x = _norm_ilogcdf(np.log(q) + _norm_logcdf(b)) z = z - (_norm_logcdf(z) - y) / _norm_logcdfprime(z) assert np.abs(z) > TRUNCNORM_TAIL_X/2

I'm not completely sure what it means, but I'm guessing it has something to do with the mean being outside the bounds. But then what is the difference in comparison to:

Domain error in arguments

For clarification, I am not sampling from a standard normal. I altered the bounds by use of the following equation:

a, b = (myclip_a - my_mean) / my_std, (myclip_b - my_mean) / my_std

and I enter these bounds into the function truncnorm.rvs(a,b,my_mean, my_std). Any clarification is much appreciated!

Upvotes: 1

Views: 706

Answers (1)

Nitalon
Nitalon

Reputation: 11

I've encountered the same problem as well. The thing is that the lower bound a is above (or close to) the 99% quantile of the Normal distribution. Thus the truncation causes scipy to crush. The only solution I came out with is to check before truncation if myclip_a is above the 99% quantile and if so avoid the update. I hope that someone will find a better solution than mine!

Upvotes: 1

Related Questions