Py学习  »  Python

python随机_样本最小值

Cédric • 4 年前 • 1916 次点击  

我目前正在使用random_sample为3只股票生成权重分配,其中每一行的值加起来等于1。

for portfolio in range (10):
    weights = np.random.random_sample(3)
    weights  = weights/ np.sum(weights)
    print (weights)

[0.39055438 0.44055996 0.16888567]
[0.22401792 0.26961926 0.50636282]
[0.67856154 0.21523207 0.10620639]
[0.33449127 0.36491387 0.30059486]
[0.55274192 0.23291811 0.21433997]
[0.20980909 0.38639029 0.40380063]
[0.24600751 0.199761   0.5542315 ]
[0.50743661 0.26633377 0.22622962]
[0.1154567  0.36803903 0.51650427]
[0.29092731 0.34675988 0.36231281]

我能做到,但有没有办法确保最低重量分配大于0.05?这意味着最小重量分配只能是[0.05 0.9 0.05]左右

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/132046
文章 [ 2 ]  |  最新文章 4 年前
gimix
Reply   •   1 楼
gimix    4 年前

看一看 docs

结果来自所述时间间隔内的连续均匀分布。取样 Unif(a,b),b>A. 将…的输出相乘 random_sample 通过 (b-a) 加上 a .

在这种情况下, 0.95 * weight + 0.05

Nin17
Reply   •   2 楼
Nin17    4 年前

你可以忽略它们:

n = 0
while n < 10:
    weights = np.random.random_sample(3)
    weights  = weights/ np.sum(weights)
    if any(i < 0.05 for i in weights):
        continue
    n += 1
    print (weights)