Keyboard Scientist

  • Blog
  • About
  • Blog
  • About

Sensitivity Analysis with SALib

10/20/2015

4 Comments

 
Sensitivity analysis is the "study of how the uncertainty in the output of a mathematical model or system can be apportioned to different sources of uncertainty in its inputs" [Wikipedia].  Sensitivity analysis is a powerful technique for gaining insight into a model by understanding in general terms how the model's output is influenced by the model's inputs.  In this post, we provide a short introduction to sensitivity analysis using visual scatter plots followed by an introduction of SALib, an open source Python library for sensitivity analysis.

SA using Scatter Plots

Sensitivity analysis can be demonstrated visually using scatter plots.  Suppose we have a model, y = f(x) + ε, with a single input, x, and an error term, ε.  We can generate a scatter plot by randomly sampling values of x and recording the output y:
Picture
In this example, we observe that increasing values of x result in increasing values of y.  This indicates there is a positive correlation between x and y, which is implies that changing the value of x has a direct influence on the value of y.  If we fit a line to these points, the sensitivity is the slope of the line.  In this example, the sensitivity is approximately 1.  Any increase or decrease in the value of x results in a proportional change to y.

Take for example the following:
Picture
In this case, the slope of the line is 0, indicating the sensitivity of y given x is 0.  The value of y is independent of the value of x.

Suppose now we have a function with two inputs, y = f(x1, x2) + ε.  Like before, we sample x1 and x2 independently and generate the scatter plot.  The black points show the output caused by varying x1 and the red points show the output caused by varying x2.
Picture
The steeper slope of the black line with respect to the red line indicates that y is more sensitive to x1 than x2.  Furthermore, the relative slope of the lines indicates the relative magnitude of the sensitivities.  In this example, y is approximately 5 times more sensitive to changes in x1 than x2 (since the slope of the black line is ~1.0 and the slope of the red line is ~0.2).

Using SALib

This visual approach is useful for simple models, but can be challenging to use and error prone for more complex models.  To address these issues, a variety of sensitivity analysis methods have been developed over the years.  These methods typically calculate what are called sensitivity indices as numeric values.  Depending on the specific method, they can compute:
  1. First-order indices: measures the contribution to the output variance by a single model input alone.
  2. Second-order indices: measures the contribution to the output variance caused by the interaction of two model inputs.
  3. Total-order index: measures the contribution to the output variance caused by a model input, including both its first-order effects (the input varying alone) and all higher-order interactions.
SALib is an open source library written in Python that contains a variety of sensitivity analysis methods.  To download and install SALib, we you will first need to install Git and Python.  Next, run the following commands.
    git clone https://github.com/SALib/SALib.git
    cd SALib
    python setup.py develop

To demonstrate the use of SALib, we will perform a Sobol’ sensitivity analysis of the Ishigami function, shown below. The Ishigami function is commonly used to test uncertainty and sensitivity analysis methods because it exhibits strong nonlinearity and nonmonotonicity.
Picture
To begin, start a new interactive Python session.  Next, import the necessary libraries. In SALib, the sample and analyze functions are stored in separate Python modules. For example, below we import the saltelli sample function and the sobol analyze function. We also import the Ishigami function, which is provided as a test function within SALib. Lastly, we import numpy, as it is used by SALib to store the model inputs and outputs in a matrix.
Importing Python Libraries


​Next, we must define the model inputs. The Ishigami function has three inputs, x1, x2, and x3, where x ∈ [-π, π]. In SALib, we use a dict defining the number of inputs, the names of the inputs, and the bounds on each input, as shown below.
Defining Problem


Next, we generate the samples. Since we are performing a Sobol’ sensitivity analysis, we need to generate samples using the Saltelli sampler, as shown below.
Generating Input Samples


Here, param_values is a NumPy matrix. If we run param_values.shape, we see that the matrix is 8000 by 3. The Saltelli sampler generated 8000 samples. The Saltelli sampler generates N*(2D+2) samples, where in this example N is 1000 (the argument we supplied) and D is 3 (the number of model inputs).

Next, we must evaluate the model using our generated samples.  In this example, we are using the Ishigami function provided by SALib. We can evaluate these test functions as shown below:
Evaluating the Model


With the model outputs computed, we can finally compute the sensitivity indices. In this example, we use sobol.analyze, which will compute first, second, and total-order indices.
Analyze the Outputs


Si is a Python dict with the keys "S1", "S2", "ST", "S1_conf", "S2_conf", and "ST_conf". The _conf keys store the corresponding confidence intervals, typically with a confidence level of 95%. We can print the values as shown below.
>>> # Print the first-order indices
>>> print Si['S1']
[ 0.30644324  0.44776661 -0.00104936 ]

>>> # Print the total-order indices
>>> print Si['ST']
[ 0.56013728  0.4387225   0.24284474]

>>> # Print the interactive effects
>>> print "x1-x2:", Si['S2'][0,1]
>>> print "x1-x3:", Si['S2'][0,2]
>>> print "x2-x3:", Si['S2'][1,2]
x1-x2: 0.0155279
x1-x3: 0.25484902
x2-x3: -0.00995392

From this output, we make several observations.  First, x1 and x2 show strong first-order sensitivities, but x3 has no first-order sensitivity.  x3 has interactive effects as seen by the second and total-order indices.

Conclusion

SALib is a powerful toolbox for sensitivity analysis.  In this post, we walked through an example using Sobol' sensitivity analysis.  SALib includes other methods, including the Fourier Amplitude Sensitivity Test (FAST), Method of Morris, and Delta-Moment Independent Measure.

We also want to note that while SALib is a Python library, it is designed to work with models in any form.  SALib includes a convenient command line interface for generating the model inputs and analyzing the model outputs.  Additional documentation for SALib is available online at ReadTheDocs.
4 Comments
omar
1/20/2016 03:21:57 am

Dear Sir,
I hope my email finds you well. indeed, I am new to SALib library and I want to perform the sobol`s sensitivity analysis. My problem is that I have huge number of parameters and I would like to divide the parameters into groups. let`s say that I have 30 parameters and I want to make 5 groups (each has 6 parameters) and perform the analysis. If it is possible, guide me on how to write the groups name in the script below.

Thanks in advance,

best regards,



problem = {
'num_vars': 30,
'names': ['TBASEM', 'TEFFMX', 'TSUM1', 'TSUM2', 'RARLAI', 'LAIEM',
'SPAN', 'SLATB', 'SLATB1', 'AMAXTB', 'AMAXTB1', 'AMAXTB2',
'AMAXTB3', 'AMAXTB4', 'EFFTB', 'KDIFTB', 'CVO', 'CVS', 'CVL',
'CVR', 'RMS', 'RML', 'Q10', 'RMO', 'RMR', 'PERDL', 'CFET', 'RRI',
'RDI', 'RDMCR'],
'bounds': [[2, 5],
[20, 30],
[700, 900],
[800, 1200],
[0.01, 0.04],
[0.1, 0.2],
[30, 36],
[0.002, 0.003],
[0.001, 0.002],
[50, 70],
[50, 70],
[50, 70],
[30, 50],
[0, 25],
[0.4, 0.5],
[0.5, 0.7],
[0.6, 0.8],
[0.59, 0.76],
[0.61, 0.75],
[0.62, 0.76],
[0.013, 0.02],
[0.027, 0.033],
[1.6, 2],
[0.005, 0.015],
[0.01, 0.016],
[0.02, 0.06],
[0.7, 1.2],
[2, 3],
[7, 14],
[90.5, 120]]
}

Reply
omar
1/20/2016 03:22:38 am

Dear Sir,
I hope my email finds you well. indeed, I am new to SALib library and I want to perform the sobol`s sensitivity analysis. My problem is that I have huge number of parameters and I would like to divide the parameters into groups. let`s say that I have 30 parameters and I want to make 5 groups (each has 6 parameters) and perform the analysis. If it is possible, guide me on how to write the groups name in the script below.

Thanks in advance,

best regards,

Omar

problem = {
'num_vars': 30,
'names': ['TBASEM', 'TEFFMX', 'TSUM1', 'TSUM2', 'RARLAI', 'LAIEM',
'SPAN', 'SLATB', 'SLATB1', 'AMAXTB', 'AMAXTB1', 'AMAXTB2',
'AMAXTB3', 'AMAXTB4', 'EFFTB', 'KDIFTB', 'CVO', 'CVS', 'CVL',
'CVR', 'RMS', 'RML', 'Q10', 'RMO', 'RMR', 'PERDL', 'CFET', 'RRI',
'RDI', 'RDMCR'],
'bounds': [[2, 5],
[20, 30],
[700, 900],
[800, 1200],
[0.01, 0.04],
[0.1, 0.2],
[30, 36],
[0.002, 0.003],
[0.001, 0.002],
[50, 70],
[50, 70],
[50, 70],
[30, 50],
[0, 25],
[0.4, 0.5],
[0.5, 0.7],
[0.6, 0.8],
[0.59, 0.76],
[0.61, 0.75],
[0.62, 0.76],
[0.013, 0.02],
[0.027, 0.033],
[1.6, 2],
[0.005, 0.015],
[0.01, 0.016],
[0.02, 0.06],
[0.7, 1.2],
[2, 3],
[7, 14],
[90.5, 120]]
}

Reply
David
1/20/2016 05:28:29 am

From what I understand, only the Morris method supports groups in SALib. You can either set it up using a parameter file, where the last column specifies the group:

P1 0.0 1.0 Group_1
P2 0.0 5.0 Group_2
P3 0.0 5.0 Group_2

or as a dict:

{'num_vars': 3,
'names': ['P1', 'P2', 'P3'],
'groups': (matrix([[1, 0],
[0, 1],
[0, 1]]),
['Group_1', 'Group_2']),
'bounds': [[0.0, 1.0], [0.0, 5.0], [0.0, 5.0]]}

You can see the "groups" entry is a tuple. The first value in the tuple is a matrix containing 0's and 1's. If a 1, then the parameter is part of the specified group. The second value in the tuple is a list of the unique group names.

Reply
Bahar
8/8/2020 08:46:43 pm

Hello dear
I have a problem and i don’t understand instead of benchmark Inshigma i can use my model for finding y?

Reply



Leave a Reply.

    Who we are

    Keyboard Scientist is a blog about conducting science in a digital world, discussing topics ranging from programming, model design and analysis, and publishing. In addition, we post a range of topics concerning our open source optimization framework.

    Subscribe

    Enter your e-mail address below to receive notifications of new posts.

    Archives

    January 2016
    December 2015
    October 2015
    September 2015

    Categories

    All
    Deployment
    Git
    Java
    Licensing
    MOEA Framework
    Statistics

    RSS Feed

Powered by Create your own unique website with customizable templates.