使用SALib的Python中的灵敏度分析错误

时间:2019-01-30 19:22:11

标签: python-3.x

Performing a sensitivity analysis with python之后,我收到一条错误消息。

我的代码如下:

from SALib.sample import saltelli
from SALib.analyze import sobol

def ls(X):
    # column 0 = demand, column 1 =  bookings, column 2 = inventory
    return max(X[:,0] - X[:,1] - X[:,2])

problem = {'num_vars': 3,
           'names': ['demand', 'bookings', 'inventory'],
           'bounds': [[0, 1250],
                     [0, 11000],
                     [0, 120000]]
           } 

# Generate samples
param_values = saltelli.sample(problem, 10000000, calc_second_order=False)

# Run model
Y = ls(param_values)

# Perform analysis
Si = sobol.analyze(problem, Y)

我收到以下错误。 RuntimeError:         模型输出文件中的样本数不正确。         确认calc_second_order与采样期间使用的选项匹配。

当我看着https://github.com/SALib/SALib/blob/master/SALib/analyze/sobol.py时 我看到了:

if calc_second_order and Y.size % (2 * D + 2) == 0:
        N = int(Y.size / (2 * D + 2))
    elif not calc_second_order and Y.size % (D + 2) == 0:
        N = int(Y.size / (D + 2))
    else:
        raise RuntimeError("""
        Incorrect number of samples in model output file.
        Confirm that calc_second_order matches option used during 
sampling.""")

但是,这并不能真正帮助我解决错误。它与saltelli.sample()中的第二个参数有关吗?

1 个答案:

答案 0 :(得分:0)

有两个问题。首先,函数ls()的长度为等于param_values中的行数的向量时,将返回单个值。其次,saltelli.sample()sobol.analyze()中的默认假设是拥有calc_second_order=True,因此在两种情况下都需要将其设置为False。这是一些运行的示例代码,但是您可能必须调整ls()函数以满足您的要求。

from SALib.sample import saltelli
from SALib.analyze import sobol

def ls(X):
    # column 0 = demand, column 1 =  bookings, column 2 = inventory
    # modified to return a vector (max was returning max value of vector)
    return (X[:,0] - X[:,1] - X[:,2])

problem = {'num_vars': 3,
           'names': ['demand', 'bookings', 'inventory'],
           'bounds': [[0, 1250],
                     [0, 11000],
                     [0, 120000]]
           } 

# Generate samples
param_values = saltelli.sample(problem, 10000000, calc_second_order=False)

# Run model
Y = ls(param_values)

# Perform analysis
Si = sobol.analyze(problem, Y,calc_second_order=False)