Negative
loglikelihood functions for supported Statistics and Machine
Learning Toolbox™ distributions all end with `like`

, as in `explike`

. Each function represents a parametric family of distributions.
Input arguments are lists of parameter values specifying a particular member of the
distribution family followed by an array of data. Functions return the negative
loglikelihood of the parameters, given the data.

To find maximum likelihood estimates (MLEs), you can use a negative loglikelihood function as
an objective function of the optimization problem and solve it by using the MATLAB^{®} function `fminsearch`

or functions in Optimization
Toolbox™ and Global Optimization
Toolbox. These functions allow you to choose a search algorithm and exercise
low-level control over algorithm execution. By contrast, the `mle`

function and the distribution fitting functions that end with
`fit`

, such as `normfit`

and `gamfit`

, use preset algorithms with
options limited to those set by the `statset`

function.

Likelihoods are conditional probability densities. You can specify a parametric family of
distributions by using a function
*f*(*x*,*a*), where
*x* and *a* represent a random variable and
parameters, respectively. When *a* is fixed, the function is the
probability density function (pdf) at *x*,
*f*(*x*|*a*). When
*x* is fixed, the function is the *likelihood*
of the parameters *a*,
*f*(*a*|*x*). The joint
likelihood of the parameters over an independent random sample data set
*X* is

$$L(a)={\displaystyle \prod _{x\in X}f(a|x)}.$$

Given *X*, MLEs maximize *L*(*a*) over all
possible *a*. Numerical algorithms find MLEs that (equivalently)
maximize the loglikelihood function, log(*L*(*a*)).
The logarithm transforms the product of potentially small likelihoods into a sum of
logs, which is easier to distinguish from 0 in computation. For convenience, Statistics and Machine
Learning Toolbox negative loglikelihood functions return the *negative*
of this sum because optimization algorithms typically search for minima rather than
maxima.

This example shows how to find MLEs by using the `gamlike`

and `fminsearch`

functions.

Use the `gamrnd`

function to generate a random sample from a specific Gamma Distribution.

rng default; % For reproducibility a = [1,2]; X = gamrnd(a(1),a(2),1e3,1);

Visualize the likelihood surface in the neighborhood of `a`

given `X`

by using the `gamlike`

function.

mesh = 50; delta = 0.5; a1 = linspace(a(1)-delta,a(1)+delta,mesh); a2 = linspace(a(2)-delta,a(2)+delta,mesh); logL = zeros(mesh); % Preallocate memory for i = 1:mesh for j = 1:mesh logL(i,j) = gamlike([a1(i),a2(j)],X); end end [A1,A2] = meshgrid(a1,a2); surfc(A1,A2,logL)

Search for the minimum of the likelihood surface by using the `fminsearch`

function.

```
LL = @(u)gamlike([u(1),u(2)],X); % Likelihood given X
MLES = fminsearch(LL,[1,2])
```

`MLES = `*1×2*
0.9980 2.0172

Compare `MLES`

to the estimates returned by the `gamfit`

function.

ahat = gamfit(X)

`ahat = `*1×2*
0.9980 2.0172

The difference of each parameter between `MLES`

and `ahat`

is less than `1e-4`

.

Add the MLEs to the surface plot.

hold on plot3(MLES(1),MLES(2),LL(MLES),'ro','MarkerSize',5,'MarkerFaceColor','r') view([-60 40]) % Rotate to show the minimum

`fminsearch`

| `negloglik`

| `statset`

| `surfc`