# normlike

Normal negative loglikelihood

## Description

example

nlogL = normlike(params,x) returns the normal negative loglikelihood of the distribution parameters (params) given the sample data (x). params(1) and params(2) correspond to the mean and standard deviation of the normal distribution, respectively.

example

nlogL = normlike(params,x,censoring) specifies whether each value in x is right-censored or not. Use the logical vector censoring in which 1 indicates observations that are right-censored and 0 indicates observations that are fully observed.

nlogL = normlike(params,x,censoring,freq) specifies the frequency or weights of observations. To specify freq without specifying censoring, you can pass [] for censoring.

example

[nlogL,aVar] = normlike(___) also returns the inverse of the Fisher information matrix aVar, using any of the input argument combinations in the previous syntaxes. If values in params are the maximum likelihood estimates (MLEs) of the parameters, aVar is an approximation to the asymptotic covariance matrix.

## Examples

collapse all

Find the MLEs of a data set with censoring by using normfit, and then find the negative loglikelihood of the MLEs by using normlike.

The first column of the data contains the lifetime (in hours) of two types of bulbs. The second column contains the binary variable indicating whether the bulb is fluorescent or incandescent. 1 indicates that the bulb is fluorescent, and 0 indicates that the bulb is incandescent. The third column contains the censorship information, where 0 indicates the bulb is observed until failure, and 1 indicates the item (bulb) is censored.

Find the indices for fluorescent bulbs.

idx = find(lightbulb(:,2) == 0);

Find the MLEs of the normal distribution parameters. The second input argument of normfit specifies the confidence level. Pass in [] to use its default value 0.05. The third input argument specifies the censorship information.

censoring = lightbulb(idx,3) == 1;
[muHat,sigmaHat] = normfit(lightbulb(idx,1),[],censoring)
muHat = 9.4966e+03
sigmaHat = 3.0640e+03

Find the negative loglikelihood of the MLEs.

nlogL = normlike([muHat,sigmaHat],lightbulb(idx,1),censoring)
nlogL = 376.2305

The function normfit finds the sample mean and the square root of the unbiased estimator of the variance with no censoring. The sample mean is equal to the MLE of the mean parameter, but the square root of the unbiased estimator of the variance is not equal to the MLE of the standard deviation parameter.

Find the normal distribution parameters by using normfit, convert them into MLEs, and then compare the negative log likelihoods of the estimates by using normlike.

Generate 100 normal random numbers from the standard normal distribution.

rng('default') % For reproducibility
n = 100;
x = normrnd(0,1,[n,1]);

Find the sample mean and the square root of the unbiased estimator of the variance.

[muHat,sigmaHat] = normfit(x)
muHat = 0.1231
sigmaHat = 1.1624

Convert the square root of the unbiased estimator of the variance into the MLE of the standard deviation parameter.

sigmaHat_MLE = sqrt((n-1)/n)*sigmaHat
sigmaHat_MLE = 1.1566

The difference between sigmaHat and sigmaHat_MLE is negligible for large n.

Alternatively, you can find the MLEs by using the function mle.

phat = mle(x)
phat = 1×2

0.1231    1.1566

phat(1) and phat(2) are the MLEs of the mean and the standard deviation parameter, respectively.

Confirm that the log likelihood of the MLEs (muHat and sigmaHat_MLE) is greater than the log likelihood of the unbiased estimators (muHat and sigmaHat) by using the normlike function.

logL = -normlike([muHat,sigmaHat],x)
logL = -156.4424
logL_MLE = -normlike([muHat,sigmaHat_MLE],x)
logL_MLE = -156.4399

Find the maximum likelihood estimates (MLEs) of the normal distribution parameters, and then find the confidence interval of the corresponding inverse cdf value.

Generate 1000 normal random numbers from the normal distribution with mean 5 and standard deviation 2.

rng('default') % For reproducibility
n = 1000; % Number of samples
x = normrnd(5,2,[n,1]);

Find the MLEs for the distribution parameters (mean and standard deviation) by using mle.

phat = mle(x)
phat = 1×2

4.9347    1.9969

muHat = phat(1);
sigmaHat = phat(2);

Estimate the covariance of the distribution parameters by using normlike. The function normlike returns an approximation to the asymptotic covariance matrix if you pass the MLEs and the samples used to estimate the MLEs.

[~,pCov] = normlike([muHat,sigmaHat],x)
pCov = 2×2

0.0040   -0.0000
-0.0000    0.0020

Find the inverse cdf value at 0.5 and its 99% confidence interval.

[x,xLo,xUp] = norminv(0.5,muHat,sigmaHat,pCov,0.01)
x = 4.9347
xLo = 4.7721
xUp = 5.0974

x is the inverse cdf value using the normal distribution with the parameters muHat and sigmaHat. The interval [xLo,xUp] is the 99% confidence interval of the inverse cdf value evaluated at 0.5, considering the uncertainty of muHat and sigmaHat using pCov. The 99% confidence interval means the probability that [xLo,xUp] contains the true inverse cdf value is 0.99.

## Input Arguments

collapse all

Normal distribution parameters consisting of the mean and standard deviation, specified as a vector of two numeric values. params(1) and params(2) correspond to the mean and standard deviation of the normal distribution, respectively. params(2) must be positive.

Example: [0,1]

Data Types: single | double

Sample data, specified as a vector.

Data Types: single | double

Indicator for the censoring of each value in x, specified as a logical vector of the same size as x. Use 1 for observations that are right-censored and 0 for observations that are fully observed.

The default is an array of 0s, meaning that all observations are fully observed.

Data Types: logical

Frequency or weights of observations, specified as a nonnegative vector that is the same size as x. The freq input argument typically contains nonnegative integer counts for the corresponding elements in x, but can contain any nonnegative values.

To obtain the weighted negative loglikelihood for a data set with censoring, specify weights of observations, normalized to the number of observations in x.

The default is an array of 1s, meaning one observation per element of x.

Data Types: single | double

## Output Arguments

collapse all

Negative loglikelihood value of the distribution parameters (params) given the sample data (x), returned as a numeric scalar.

Inverse of the Fisher information matrix, returned as a 2-by-2 numeric matrix. aVar is based on the observed Fisher information given the observed data (x), not the expected information.

If values in params are the MLEs of the parameters, aVar is an approximation to the asymptotic variance-covariance matrix (also known as the asymptotic covariance matrix). To find the MLEs, use mle.

## Alternative Functionality

normlike is a function specific to normal distribution. Statistics and Machine Learning Toolbox™ also offers the generic functions mlecov, fitdist, negloglik, and proflik and the Distribution Fitter app, which support various probability distributions.

• mlecov returns the asymptotic covariance matrix of the MLEs of the parameters for a distribution specified by a custom probability density function. For example, mlecov(params,x,'pdf',@normpdf) returns the asymptotic covariance matrix of the MLEs for the normal distribution.

• Create a NormalDistribution probability distribution object by fitting the distribution to data using the fitdist function or the Distribution Fitter app. The object property ParameterCovariance stores the covariance matrix of the parameter estimates. To obtain the negative loglikelihood of the parameter estimates and the profile of the likelihood function, pass the object to negloglik and proflik, respectively.

## References

[1] Evans, M., N. Hastings, and B. Peacock. Statistical Distributions. 2nd ed. Hoboken, NJ: John Wiley & Sons, Inc., 1993.

[2] Lawless, J. F. Statistical Models and Methods for Lifetime Data. Hoboken, NJ: Wiley-Interscience, 1982.

[3] Meeker, W. Q., and L. A. Escobar. Statistical Methods for Reliability Data. Hoboken, NJ: John Wiley & Sons, Inc., 1998.

## Version History

Introduced before R2006a