Note
Click here to download the full example code
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 according to a parametric density function is:
from __future__ import print_function
import openturns as ot
import math as m
ot.Log.Show(ot.Log.NONE)
Create data from a normal PDF with , .
sample = ot.Normal(4.0, 1.5).getSample(200)
Create the search interval of (, ) : the constraint is .
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 : the first point,
for : a value evaluated from the two first points.
mu0 = sample[0][0]
sigma0 = m.sqrt((sample[1][0] - sample[0][0]) * (sample[1][0] - sample[0][0]))
startingPoint = [mu0, sigma0]
Create the estimator from a parametric PDF.
pdf = ot.Normal()
factory = ot.MaximumLikelihoodFactory(pdf)
factory.setOptimizationBounds(bounds)
Set the starting point via the solver.
solver = factory.getOptimizationAlgorithm()
solver.setStartingPoint(startingPoint)
factory.setOptimizationAlgorithm(solver)
Estimate the parametric model.
distribution = factory.build(sample)
str(distribution)
Out:
'Normal(mu = 3.94775, sigma = 1.49821)'
Retrieve the estimated parameters.
print(distribution.getParameter())
Out:
[3.94775,1.49821]
Total running time of the script: ( 0 minutes 0.014 seconds)