Note
Go to the end to download the full example code.
Fit a parametric distribution¶
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 Parametric Estimation).
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
ot.Log.Show(ot.Log.NONE)
The Normal distribution¶
The parameters are estimated by the method of moments.
We consider a sample, here created from a standard Normal
distribution :
sample = ot.Normal().getSample(1000)
We can estimate a Normal distribution with NormalFactory
:
distribution = ot.NormalFactory().build(sample)
We take a look at the estimated parameters with the getParameter method :
print(distribution.getParameter())
[-0.00456302,0.961816]
We draw the fitted distribution
graph = distribution.drawPDF()
graph.setTitle("Fitted Normal distribution")
view = viewer.View(graph)
The Student distribution¶
The parameters of the Student law are estimated by a mixed method of moments and reduces MLE.
We generate a sample from a Student
distribution with parameters , and a scale parameter .
sample = ot.Student(5.0, -0.5, 2.0).getSample(1000)
We use the factory to build an estimated distribution :
distribution = ot.StudentFactory().build(sample)
We can obtain the estimated parameters with the getParameter method :
print(distribution.getParameter())
[4.80998,-0.651099,1.85308]
Draw fitted distribution
graph = distribution.drawPDF()
graph.setTitle("Fitted Student distribution")
view = viewer.View(graph)
The Pareto distribution¶
By default the parameters of the Pareto
distribution are estimated by least squares.
We use a sample from a Pareto distribution with a scale parameter , a shape parameter and a location parameter .
sample = ot.Pareto(1.0, 1.0, 0.0).getSample(1000)
Draw fitted distribution
distribution = ot.ParetoFactory().build(sample)
print(distribution.getParameter())
graph = distribution.drawPDF()
graph.setTitle("Fitted Pareto distribution")
view = viewer.View(graph)
plt.show()
[0.739463,0.861031,0.253954]