.. 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-14 .. 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 15-19 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 21-22 We create two samples of size 500. .. GENERATED FROM PYTHON SOURCE LINES 22-24 .. code-block:: Python N = 500 .. GENERATED FROM PYTHON SOURCE LINES 25-29 .. 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 30-34 .. 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 35-36 We change the parameter for the evaluation of :math:`\Expect{W_i}` thanks to the :class:`~openturns.ResourceMap` : .. GENERATED FROM PYTHON SOURCE LINES 36-38 .. code-block:: Python ot.ResourceMap.SetAsUnsignedInteger("VisualTest-KendallPlot-MonteCarloSize", 25) .. GENERATED FROM PYTHON SOURCE LINES 39-40 We can test a specific copula model for a given sample, .. GENERATED FROM PYTHON SOURCE LINES 40-44 .. 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 45-46 or test whether two samples have the same copula model .. GENERATED FROM PYTHON SOURCE LINES 46-49 .. 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 50-51 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 54-59 Graphical copula validation --------------------------- In this paragraph we visualize an estimated copula versus the data in the rank space. .. GENERATED FROM PYTHON SOURCE LINES 61-62 First we create data .. GENERATED FROM PYTHON SOURCE LINES 62-67 .. 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 68-69 We build a estimate copula from the previous sample using the :class:`~openturns.ClaytonCopulaFactory` : .. GENERATED FROM PYTHON SOURCE LINES 69-71 .. code-block:: Python estimated = ot.ClaytonCopulaFactory().build(sample) .. GENERATED FROM PYTHON SOURCE LINES 72-73 We represent data as a cloud in the rank space : .. GENERATED FROM PYTHON SOURCE LINES 73-79 .. 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 80-81 We can plot the graph with rank sample and estimated copula : .. GENERATED FROM PYTHON SOURCE LINES 81-85 .. 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 86-87 and draw the iso-curves of the estimated copula .. GENERATED FROM PYTHON SOURCE LINES 87-109 .. 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 110-111 Display figures .. GENERATED FROM PYTHON SOURCE LINES 111-113 .. code-block:: Python plt.show() .. GENERATED FROM PYTHON SOURCE LINES 114-115 Reset default settings .. GENERATED FROM PYTHON SOURCE LINES 115-116 .. 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 ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_test_copula.zip `