.. 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_normal.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_distribution_fitting_plot_estimate_normal.py: Fit a parametric distribution ============================= .. GENERATED FROM PYTHON SOURCE LINES 9-14 In this example we estimate the parameters of a distribution from a given sample. Once we are settled on a good candidate, we use the corresponding factory to fit the distribution. Each distribution factory has one or several estimators available. They are all derived from either the Maximum Likelihood method or from the method of moments (see :ref:`parametric_estimation`). .. GENERATED FROM PYTHON SOURCE LINES 16-23 .. code-block:: default from __future__ import print_function 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 24-29 The Normal distribution ----------------------- The parameters are estimated by the method of moments. .. GENERATED FROM PYTHON SOURCE LINES 31-32 We consider a sample, here created from a standard normal distribution : .. GENERATED FROM PYTHON SOURCE LINES 32-34 .. code-block:: default sample = ot.Normal().getSample(1000) .. GENERATED FROM PYTHON SOURCE LINES 35-36 We can estimate a normal distribution with `ǸormalFactory` : .. GENERATED FROM PYTHON SOURCE LINES 36-38 .. code-block:: default distribution = ot.NormalFactory().build(sample) .. GENERATED FROM PYTHON SOURCE LINES 39-40 We take a look at the estimated parameters with the `getParameter` method : .. GENERATED FROM PYTHON SOURCE LINES 40-42 .. code-block:: default print(distribution.getParameter()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [0.00320214,1.02733] .. GENERATED FROM PYTHON SOURCE LINES 43-44 We draw the fitted distribution .. GENERATED FROM PYTHON SOURCE LINES 44-48 .. code-block:: default graph = distribution.drawPDF() graph.setTitle("Fitted Normal distribution") view = viewer.View(graph) .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_normal_001.png :alt: Fitted Normal distribution :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_normal_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 49-54 The Student distribution ------------------------ The parameters of the Student law are estimated by a mixed method of moments and reduces MLE. .. GENERATED FROM PYTHON SOURCE LINES 56-57 We generate a sample from a Student distribution with parameters :math:`\nu=5.0`, :math:`\mu = -0.5` and a scale parameter :math:`\sigma=2.0`. .. GENERATED FROM PYTHON SOURCE LINES 57-59 .. code-block:: default sample = ot.Student(5.0, -0.5, 2.0).getSample(1000) .. GENERATED FROM PYTHON SOURCE LINES 60-61 We use the factory to build an estimated distribution : .. GENERATED FROM PYTHON SOURCE LINES 61-63 .. code-block:: default distribution = ot.StudentFactory().build(sample) .. GENERATED FROM PYTHON SOURCE LINES 64-65 We can obtain the estimated parameters with the `getParameter` method : .. GENERATED FROM PYTHON SOURCE LINES 65-68 .. code-block:: default print(distribution.getParameter()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [3.65576,-0.515215,1.84614] .. GENERATED FROM PYTHON SOURCE LINES 69-70 Draw fitted distribution .. GENERATED FROM PYTHON SOURCE LINES 70-74 .. code-block:: default graph = distribution.drawPDF() graph.setTitle("Fitted Student distribution") view = viewer.View(graph) .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_normal_002.png :alt: Fitted Student distribution :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_normal_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 75-80 The Pareto distribution ----------------------- By default the parameters of the Pareto distribution are estimated by least squares. .. GENERATED FROM PYTHON SOURCE LINES 82-83 We use a sample from a Pareto distribution with a scale parameter :math:`\beta=1.0`, a shape parameter :math:`\alpha > 1.0` and a location parameter :math:`\gamma = 0.0`. .. GENERATED FROM PYTHON SOURCE LINES 83-85 .. code-block:: default sample = ot.Pareto(1.0, 1.0, 0.0).getSample(1000) .. GENERATED FROM PYTHON SOURCE LINES 86-87 Draw fitted distribution .. GENERATED FROM PYTHON SOURCE LINES 87-97 .. code-block:: default distribution = ot.ParetoFactory().build(sample) print(distribution.getParameter()) graph = distribution.drawPDF() graph.setTitle("Fitted Pareto distribution") view = viewer.View(graph) plt.show() .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_normal_003.png :alt: Fitted Pareto distribution :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_normal_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [0.787856,0.944192,0.246677] .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.407 seconds) .. _sphx_glr_download_auto_data_analysis_distribution_fitting_plot_estimate_normal.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_estimate_normal.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_estimate_normal.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_