Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何模拟具有固定间隔的变量?_Python_Scipy_Statistics_Simulation_Montecarlo - Fatal编程技术网

Python 如何模拟具有固定间隔的变量?

Python 如何模拟具有固定间隔的变量?,python,scipy,statistics,simulation,montecarlo,Python,Scipy,Statistics,Simulation,Montecarlo,我试图模拟现实生活中的过程。历史上测量过的变量显示了一个固定的区间,因此越低越大,这些值在物理上是不可能的 为了模拟过程输出,每个输入变量的历史数据分别表示为最佳拟合概率分布(使用这种方法:) 然而,模拟n次时得到的理论分布并不代表实际生活中预期的最小值和最大值。我正在考虑对每个模拟应用“除测试外的尝试”来检查每个模拟值是否在预期间隔之间,但我不确定这是否是处理此问题的最佳方法,因为没有达到实验平均值和方差。您可以在numpy中使用布尔掩码来重新生成超出所需边界的值。例如: def random

我试图模拟现实生活中的过程。历史上测量过的变量显示了一个固定的区间,因此越低越大,这些值在物理上是不可能的

为了模拟过程输出,每个输入变量的历史数据分别表示为最佳拟合概率分布(使用这种方法:)


然而,模拟n次时得到的理论分布并不代表实际生活中预期的最小值和最大值。我正在考虑对每个模拟应用“除测试外的尝试”来检查每个模拟值是否在预期间隔之间,但我不确定这是否是处理此问题的最佳方法,因为没有达到实验平均值和方差。

您可以在numpy中使用布尔掩码来重新生成超出所需边界的值。例如:

def random_with_bounds(func, size, bounds):
    x = func(size=size)
    r = (x < bounds[0]) | (x > bounds[1])
    while r.any():
        x[r] = func(size=r.sum())
        r[r] = (x[r] < bounds[0]) | (x[r] > bounds[1])
    return x
另一个版本通过
np.arg使用索引数组,其中
略微提高了性能:

def random_with_bounds_2(func, size, bounds):
    x = func(size=size)
    r = np.argwhere((x < bounds[0]) | (x > bounds[1])).ravel()
    while r.size > 0:
        x[r] = func(size=r.size)
        r = r[(x[r] < bounds[0]) | (x[r] > bounds[1])]
    return x
def random_和_-bounds_2(func,size,bounds):
x=func(大小=大小)
r=np.argwhere((xbounds[1])).ravel()
当r.size>0时:
x[r]=func(大小=r.size)
r=r[(x[r]bounds[1])]
返回x

请给出您当前所做工作的实际代码,我发现您的口头解释不清楚。
def random_with_bounds_2(func, size, bounds):
    x = func(size=size)
    r = np.argwhere((x < bounds[0]) | (x > bounds[1])).ravel()
    while r.size > 0:
        x[r] = func(size=r.size)
        r = r[(x[r] < bounds[0]) | (x[r] > bounds[1])]
    return x