.. 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_maximumlikelihood_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_maximumlikelihood_estimator.py: Fit a distribution by maximum likelihood ======================================== .. GENERATED FROM PYTHON SOURCE LINES 6-13 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:`(\vect{x}_1, \dots, \vect{x}_n)` according to a parametric density function :math:`p_{\vect{\theta}}` is: .. math:: \ell(\vect{x}_1, \dots, \vect{x}_n,\vect{\theta}) = \prod_{i=1}^n p_{\vect{\theta}}(\vect{x}_i) .. GENERATED FROM PYTHON SOURCE LINES 15-20 .. code-block:: Python import openturns as ot import math as m ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 21-22 Create data from a normal PDF with :math:`\mu=4`, :math:`\sigma=1.5`. .. GENERATED FROM PYTHON SOURCE LINES 22-24 .. code-block:: Python sample = ot.Normal(4.0, 1.5).getSample(200) .. GENERATED FROM PYTHON SOURCE LINES 25-26 Create the search interval of (:math:`\mu`, :math:`\sigma`) : the constraint is :math:`\sigma>0`. .. GENERATED FROM PYTHON SOURCE LINES 26-32 .. code-block:: Python lowerBound = [-1.0, 1.0e-4] upperBound = [-1.0, -1.0] finiteLowerBound = [False, True] finiteUpperBound = [False, False] bounds = ot.Interval(lowerBound, upperBound, finiteLowerBound, finiteUpperBound) .. GENERATED FROM PYTHON SOURCE LINES 33-37 Create the starting point of the research: - for :math:`\mu` : the first point, - for :math:`\sigma` : a value evaluated from the two first points. .. GENERATED FROM PYTHON SOURCE LINES 37-41 .. code-block:: Python mu0 = sample[0][0] sigma0 = m.sqrt((sample[1][0] - sample[0][0]) * (sample[1][0] - sample[0][0])) startingPoint = [mu0, sigma0] .. GENERATED FROM PYTHON SOURCE LINES 42-43 Create the estimator from a parametric PDF. .. GENERATED FROM PYTHON SOURCE LINES 43-47 .. code-block:: Python pdf = ot.Normal() factory = ot.MaximumLikelihoodFactory(pdf) factory.setOptimizationBounds(bounds) .. GENERATED FROM PYTHON SOURCE LINES 48-49 Set the starting point via the solver. .. GENERATED FROM PYTHON SOURCE LINES 49-53 .. code-block:: Python solver = factory.getOptimizationAlgorithm() solver.setStartingPoint(startingPoint) factory.setOptimizationAlgorithm(solver) .. GENERATED FROM PYTHON SOURCE LINES 54-55 Estimate the parametric model. .. GENERATED FROM PYTHON SOURCE LINES 55-58 .. code-block:: Python distribution = factory.build(sample) str(distribution) .. rst-class:: sphx-glr-script-out .. code-block:: none 'Normal(mu = 3.94738, sigma = 1.52392)' .. GENERATED FROM PYTHON SOURCE LINES 59-60 Retrieve the estimated parameters. .. GENERATED FROM PYTHON SOURCE LINES 60-61 .. code-block:: Python print(distribution.getParameter()) .. rst-class:: sphx-glr-script-out .. code-block:: none [3.94738,1.52392] .. _sphx_glr_download_auto_data_analysis_distribution_fitting_plot_maximumlikelihood_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_maximumlikelihood_estimator.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_maximumlikelihood_estimator.py `