.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/uncertainty/sensitivity/plot_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_examples_uncertainty_sensitivity_plot_sobol.py: Sobol' analysis =============== .. GENERATED FROM PYTHON SOURCE LINES 25-35 .. code-block:: default from __future__ import annotations import pprint from gemseo.algos.parameter_space import ParameterSpace from gemseo.api import create_discipline from gemseo.uncertainty.sensitivity.sobol.analysis import SobolAnalysis from matplotlib import pyplot as plt from numpy import pi .. GENERATED FROM PYTHON SOURCE LINES 36-44 In this example, we consider a function from :math:`[-\pi,\pi]^3` to :math:`\mathbb{R}^3`: .. math:: (y_1,y_2)=\left(f(x_1,x_2,x_3),f(x_2,x_1,x_3)\right) where :math:`f(a,b,c)=\sin(a)+7\sin(b)^2+0.1*c^4\sin(a)` is the Ishigami function: .. GENERATED FROM PYTHON SOURCE LINES 44-53 .. code-block:: default expressions = { "y1": "sin(x1)+7*sin(x2)**2+0.1*x3**4*sin(x1)", "y2": "sin(x2)+7*sin(x1)**2+0.1*x3**4*sin(x2)", } discipline = create_discipline( "AnalyticDiscipline", expressions=expressions, name="Ishigami2" ) .. GENERATED FROM PYTHON SOURCE LINES 54-60 Then, we consider the case where the deterministic variables :math:`x_1`, :math:`x_2` and :math:`x_3` are replaced by the uncertain variables :math:`X_1`, :math:`X_2` and :math:`X_3`. The latter are independent and identically distributed according to the uniform distribution between :math:`-\pi` and :math:`\pi`: .. GENERATED FROM PYTHON SOURCE LINES 60-66 .. code-block:: default uncertain_space = ParameterSpace() for variable_name in ["x1", "x2", "x3"]: uncertain_space.add_random_variable( variable_name, "OTUniformDistribution", minimum=-pi, maximum=pi ) .. GENERATED FROM PYTHON SOURCE LINES 67-72 From that, we would like to carry out a sensitivity analysis with the random outputs :math:`Y_1=f(X_1,X_2,X_3)` and :math:`Y_2=f(X_2,X_1,X_3)`. For that, we can compute the sensitivity indices from a :class:`.SobolAnalysis`: .. GENERATED FROM PYTHON SOURCE LINES 72-76 .. code-block:: default sobol_analysis = SobolAnalysis([discipline], uncertain_space, 100) sobol_analysis.main_method = "total" sobol_analysis.compute_indices() .. rst-class:: sphx-glr-script-out .. code-block:: none {'first': {'y2': [{'x1': array([0.75387699]), 'x2': array([0.26516667]), 'x3': array([0.35758815])}], 'y1': [{'x1': array([0.19426511]), 'x2': array([0.21211854]), 'x3': array([0.01782884])}]}, 'total': {'y2': [{'x1': array([0.36991381]), 'x2': array([0.60974968]), 'x3': array([0.34776012])}], 'y1': [{'x1': array([0.87052721]), 'x2': array([0.37726189]), 'x3': array([0.29945874])}]}} .. GENERATED FROM PYTHON SOURCE LINES 77-78 The resulting indices are the first-order and total Sobol' indices: .. GENERATED FROM PYTHON SOURCE LINES 78-80 .. code-block:: default pprint.pprint(sobol_analysis.indices) .. rst-class:: sphx-glr-script-out .. code-block:: none {'first': {'y1': [{'x1': array([0.19426511]), 'x2': array([0.21211854]), 'x3': array([0.01782884])}], 'y2': [{'x1': array([0.75387699]), 'x2': array([0.26516667]), 'x3': array([0.35758815])}]}, 'total': {'y1': [{'x1': array([0.87052721]), 'x2': array([0.37726189]), 'x3': array([0.29945874])}], 'y2': [{'x1': array([0.36991381]), 'x2': array([0.60974968]), 'x3': array([0.34776012])}]}} .. GENERATED FROM PYTHON SOURCE LINES 81-82 They can also be accessed separately: .. GENERATED FROM PYTHON SOURCE LINES 82-85 .. code-block:: default pprint.pprint(sobol_analysis.first_order_indices) pprint.pprint(sobol_analysis.total_order_indices) .. rst-class:: sphx-glr-script-out .. code-block:: none {'y1': [{'x1': array([0.19426511]), 'x2': array([0.21211854]), 'x3': array([0.01782884])}], 'y2': [{'x1': array([0.75387699]), 'x2': array([0.26516667]), 'x3': array([0.35758815])}]} {'y1': [{'x1': array([0.87052721]), 'x2': array([0.37726189]), 'x3': array([0.29945874])}], 'y2': [{'x1': array([0.36991381]), 'x2': array([0.60974968]), 'x3': array([0.34776012])}]} .. GENERATED FROM PYTHON SOURCE LINES 86-87 One can also obtain their confidence intervals: .. GENERATED FROM PYTHON SOURCE LINES 87-90 .. code-block:: default pprint.pprint(sobol_analysis.get_intervals()) pprint.pprint(sobol_analysis.get_intervals(first_order=False)) .. rst-class:: sphx-glr-script-out .. code-block:: none {'y1': [{'x1': array([-0.03400625, 0.54199216]), 'x2': array([-0.00512583, 0.66154825]), 'x3': array([-0.40647314, 0.44110609])}], 'y2': [{'x1': array([0.10944697, 1.99005049]), 'x2': array([-0.31379805, 0.99720365]), 'x3': array([-0.43222646, 1.8321417 ])}]} {'y1': [{'x1': array([0.50048303, 1.91420643]), 'x2': array([-0.35083774, 1.67786025]), 'x3': array([-0.19380841, 1.59540502])}], 'y2': [{'x1': array([-0.63775393, 3.12446122]), 'x2': array([-0.01837286, 2.23112503]), 'x3': array([-0.21486175, 1.97989239])}]} .. GENERATED FROM PYTHON SOURCE LINES 91-94 The main indices are the total Sobol' indices (:attr:`.SobolAnalysis.main_method` can also be set to `"first"` to use the first-order indices as main indices): .. GENERATED FROM PYTHON SOURCE LINES 94-96 .. code-block:: default pprint.pprint(sobol_analysis.main_indices) .. rst-class:: sphx-glr-script-out .. code-block:: none {'y1': [{'x1': array([0.87052721]), 'x2': array([0.37726189]), 'x3': array([0.29945874])}], 'y2': [{'x1': array([0.36991381]), 'x2': array([0.60974968]), 'x3': array([0.34776012])}]} .. GENERATED FROM PYTHON SOURCE LINES 97-100 These main indices are used to sort the input parameters by decreasing order of influence. We can observe that this ranking is not the same for both outputs: .. GENERATED FROM PYTHON SOURCE LINES 100-103 .. code-block:: default print(sobol_analysis.sort_parameters("y1")) print(sobol_analysis.sort_parameters("y2")) .. rst-class:: sphx-glr-script-out .. code-block:: none ['x1', 'x2', 'x3'] ['x2', 'x1', 'x3'] .. GENERATED FROM PYTHON SOURCE LINES 104-107 Lastly, we can use the method :meth:`.SobolAnalysis.plot` to visualize both first-order and total Sobol' indices: .. GENERATED FROM PYTHON SOURCE LINES 107-111 .. code-block:: default sobol_analysis.plot("y1", save=False, show=False) sobol_analysis.plot("y2", save=False, show=False) # Workaround for HTML rendering, instead of ``show=True`` plt.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/uncertainty/sensitivity/images/sphx_glr_plot_sobol_001.png :alt: Sobol indices for the output y1(0) :srcset: /examples/uncertainty/sensitivity/images/sphx_glr_plot_sobol_001.png :class: sphx-glr-multi-img * .. image-sg:: /examples/uncertainty/sensitivity/images/sphx_glr_plot_sobol_002.png :alt: Sobol indices for the output y2(0) :srcset: /examples/uncertainty/sensitivity/images/sphx_glr_plot_sobol_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.458 seconds) .. _sphx_glr_download_examples_uncertainty_sensitivity_plot_sobol.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sobol.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_sobol.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_