.. 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_quantilematching_estimator.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_quantilematching_estimator.py: Define a distribution from quantiles ==================================== .. GENERATED FROM PYTHON SOURCE LINES 7-8 In this example we are going to estimate a parametric distribution by numerical optimization of some quantiles. .. GENERATED FROM PYTHON SOURCE LINES 10-13 .. code-block:: Python import openturns as ot from openturns.viewer import View .. GENERATED FROM PYTHON SOURCE LINES 14-21 We need as many quantile values as there are parameters of the distribution. For example, for a normal distribution, the two parameters are the mean and the standard deviation. This implies that two quantiles are required to estimate the parameters of a normal distribution. The values of the parameters :math:`\mu`, :math:`\sigma` will be used as the reference to assess the optimization. .. GENERATED FROM PYTHON SOURCE LINES 21-29 .. code-block:: Python dist_ref = ot.Normal(17.0, 34.0) dist_ref.setDescription(["reference"]) p1 = 0.05 p2 = 0.95 q1 = dist_ref.computeQuantile(p1)[0] q2 = dist_ref.computeQuantile(p2)[0] print(q1, q2) .. rst-class:: sphx-glr-script-out .. code-block:: none -38.92502331635007 72.92502331635006 .. GENERATED FROM PYTHON SOURCE LINES 30-32 Fit a normal distribution from these quantiles by numerical optimization. Ensure we get the same values as the reference. .. GENERATED FROM PYTHON SOURCE LINES 32-37 .. code-block:: Python factory = ot.QuantileMatchingFactory(ot.Normal(), [p1, p2]) dist_estim = factory.buildFromQuantiles([q1, q2]) dist_estim.setDescription(["estimated"]) print(dist_estim) .. rst-class:: sphx-glr-script-out .. code-block:: none Normal(mu = 17, sigma = 34) .. GENERATED FROM PYTHON SOURCE LINES 38-39 Graphically validate if the estimated distribution verifies the imposed quantiles. .. GENERATED FROM PYTHON SOURCE LINES 39-54 .. code-block:: Python graph = dist_estim.drawCDF() curve_q1 = ot.Curve([q1, q1, 0.0], [0.0, p1, p1]) curve_q2 = ot.Curve([q2, q2, 0.0], [0.0, p2, p2]) graph.add(curve_q1) graph.add(curve_q2) text_q1 = ot.Text([[q1, -0.1]], [r"$q_1$"]) text_p1 = ot.Text([[0.0, p1]], [r"$p_1$"]) graph.add(text_q1) graph.add(text_p1) text_q2 = ot.Text([[q2, -0.1]], [r"$q_2$"]) text_p2 = ot.Text([[0.0, p2]], [r"$p_2$"]) graph.add(text_q2) graph.add(text_p2) _ = View(graph) .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_quantilematching_estimator_001.png :alt: plot quantilematching estimator :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_quantilematching_estimator_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 55-57 It is also possible to define a Histogram distribution from quantiles. As it is a non-parametric distribution we can define as many quantiles as needed. .. GENERATED FROM PYTHON SOURCE LINES 57-62 .. code-block:: Python N = 10 probabilities = [(i + 1) / N for i in range(N)] probabilities[-1] = 1.0 - 1e-3 quantiles = [dist_ref.computeQuantile(pi)[0] for pi in probabilities] .. GENERATED FROM PYTHON SOURCE LINES 63-65 The service is part of the :class:`~openturns.HistogramFactory` class. We also need to define the lower bound of the Histogram to build the distribution. .. GENERATED FROM PYTHON SOURCE LINES 65-70 .. code-block:: Python lowerBound = quantiles[0] - 10.0 histo_quant = ot.HistogramFactory().buildFromQuantiles( lowerBound, probabilities, quantiles ) .. GENERATED FROM PYTHON SOURCE LINES 71-73 Graphically validate if the estimated distribution verifies the imposed quantiles. We can see that the CDF of the estimated Histogram matches all the quantile dots. .. GENERATED FROM PYTHON SOURCE LINES 73-81 .. code-block:: Python histo_quant.setDescription(["estimated"]) graph = histo_quant.drawCDF() curve_qi = ot.Cloud(quantiles, probabilities) curve_qi.setLegend("quantiles") graph.add(curve_qi) _ = View(graph) View.ShowAll() .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_quantilematching_estimator_002.png :alt: plot quantilematching estimator :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_quantilematching_estimator_002.png :class: sphx-glr-single-img .. _sphx_glr_download_auto_data_analysis_distribution_fitting_plot_quantilematching_estimator.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_quantilematching_estimator.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_quantilematching_estimator.py `