The data and one candidate curve

The likelihood

◆ The Blueprint
Likelihood and density

The density \(f(x \mid \theta)\) fixes the parameter and asks how likely each \(x\) is. The likelihood fixes the observed data and asks how likely they are under each \(\theta\). It is the same formula, read as a function of \(\theta\).

$$L(\theta) = \prod_{i=1}^{n} f(x_i \mid \theta) \qquad \ell(\theta) = \ln L(\theta) = \sum_{i=1}^{n} \ln f(x_i \mid \theta)$$

The product assumes the observations are independent. For discrete data, \(f\) is a probability; for continuous data it is a density, so \(L\) is not a probability and need not be below 1.

The recipe

Write the model's density \(f\), multiply the densities into \(L\), and take logs. Set each partial derivative of \(\ell\) to zero and solve, numerically when there is no closed form. Then check that the solution is a maximum: a negative second derivative with one parameter, or a negative definite Hessian with several.

This tab's model

Normal with known \(\sigma = 1\), so \(\mu\) is the only unknown.

$$\ell(\mu) = -\tfrac{n}{2}\ln(2\pi\sigma^2) - \frac{1}{2\sigma^2}\sum (x_i-\mu)^2, \qquad \frac{d\ell}{d\mu} = \frac{1}{\sigma^2}\sum (x_i-\mu) = 0 \;\Rightarrow\; \hat\mu = \bar x, \qquad \frac{d^2\ell}{d\mu^2} = -\frac{n}{\sigma^2} < 0$$
Why the log matters numerically

A double cannot store a positive number below about \(10^{-324}\). The likelihood of 2,000 points is far smaller than that, so a computer stores \(L\) as 0, while \(\ell\) is an ordinary number. Every fitting routine maximizes \(\ell\) for this reason.

Invariance

The MLE of any function of \(\theta\) is that function of \(\hat\theta\). The Anvil uses this: the MLE of a B life (the hours by which a given percent have failed) is the model's quantile at the MLE.

In R

optimize(function(mu) sum(dnorm(x, mu, 1, log = TRUE)), c(0, 10), maximum = TRUE) finds the same peak numerically. Part 1 of The Script.

The lives and the candidate curve

Log-likelihood

◆ The Blueprint
The derivation in one block
$$\begin{aligned} L(\theta) &= \prod_{i=1}^{n} \frac{1}{\theta} e^{-x_i/\theta} = \theta^{-n} e^{-\sum x_i/\theta} \\ \ell(\theta) &= -n \ln\theta - \frac{\sum x_i}{\theta} \\ \ell'(\theta) &= -\frac{n}{\theta} + \frac{\sum x_i}{\theta^2} = 0 \;\Rightarrow\; \hat\theta = \bar x \\ \ell''(\hat\theta) &= \frac{n}{\hat\theta^2} - \frac{2\sum x_i}{\hat\theta^3} = -\frac{n}{\hat\theta^2} < 0 \end{aligned}$$
Standard error from the second derivative

The observed information is \(I(\hat\theta) = -\ell''(\hat\theta)\). For large \(n\), \(\hat\theta\) is approximately normal with variance \(1/I(\hat\theta)\). That gives the Wald interval.

$$\text{SE}(\hat\theta) = \frac{1}{\sqrt{I(\hat\theta)}} = \frac{\hat\theta}{\sqrt{n}}, \qquad \hat\theta \pm 1.96\,\text{SE}$$

The parabola on the plot is \(\hat\ell - (\theta - \hat\theta)^2 / (2\,\text{SE}^2)\): the quadratic with the same peak and second derivative as \(\ell\). Where \(\ell\) is lopsided (here, steeper on the left), the Wald interval is symmetric anyway. A likelihood-ratio interval follows the curve instead.

Rate or mean

R writes the exponential with rate \(\lambda = 1/\theta\): dexp(x, rate). By invariance, \(\hat\lambda = 1/\bar x\). fitdistrplus reports the rate.

In R

mean(x) is the MLE. fitdist(x / 1000, "exp") reports the rate per thousand hours; 1000 / estimate converts it back. Part 2 of The Script.

Bearing lives

Log-likelihood map

Newton's method for the shape
Peak log-likelihoods compared
Probability plots

◆ The Blueprint
Two parameters, two score equations

With \(\theta = (\theta_1, \theta_2)\), the MLE solves \(\partial\ell/\partial\theta_1 = 0\) and \(\partial\ell/\partial\theta_2 = 0\) together. For the lognormal, both equations solve in closed form. For the Weibull and the gamma, one equation gives the scale as a function of the shape, and substituting it leaves one equation in the shape (the score of the profile likelihood).

Lognormal (closed form)
$$\hat\mu = \frac1n\sum \ln x_i, \qquad \hat\sigma^2 = \frac1n\sum(\ln x_i - \hat\mu)^2$$
Weibull
$$\hat\eta(\beta) = \Big(\frac1n \sum x_i^{\beta}\Big)^{1/\beta}, \qquad h(\beta) = \frac{1}{\beta} + \frac1n\sum\ln x_i - \frac{\sum x_i^{\beta}\ln x_i}{\sum x_i^{\beta}} = 0$$

\(h'(\beta) = -1/\beta^2 - V_\beta\), where \(V_\beta\) is the variance of \(\ln x_i\) under weights proportional to \(x_i^\beta\). It is always negative, so \(h\) has a single root.

Gamma
$$\hat\theta(\alpha) = \frac{\bar x}{\alpha}, \qquad \ln\alpha - \psi(\alpha) = \ln\bar x - \frac1n\sum\ln x_i$$

\(\psi\) is the digamma function, \(\psi(\alpha) = \frac{d}{d\alpha}\ln\Gamma(\alpha)\), and \(\psi'\) is the trigamma. The right side is at least 0 (the arithmetic mean is at least the geometric mean), and \(\ln\alpha - \psi(\alpha)\) falls from \(\infty\) to 0, so there is exactly one root.

Newton's method

To solve \(g(s) = 0\): replace \(g\) by its tangent line at the current \(s\) and solve that line. \(s_{\text{new}} = s - g(s)/g'(s)\). Near the root, the number of correct digits roughly doubles each step.

Comparing models
$$\text{AIC} = 2k - 2\hat\ell, \qquad \text{BIC} = k\ln n - 2\hat\ell, \qquad \text{evidence ratio} = e^{\Delta\text{AIC}/2}$$

Nested models (exponential inside Weibull at \(\beta = 1\), and inside gamma at \(\alpha = 1\)) also allow the likelihood ratio test: \(2(\hat\ell_{\text{big}} - \hat\ell_{\text{small}})\) is approximately \(\chi^2\) with df equal to the number of extra parameters. Non-nested models are compared with AIC or BIC.

In R

fitdist(x, "weibull"), "gamma", and "lnorm" fit each family; summary() prints estimates, standard errors, logLik, and AIC; denscomp() and qqcomp() draw the comparisons. The Newton loops are written out in part 3 of The Script.

Part lives

Models ranked by AIC

Three ways to handle running parts
Share failed by each age

Probability plots, log scales

B life under each model

◆ The Blueprint
The performance metric

Within one family, MLE maximizes \(\ell(\theta)\). Across families, compare the maxima \(\hat\ell\), charged for parameters: AIC \(= 2k - 2\hat\ell\), lower is better. Only differences matter. With equal \(k\), \(\Delta\text{AIC} = 2\Delta\hat\ell\), and \(e^{\Delta\text{AIC}/2}\) is how many times as likely the data are under the leader.

What AIC does not tell you

AIC ranks the candidates you gave it; it says nothing about whether any of them fits well. The probability plot shows where a model misses. Goodness-of-fit tests (Kolmogorov-Smirnov, Anderson-Darling) ask whether one family fits. With thousands of real observations they often reject every family, because no family is exactly right. gofstat() in fitdistrplus prints these statistics beside AIC and BIC.

B lives and their intervals

\(B_p\) is the \(p\) quantile: the age by which a share \(p\) of parts have failed. By invariance its MLE is the fitted model's quantile. The interval uses the delta method on the log scale:

$$\text{SE}(\ln \hat B_p) = \sqrt{\nabla^\top V \nabla}, \qquad \hat B_p\, e^{\pm 1.96\,\text{SE}}$$

\(V\) is the inverse of the observed information (minus the Hessian of \(\ell\) at the peak), and \(\nabla\) is the gradient of \(\ln B_p\) in the parameters. bootdist() gives a bootstrap interval instead; for the Weibull \(B_1\) here the two agree within an hour.

Parts still running (right censoring)

A failed part contributes its density \(f(t_i)\). A running part at age \(t_i\) contributes only the probability that its life exceeds \(t_i\), the survival function \(S(t_i)\).

$$\ell(\beta, \eta) = \sum_{\text{failed}} \ln f(t_i) + \sum_{\text{running}} \ln S(t_i), \qquad S(t) = e^{-(t/\eta)^\beta}$$

For the Weibull the scale still solves in closed form, \(\hat\eta^\beta = \frac1r\sum_{\text{all}} t_i^\beta\) with \(r\) failures, and the shape solves \(\frac1\beta + \frac1r\sum_{\text{failed}}\ln t_i - \frac{\sum_{\text{all}} t_i^\beta\ln t_i}{\sum_{\text{all}} t_i^\beta} = 0\).

The simulation

Lives: Weibull with \(\beta = 1.8\) and \(\eta = 1{,}500\) hours, seed 2026. Ages on the record date: uniform from 0 to 3,000 hours, seed 2027. The B life chart marks the true values this implies.

In R

fitdist() for each family, gofstat() for the table, quantile(fit, probs = 0.01) for B lives, bootdist() for intervals, and fitdistcens() for running parts. Part 4 of The Script.

fitdistrplus, function by function
CallWhat it returns
fitdist(x, "weibull")The MLE. Families: "exp", "weibull", "gamma", "lnorm", or any with a d function.
summary(fit)Estimates, standard errors from the observed information, logLik, AIC, and BIC.
gofstat(list_of_fits)AIC and BIC side by side, plus Kolmogorov-Smirnov, Cramér-von Mises, and Anderson-Darling statistics.
denscomp(), qqcomp(), cdfcomp(), ppcomp()Fitted densities over the histogram, and probability plots for several fits at once.
quantile(fit, probs = 0.01)A fitted quantile, such as the B1 life.
bootdist(fit)Parametric bootstrap. quantile() on it gives intervals for quantiles.
fitdistcens(df, "weibull")The MLE with censored data. Columns left and right; right = NA marks a part still running.
mle_in_R.R