.. 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 :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_normal.py: Fit a parametric distribution ============================= .. GENERATED FROM PYTHON SOURCE LINES 7-12 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 14-17 .. code-block:: Python import openturns as ot import openturns.viewer as otv .. GENERATED FROM PYTHON SOURCE LINES 18-23 The Normal distribution ----------------------- The parameters are estimated by the method of moments. .. GENERATED FROM PYTHON SOURCE LINES 25-26 We consider a sample, here created from a standard :class:`~openturns.Normal` distribution : .. GENERATED FROM PYTHON SOURCE LINES 26-28 .. code-block:: Python sample = ot.Normal().getSample(1000) .. GENERATED FROM PYTHON SOURCE LINES 29-30 We can estimate a Normal distribution with :class:`~openturns.NormalFactory` : .. GENERATED FROM PYTHON SOURCE LINES 30-32 .. code-block:: Python distribution = ot.NormalFactory().build(sample) .. GENERATED FROM PYTHON SOURCE LINES 33-34 We take a look at the estimated parameters with the `getParameter` method : .. GENERATED FROM PYTHON SOURCE LINES 34-36 .. code-block:: Python print(distribution.getParameter()) .. rst-class:: sphx-glr-script-out .. code-block:: none [0.00679743,0.980133] .. GENERATED FROM PYTHON SOURCE LINES 37-38 We draw the fitted distribution .. GENERATED FROM PYTHON SOURCE LINES 38-42 .. code-block:: Python graph = distribution.drawPDF() graph.setTitle("Fitted Normal distribution") view = otv.View(graph) .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_normal_001.svg :alt: Fitted Normal distribution :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_normal_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 43-48 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 50-51 We generate a sample from a :class:`~openturns.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 51-53 .. code-block:: Python sample = ot.Student(5.0, -0.5, 2.0).getSample(1000) .. GENERATED FROM PYTHON SOURCE LINES 54-55 We use the factory to build an estimated distribution : .. GENERATED FROM PYTHON SOURCE LINES 55-57 .. code-block:: Python distribution = ot.StudentFactory().build(sample) .. GENERATED FROM PYTHON SOURCE LINES 58-59 We can obtain the estimated parameters with the `getParameter` method : .. GENERATED FROM PYTHON SOURCE LINES 59-62 .. code-block:: Python print(distribution.getParameter()) .. rst-class:: sphx-glr-script-out .. code-block:: none [4.90307,-0.634589,1.85743] .. GENERATED FROM PYTHON SOURCE LINES 63-64 Draw fitted distribution .. GENERATED FROM PYTHON SOURCE LINES 64-68 .. code-block:: Python graph = distribution.drawPDF() graph.setTitle("Fitted Student distribution") view = otv.View(graph) .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_normal_002.svg :alt: Fitted Student distribution :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_normal_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 69-74 The Pareto distribution ----------------------- By default the parameters of the :class:`~openturns.Pareto` distribution are estimated by least squares. .. GENERATED FROM PYTHON SOURCE LINES 76-77 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 77-79 .. code-block:: Python sample = ot.Pareto(1.0, 1.0, 0.0).getSample(1000) .. GENERATED FROM PYTHON SOURCE LINES 80-81 Draw fitted distribution .. GENERATED FROM PYTHON SOURCE LINES 81-89 .. code-block:: Python distribution = ot.ParetoFactory().build(sample) print(distribution.getParameter()) graph = distribution.drawPDF() graph.setTitle("Fitted Pareto distribution") view = otv.View(graph) .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_normal_003.svg :alt: Fitted Pareto distribution :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_estimate_normal_003.svg :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [0.657018,0.861117,0.348716] .. GENERATED FROM PYTHON SOURCE LINES 90-91 Display all graphs .. GENERATED FROM PYTHON SOURCE LINES 91-92 .. code-block:: Python otv.View.ShowAll() .. _sphx_glr_download_auto_data_analysis_distribution_fitting_plot_estimate_normal.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_normal.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_estimate_normal.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_estimate_normal.zip `