.. 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 7-10 .. code-block:: Python import openturns as ot import openturns.viewer as viewer .. GENERATED FROM PYTHON SOURCE LINES 11-15 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 17-18 We create two samples of size 500. .. GENERATED FROM PYTHON SOURCE LINES 18-20 .. code-block:: Python N = 500 .. GENERATED FROM PYTHON SOURCE LINES 21-25 .. code-block:: Python dist1 = ot.JointDistribution([ot.Normal()] * 2, ot.GumbelCopula(3.0)) sample1 = dist1.getSample(N) sample1.setName("sample1") .. GENERATED FROM PYTHON SOURCE LINES 26-30 .. code-block:: Python dist2 = ot.JointDistribution([ot.Normal()] * 2, ot.ClaytonCopula(0.2)) sample2 = dist2.getSample(N) sample2.setName("sample2") .. GENERATED FROM PYTHON SOURCE LINES 31-32 We change the parameter for the evaluation of :math:`\Expect{W_i}` thanks to the :class:`~openturns.ResourceMap` : .. GENERATED FROM PYTHON SOURCE LINES 32-34 .. code-block:: Python ot.ResourceMap.SetAsUnsignedInteger("VisualTest-KendallPlot-MonteCarloSize", 25) .. GENERATED FROM PYTHON SOURCE LINES 35-36 We can test a specific copula model for a given sample, .. GENERATED FROM PYTHON SOURCE LINES 36-40 .. 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.svg :alt: Kendall Plot :srcset: /auto_data_analysis/statistical_tests/images/sphx_glr_plot_test_copula_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 41-42 or test whether two samples have the same copula model .. GENERATED FROM PYTHON SOURCE LINES 42-45 .. 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.svg :alt: Kendall Plot :srcset: /auto_data_analysis/statistical_tests/images/sphx_glr_plot_test_copula_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 46-47 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 50-55 Graphical copula validation --------------------------- In this paragraph we visualize an estimated copula versus the data in the rank space. .. GENERATED FROM PYTHON SOURCE LINES 57-58 First we create data .. GENERATED FROM PYTHON SOURCE LINES 58-63 .. code-block:: Python marginals = [ot.Normal()] * 2 dist = ot.JointDistribution(marginals, ot.ClaytonCopula(3)) N = 500 sample = dist.getSample(N) .. GENERATED FROM PYTHON SOURCE LINES 64-65 We build a estimate copula from the previous sample using the :class:`~openturns.ClaytonCopulaFactory` : .. GENERATED FROM PYTHON SOURCE LINES 65-67 .. code-block:: Python estimated = ot.ClaytonCopulaFactory().build(sample) .. GENERATED FROM PYTHON SOURCE LINES 68-69 We represent data as a cloud in the rank space : .. GENERATED FROM PYTHON SOURCE LINES 69-75 .. 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 76-77 We can plot the graph with rank sample and estimated copula : .. GENERATED FROM PYTHON SOURCE LINES 77-81 .. 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 82-83 and draw the iso-curves of the estimated copula .. GENERATED FROM PYTHON SOURCE LINES 83-105 .. 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.svg :alt: Parametric estimation of the copula :srcset: /auto_data_analysis/statistical_tests/images/sphx_glr_plot_test_copula_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 106-107 Display figures .. GENERATED FROM PYTHON SOURCE LINES 107-108 .. code-block:: Python viewer.View.ShowAll() .. _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 ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_test_copula.zip `