.. 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_maximumlikelihood_estimator.py:
Fit a distribution by maximum likelihood
========================================
In this example we are going to estimate the parameters of a parametric by generic numerical optimization of the likelihood.
The likelihood of a sample :math:`X` according to a parametric density function :math:`p_{\underline{\theta}}` is:
.. math::
likelihood(\underline{x}_1, \dots, \underline{x}_n,\underline{\theta}) = \prod_{i=1}^n p_{\underline{\theta}}(\underline{x}_i)
.. code-block:: default
from __future__ import print_function
import openturns as ot
import math as m
ot.Log.Show(ot.Log.NONE)
Create data from a gaussian pdf with mu=4, sigma=1.5
.. code-block:: default
sample = ot.Normal(4.0, 1.5).getSample(200)
Create the search interval of (mu, sigma): the constraint is sigma>0
.. code-block:: default
lowerBound = [-1.0, 1.0e-4]
upperBound = [-1.0, -1.0]
finiteLowerBound = [False, True]
finiteUpperBound = [False, False]
bounds = ot.Interval(lowerBound, upperBound, finiteLowerBound, finiteUpperBound)
Create the starting point of the research
For mu : the first point
For sigma : a value evaluated from the two first points
.. code-block:: default
mu0 = sample[0][0]
sigma0 = m.sqrt((sample[1][0] - sample[0][0]) * (sample[1][0] - sample[0][0]))
startingPoint = [mu0, sigma0]
ot.Point(startingPoint)
.. raw:: html
[2.39784,4.01969]
Create the estimator from a parametric pdf
.. code-block:: default
pdf = ot.Normal()
factory = ot.MaximumLikelihoodFactory(pdf)
factory.setOptimizationBounds(bounds)
Set the starting point via the solver
.. code-block:: default
solver = factory.getOptimizationAlgorithm()
solver.setStartingPoint(startingPoint)
factory.setOptimizationAlgorithm(solver)
Estimate the parametric model
.. code-block:: default
distribution = factory.build(sample)
str(distribution)
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
'Normal(mu = 3.94775, sigma = 1.49821)'
Retrieve the estimated parameters
.. code-block:: default
distribution.getParameter()
.. raw:: html
[3.94775,1.49821]
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 0.004 seconds)
.. _sphx_glr_download_auto_data_analysis_distribution_fitting_plot_maximumlikelihood_estimator.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_maximumlikelihood_estimator.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_maximumlikelihood_estimator.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_