1.2 Maximum likelihood estimation

In this section we discuss how to find the maximum likelihood estimate. As in the previous section we consider an example:

We will assume that time until something happens is exponentially distributed with parameter \(\lambda\). Therefore the probability density of observing \(T=t\) is \[ p_T(t) = \lambda e^{-\lambda t}. \] On the other hand, we can also think of this as the likelihood function of the parameter \(\lambda\), \[ L(\lambda) = \lambda e^{-\lambda t}. \] If we have \(n\) independent observations, the joint density of these observations is the product of the marginal densities. Therefore the likelihood is also the product of the marginal likelihoods. That is, if we write \(L_n(\lambda)\) for the likelihood of \(n\) observations, \[ L_n(\lambda) = \prod_{i=1}^n \lambda e^{-\lambda t_i}. \]

Many times it is easier to instead consider the logarithm of the likelihood, the log-likelihood, \[ l_n(\lambda):=\ln L_n(\lambda) = \sum_{i=1}^n( \ln \lambda - \lambda t_i ) = n\ln\lambda - \lambda\sum_{i=1}^n t_i= n(\ln\lambda - \lambda \bar t) , \] where we as usual let \(\bar t = \frac{1}{n}\sum_{i=1}^n t_i\).

Let us say we observe a sample of size 100.
Histogram of the sample

Figure 1.2: Histogram of the sample

We define a function in R that calculates the log-likelihood

logLn <- function(lambda, data){
  n <- length(data)
  tbar <- mean(data)
  
  n*(log(lambda) - lambda*tbar)
}

Then we may calculate the log-likelihood of, for example, \(\lambda = 0.1\),

logLn(0.1, t)
## [1] -316.1482

Here the variable t is a vector that contains the observations.

Let us plot the log-likelihood for a range of \(\lambda\)-values.

## Warning in geom_point(aes(x = 1/mean(t), y = logLn(1/mean(t), t)), colour = palette.colors(2), : All aesthetics have length 1, but the data has 2 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
##   a single row.
Log likelihood of the sample

Figure 1.3: Log likelihood of the sample

We can find the maximum likelihood estimate as the \(\lambda\) that maximizes the likelihood (or log likelihood). That is:

\[ \hat\lambda = \operatorname*{argmax}_\lambda L_n(\lambda) = \operatorname*{argmax}_\lambda l_n(\lambda). \]

Since the likelihood is simple, we can find \(\hat \lambda\) directly: \[ \partial_\lambda l_n(\lambda)\Big|_{\lambda = \hat\lambda} = n\left(\frac{1}{\hat \lambda} - \bar t\right) = 0. \] With solution \(\hat\lambda =1/ \bar t\). For this sample:

lambdaHat <- 1/mean(t)
lambdaHat
## [1] 0.1164284

We may also find the estimate using numerical optimization.

optimResult <- optimise(
  logLn,
  lower = 0.01, 
  upper = 10.0,
  data = t,
  maximum = TRUE)
optimResult$maximum
## [1] 0.1164178