.. 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_data_analysis_manage_data_and_samples_plot_sample_correlation.py: Estimate correlation coefficients ================================= In this example we are going to estimate the correlation between an output sample Y and the corresponding inputs using various estimators: - Pearson coefficients - Spearman coefficients - PCC: Partial Correlation Coefficients - PRCC: Partial Rank Correlation Coefficient - SRC: Standard Regression Coefficients - SRRC: Standard Rank Regression Coefficient .. code-block:: default from __future__ import print_function import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) To illustrate the usage of the method mentionned above, we define a set of X/Y data using the :ref:`Ishigami model `. This classical model is defined in a data class : .. code-block:: default from openturns.usecases import ishigami_function as ishigami_function im = ishigami_function.IshigamiModel() Create X/Y data We get the input variables description : .. code-block:: default input_names = im.distributionX.getDescription() size = 100 inputDesign = ot.SobolIndicesExperiment(im.distributionX, size, True).generate() outputDesign = im.model(inputDesign) PCC coefficients ------------------ We compute here `PCC` coefficients using the `CorrelationAnalysis` .. code-block:: default pcc_indices = ot.CorrelationAnalysis.PCC(inputDesign, outputDesign) print(pcc_indices) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [0.48083,0.0118573,-0.0399335] .. code-block:: default graph = ot.SobolIndicesAlgorithm.DrawCorrelationCoefficients(pcc_indices, input_names, "PCC coefficients") view = viewer.View(graph) .. image:: /auto_data_analysis/manage_data_and_samples/images/sphx_glr_plot_sample_correlation_001.png :alt: PCC coefficients :class: sphx-glr-single-img PRCC coefficients -------------------- We compute here `PRCC` coefficients using the `CorrelationAnalysis` .. code-block:: default prcc_indices = ot.CorrelationAnalysis.PRCC(inputDesign, outputDesign) print(prcc_indices) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [0.48438,-0.00850357,-0.0310585] .. code-block:: default graph = ot.SobolIndicesAlgorithm.DrawCorrelationCoefficients(prcc_indices, input_names, "PRCC coefficients") view = viewer.View(graph) .. image:: /auto_data_analysis/manage_data_and_samples/images/sphx_glr_plot_sample_correlation_002.png :alt: PRCC coefficients :class: sphx-glr-single-img SRC coefficients ------------------- We compute here `SRC` coefficients using the `CorrelationAnalysis` .. code-block:: default src_indices = ot.CorrelationAnalysis.SRC(inputDesign, outputDesign) print(src_indices) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [0.231036,0.000107773,0.00122827] .. code-block:: default graph = ot.SobolIndicesAlgorithm.DrawCorrelationCoefficients(src_indices, input_names, 'SRC coefficients') view = viewer.View(graph) .. image:: /auto_data_analysis/manage_data_and_samples/images/sphx_glr_plot_sample_correlation_003.png :alt: SRC coefficients :class: sphx-glr-single-img Case where coefficients sum to 1 : .. code-block:: default scale_src_indices = ot.CorrelationAnalysis.SRC(inputDesign, outputDesign, True) print(scale_src_indices) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [0.99425,0.000463796,0.00528582] And its associated graph: .. code-block:: default graph = ot.SobolIndicesAlgorithm.DrawCorrelationCoefficients(scale_src_indices, input_names, 'Scaled SRC coefficients') view = viewer.View(graph) .. image:: /auto_data_analysis/manage_data_and_samples/images/sphx_glr_plot_sample_correlation_004.png :alt: Scaled SRC coefficients :class: sphx-glr-single-img Finally, using signed src: we get the trend importance : .. code-block:: default signed_src_indices = ot.CorrelationAnalysis.SignedSRC(inputDesign, outputDesign) print(signed_src_indices) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [0.480662,0.0103814,-0.0350468] and its graph : .. code-block:: default graph = ot.SobolIndicesAlgorithm.DrawCorrelationCoefficients(signed_src_indices, input_names, 'Signed SRC coefficients') view = viewer.View(graph) .. image:: /auto_data_analysis/manage_data_and_samples/images/sphx_glr_plot_sample_correlation_005.png :alt: Signed SRC coefficients :class: sphx-glr-single-img SRRC coefficients -------------------- We compute here `SRRC` coefficients using the `CorrelationAnalysis` .. code-block:: default srrc_indices = ot.CorrelationAnalysis.SRRC(inputDesign, outputDesign) print(srrc_indices) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [0.234826,5.52475e-05,0.00074076] .. code-block:: default graph = ot.SobolIndicesAlgorithm.DrawCorrelationCoefficients(srrc_indices, input_names, 'SRRC coefficients') view = viewer.View(graph) .. image:: /auto_data_analysis/manage_data_and_samples/images/sphx_glr_plot_sample_correlation_006.png :alt: SRRC coefficients :class: sphx-glr-single-img Pearson coefficients ----------------------- We compute here the Pearson :math:`\rho` coefficients using the `CorrelationAnalysis` .. code-block:: default pearson_correlation = ot.CorrelationAnalysis.PearsonCorrelation(inputDesign, outputDesign) print(pearson_correlation) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [0.482871,0.0178456,-0.0638373] .. code-block:: default graph = ot.SobolIndicesAlgorithm.DrawCorrelationCoefficients(pearson_correlation, input_names, "Pearson correlation coefficients") view = viewer.View(graph) .. image:: /auto_data_analysis/manage_data_and_samples/images/sphx_glr_plot_sample_correlation_007.png :alt: Pearson correlation coefficients :class: sphx-glr-single-img Spearman coefficients ----------------------- We compute here the Pearson :math:`\rho_s` coefficients using the `CorrelationAnalysis` .. code-block:: default spearman_correlation = ot.CorrelationAnalysis.SpearmanCorrelation(inputDesign, outputDesign) print(spearman_correlation) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [0.486298,-0.00194796,-0.0585667] .. code-block:: default graph = ot.SobolIndicesAlgorithm.DrawCorrelationCoefficients(spearman_correlation, input_names, "Spearman correlation coefficients") view = viewer.View(graph) plt.show() .. image:: /auto_data_analysis/manage_data_and_samples/images/sphx_glr_plot_sample_correlation_008.png :alt: Spearman correlation coefficients :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.590 seconds) .. _sphx_glr_download_auto_data_analysis_manage_data_and_samples_plot_sample_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_sample_correlation.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_sample_correlation.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_