This post was updated on January 16, 2015 to correct a few errors and 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. This post will describe how to run the Method of Morris (also known as the Elementary Effects Method) using SALib. Note that the default use case for SALib is to perform decoupled sensitivity analysis, i.e. the sampling and analysis steps are performed separately, and the model evaluations that occur in between are left to the user.
Step 0: Get the library
The easiest approach is to
pip install SALib, which will pull the latest version from the Python package index. Or, you can download at the above link as a zip/tar file 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
Create a simple text file with the form [parameter] [lower bound] [upper bound]. For example, such a file for the “Sobol G” test function might look like this:
x1 0.0 1.0 x2 0.0 1.0 x3 0.0 1.0 x4 0.0 1.0 x5 0.0 1.0 x6 0.0 1.0 x7 0.0 1.0 x8 0.0 1.0
The bounds are used to sample parameter values. The variable names will only appear in the printed output, and they will not affect the method itself. Let’s call this file params.txt.
Step 2: Generate parameter sets
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.morris -n 1000 -p params.txt -o my_samples_file.txt
The -n flag specifies the number of initial samples to generate. The -p flag specifies the parameter bounds file that you created in the first step. Finally, the -o flag tells the program where to output the matrix of parameter samples. A total of N(p+1) samples will be generated; in this case, N = 1000 and p = 8, leading to a total of 9000 model evaluations.
The sampling command also has two options that aren’t required, --num-levels and --grid-jump. By default, these are set to 4 and 2, respectively.
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 Morris 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 SGOutput.txt (the output from the Sobol G function). We need to send this information back to SALib to compute the sensitivity indices, using following command:
python -m SALib.analyze.morris -p params.txt -X my_samples_file.txt -Y SGOutput.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 morrisIndices.txt. If you open this file in a text editor, it will look something like this:
Parameter Mu Sigma Mu_Star x1 0.040674 2.573977 2.077549 x2 -0.113902 1.514879 1.109394 x3 -0.025667 0.620538 0.454424 x4 -0.001532 0.324167 0.229770 x5 -0.001736 0.032333 0.023077 x6 -0.000858 0.032265 0.022693 x7 -0.000976 0.032779 0.022949 x8 -0.000224 0.034278 0.024575
The parameter names will match the ones you specified in params.txt. The mean and variance of each parameter’s elementary effects are given by mu and sigma, respectively. Mu_star is the mean of the absolute values of the elementary effects, following Campolongo et al. (2007). This Mu_star value is the best approximation of “total” sensitivity provided by the Morris method. Note that these indices do not have a direct interpretation as an “attribution of variance”, like we saw in the example results from the Sobol method. Instead, they should be used to understand the ranking of the most sensitive parameters, and to provide an approximate quantification of sensitivity.
For a full description of available methods and options, please consult the readme in the Github repository or on the SALib website. Github users can also post issues or submit pull requests. Thanks for reading!