 Example: Maximum Likelihood Estimation - Maple Help

Home : Support : Online Help : Mathematics : Optimization : GlobalOptimization Package : Example: Maximum Likelihood Estimation

Maximum Likelihood Estimation Introduction

The likelihood function describes how closely a probability distribution describes a data set.  This application will demonstrate how to:

 • Generate a data set according to a Weibull distribution with a specified scale and shape parameter.
 • Backsolve the scale and shape parameters for this data set by maximizing the likelihood function using the Global Optimization Toolbox.
 • Compare the resulting probability distribution against a histogram of the data set. Theory The Weibull probability distribution has the following density function: $\frac{\mathrm{\beta }{\mathrm{t}}^{\mathrm{\beta }-1}{ⅇ}^{-{\left(\frac{\mathrm{t}}{\mathrm{\alpha }}\right)}^{\mathrm{\beta }}}}{{\mathrm{\alpha }}^{\mathrm{β}}},$   where α and β are the scale and shape parameters.  The likelihood that a data set fits this distribution is:   Taking the log of this gives:   Hence we derive the following expression describing the likelihood function: > $\mathrm{restart}:$
 > $\mathrm{with}\left(\mathrm{Statistics}\right):$$\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathrm{with}\left(\mathrm{GlobalOptimization}\right):\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathrm{with}\left(\mathrm{plots}\right):$ Generate Data Set of Random Numbers

Number of random numbers to generate

 > $n≔10000:$

Define the desired scale and shape parameters for the data set.

 > $\mathrm{α}\mathit{≔}73\mathit{:}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathrm{β}\mathit{≔}17\mathit{:}$

Generate a sample of random numbers according to a Weibull distribution

 > $\mathrm{Data}\mathit{:=}\mathrm{Sample}\left(\mathrm{RandomVariable}\left(\mathrm{Weibull}\left(\alpha \mathit{,}\beta \right)\right)\mathit{,}n\right)\mathit{:}$ Maximizing the Likelihood Function

The probability density of a Weibull distribution

 >

The likelihood function

 > $\mathrm{maxLike}\mathit{:=}\left(\mathrm{α}\mathit{,}\mathrm{β}\right)\mathit{→}\mathrm{add}\left(\mathrm{ln}\left(P\left({\mathrm{Data}}_{i}\mathit{,}\mathrm{α}\mathit{,}\mathrm{β}\right)\right)\mathit{,}i\mathit{=}\mathit{1}\mathit{..}n\right)\mathit{:}$

Find the values of α and β that maximize the likelihood function with the Global Optimization Toolbox

 > $\mathrm{results}\mathit{:=}{\mathrm{GlobalSolve}\left(\mathrm{maxLike}\left(\mathrm{alpha_ML}\mathit{,}\mathrm{beta_ML}\right)\mathit{,}\mathrm{alpha_ML}\mathit{=}1..100,\mathrm{beta_ML}\mathit{=}1..100\mathit{,}\mathrm{maximize}\right)}_{}\left[2\right]\mathit{;}$
 ${\mathrm{results}}{:=}\left[{\mathrm{beta_ML}}{=}{17.1301969320734}{,}{\mathrm{alpha_ML}}{=}{72.9747976582517}\right]$ (5.1)
 >
 $\mathrm{α__GOT}{:=}{72.9747976582517}$
 $\mathrm{β__GOT}{:=}{17.1301969320734}$ (5.2)

These values match those used to generate the data set.

You can also match moments to find the shape and scale parameters

 >
 $\left\{{a}{=}{72.98206445}{,}{b}{=}{17.00542373}\right\}$ (5.3) Histogram

 > $\mathrm{plotopts}:=\mathrm{axesfont}=\left[\mathrm{Calibri}\right],\mathrm{labelfont}=\left[\mathrm{Calibri}\right],\mathrm{labels}=\left["t","Probability Density"\right],\mathrm{labeldirections}=\left[\mathrm{horizontal},\mathrm{vertical}\right]:$$\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathrm{p1}≔\mathrm{Histogram}\left(\mathrm{Data},\mathrm{color}="Gainsboro",\mathrm{plotopts}\right):$$\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathrm{p2}:=\mathrm{plot}\left(P\left(t,\mathrm{α},\mathrm{β}\right),t={\mathrm{sort}\left(\mathrm{Data}\right)}_{1}..{\mathrm{sort}\left(\mathrm{Data}\right)}_{n},\mathrm{color}="Black",\mathrm{legend}="Sample Parameters",\mathrm{plotopts}\right):$$\mathrm{p3}:=\mathrm{plot}\left(P\left(t,\mathrm{α__GOT},\mathrm{β__GOT}\right),t={\mathrm{sort}\left(\mathrm{Data}\right)}_{1}..{\mathrm{sort}\left(\mathrm{Data}\right)}_{n},\mathrm{color}="Red",\mathrm{legend}="Estimated Parameters",\mathrm{plotopts}\right):$$\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathrm{display}\left(\mathrm{p1},\mathrm{p2},\mathrm{p3},\mathrm{size}=\left[800,"golden"\right]\right)$ In the above plot, the red line corresponds to the estimated parameters based on the sample population and the black line corresponds to the desired sample parameters.