.. 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-7 .. code-block:: Python # sphinx_gallery_thumbnail_number = 8 .. GENERATED FROM PYTHON SOURCE LINES 8-18 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 20-24 .. code-block:: Python import openturns as ot import openturns.viewer as viewer import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 25-29 Defining the marginals ---------------------- We consider two independent Normal marginals : .. GENERATED FROM PYTHON SOURCE LINES 29-33 .. code-block:: Python X1 = ot.Normal(0.0, 1.0) X2 = ot.Normal(0.0, 3.0) .. GENERATED FROM PYTHON SOURCE LINES 34-36 Independent marginals --------------------- .. GENERATED FROM PYTHON SOURCE LINES 36-41 .. code-block:: Python distX = ot.JointDistribution([X1, X2]) sample = distX.getSample(1000) .. GENERATED FROM PYTHON SOURCE LINES 42-43 Let's see the data .. GENERATED FROM PYTHON SOURCE LINES 45-53 .. 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.svg :alt: A sample from $X=(X_1, X_2)$ :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 54-55 We draw the isolines of the PDF of :math:`X` : .. GENERATED FROM PYTHON SOURCE LINES 55-61 .. 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.svg :alt: iso-PDF of $X=(X_1, X_2)$ :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 62-63 We estimate the density with kernel smoothing : .. GENERATED FROM PYTHON SOURCE LINES 63-67 .. code-block:: Python kernel = ot.KernelSmoothing() estimated = kernel.build(sample) .. GENERATED FROM PYTHON SOURCE LINES 68-69 We draw the isolines of the estimated PDF of :math:`X` : .. GENERATED FROM PYTHON SOURCE LINES 69-76 .. 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.svg :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.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 77-79 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 81-82 We first create :math:`\sampleSize` observation points in :math:`[-3.0, 3.0]` : .. GENERATED FROM PYTHON SOURCE LINES 84-88 .. 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 89-90 We create curves of the exact and approximated quantile :math:`Q_1` .. GENERATED FROM PYTHON SOURCE LINES 92-96 .. 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 97-106 .. 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") view = viewer.View(graph) .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_004.svg :alt: 90% quantile of $X_2 | X_1=x_1$ :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_004.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 107-109 In this case the :math:`Q_2` quantile is constant because of the independence of the marginals. .. GENERATED FROM PYTHON SOURCE LINES 111-118 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 120-123 .. code-block:: Python copula = ot.ClaytonCopula(2.5) distX = ot.JointDistribution([X1, X2], copula) .. GENERATED FROM PYTHON SOURCE LINES 124-125 We generate a sample from the distribution : .. GENERATED FROM PYTHON SOURCE LINES 125-127 .. code-block:: Python sample = distX.getSample(1000) .. GENERATED FROM PYTHON SOURCE LINES 128-129 Let's see the data .. GENERATED FROM PYTHON SOURCE LINES 131-139 .. 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.svg :alt: A sample from $X=(X_1, X_2)$ :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_005.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 140-141 We draw the isolines of the PDF of :math:`X` : .. GENERATED FROM PYTHON SOURCE LINES 141-147 .. 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.svg :alt: iso-PDF of $X=(X_1, X_2)$ :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_006.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 148-149 We estimate the density with kernel smoothing : .. GENERATED FROM PYTHON SOURCE LINES 149-152 .. code-block:: Python kernel = ot.KernelSmoothing() estimated = kernel.build(sample) .. GENERATED FROM PYTHON SOURCE LINES 153-154 We draw the isolines of the estimated PDF of :math:`X` : .. GENERATED FROM PYTHON SOURCE LINES 154-161 .. 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.svg :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.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 162-164 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 166-167 We first create :math:`\sampleSize` observation points in :math:`[-3.0, 3.0]` : .. GENERATED FROM PYTHON SOURCE LINES 169-173 .. 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 174-175 We create curves of the exact and approximated quantile :math:`Q_1` .. GENERATED FROM PYTHON SOURCE LINES 177-181 .. 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 182-191 .. 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") view = viewer.View(graph) .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_008.svg :alt: 90% quantile of $X_2 | X_1=x_1$ :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_conditional_quantile_008.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 192-193 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 195-196 Display the graphs .. GENERATED FROM PYTHON SOURCE LINES 196-197 .. code-block:: Python view.ShowAll() .. _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 ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_estimate_conditional_quantile.zip `