.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_data_analysis/statistical_tests/plot_test_copula.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_data_analysis_statistical_tests_plot_test_copula.py: Test the copula =============== .. GENERATED FROM PYTHON SOURCE LINES 6-13 .. code-block:: Python import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 14-18 Copula fitting test using Kendall plot -------------------------------------- We first perform a visual goodness-of-fit test for a copula with the Kendall plot test. .. GENERATED FROM PYTHON SOURCE LINES 20-21 We create two samples of size 500. .. GENERATED FROM PYTHON SOURCE LINES 21-23 .. code-block:: Python N = 500 .. GENERATED FROM PYTHON SOURCE LINES 24-28 .. code-block:: Python dist1 = ot.ComposedDistribution([ot.Normal()] * 2, ot.GumbelCopula(3.0)) sample1 = dist1.getSample(N) sample1.setName("sample1") .. GENERATED FROM PYTHON SOURCE LINES 29-33 .. code-block:: Python dist2 = ot.ComposedDistribution([ot.Normal()] * 2, ot.ClaytonCopula(0.2)) sample2 = dist2.getSample(N) sample2.setName("sample2") .. GENERATED FROM PYTHON SOURCE LINES 34-35 We change the parameter for the evaluation of E(Wi) thanks to the `ResourceMap` : .. GENERATED FROM PYTHON SOURCE LINES 35-37 .. code-block:: Python ot.ResourceMap.SetAsUnsignedInteger("VisualTest-KendallPlot-MonteCarloSize", 25) .. GENERATED FROM PYTHON SOURCE LINES 38-39 We can test a specific copula model for a given sample, .. GENERATED FROM PYTHON SOURCE LINES 39-43 .. code-block:: Python copula_test = ot.GumbelCopula(3) graph = ot.VisualTest.DrawKendallPlot(sample1, copula_test) view = viewer.View(graph) .. image-sg:: /auto_data_analysis/statistical_tests/images/sphx_glr_plot_test_copula_001.png :alt: Kendall Plot :srcset: /auto_data_analysis/statistical_tests/images/sphx_glr_plot_test_copula_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 44-45 or test whether two samples have the same copula model .. GENERATED FROM PYTHON SOURCE LINES 45-48 .. code-block:: Python graph = ot.VisualTest.DrawKendallPlot(sample1, sample2) view = viewer.View(graph) .. image-sg:: /auto_data_analysis/statistical_tests/images/sphx_glr_plot_test_copula_002.png :alt: Kendall Plot :srcset: /auto_data_analysis/statistical_tests/images/sphx_glr_plot_test_copula_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 49-50 The first test gives a positive result as the blue curve is near the identity line which is not the case for the second test. .. GENERATED FROM PYTHON SOURCE LINES 53-58 Graphical copula validation --------------------------- In this paragraph we visualize an estimated copula versus the data in the rank space. .. GENERATED FROM PYTHON SOURCE LINES 60-61 First we create data .. GENERATED FROM PYTHON SOURCE LINES 61-66 .. code-block:: Python marginals = [ot.Normal()] * 2 dist = ot.ComposedDistribution(marginals, ot.ClaytonCopula(3)) N = 500 sample = dist.getSample(N) .. GENERATED FROM PYTHON SOURCE LINES 67-68 We build a estimate copula from the previous sample using the :class:`~openturns.ClaytonCopulaFactory` : .. GENERATED FROM PYTHON SOURCE LINES 68-70 .. code-block:: Python estimated = ot.ClaytonCopulaFactory().build(sample) .. GENERATED FROM PYTHON SOURCE LINES 71-72 We represent data as a cloud in the rank space : .. GENERATED FROM PYTHON SOURCE LINES 72-78 .. code-block:: Python ranksTransf = ot.MarginalTransformationEvaluation( marginals, ot.MarginalTransformationEvaluation.FROM ) rankSample = ranksTransf(sample) rankCloud = ot.Cloud(rankSample, "blue", "plus", "sample") .. GENERATED FROM PYTHON SOURCE LINES 79-80 We can plot the graph with rank sample and estimated copula : .. GENERATED FROM PYTHON SOURCE LINES 80-84 .. code-block:: Python myGraph = ot.Graph("Parametric estimation of the copula", "X", "Y", True, "upper left") myGraph.setLegendPosition("lower right") myGraph.add(rankCloud) .. GENERATED FROM PYTHON SOURCE LINES 85-86 and draw the iso-curves of the estimated copula .. GENERATED FROM PYTHON SOURCE LINES 86-108 .. code-block:: Python minPoint = [0.0] * 2 maxPoint = [1.0] * 2 pointNumber = [201] * 2 graphCop = estimated.drawPDF(minPoint, maxPoint, pointNumber) contour_estCop = graphCop.getDrawable(0) # Erase the labels of the iso-curves contour_estCop.setDrawLabels(False) # Change the levels of the iso-curves nlev = 21 levels = [0.0] * nlev for i in range(nlev): levels[i] = 0.25 * nlev / (nlev - i) contour_estCop.setLevels(levels) # Change the legend of the curves contour_estCop.setLegend("Gumbel copula") # Change the color of the iso-curves contour_estCop.setColor("red") # Add the iso-curves graph into the cloud one myGraph.add(contour_estCop) view = viewer.View(myGraph) .. image-sg:: /auto_data_analysis/statistical_tests/images/sphx_glr_plot_test_copula_003.png :alt: Parametric estimation of the copula :srcset: /auto_data_analysis/statistical_tests/images/sphx_glr_plot_test_copula_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 109-110 Display figures .. GENERATED FROM PYTHON SOURCE LINES 110-112 .. code-block:: Python plt.show() .. GENERATED FROM PYTHON SOURCE LINES 113-114 Reset default settings .. GENERATED FROM PYTHON SOURCE LINES 114-115 .. code-block:: Python ot.ResourceMap.Reload() .. _sphx_glr_download_auto_data_analysis_statistical_tests_plot_test_copula.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_test_copula.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_test_copula.py `