.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_data_analysis/distribution_fitting/plot_estimate_conditional_quantile.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_distribution_fitting_plot_estimate_conditional_quantile.py: Estimate a conditional quantile =============================== .. GENERATED FROM PYTHON SOURCE LINES 5-6 .. code-block:: Python # sphinx_gallery_thumbnail_number = 8 .. GENERATED FROM PYTHON SOURCE LINES 7-17 From a multivariate data sample, we estimate a distribution with kernel smoothing. Here we present a bivariate distribution :math:`X= (X_1, X_2)`. We use the `computeConditionalQuantile` method to estimate the 90% quantile :math:`Q_1` of the conditional variable :math:`X_2|X_1` : .. math:: Q_2 : x_1 \mapsto q_{0.9}(X_2|X_1=x_1) We then draw the curve :math:`Q_2 : x_1 \mapsto Q_2(x_1)`. We first start with independent normals then we consider dependent marginals with a Clayton copula. .. GENERATED FROM PYTHON SOURCE LINES 19-26 .. code-block:: Python import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt import numpy as np ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 27-28 Set the random generator seed .. GENERATED FROM PYTHON SOURCE LINES 28-30 .. code-block:: Python ot.RandomGenerator.SetSeed(0) .. GENERATED FROM PYTHON SOURCE LINES 31-35 Defining the marginals ---------------------- We consider two independent normal marginals : .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: Python X1 = ot.Normal(0.0, 1.0) X2 = ot.Normal(0.0, 3.0) .. GENERATED FROM PYTHON SOURCE LINES 40-42 Independent marginals --------------------- .. GENERATED FROM PYTHON SOURCE LINES 42-47 .. code-block:: Python distX = ot.ComposedDistribution([X1, X2]) sample = distX.getSample(1000) .. GENERATED FROM PYTHON SOURCE LINES 48-49 Let's see the data .. GENERATED FROM PYTHON SOURCE LINES 51-59 .. code-block:: Python graph = ot.Graph("2D-Normal sample", "x1", "x2", True, "") cloud = ot.Cloud(sample, "blue", "fsquare", "My Cloud") graph.add(cloud) graph.setXTitle("$X_1$") graph.setYTitle("$X_2$") graph.setTitle("A sample from $X=(X_1, X_2)$") view = viewer.View(graph) .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_001.png :alt: A sample from $X=(X_1, X_2)$ :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 60-61 We draw the isolines of the PDF of :math:`X` : .. GENERATED FROM PYTHON SOURCE LINES 61-67 .. code-block:: Python graph = distX.drawPDF() graph.setXTitle("$X_1$") graph.setYTitle("$X_2$") graph.setTitle("iso-PDF of $X=(X_1, X_2)$") view = viewer.View(graph) .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_002.png :alt: iso-PDF of $X=(X_1, X_2)$ :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 68-69 We estimate the density with kernel smoothing : .. GENERATED FROM PYTHON SOURCE LINES 69-73 .. code-block:: Python kernel = ot.KernelSmoothing() estimated = kernel.build(sample) .. GENERATED FROM PYTHON SOURCE LINES 74-75 We draw the isolines of the estimated PDF of :math:`X` : .. GENERATED FROM PYTHON SOURCE LINES 75-82 .. code-block:: Python graph = estimated.drawPDF() graph.setXTitle("$X_1$") graph.setYTitle("$X_2$") graph.setTitle("iso-PDF of $X=(X_1, X_2)$ estimated by kernel smoothing") view = viewer.View(graph) .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_003.png :alt: iso-PDF of $X=(X_1, X_2)$ estimated by kernel smoothing :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 83-85 We can compute the conditional quantile of :math:`X_2 | X_1` with the `computeConditionalQuantile` method and draw it after. .. GENERATED FROM PYTHON SOURCE LINES 87-88 We first create N observation points in :math:`[-3.0, 3.0]` : .. GENERATED FROM PYTHON SOURCE LINES 90-94 .. code-block:: Python N = 301 xobs = np.linspace(-3.0, 3.0, N) sampleObs = ot.Sample([[xi] for xi in xobs]) .. GENERATED FROM PYTHON SOURCE LINES 95-96 We create curves of the exact and approximated quantile :math:`Q_1` .. GENERATED FROM PYTHON SOURCE LINES 98-102 .. code-block:: Python x = [xi for xi in xobs] yapp = [estimated.computeConditionalQuantile(0.9, sampleObs[i]) for i in range(N)] yex = [distX.computeConditionalQuantile(0.9, sampleObs[i]) for i in range(N)] .. GENERATED FROM PYTHON SOURCE LINES 103-113 .. code-block:: Python cxy_app = ot.Curve(x, yapp) cxy_ex = ot.Curve(x, yex) graph = ot.Graph("90% quantile of $X_2 | X_1=x_1$", "$x_1$", "$Q_2(x_1)$", True, "") graph.add(cxy_app) graph.add(cxy_ex) graph.setLegends(["$Q_2$ kernel smoothing", "$Q_2$ exact"]) graph.setLegendPosition("lower right") graph.setColors(["red", "blue"]) view = viewer.View(graph) .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_004.png :alt: 90% quantile of $X_2 | X_1=x_1$ :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 114-116 In this case the :math:`Q_2` quantile is constant because of the independence of the marginals. .. GENERATED FROM PYTHON SOURCE LINES 118-125 Dependence through a Clayton copula ----------------------------------- We now define a Clayton copula to model the dependence between our marginals. The Clayton copula is a bivariate asymmmetric Archimedean copula, exhibiting greater dependence in the negative tail than in the positive. .. GENERATED FROM PYTHON SOURCE LINES 127-130 .. code-block:: Python copula = ot.ClaytonCopula(2.5) distX = ot.ComposedDistribution([X1, X2], copula) .. GENERATED FROM PYTHON SOURCE LINES 131-132 We generate a sample from the distribution : .. GENERATED FROM PYTHON SOURCE LINES 132-134 .. code-block:: Python sample = distX.getSample(1000) .. GENERATED FROM PYTHON SOURCE LINES 135-136 Let's see the data .. GENERATED FROM PYTHON SOURCE LINES 138-146 .. code-block:: Python graph = ot.Graph("2D-Normal sample", "x1", "x2", True, "") cloud = ot.Cloud(sample, "blue", "fsquare", "My Cloud") graph.add(cloud) graph.setXTitle("$X_1$") graph.setYTitle("$X_2$") graph.setTitle("A sample from $X=(X_1, X_2)$") view = viewer.View(graph) .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_005.png :alt: A sample from $X=(X_1, X_2)$ :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 147-148 We draw the isolines of the PDF of :math:`X` : .. GENERATED FROM PYTHON SOURCE LINES 148-154 .. code-block:: Python graph = distX.drawPDF() graph.setXTitle("$X_1$") graph.setYTitle("$X_2$") graph.setTitle("iso-PDF of $X=(X_1, X_2)$") view = viewer.View(graph) .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_006.png :alt: iso-PDF of $X=(X_1, X_2)$ :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 155-156 We estimate the density with kernel smoothing : .. GENERATED FROM PYTHON SOURCE LINES 156-159 .. code-block:: Python kernel = ot.KernelSmoothing() estimated = kernel.build(sample) .. GENERATED FROM PYTHON SOURCE LINES 160-161 We draw the isolines of the estimated PDF of :math:`X` : .. GENERATED FROM PYTHON SOURCE LINES 161-168 .. code-block:: Python graph = estimated.drawPDF() graph.setXTitle("$X_1$") graph.setYTitle("$X_2$") graph.setTitle("iso-PDF of $X=(X_1, X_2)$ estimated by kernel smoothing") view = viewer.View(graph) .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_007.png :alt: iso-PDF of $X=(X_1, X_2)$ estimated by kernel smoothing :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_007.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 169-171 We can compute the conditional quantile of :math:`X_2 | X_1=x1` with the `computeConditionalQuantile` method and draw it after. .. GENERATED FROM PYTHON SOURCE LINES 173-174 We first create N observation points in :math:`[-3.0, 3.0]` : .. GENERATED FROM PYTHON SOURCE LINES 176-180 .. code-block:: Python N = 301 xobs = np.linspace(-3.0, 3.0, N) sampleObs = ot.Sample([[xi] for xi in xobs]) .. GENERATED FROM PYTHON SOURCE LINES 181-182 We create curves of the exact and approximated quantile :math:`Q_1` .. GENERATED FROM PYTHON SOURCE LINES 184-188 .. code-block:: Python x = [xi for xi in xobs] yapp = [estimated.computeConditionalQuantile(0.9, sampleObs[i]) for i in range(N)] yex = [distX.computeConditionalQuantile(0.9, sampleObs[i]) for i in range(N)] .. GENERATED FROM PYTHON SOURCE LINES 189-199 .. code-block:: Python cxy_app = ot.Curve(x, yapp) cxy_ex = ot.Curve(x, yex) graph = ot.Graph("90% quantile of $X_2 | X_1=x_1$", "$x_1$", "$Q_2(x_1)$", True, "") graph.add(cxy_app) graph.add(cxy_ex) graph.setLegends(["$Q_2$ kernel smoothing", "$Q_2$ exact"]) graph.setLegendPosition("lower right") graph.setColors(["red", "blue"]) view = viewer.View(graph) .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_008.png :alt: 90% quantile of $X_2 | X_1=x_1$ :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_008.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 200-202 Our estimated conditional quantile is a good approximate and should be better the more data we have. We can observe it by increasing the number of samples. .. GENERATED FROM PYTHON SOURCE LINES 202-203 .. code-block:: Python plt.show() .. _sphx_glr_download_auto_data_analysis_distribution_fitting_plot_estimate_conditional_quantile.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_estimate_conditional_quantile.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_estimate_conditional_quantile.py `