Performing a sensitivity analysis with python

2020-07-26 11:31发布

问题:

I'm trying to perform a sensitivity analysis and I started to learn python so I wanted to accomplish this in python. I found a package called SALib but I don't really get how to implement my own equation. For example this is my equation:

ET = 0,0031*C*(R+209)*(t*(t+15)**-1)

At first I have to define my problem:

problem = {'num_vars': 3,
           'names': ['C', 'R', 't'],
           'bounds': [[10, 100],
                     [3, 7],
                     [-10, 30]]
           }

After this I have to generae Input Samples but I how do I generate these with my own equation? Maybe someone has expierience with SALib and can help me. I don't find the package documentation really helpful.

回答1:

The function saltelli.sample() will generate a matrix with each column representing a variable defined in problem and sampled in the corresponding bounds defined in problem. After that, you can define your model as a function, as shown below, and compute the value of the function ET() for these inputs. The result is a vector of function values, which can be sent the the other SALib functions as given in the documentation (https://github.com/SALib/SALib).

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

def ET(X):
    # column 0 = C, column 1 = R, column 2 = t
    return(0.0031*X[:,0]*(X[:,1]+209)*(X[:,2]*(X[:,2]+15))**-1)

problem = {'num_vars': 3,
           'names': ['C', 'R', 't'],
           'bounds': [[10, 100],
                     [3, 7],
                     [-10, 30]]
           }

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

# Run model (example)
Y = ET(param_values)

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