I am using this code to sample from a truncated normal:
np.random.seed(1234)
Z = truncnorm.rvs(a = 31.399904/0.822358, b = np.inf, loc = 31.399904, scale = 0.822358)
It generates inf values for Z, which mess up with the rest of my code. Would you have an idea how to avoid this?
If you want a truncnorm supported on [lower, upper]
use
X = stats.truncnorm(
(lower - mu) / sigma, (upper - mu) / sigma, loc=mu, scale=sigma)
For example, if you want a truncated norm supported on [31.399904, np.inf]
with mean 32 and std deviation 0.822358, then you would use
import scipy.stats as stats
import matplotlib.pyplot as plt
lower, upper = 31.399904, np.inf
mu, sigma = 32, 0.822358
X = stats.truncnorm(
(lower - mu) / sigma, (upper - mu) / sigma, loc=mu, scale=sigma)
N = stats.norm(loc=mu, scale=sigma)
data = X.rvs(10000)
fig, ax = plt.subplots(2, sharex=True)
ax[0].hist(data, density=True)
ax[1].hist(N.rvs(10000), density=True)
plt.show()
The top histogram shows the distribution of a sample from the truncnorm, the lower histogram show the distribution of a sample from a standard normal with the same mean and std deviation.
If you also want the mean mu
to equal the left endpoint of the support, then you would use
lower, upper = 31.399904, np.inf
mu, sigma = lower, 0.822358
Z = stats.truncnorm.rvs((lower - mu) / sigma, (upper - mu) / sigma, loc=mu, scale=sigma)
which is equivalent to
Z = stats.truncnorm.rvs(0, np.inf, loc=31.399904, scale=0.822358)
For example,
In [47]: np.random.seed(1234)
In [49]: stats.truncnorm.rvs(0, np.inf, loc=31.399904, scale=0.822358)
Out[49]: 31.599232630594255
The problem with
Z = truncnorm.rvs(a = 31.399904/0.822358, b = np.inf, loc = 31.399904, scale = 0.822358)
is that the mean is at loc=31.3999904, but the left endpoint is around 38.2:
In [51]: a = 31.399904/0.822358; a
Out[51]: 38.18276711602489
The most likely value of a truncnorm should be at its mean, but the mean falls outside of the support, [38.2, np.inf]
. This contradiction leads to the bizarre behavior.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.