Note
Go to the end to download the full example code.
Example of sensitivity analyses on the wing weight model¶
This example is a brief overview of the use of the most usual sensitivity analysis techniques and how to call them:
PCC: Partial Correlation Coefficients
PRCC: Partial Rank Correlation Coefficients
SRC: Standard Regression Coefficients
SRRC: Standard Rank Regression Coefficients
Pearson coefficients
Spearman coefficients
Taylor expansion importance factors
Sobol’ indices
Rank-based estimation of Sobol’ indices
HSIC : Hilbert-Schmidt Independence Criterion
We present the methods on the WingWeight function and use the same notations.
Definition of the model¶
We load the model from the usecases module.
import openturns as ot
import openturns.experimental as otexp
import openturns.viewer as otv
from openturns.usecases.wingweight_function import WingWeightModel
from matplotlib import pylab as plt
ot.Log.Show(ot.Log.NONE)
m = WingWeightModel()
Cross cuts of the function¶
Let’s have a look on 2D cross cuts of the wing weight function. For each 2D cross cut, the other variables are fixed to the input distribution mean values. This graph allows one to have a first idea of the variations of the function in pair of dimensions. The colors of each contour plot are comparable.
lowerBound = m.inputDistribution.getRange().getLowerBound()
upperBound = m.inputDistribution.getRange().getUpperBound()
nX = ot.ResourceMap.GetAsUnsignedInteger("Evaluation-DefaultPointNumber")
description = m.inputDistribution.getDescription()
description.add("")
m.model.setDescription(description)
m.model.setName("wing weight model")
grid = m.model.drawCrossCuts(
m.inputDistribution.getMean(),
lowerBound,
upperBound,
[nX] * m.model.getInputDimension(),
False,
True,
176.0,
363.0,
)
grid.setTitle("")
# Get View object to manipulate the underlying figure
# Here we decide the colormap and the number of levels used for all contours
view = otv.View(grid, contour_kw={"cmap": "hsv", "levels": 55})
axes = view.getAxes()
fig = view.getFigure()
fig.set_size_inches(12, 12) # reduce the size
# Setup a large colorbar
fig.colorbar(
view.getSubviews()[1][0].getContourSets()[0], ax=axes[:-2, -1], fraction=0.3
)
# Hide unwanted axes labels
for i in range(len(axes)):
for j in range(i + 1):
if i < len(axes) - 1:
axes[i][j].xaxis.set_ticklabels([])
if j > 0:
axes[i][j].yaxis.set_ticklabels([])
fig.subplots_adjust(top=0.99, bottom=0.05, left=0.06, right=0.99)
We can see that the variables seem to be influent on the wing weight whereas have less influence on the function.
Data generation¶
We create the input and output data for the estimation of the different sensitivity coefficients and we get the input variables description:
inputNames = m.inputDistribution.getDescription()
size = 500
inputDesign = m.inputDistribution.getSample(size)
outputDesign = m.model(inputDesign)
Let’s estimate the PCC, PRCC, SRC, SRRC, Pearson and Spearman coefficients, display and analyze them.
We create a CorrelationAnalysis
model.
corr_analysis = ot.CorrelationAnalysis(inputDesign, outputDesign)
PCC coefficients¶
We compute here PCC coefficients using the CorrelationAnalysis
.
pcc_indices = corr_analysis.computePCC()
print(pcc_indices)
[0.942801,0.0146971,0.963954,0.0471221,-0.0171501,0.319334,-0.944516,0.979952,0.907646,0.435131]#10
graph = ot.SobolIndicesAlgorithm.DrawCorrelationCoefficients(
pcc_indices, inputNames, "PCC coefficients - Wing weight"
)
view = otv.View(graph)
PRCC coefficients¶
We compute here PRCC coefficients using the CorrelationAnalysis
.
prcc_indices = corr_analysis.computePRCC()
print(prcc_indices)
[0.887607,0.0149862,0.927025,0.0148019,0.0955716,0.240199,-0.889329,0.959604,0.824026,0.274503]#10
graph = ot.SobolIndicesAlgorithm.DrawCorrelationCoefficients(
prcc_indices, inputNames, "PRCC coefficients - Wing weight"
)
view = otv.View(graph)
SRC coefficients¶
We compute here SRC coefficients using the CorrelationAnalysis
.
src_indices = corr_analysis.computeSRC()
print(src_indices)
[0.369054,0.00191698,0.474967,0.00613359,-0.00223852,0.0443933,-0.375146,0.643262,0.28408,0.0631151]#10
graph = ot.SobolIndicesAlgorithm.DrawCorrelationCoefficients(
src_indices, inputNames, "SRC coefficients - Wing weight"
)
view = otv.View(graph)
Normalized squared SRC coefficients (coefficients are made to sum to 1) :
squared_src_indices = corr_analysis.computeSquaredSRC(True)
print(squared_src_indices)
[0.135791,3.66377e-06,0.224915,3.75077e-05,4.99592e-06,0.00196484,0.140311,0.412542,0.0804589,0.00397154]#10
And their associated graph:
graph = ot.SobolIndicesAlgorithm.DrawCorrelationCoefficients(
squared_src_indices, inputNames, "Squared SRC coefficients - Wing weight"
)
view = otv.View(graph)
SRRC coefficients¶
We compute here SRRC coefficients using the CorrelationAnalysis
.
srrc_indices = corr_analysis.computeSRRC()
print(srrc_indices)
[0.362864,0.00281988,0.467839,0.00277594,0.0180698,0.047034,-0.366132,0.642975,0.275605,0.0537757]#10
graph = ot.SobolIndicesAlgorithm.DrawCorrelationCoefficients(
srrc_indices, inputNames, "SRRC coefficients - Wing weight"
)
view = otv.View(graph)
Pearson coefficients¶
We compute here the Pearson coefficients using the CorrelationAnalysis
.
pearson_correlation = corr_analysis.computeLinearCorrelation()
print(pearson_correlation)
[0.343082,0.0393668,0.503811,-0.0214446,0.0565804,0.0676019,-0.33644,0.610386,0.298603,0.172064]#10
title_pearson_graph = "Pearson correlation coefficients - Wing weight"
graph = ot.SobolIndicesAlgorithm.DrawCorrelationCoefficients(
pearson_correlation, inputNames, title_pearson_graph
)
view = otv.View(graph)
Spearman coefficients¶
We compute here the Spearman coefficients using the CorrelationAnalysis
.
spearman_correlation = corr_analysis.computeSpearmanCorrelation()
print(spearman_correlation)
[0.335585,0.0370791,0.50151,-0.0208144,0.0746623,0.0683962,-0.327675,0.613847,0.293277,0.161524]#10
title_spearman_graph = "Spearman correlation coefficients - Wing weight"
graph = ot.SobolIndicesAlgorithm.DrawCorrelationCoefficients(
spearman_correlation, inputNames, title_spearman_graph
)
view = otv.View(graph)
plt.show()
The different computed correlation estimators show that the variables seem to be the most correlated with the wing weight in absolute value. Pearson and Spearman coefficients do not reveal any linear nor monotonic correlation as no coefficients are equal to +/- 1. Coefficients about are negative revealing a negative correlation with the wing weight, that is consistent with the model expression.
Taylor expansion importance factors¶
We compute here the Taylor expansion importance factors using TaylorExpansionMoments
.
We create a distribution-based RandomVector.
X = ot.RandomVector(m.inputDistribution)
We create a composite RandomVector Y from X and m.model.
Y = ot.CompositeRandomVector(m.model, X)
We create a Taylor expansion method to approximate moments.
taylor = ot.TaylorExpansionMoments(Y)
We get the importance factors.
print(taylor.getImportanceFactors())
[Sw : 0.130315, Wfw : 2.94004e-06, A : 0.228153, Lambda : 0, q : 8.25053e-05, l : 0.00180269, tc : 0.135002, Nz : 0.412794, Wdg : 0.0883317, Wp : 0.00351621]
We draw the importance factors
graph = taylor.drawImportanceFactors()
graph.setTitle("Taylor expansion imporfance factors - Wing weight")
view = otv.View(graph)
The Taylor expansion importance factors is consistent with the previous estimators as seem to be the most influent variables. To analyze the relevance of the previous indices, a Sobol’ analysis is now carried out.
Sobol’ indices¶
We compute the Sobol’ indices from both sampling approach and Polynomial Chaos Expansion.
sizeSobol = 1000
sie = ot.SobolIndicesExperiment(m.inputDistribution, sizeSobol)
inputDesignSobol = sie.generate()
inputNames = m.inputDistribution.getDescription()
inputDesignSobol.setDescription(inputNames)
inputDesignSobol.getSize()
12000
We see that 12000 function evaluations are required to estimate the first order and total Sobol’ indices.
Then, we evaluate the outputs corresponding to this design of experiments.
outputDesignSobol = m.model(inputDesignSobol)
We estimate the Sobol’ indices with the SaltelliSensitivityAlgorithm
.
sensitivityAnalysis = ot.SaltelliSensitivityAlgorithm(
inputDesignSobol, outputDesignSobol, sizeSobol
)
The getFirstOrderIndices and getTotalOrderIndices methods respectively return estimates of all first order and total Sobol’ indices.
print("First order indices:", sensitivityAnalysis.getFirstOrderIndices())
First order indices: [0.104592,-0.0127142,0.195142,-0.0129334,-0.0121335,-0.012292,0.111261,0.359861,0.0613847,-0.00973436]#10
print("Total order indices:", sensitivityAnalysis.getTotalOrderIndices())
Total order indices: [0.16211,6.69558e-07,0.234316,0.0012256,-0.000845109,0.000685252,0.150425,0.384081,0.0848994,0.000413006]#10
The draw method produces the following graph. The vertical bars represent the 95% confidence intervals of the estimates.
graph = sensitivityAnalysis.draw()
graph.setTitle("Sobol indices with Saltelli - wing weight")
view = otv.View(graph)
We see that several Sobol’ indices are negative, that is inconsistent with the theory. Therefore, a larger number of samples is required to get consistent indices
sizeSobol = 10000
sie = ot.SobolIndicesExperiment(m.inputDistribution, sizeSobol)
inputDesignSobol = sie.generate()
inputNames = m.inputDistribution.getDescription()
inputDesignSobol.setDescription(inputNames)
inputDesignSobol.getSize()
outputDesignSobol = m.model(inputDesignSobol)
sensitivityAnalysis = ot.SaltelliSensitivityAlgorithm(
inputDesignSobol, outputDesignSobol, sizeSobol
)
sensitivityAnalysis.getFirstOrderIndices()
sensitivityAnalysis.getTotalOrderIndices()
graph = sensitivityAnalysis.draw()
graph.setTitle("Sobol indices with Saltelli - wing weight")
view = otv.View(graph)
It improves the accuracy of the estimation but, for very low indices, Saltelli scheme is not accurate since several confidence intervals provide negative lower bounds.
Now, we estimate the Sobol’ indices using Polynomial Chaos Expansion. We create a Functional Chaos Expansion.
sizePCE = 800
inputDesignPCE = m.inputDistribution.getSample(sizePCE)
outputDesignPCE = m.model(inputDesignPCE)
algo = ot.FunctionalChaosAlgorithm(inputDesignPCE, outputDesignPCE, m.inputDistribution)
algo.run()
result = algo.getResult()
print(result.getResiduals())
print(result.getRelativeErrors())
[0.000332456]
[3.71763e-08]
The relative errors are low : this indicates that the PCE model has good accuracy. Then, we exploit the surrogate model to compute the Sobol’ indices.
sensitivityAnalysis = ot.FunctionalChaosSobolIndices(result)
sensitivityAnalysis
firstOrder = [sensitivityAnalysis.getSobolIndex(i) for i in range(m.dim)]
totalOrder = [sensitivityAnalysis.getSobolTotalIndex(i) for i in range(m.dim)]
graph = ot.SobolIndicesAlgorithm.DrawSobolIndices(inputNames, firstOrder, totalOrder)
graph.setTitle("Sobol indices by Polynomial Chaos Expansion - wing weight")
view = otv.View(graph)
Furthermore, first order Sobol’ indices can also been estimated in a data-driven way using a rank-based sensitivity algorithm. In such a way, the estimation of sensitivity indices does not involve any surrogate model.
sizeRankSobol = 800
inputDesignRankSobol = m.inputDistribution.getSample(sizeRankSobol)
outputDesignankSobol = m.model(inputDesignRankSobol)
myRankSobol = otexp.RankSobolSensitivityAlgorithm(
inputDesignRankSobol, outputDesignankSobol
)
indicesrankSobol = myRankSobol.getFirstOrderIndices()
print("First order indices:", indicesrankSobol)
graph = myRankSobol.draw()
graph.setTitle("Sobol indices by rank-based estimation - wing weight")
view = otv.View(graph)
First order indices: [0.219707,0.056403,0.180796,0.0263883,0.0380841,-0.00340235,0.107226,0.44097,0.0450809,0.0523331]#10
The Sobol’ indices confirm the previous analyses, in terms of ranking of the most influent variables. We also see that five variables have a quasi null total Sobol’ indices, that indicates almost no influence on the wing weight. There is no discrepancy between first order and total Sobol’ indices, that indicates no or very low interaction between the variables in the variance of the output. As the most important variables act only through decoupled first degree contributions, the hypothesis of a linear dependence between the input variables and the weight is legitimate. This explains why both squared SRC and Taylor give the exact same results even if the first one is based on a linear approximation and the second one is based on a linear expansion around the mean value of the input variables.
HSIC indices¶
We then estimate the HSIC indices using a data-driven approach.
sizeHSIC = 250
inputDesignHSIC = m.inputDistribution.getSample(sizeHSIC)
outputDesignHSIC = m.model(inputDesignHSIC)
covarianceModelCollection = []
for i in range(m.dim):
Xi = inputDesignHSIC.getMarginal(i)
inputCovariance = ot.SquaredExponential(1)
inputCovariance.setScale(Xi.computeStandardDeviation())
covarianceModelCollection.append(inputCovariance)
We define a covariance kernel associated to the output variable.
outputCovariance = ot.SquaredExponential(1)
outputCovariance.setScale(outputDesignHSIC.computeStandardDeviation())
covarianceModelCollection.append(outputCovariance)
In this paragraph, we perform the analysis on the raw data: that is the global HSIC estimator.
estimatorType = ot.HSICUStat()
We now build the HSIC estimator:
globHSIC = ot.HSICEstimatorGlobalSensitivity(
covarianceModelCollection, inputDesignHSIC, outputDesignHSIC, estimatorType
)
We get the R2-HSIC indices:
R2HSICIndices = globHSIC.getR2HSICIndices()
print("\n Global HSIC analysis")
print("R2-HSIC Indices: ", R2HSICIndices)
Global HSIC analysis
R2-HSIC Indices: [0.082831,-0.00416762,0.192346,-0.00515997,-0.00166083,8.15814e-05,0.0818061,0.353705,0.0493093,-0.00190752]#10
and the HSIC indices:
HSICIndices = globHSIC.getHSICIndices()
print("HSIC Indices: ", HSICIndices)
HSIC Indices: [0.00721601,-0.000366066,0.0171733,-0.000465613,-0.000149481,7.08356e-06,0.00737526,0.0316188,0.00441525,-0.00017465]#10
The p-value by permutation.
pvperm = globHSIC.getPValuesPermutation()
print("p-value (permutation): ", pvperm)
p-value (permutation): [0,0.772277,0,0.881188,0.485149,0.267327,0,0,0,0.584158]#10
We have an asymptotic estimate of the value for this estimator.
pvas = globHSIC.getPValuesAsymptotic()
print("p-value (asymptotic): ", pvas)
p-value (asymptotic): [1.54432e-09,0.761116,6.48955e-21,0.848855,0.54071,0.409683,4.4423e-09,6.40633e-32,5.97639e-06,0.562308]#10
We vizualise the results.
graph1 = globHSIC.drawHSICIndices()
view1 = otv.View(graph1)
graph2 = globHSIC.drawPValuesAsymptotic()
view2 = otv.View(graph2)
graph3 = globHSIC.drawR2HSICIndices()
view3 = otv.View(graph3)
graph4 = globHSIC.drawPValuesPermutation()
view4 = otv.View(graph4)
The HSIC indices go in the same way as the other estimators in terms the most influent variables. The variables seem to be independent to the output as the corresponding p-values are high. We can also see that the asymptotic p-values and p-values estimated by permutation are quite similar.
Reset default settings
ot.ResourceMap.Reload()
Total running time of the script: (0 minutes 18.001 seconds)