我在一个answer中偶然发现了这段代码,我想自动化使分布完全适应两个边界的过程。
import numpy as np
from scipy import stats
bounds = [0, 100]
n = np.mean(bounds)
# your distribution:
distribution = stats.norm(loc=n, scale=20)
# percentile point, the range for the inverse cumulative distribution function:
bounds_for_range = distribution.cdf(bounds)
# Linspace for the inverse cdf:
pp = np.linspace(*bounds_for_range, num=1000)
x = distribution.ppf(pp)
# And just to check that it makes sense you can try:
from matplotlib import pyplot as plt
plt.hist(x)
plt.show()
假设我要使用[720, 965]
值或任何其他范围来适应我的分布。有没有一种方法可以对scale
中的stats.norm
的调整进行软编码,以使该分布跨我的边界而没有任何不合理的差距?还是有任何具有这种功能的功能?
示例代码的scale
约为20,但是对于[720, 965]
的示例,我不得不将其调整为〜50。
答案 0 :(得分:0)
我不确定,但是truncated normal distribution应该正是您想要的。
from scipy.stats import truncnorm
distr_ab = truncnorm(a, b) # truncated normal distribution in the interval [a, b]
distr_ab.rvs(size=100) # get 100 samples from the distribution
# distr_ab.cdf, distr_ab.ppf etc... all accessible