Main Content

gamlike

Gamma negative loglikelihood

Description

nlogL = gamlike(params,x) returns the gamma negative loglikelihood of the distribution parameters (params) given the sample data (x). params(1) and params(2) are the shape parameter a and scale parameter b, respectively.

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

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

[nlogL,aVar] = gamlike(___) also returns the inverse of the Fisher information matrix aVar, using any of the input argument combinations in the previous syntaxes. If the values in params are the maximum likelihood estimates (MLEs) of the parameters, the diagonal elements of aVar are the asymptotic variances of their respective parameters. aVar is based on the observed Fisher information, not the expected information.

gamlike is a utility function for maximum likelihood estimation of the gamma distribution. Because gamlike returns the negative gamma loglikelihood function, minimizing gamlike using fminsearch is the same as maximizing the likelihood.

example

Examples

collapse all

Find the maximum likelihood estimates (MLEs) of a random data set drawn from the gamma distribution by using the mle function, and then find the negative loglikelihood of the MLEs by using the gamlike function.

Generate 1000 random numbers from the gamma distribution with shape parameter a=5 and scale parameter b=3.

rng(0,"twister") % For reproducibility
n = 1000; % Number of samples
a = 5;
b = 3;
x = gamrnd(a,b,[n,1]);

Find the MLEs for the distribution parameters.

pHat = mle(x,Distribution="Gamma")
pHat = 1×2

    5.0445    2.9896

Compute the negative loglikelihood of the MLEs and the inverse of Fisher's information matrix.

[nlogL,aVar] = gplike(pHat,x)
nlogL = 
4.9076e+03
aVar = 2×2

   -0.0837    0.0349
    0.0349    0.0313

Because pHat contains MLE values, the gamlike function returns their asymptotic variances in the diagonal elements of aVar.

Input Arguments

collapse all

Gamma distribution parameters, specified as a vector of two numeric values. params(1) and params(2) are the shape and scale values, respectively. params(2) must be positive.

Data Types: single | double

Sample data, specified as a numeric 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 the 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

gamlike is a function specific to the gamma 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",@gampdf) returns the asymptotic covariance matrix of the MLEs for the gamma distribution.

  • Create a GammaDistribution 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.

Extended Capabilities

expand all

Version History

Introduced before R2006a