.. 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 Click :ref:`here ` 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-12 .. code-block:: default 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 13-17 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 19-20 We create two samples of size 500. .. GENERATED FROM PYTHON SOURCE LINES 20-22 .. code-block:: default N = 500 .. GENERATED FROM PYTHON SOURCE LINES 23-27 .. code-block:: default dist1 = ot.ComposedDistribution([ot.Normal()] * 2, ot.GumbelCopula(3.0)) sample1 = dist1.getSample(N) sample1.setName('sample1') .. GENERATED FROM PYTHON SOURCE LINES 28-32 .. code-block:: default dist2 = ot.ComposedDistribution([ot.Normal()] * 2, ot.ClaytonCopula(0.2)) sample2 = dist2.getSample(N) sample2.setName('sample2') .. GENERATED FROM PYTHON SOURCE LINES 33-34 We change the parameter for the evaluation of E(Wi) thanks to the `ResourceMap` : .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: default 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:: default 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:: default 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:: default 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:: default 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-77 .. code-block:: default ranksTransf = ot.MarginalTransformationEvaluation( marginals, ot.MarginalTransformationEvaluation.FROM) rankSample = ranksTransf(sample) rankCloud = ot.Cloud(rankSample, 'blue', 'plus', 'sample') .. GENERATED FROM PYTHON SOURCE LINES 78-79 We can plot the graph with rank sample and estimated copula : .. GENERATED FROM PYTHON SOURCE LINES 79-84 .. code-block:: default myGraph = ot.Graph('Parametric estimation of the copula', 'X', 'Y', True, 'topleft') myGraph.setLegendPosition('bottomright') 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:: default 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-111 .. code-block:: default plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.291 seconds) .. _sphx_glr_download_auto_data_analysis_statistical_tests_plot_test_copula.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_test_copula.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_test_copula.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_