.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_reliability_sensitivity/sensitivity_analysis/plot_sensitivity_sobol.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_reliability_sensitivity_sensitivity_analysis_plot_sensitivity_sobol.py: Estimate Sobol' indices for the Ishigami function by a sampling method: a quick start guide to sensitivity analysis =================================================================================================================== .. GENERATED FROM PYTHON SOURCE LINES 6-8 In this example, we estimate the Sobol' indices for the :ref:`Ishigami function ` by sampling methods. .. GENERATED FROM PYTHON SOURCE LINES 11-35 Introduction ------------ In this example we are going to quantify the correlation between the input variables and the output variable of a model thanks to Sobol indices. Sobol indices are designed to evaluate the importance of a single variable or a specific set of variables. Here the Sobol indices are estimated by sampling from the distributions of the input variables and propagating uncertainty through a function. In theory, Sobol indices range from 0 to 1; the closer an index value is to 1, the better the associated input variable explains the function output. Let us denote by :math:`d` the input dimension of the model. Sobol' indices can be computed at different orders. * First order indices evaluate the importance of one input variable at a time. * Total indices give the relative importance of one input variable and all its interactions with other variables. Alternatively, they can be viewed as measuring how much wriggle room remains to the output when all but one input variables are fixed. * In general, we are only interested in first order and total Sobol' indices. There are situations, however, where we want to estimate interactions. Second order indices evaluate the importance of every pair of input variables. The number of second order indices is: .. math:: \binom{d}{2} = \frac{d \times \left( d-1\right)}{2}. In practice, when the number of input variables is not small (say, when :math:`d>5`), then the number of second order indices is too large to be easily analyzed. In these situations, we limit the analysis to the first order and total Sobol' indices. .. GENERATED FROM PYTHON SOURCE LINES 37-39 Define the model ---------------- .. GENERATED FROM PYTHON SOURCE LINES 41-50 .. code-block:: default from openturns.usecases import ishigami_function import openturns as ot import numpy as np import pylab as pl import openturns.viewer import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 51-52 We load the Ishigami model from the usecases model : .. GENERATED FROM PYTHON SOURCE LINES 52-54 .. code-block:: default im = ishigami_function.IshigamiModel() .. GENERATED FROM PYTHON SOURCE LINES 55-57 The `IshigamiModel` data class contains the input distribution :math:`X=(X_1, X_2, X_3)` in `im.distributionX` and the Ishigami function in `im.model`. We also have access to the input variable names with .. GENERATED FROM PYTHON SOURCE LINES 57-59 .. code-block:: default input_names = im.distributionX.getDescription() .. GENERATED FROM PYTHON SOURCE LINES 60-62 Draw the function ----------------- .. GENERATED FROM PYTHON SOURCE LINES 64-69 .. code-block:: default n = 10000 sampleX = im.distributionX.getSample(n) sampleY = im.model(sampleX) .. GENERATED FROM PYTHON SOURCE LINES 70-85 .. code-block:: default def plotXvsY(sampleX, sampleY, figsize=(15, 3)): dimX = sampleX.getDimension() inputdescr = sampleX.getDescription() fig = pl.figure(figsize=figsize) for i in range(dimX): ax = fig.add_subplot(1, dimX, i+1) graph = ot.Graph('', inputdescr[i], 'Y', True, '') cloud = ot.Cloud(sampleX[:, i], sampleY) graph.add(cloud) _ = ot.viewer.View(graph, figure=fig, axes=[ax]) return fig plotXvsY(sampleX, sampleY, figsize=(10, 4)) .. image-sg:: /auto_reliability_sensitivity/sensitivity_analysis/images/sphx_glr_plot_sensitivity_sobol_001.png :alt: plot sensitivity sobol :srcset: /auto_reliability_sensitivity/sensitivity_analysis/images/sphx_glr_plot_sensitivity_sobol_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 86-89 .. code-block:: default graph = ot.HistogramFactory().build(sampleY).drawPDF() view = viewer.View(graph) .. image-sg:: /auto_reliability_sensitivity/sensitivity_analysis/images/sphx_glr_plot_sensitivity_sobol_002.png :alt: y0 PDF :srcset: /auto_reliability_sensitivity/sensitivity_analysis/images/sphx_glr_plot_sensitivity_sobol_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 90-91 We see that the distribution of the output has two modes. .. GENERATED FROM PYTHON SOURCE LINES 93-95 Estimate the Sobol' indices --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 97-98 We first create a design of experiments with the `SobolIndicesExperiment`. Since we are not interested in second order indices for the moment, we use the default value of the third argument (we will come back to this topic later). .. GENERATED FROM PYTHON SOURCE LINES 100-107 .. code-block:: default size = 1000 sie = ot.SobolIndicesExperiment(im.distributionX, size) inputDesign = sie.generate() input_names = im.distributionX.getDescription() inputDesign.setDescription(input_names) inputDesign.getSize() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 5000 .. GENERATED FROM PYTHON SOURCE LINES 108-109 We see that 5000 function evaluations are required to estimate the first order and total Sobol' indices. .. GENERATED FROM PYTHON SOURCE LINES 111-112 Then we evaluate the outputs corresponding to this design of experiments. .. GENERATED FROM PYTHON SOURCE LINES 114-116 .. code-block:: default outputDesign = im.model(inputDesign) .. GENERATED FROM PYTHON SOURCE LINES 117-118 Then we estimate the Sobol' indices with the `SaltelliSensitivityAlgorithm`. .. GENERATED FROM PYTHON SOURCE LINES 120-123 .. code-block:: default sensitivityAnalysis = ot.SaltelliSensitivityAlgorithm( inputDesign, outputDesign, size) .. GENERATED FROM PYTHON SOURCE LINES 124-125 The `getFirstOrderIndices` and `getTotalOrderIndices` method respectively return estimates of all first order and total Sobol' indices. .. GENERATED FROM PYTHON SOURCE LINES 127-129 .. code-block:: default sensitivityAnalysis.getFirstOrderIndices() .. raw:: html

[0.369585,0.491331,-0.0446461]



.. GENERATED FROM PYTHON SOURCE LINES 130-132 .. code-block:: default sensitivityAnalysis.getTotalOrderIndices() .. raw:: html

[0.486853,0.425027,0.235277]



.. GENERATED FROM PYTHON SOURCE LINES 133-134 The `draw` method produces the following graph. The vertical bars represent the 95% confidence intervals of the estimates. .. GENERATED FROM PYTHON SOURCE LINES 136-139 .. code-block:: default graph = sensitivityAnalysis.draw() view = viewer.View(graph) .. image-sg:: /auto_reliability_sensitivity/sensitivity_analysis/images/sphx_glr_plot_sensitivity_sobol_003.png :alt: Sobol' indices - SaltelliSensitivityAlgorithm :srcset: /auto_reliability_sensitivity/sensitivity_analysis/images/sphx_glr_plot_sensitivity_sobol_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 140-144 - We see that the variable :math:`X_1`, with a total Sobol' index close to 0.6, is the most significant variable, taking into account both its direct effect and its interactions with other variables. Its first order index is close to 0.3, which implies that its interactions alone produce almost 30% (0.6 - 0.3) of the total variance. - The variable :math:`X_2` has the highest first order index: approximately 0.4. However, it has little interaction with other variables since its total order indice is close to its first order index. - The variable :math:`X_3` has a first order index close to zero. However, it has an impact to the total variance thanks to its interactions with :math:`X_1`. - We see that the variability of the estimates is quite high even with a relatively large sample size. Moreover, since the exact first order Sobol' index for :math:`X_3` is zero, its estimate has a 50% chance of being nonpositive. .. GENERATED FROM PYTHON SOURCE LINES 146-148 Estimate the second order indices --------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 150-158 .. code-block:: default size = 1000 computeSecondOrder = True sie = ot.SobolIndicesExperiment(im.distributionX, size, computeSecondOrder) inputDesign = sie.generate() print(inputDesign.getSize()) inputDesign.setDescription(input_names) outputDesign = im.model(inputDesign) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 8000 .. GENERATED FROM PYTHON SOURCE LINES 159-160 We see that 8000 function evaluations are now required; that is 3000 more evaluations than in the previous situation. .. GENERATED FROM PYTHON SOURCE LINES 162-165 .. code-block:: default sensitivityAnalysis = ot.SaltelliSensitivityAlgorithm( inputDesign, outputDesign, size) .. GENERATED FROM PYTHON SOURCE LINES 166-171 .. code-block:: default second_order = sensitivityAnalysis.getSecondOrderIndices() for i in range(im.dim): for j in range(i): print('2nd order indice (%d,%d)=%g' % (i, j, second_order[i, j])) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 2nd order indice (1,0)=-0.0010702 2nd order indice (2,0)=0.220473 2nd order indice (2,1)=-0.0584923 .. GENERATED FROM PYTHON SOURCE LINES 172-173 This shows that the only significant interaction is the one between :math:`X_1` and :math:`X_3` (beware of Python's index shift: 0 denotes the first input variable). .. GENERATED FROM PYTHON SOURCE LINES 175-185 Using a different estimator --------------------------- We have used the `SaltelliSensitivityAlgorithm` class to estimate the indices. Others are available in the library: * `SaltelliSensitivityAlgorithm` * `MartinezSensitivityAlgorithm` * `JansenSensitivityAlgorithm` * `MauntzKucherenkoSensitivityAlgorithm` .. GENERATED FROM PYTHON SOURCE LINES 187-188 In order to compare the results with another method, we use the `MartinezSensitivityAlgorithm` class. .. GENERATED FROM PYTHON SOURCE LINES 190-193 .. code-block:: default sensitivityAnalysis = ot.MartinezSensitivityAlgorithm( inputDesign, outputDesign, size) .. GENERATED FROM PYTHON SOURCE LINES 194-198 .. code-block:: default graph = sensitivityAnalysis.draw() view = viewer.View(graph) plt.show() .. image-sg:: /auto_reliability_sensitivity/sensitivity_analysis/images/sphx_glr_plot_sensitivity_sobol_004.png :alt: Sobol' indices - MartinezSensitivityAlgorithm :srcset: /auto_reliability_sensitivity/sensitivity_analysis/images/sphx_glr_plot_sensitivity_sobol_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 199-200 We see that the results do not change significantly in this particular situation. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.650 seconds) .. _sphx_glr_download_auto_reliability_sensitivity_sensitivity_analysis_plot_sensitivity_sobol.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sensitivity_sobol.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_sensitivity_sobol.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_