Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

## Negative Loglikelihood Functions

Negative loglikelihood functions for supported Statistics 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 log-likelihood of the parameters, given the data.

Negative log-likelihood functions are used as objective functions in search algorithms such as the one implemented by the MATLAB® function fminsearch. Additional search algorithms are implemented by Optimization Toolbox™ functions and Global Optimization Toolbox functions.

When used to compute maximum likelihood estimates (MLEs), negative log-likelihood functions allow you to choose a search algorithm and exercise low-level control over algorithm execution. By contrast, the functions discussed in Maximum Likelihood Estimation use preset algorithms with options limited to those set by the statset function.

Likelihoods are conditional probability densities. A parametric family of distributions is specified by its pdf f(x,a), where x and a represent the variables and parameters, respectively. When a is fixed, the pdf is used to compute the density at x, f(x|a). When x is fixed, the pdf is used to compute the likelihood of the parameters a, f(a|x). The joint likelihood of the parameters over an independent random sample X is

Given X, MLEs maximize L(a) over all possible a.

In numerical algorithms, the log-likelihood function, log(L(a)), is (equivalently) optimized. 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 Toolbox negative log-likelihood functions return the negative of this sum, since the optimization algorithms to which the values are passed typically search for minima rather than maxima.

For example, use gamrnd 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);
```

Given X, the gamlike function can be used to visualize the likelihood surface in the neighborhood of a:

```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)
```

The MATLAB function fminsearch is used to search for the minimum of the likelihood surface:

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

0.9980    2.0172

```

These can be compared to the MLEs returned by the gamfit function, which uses a combination search and solve algorithm:

```ahat = gamfit(X)
```
```ahat =

0.9980    2.0172

```

The MLEs can be added to the surface plot (rotated to show the minimum):

```hold on
plot3(MLES(1),MLES(2),LL(MLES),...
'ro','MarkerSize',5,...
'MarkerFaceColor','r')
```