This post was updated on August 11, 2014 to update the SALib module structure.
The Sensitivity Analysis Library (SALib) is an open-source Python library for common sensitivity analysis routines, including the Sobol, Morris, and FAST methods.
Step 0: Get the library
The easiest way to install is
pip install SALib, which will pull the latest version from the Python package index. Or, you can download at the above link and run
python setup.py install. If you’re interested in contributing, you can clone the git repository:
git clone https://github.com/jdherman/SALib .
Step 1: Choose sampling bounds for your parameters
First, you will need to create a simple text file with the form [parameter] [lower bound] [upper bound]. For example, such a file for the Hymod model might look like this:
Cmax 0.0 1000.0 B 0.0 3.0 Alpha 0.0 1.0 Kq 0.15 1.0 Ks 0.0 0.15
The bounds are used to sample parameter values. The names of the parameters themselves are only used to print the final sensitivity indices, so you can name them whatever you want. Let’s call this file params.txt.
Step 2: Generate parameter sets using the Sobol Sequence
Put your params.txt file in the same directory as the SALib folder. Move to this directory and type the following command:
python -m SALib.sample.saltelli -n 1000 -p params.txt -o my_samples_file.txt
The -n flag specifies the number of initial samples to generate from the pseudo-random Sobol sequence. The -p flag specifies the parameter bounds file that you created in the first step.
In this example, 1000 parameter sets are generated from the Sobol sequence. After that, the Saltelli method of cross-sampling is applied (for more information, see: Saltelli 2008, “Global Sensitivity Analysis: The Primer“). The cross-sampling scheme creates a total of 2N(p+1) total parameter sets to be run in your model; for the Hymod example, we would have 1000*(5+1) = 6000 total model runs. The parameter samples will be printed to the file specified with the -o flag, which in this case is called my_samples_file.txt.
Note that the Sobol method can be computationally intensive depending on the model being analyzed. Even for a simple model like Hymod, from personal experience I would recommend a sample size of at least N = 10,000 (which translates to 60,000 model runs). More complex models will be slower to run and will also require more samples to calculate accurate estimates of Sobol indices. Once you complete this process, pay attention to the confidence bounds on your sensitivity indices to see whether you need to run more samples.
Step 3: Run the parameter sets through your model
The parameter sets are now saved in my_samples_file.txt. Run these parameter sets through your model, and save the output(s) to a text file. The output file should contain one row of output values for each model run. This process is performed outside of SALib, so the details will be language-specific. Be careful to read in your parameter sets in the same order they were sampled.
Step 4: Calculate Sobol Indices
You now have the output values for all of your model runs saved to a file. For the sake of example, let’s call that file objectiveValues.txt. We need to send this information back to SALib to compute the sensitivity indices, using following command:
python -m SALib.analyze.sobol -p params.txt -Y objectiveValues.txt -c 0
The options here are: the parameter file (-p), the file containing calculated outputs (-Y), and the column of the objective values file to read (-c). The columns are assumed to be zero-indexed; if you have calculated multiple objective values, you would continue on to -m 1, etc., repeating the same command as above. By default, this will print sensitivity indices to the command line. You may want to print them to a file using the “>” operator.
Step 5: Interpret your results
Say that you saved the indices from the above command into the file sobolIndices.txt. If you open this file in a text editor, it will look something like this:
Parameter First_Order First_Order_Conf Total_Order Total_Order_Conf x1 0.696371 0.183873 0.704233 0.211868 x2 0.232399 0.119619 0.264305 0.129080 x3 -0.021573 0.048209 0.027243 0.066093 ...(etc.) Parameter_1 Parameter_2 Second_Order Second_Order_Conf x1 x2 -0.142104 0.307560 x1 x3 -0.009698 0.271062 x1 x4 -0.049298 0.283457 ...(etc.)
The parameter names will match the ones you specified in params.txt (Here they don’t, but this is just an example). The first order, total order, and second order sensitivities are specified as indicated, along with their respective confidence intervals. Most of the indices are omitted here for the sake of brevity. Typically we use the total order indices to get a broad picture of model behavior, since they estimate all of the interactions between parameters. If the confidence intervals of your dominant indices are larger than roughly 10% of the value itself, you may want to consider increasing your sample size as computation permits. For total-order indices to be important, they will usually need to be above 0.05 at the very least (the most dominant parameters will have values upward of 0.8).
For a full description of available methods and options, please consult the readme and examples in the Github repository or on the SALib website. Github users can also post issues or submit pull requests. Thanks for reading!