How to Perform Sensitivity Analysis Using OpenTURNS and Python
When building numerical models, it is crucial to understand how uncertainty in your input parameters affects your final output. Sensitivity analysis identifies which variables drive model variability and which ones are negligible.
OpenTURNS is a powerful, open-source Python library specifically designed to treat uncertainties in numerical models. This guide demonstrates how to perform a variance-based sensitivity analysis using Sobol indices in OpenTURNS. Step 1: Install OpenTURNS
First, install the library using pip or conda via your terminal. pip install openturns Use code with caution. Step 2: Define Your Input Random Variables
OpenTURNS requires you to define the statistical distributions of your inputs. This creates a joint distribution, assuming independence or using copulas.
import openturns as ot # Define marginal distributions for three inputs X0 = ot.Uniform(0, 1) X1 = ot.Uniform(0, 1) X2 = ot.Uniform(0, 1) # Combine into a joint distribution (independent by default) input_distribution = ot.ComposedDistribution([X0, X1, X2]) input_distribution.setDescription([‘X0’, ‘X1’, ‘X2’]) Use code with caution. Step 3: Implement Your Numerical Model
Wrap your mathematical equation or simulation code into an OpenTURNS PythonFunction. We will use the standard Ishigami function, a classic benchmark for sensitivity analysis.
import math # Define the math function def ishigami_function(X): x0, x1, x2 = X y = math.sin(x0) + 7(math.sin(x1))2 + 0.1 * (x24) * math.sin(x0) return [y] # Convert to an OpenTURNS PythonFunction # 3 inputs, 1 output model = ot.PythonFunction(3, 1, ishigami_function) Use code with caution. Step 4: Generate Input Samples
Sobol sensitivity analysis relies on specific sampling matrices. OpenTURNS provides automated experiment designs, such as Saltelli’s sequence, to generate these samples efficiently.
# Set the base sample size (higher equals more precise indices) N = 1000 # Create the Sobol experiment design sobol_experiment = ot.SobolIndicesExperiment(input_distribution, N) input_design = sobol_experiment.generate() # Print the shape of the generated matrix print(“Design shape:”, input_design.getSize()) Use code with caution. Step 5: Run the Model and Compute Sobol Indices
Pass the generated input matrix through your model to get the output vector. Then, use the SaltelliSensitivityAlgorithm to compute the First-Order and Total Sobol indices.
# Evaluate the model on the design matrix output_design = model(input_design) # Compute sensitivity indices sensitivity_analysis = ot.SaltelliSensitivityAlgorithm(input_design, output_design, N) # Retrieve indices first_order_indices = sensitivity_analysis.getFirstOrderIndices() total_order_indices = sensitivity_analysis.getTotalOrderIndices() Use code with caution. Step 6: Visualize the Results
OpenTURNS includes built-in plotting tools to quickly visualize the importance of each variable.
# Generate a bar chart for First-Order and Total indices graph = sensitivity_analysis.draw() # Display or save the graph (requires a matplotlib backend environment) from openturns.viewer import View view = View(graph) view.show() Use code with caution. Understanding Your Output
First-Order Indices (S1): Measure the variance contribution of a single input acting alone.
Total-Order Indices (ST): Measure the total variance contribution of an input, including its interactions with all other variables. Interpretation: If
Leave a Reply