.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/uncertainty/sensitivity/plot_correlation.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_correlation.py: Correlation analysis ==================== .. GENERATED FROM PYTHON SOURCE LINES 25-33 .. code-block:: default import pprint from gemseo.algos.parameter_space import ParameterSpace from gemseo.api import create_discipline from gemseo.uncertainty.sensitivity.correlation.analysis import CorrelationAnalysis from matplotlib import pyplot as plt from numpy import pi .. GENERATED FROM PYTHON SOURCE LINES 34-42 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 42-51 .. 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 52-58 Then, we consider the case where the deterministic variables :math:`x_1`, :math:`x_2` and :math:`x_3` are replaced with the uncertain variables :math:`X_1`, :math:`X_2` and :math:`X_3`. The latter are independent and identically distributed according to an uniform distribution between :math:`-\pi` and :math:`\pi`: .. GENERATED FROM PYTHON SOURCE LINES 58-64 .. code-block:: default space = ParameterSpace() for variable in ["x1", "x2", "x3"]: space.add_random_variable( variable, "OTUniformDistribution", minimum=-pi, maximum=pi ) .. GENERATED FROM PYTHON SOURCE LINES 65-70 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 correlation coefficients from a :class:`.CorrelationAnalysis`: .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: default correlation = CorrelationAnalysis([discipline], space, 1000) correlation.compute_indices() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'pearson': {'y1': [{'x1': array([0.46230446]), 'x2': array([0.02059071]), 'x3': array([0.04979026])}], 'y2': [{'x1': array([-0.01646096]), 'x2': array([0.43717338]), 'x3': array([0.0273529])}]}, 'spearman': {'y1': [{'x1': array([0.47177886]), 'x2': array([0.03220921]), 'x3': array([0.04128518])}], 'y2': [{'x1': array([-0.02257586]), 'x2': array([0.42762945]), 'x3': array([0.01117796])}]}, 'pcc': {'y1': [{'x1': array([0.46282123]), 'x2': array([0.0306214]), 'x3': array([0.0512473])}], 'y2': [{'x1': array([-0.01225501]), 'x2': array([0.4375029]), 'x3': array([0.03525319])}]}, 'prcc': {'y1': [{'x1': array([0.47248718]), 'x2': array([0.04368652]), 'x3': array([0.0419606])}], 'y2': [{'x1': array([-0.01922223]), 'x2': array([0.42765077]), 'x3': array([0.01729782])}]}, 'src': {'y1': [{'x1': array([0.21363529]), 'x2': array([0.00073555]), 'x3': array([0.00206351])}], 'y2': [{'x1': array([0.00012136]), 'x2': array([0.19126269]), 'x3': array([0.00100532])}]}, 'srrc': {'y1': [{'x1': array([0.22268253]), 'x2': array([0.00148155]), 'x3': array([0.00136652])}], 'y2': [{'x1': array([0.00030191]), 'x2': array([0.18281477]), 'x3': array([0.00024446])}]}, 'ssrrc': {'y1': [{'x1': array([0.46220697]), 'x2': array([0.027121]), 'x3': array([0.04542585])}], 'y2': [{'x1': array([-0.01101656]), 'x2': array([0.4373359]), 'x3': array([0.03170672])}]}} .. GENERATED FROM PYTHON SOURCE LINES 74-82 The resulting indices are the Pearson correlation coefficients, the Spearman correlation coefficients, the Partial Correlation Coefficients (PCC), the Partial Rank Correlation Coefficients (PRCC), the Standard Regression Coefficients (SRC), the Standard Rank Regression Coefficient (SRRC) and the Signed Standard Rank Regression Coefficient (SSRRC): .. GENERATED FROM PYTHON SOURCE LINES 82-84 .. code-block:: default pprint.pprint(correlation.indices) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'pcc': {'y1': [{'x1': array([0.46282123]), 'x2': array([0.0306214]), 'x3': array([0.0512473])}], 'y2': [{'x1': array([-0.01225501]), 'x2': array([0.4375029]), 'x3': array([0.03525319])}]}, 'pearson': {'y1': [{'x1': array([0.46230446]), 'x2': array([0.02059071]), 'x3': array([0.04979026])}], 'y2': [{'x1': array([-0.01646096]), 'x2': array([0.43717338]), 'x3': array([0.0273529])}]}, 'prcc': {'y1': [{'x1': array([0.47248718]), 'x2': array([0.04368652]), 'x3': array([0.0419606])}], 'y2': [{'x1': array([-0.01922223]), 'x2': array([0.42765077]), 'x3': array([0.01729782])}]}, 'spearman': {'y1': [{'x1': array([0.47177886]), 'x2': array([0.03220921]), 'x3': array([0.04128518])}], 'y2': [{'x1': array([-0.02257586]), 'x2': array([0.42762945]), 'x3': array([0.01117796])}]}, 'src': {'y1': [{'x1': array([0.21363529]), 'x2': array([0.00073555]), 'x3': array([0.00206351])}], 'y2': [{'x1': array([0.00012136]), 'x2': array([0.19126269]), 'x3': array([0.00100532])}]}, 'srrc': {'y1': [{'x1': array([0.22268253]), 'x2': array([0.00148155]), 'x3': array([0.00136652])}], 'y2': [{'x1': array([0.00030191]), 'x2': array([0.18281477]), 'x3': array([0.00024446])}]}, 'ssrrc': {'y1': [{'x1': array([0.46220697]), 'x2': array([0.027121]), 'x3': array([0.04542585])}], 'y2': [{'x1': array([-0.01101656]), 'x2': array([0.4373359]), 'x3': array([0.03170672])}]}} .. GENERATED FROM PYTHON SOURCE LINES 85-87 The main indices corresponds to the Spearman correlation indices (this main method can be changed with :attr:`.CorrelationAnalysis.main_method`): .. GENERATED FROM PYTHON SOURCE LINES 87-89 .. code-block:: default pprint.pprint(correlation.main_indices) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'y1': [{'x1': array([0.47177886]), 'x2': array([0.03220921]), 'x3': array([0.04128518])}], 'y2': [{'x1': array([-0.02257586]), 'x2': array([0.42762945]), 'x3': array([0.01117796])}]} .. GENERATED FROM PYTHON SOURCE LINES 90-92 We can also sort the input parameters by decreasing order of influence and observe that this ranking is not the same for both outputs: .. GENERATED FROM PYTHON SOURCE LINES 92-95 .. code-block:: default print(correlation.sort_parameters("y1")) print(correlation.sort_parameters("y2")) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ['x1', 'x3', 'x2'] ['x2', 'x3', 'x1'] .. GENERATED FROM PYTHON SOURCE LINES 96-99 Lastly, we can use the method :meth:`.CorrelationAnalysis.plot` to visualize the different correlation coefficients: .. GENERATED FROM PYTHON SOURCE LINES 99-103 .. code-block:: default correlation.plot("y1", save=False, show=False) correlation.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_correlation_001.png :alt: Correlation indices for the output y1(0) :srcset: /examples/uncertainty/sensitivity/images/sphx_glr_plot_correlation_001.png :class: sphx-glr-multi-img * .. image-sg:: /examples/uncertainty/sensitivity/images/sphx_glr_plot_correlation_002.png :alt: Correlation indices for the output y2(0) :srcset: /examples/uncertainty/sensitivity/images/sphx_glr_plot_correlation_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 1.534 seconds) .. _sphx_glr_download_examples_uncertainty_sensitivity_plot_correlation.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_correlation.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_correlation.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_