对于我正在研究的研究项目,我需要使用以下参数生成一组随机(或伪随机)数据(比如10,000个数据):
现在显然这个分布看起来有点像
生成的那样SUPER::foo()
然而,这并未给出必要的均值或最大值。有可能解决这个问题吗?
答案 0 :(得分:1)
您需要根据需要选择概率分布。有a number of continuous distributions with bounded intervals。例如,您可以选择(缩放)beta distribution并计算参数α和β以适合您的均值和标准差:
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
def my_distribution(min_val, max_val, mean, std):
scale = max_val - min_val
location = min_val
# Mean and standard deviation of the unscaled beta distribution
unscaled_mean = (mean - min_val) / scale
unscaled_var = (std / scale) ** 2
# Computation of alpha and beta can be derived from mean and variance formulas
t = unscaled_mean / (1 - unscaled_mean)
beta = ((t / unscaled_var) - (t * t) - (2 * t) - 1) / ((t * t * t) + (3 * t * t) + (3 * t) + 1)
alpha = beta * t
# Not all parameters may produce a valid distribution
if alpha <= 0 or beta <= 0:
raise ValueError('Cannot create distribution for the given parameters.')
# Make scaled beta distribution with computed parameters
return scipy.stats.beta(alpha, beta, scale=scale, loc=location)
np.random.seed(100)
min_val = 1.5
max_val = 35
mean = 9.87
std = 3.1
my_dist = my_distribution(min_val, max_val, mean, std)
# Plot distribution PDF
x = np.linspace(min_val, max_val, 100)
plt.plot(x, my_dist.pdf(x))
# Stats
print('mean:', my_dist.mean(), 'std:', my_dist.std())
# Get a large sample to check bounds
sample = my_dist.rvs(size=100000)
print('min:', sample.min(), 'max:', sample.max())
输出:
mean: 9.87 std: 3.100000000000001
min: 1.9290674232087306 max: 25.03903889816994
概率密度函数图:
在这种情况下,并非每种可能的边界组合,均值和标准偏差都会产生有效的分布,并且β分布具有您可能想要或可能不想要的某些特定属性。可能存在无限可能的分布,这些分布符合边界的一些给定要求,具有不同质量的平均值和标准偏差(倾斜,峰度,模态,......)。您需要确定案例的最佳分布。