创建一个频率类似于钟形曲线的数字数组
creating an array of numbers whose frequency resembles bell curve
我想用 [a,b] 中的数字创建一个数组 A [1 ,1 , 2, 2 ,2 , 5, 5 ,5 ,....] 这样
直方图,其中 Y 轴是数组中数字的频率,X 轴是 [a,b] 类似于钟形曲线.
Bell Curve
- 对于[a,b]中所有i的frequency(i)*i之和大约是一个大数K
python 中有许多函数可用,例如 numpy.random.normal
或 scipsy.stats.truncnorm
,但我无法完全理解它们的用途以及它们如何帮助我创建这样的数组。
第一点很简单,对于第二点,我假设你希望 freq * x 的 "integral" 接近 K(使每个 x * freq(x) ~ K 在数学上是不可能的)。您可以通过调整样本大小来做到这一点。
第一步:a
和b
之间的钟形曲线整数,使用scipy.stats.truncnorm
。来自文档:
Notes
The standard form of this distribution is a standard normal truncated to the range [a, b] --- notice that a and b are defined over
the domain of the standard normal. To convert clip values for a
specific mean and standard deviation, use::
a, b = (myclip_a - my_mean) / my_std, (myclip_b - my_mean) / my_std
取法线在-3, 3范围内,所以曲线很好。调整均值和标准差,使 -3, 3 变为 a, b:
from scipy.stats import truncnorm
a, b = 10, 200
loc = (a + b) / 2
scale = (b - a) / 6
n = 100
f = truncnorm(-3,3, loc=(a+b)/2,scale=(b-a)/6)
现在,由于频率与概率密度函数有关:sum(freq(i) * i ) ~ n * sum(pdf(i) * i)。因此,n = K / sum(pdf(i) * i)。这可以通过以下方式获得:
K = 200000
i = np.arange(a, b +1)
n = int(K / i.dot(f.pdf(i)))
现在生成整数随机样本,并检查函数:
samples = f.rvs(size=n).astype(np.int)
import matplotlib.pyplot as plt
plt.hist(samples, bins = 20)
print(np.histogram(samples, bins=b-a+1)[0].dot(np.arange(a,b+1)))
>> 200315
我想用 [a,b] 中的数字创建一个数组 A [1 ,1 , 2, 2 ,2 , 5, 5 ,5 ,....] 这样
直方图,其中 Y 轴是数组中数字的频率,X 轴是 [a,b] 类似于钟形曲线.
Bell Curve
- 对于[a,b]中所有i的frequency(i)*i之和大约是一个大数K
python 中有许多函数可用,例如 numpy.random.normal
或 scipsy.stats.truncnorm
,但我无法完全理解它们的用途以及它们如何帮助我创建这样的数组。
第一点很简单,对于第二点,我假设你希望 freq * x 的 "integral" 接近 K(使每个 x * freq(x) ~ K 在数学上是不可能的)。您可以通过调整样本大小来做到这一点。
第一步:a
和b
之间的钟形曲线整数,使用scipy.stats.truncnorm
。来自文档:
Notes
The standard form of this distribution is a standard normal truncated to the range [a, b] --- notice that a and b are defined over the domain of the standard normal. To convert clip values for a specific mean and standard deviation, use::
a, b = (myclip_a - my_mean) / my_std, (myclip_b - my_mean) / my_std
取法线在-3, 3范围内,所以曲线很好。调整均值和标准差,使 -3, 3 变为 a, b:
from scipy.stats import truncnorm
a, b = 10, 200
loc = (a + b) / 2
scale = (b - a) / 6
n = 100
f = truncnorm(-3,3, loc=(a+b)/2,scale=(b-a)/6)
现在,由于频率与概率密度函数有关:sum(freq(i) * i ) ~ n * sum(pdf(i) * i)。因此,n = K / sum(pdf(i) * i)。这可以通过以下方式获得:
K = 200000
i = np.arange(a, b +1)
n = int(K / i.dot(f.pdf(i)))
现在生成整数随机样本,并检查函数:
samples = f.rvs(size=n).astype(np.int)
import matplotlib.pyplot as plt
plt.hist(samples, bins = 20)
print(np.histogram(samples, bins=b-a+1)[0].dot(np.arange(a,b+1)))
>> 200315