Algorithm Search⌘K

Definitioncore

Deviance

Also called residual deviance, scaled deviance.

Twice the log-likelihood gap between the saturated model and a fitted model. It plays the part of the residual sum of squares for a generalized linear model.

The deviance of a fitted generalized linear model is twice the difference between the log-likelihood of the saturated model, which fits every observation exactly, and the log-likelihood of the fitted model, multiplied by the dispersion. For a normal model with the identity link it is the residual sum of squares. The scaled deviance is the same quantity divided by the dispersion. The residual deviance is the deviance of the fitted model, and the null deviance is the deviance of the model with only an intercept. The difference of the scaled deviances of two nested models fitted to the same data is the likelihood ratio statistic of the smaller model against the larger one. The deviance of one model alone is a test of fit only in special cases.

Definition

Let y1,…,yny_1, \dots, y_n be independent, and let yiy_i have a density or mass function from an exponential dispersion family,

f(y;θi,ϕ)=exp⁡{wi(yθi−b(θi))ϕ+c(y,ϕ/wi)},f(y; \theta_i, \phi) = \exp\Bigl\{\frac{w_i\bigl(y\theta_i - b(\theta_i)\bigr)}{\phi} + c(y, \phi/w_i)\Bigr\},

with known prior weights wi>0w_i > 0, dispersion ϕ>0\phi > 0, mean μi=b′(θi)\mu_i = b'(\theta_i) and variance ϕV(μi)/wi\phi V(\mu_i)/w_i (see exponential family distribution). Write l(μ;y)l(\mu; y) for the log-likelihood as a function of the vector of means. A model with pp coefficients (the coefficient vector is bb, and b^\hat b is its maximum likelihood estimate) gives the fitted means μ^\hat\mu. The saturated model has one free mean per observation, so its fit is μ~=y\tilde\mu = y. The unit deviance is

d(y,μ)=2∫μyy−tV(t) dt=2[y(θ~−θ)−b(θ~)+b(θ)],d(y, \mu) = 2\int_\mu^y \frac{y - t}{V(t)}\,dt = 2\bigl[y(\tilde\theta - \theta) - b(\tilde\theta) + b(\theta)\bigr],

where θ\theta and θ~\tilde\theta are the natural parameters with means μ\mu and yy. The scaled deviance and the deviance are

D∗=2[l(y;y)−l(μ^;y)]=1ϕ∑i=1nwi d(yi,μ^i),D=ϕD∗=∑i=1nwi d(yi,μ^i).D^* = 2\bigl[l(y; y) - l(\hat\mu; y)\bigr] = \frac{1}{\phi}\sum_{i=1}^n w_i\,d(y_i, \hat\mu_i), \qquad D = \phi D^* = \sum_{i=1}^n w_i\,d(y_i, \hat\mu_i).

The deviance DD does not depend on ϕ\phi. For the Poisson and binomial families ϕ=1\phi = 1, so D=D∗D = D^*. The unit deviances of the common families are as follows, with 0log⁡0=00\log 0 = 0:

family V(μ)V(\mu) d(y,μ)d(y, \mu)
normal 11 (y−μ)2(y - \mu)^2
Poisson μ\mu 2[ylog⁡(y/μ)−(y−μ)]2\bigl[y\log(y/\mu) - (y - \mu)\bigr]
binomial, yy a proportion of mm trials, w=mw = m μ(1−μ)\mu(1 - \mu) 2[ylog⁡(y/μ)+(1−y)log⁡1−y1−μ]2\bigl[y\log(y/\mu) + (1 - y)\log\frac{1 - y}{1 - \mu}\bigr]
gamma μ2\mu^2 2[−log⁡(y/μ)+(y−μ)/μ]2\bigl[-\log(y/\mu) + (y - \mu)/\mu\bigr]
inverse Gaussian μ3\mu^3 (y−μ)2/(μ2y)(y - \mu)^2/(\mu^2y)

Facts.

  1. d(y,μ)≥0d(y, \mu) \ge 0, with equality only at y=μy = \mu. More exactly, w d(y,μ)/ϕ=2 KL(fθ~ ∥ fθ)w\,d(y, \mu)/\phi = 2\,\mathrm{KL}(f_{\tilde\theta} \,\|\, f_\theta), twice the Kullback-Leibler divergence from the member with mean μ\mu to the member with mean yy. Proof: log⁡fθ~(Y)−log⁡fθ(Y)=w[Y(θ~−θ)−b(θ~)+b(θ)]/ϕ\log f_{\tilde\theta}(Y) - \log f_\theta(Y) = w[Y(\tilde\theta - \theta) - b(\tilde\theta) + b(\theta)]/\phi, and the expectation of YY under fθ~f_{\tilde\theta} is yy. So the scaled deviance is twice a sum of divergences, and the maximum likelihood fit minimizes it.
  2. For the normal family with the identity link, D=∑(yi−μ^i)2D = \sum(y_i - \hat\mu_i)^2 is the residual sum of squares and ϕ=σ2\phi = \sigma^2.
  3. A difference of deviances is a likelihood ratio statistic. Let M0⊂M1M_0 \subset M_1 be nested models with p0<p1p_0 < p_1 coefficients, fitted by maximum likelihood to the same data with the same ϕ\phi. Then D0∗−D1∗=2[l(μ^1;y)−l(μ^0;y)]D_0^* - D_1^* = 2[l(\hat\mu_1; y) - l(\hat\mu_0; y)]. Proof: in D0∗−D1∗=2[l(y;y)−l(μ^0;y)]−2[l(y;y)−l(μ^1;y)]D_0^* - D_1^* = 2[l(y; y) - l(\hat\mu_0; y)] - 2[l(y; y) - l(\hat\mu_1; y)] the saturated term is the same number and cancels. So under the conditions of Wilks' theorem, with p1p_1 fixed and n→∞n \to \infty, D0∗−D1∗D_0^* - D_1^* under M0M_0 tends in law to χp1−p02\chi^2_{p_1 - p_0}. In the normal case with known σ2\sigma^2 the χ2\chi^2 law is exact.
  4. Unknown dispersion. With ϕ\phi unknown, the usual test uses F=(D0−D1)/(p1−p0)ϕ^F = \frac{(D_0 - D_1)/(p_1 - p_0)}{\hat\phi} with ϕ^=D1/(n−p1)\hat\phi = D_1/(n - p_1) or, less biased for small counts, the Pearson estimate ϕ^=X2/(n−p1)\hat\phi = X^2/(n - p_1) with X2=∑wi(yi−μ^i)2/V(μ^i)X^2 = \sum w_i(y_i - \hat\mu_i)^2/V(\hat\mu_i). In the normal case this is the regression F-test and has the exact Fp1−p0, n−p1F_{p_1 - p_0,\, n - p_1} law. In the other families the F law is an approximation.
  5. The deviance of one model is not in general χn−p2\chi^2_{n - p}. The saturated model has nn parameters, so the number of parameters grows with the data and Wilks' theorem does not apply to D∗D^* itself. D∗D^* is close to χn−p2\chi^2_{n-p} under small-dispersion limits with nn fixed: Poisson counts whose means are all large, or binomial counts with all miμim_i\mu_i and mi(1−μi)m_i(1 - \mu_i) large.
  6. Binary data. For Bernoulli responses with the logit link, DD depends on the data only through μ^\hat\mu. Proof: for y∈{0,1}y \in \{0, 1\}, d(y,μ)=−2[yη−log⁡(1+eη)]d(y, \mu) = -2[y\eta - \log(1 + e^\eta)] with η=log⁡μ1−μ\eta = \log\frac{\mu}{1 - \mu}, so D=−2∑i[yiη^i−log⁡(1+eη^i)]D = -2\sum_i[y_i\hat\eta_i - \log(1 + e^{\hat\eta_i})]. With η^=Xb^\hat\eta = X\hat b, the score equations X⊤(y−μ^)=0X^\top(y - \hat\mu) = 0 give y⊤η^=(X⊤y)⊤b^=(X⊤μ^)⊤b^=μ^⊤η^y^\top\hat\eta = (X^\top y)^\top\hat b = (X^\top\hat\mu)^\top\hat b = \hat\mu^\top\hat\eta. So D=−2∑i[μ^iη^i−log⁡(1+eη^i)]D = -2\sum_i[\hat\mu_i\hat\eta_i - \log(1 + e^{\hat\eta_i})], which is fixed once μ^\hat\mu is fixed. It cannot measure how far yy is from μ^\hat\mu.
  7. Deviance residuals. ri=sign⁡(yi−μ^i)wi d(yi,μ^i)r_i = \operatorname{sign}(y_i - \hat\mu_i)\sqrt{w_i\,d(y_i, \hat\mu_i)} satisfy ∑ri2=D\sum r_i^2 = D. Pierce and Schafer (1986) showed that they are closer to normal than the Pearson residuals (yi−μ^i)wi/V(μ^i)(y_i - \hat\mu_i)\sqrt{w_i/V(\hat\mu_i)}, so they are the usual choice for residual plots.

Examples and non-examples

Eight counts. Take x=0,1,…,7x = 0, 1, \dots, 7 and the counts y=(1,1,3,2,5,6,9,12)y = (1, 1, 3, 2, 5, 6, 9, 12). The Poisson model with log⁡μi=b0+b1xi\log\mu_i = b_0 + b_1x_i, fitted by iteratively reweighted least squares, gives b^=(0.0254,0.3561)\hat b = (0.0254, 0.3561) and

μ^=(1.026,1.464,2.091,2.985,4.262,6.084,8.687,12.402).\hat\mu = (1.026, 1.464, 2.091, 2.985, 4.262, 6.084, 8.687, 12.402).

The fitted counts sum to 39, the same as the observed counts: this is the score equation of the intercept under the canonical link. The unit deviances are 0.001,0.166,0.348,0.368,0.121,0.001,0.011,0.0130.001, 0.166, 0.348, 0.368, 0.121, 0.001, 0.011, 0.013, so the residual deviance is D1=1.029D_1 = 1.029 on 8−2=68 - 2 = 6 degrees of freedom, and the Pearson statistic is X2=1.022X^2 = 1.022. The null model μ^i=39/8\hat\mu_i = 39/8 has deviance D0=22.586D_0 = 22.586 on 7 degrees of freedom. The log-likelihoods are l1=−13.0816l_1 = -13.0816 and l0=−23.8600l_0 = -23.8600, and

D0−D1=21.557=2(l1−l0),D_0 - D_1 = 21.557 = 2(l_1 - l_0),

as Fact 3 says. On χ12\chi^2_1 its p-value is 3.4×10−63.4 \times 10^{-6}, so the slope is needed. D1D_1 is small for 6 degrees of freedom, but the next example shows why that comparison is rough for counts this small.

The mean of a unit deviance for small counts. For one Poisson count YY with mean μ\mu, the expectation of d(Y,μ)d(Y, \mu) at the true mean is:

μ\mu 0.1 0.5 1 2 5 20
E d(Y,μ)\mathrm E\,d(Y, \mu) 0.474 1.007 1.147 1.139 1.047 1.009

The χn−p2\chi^2_{n-p} reference assumes a mean near 1 per observation. With many counts below 1 the deviance is too small on average, and with means between 1 and 5 it is too large by about 5 to 15%. Across 100 cells these errors add up to many units of the reference scale.

Non-example: deviance as a test of fit for binary data. Fit only an intercept to nn Bernoulli responses with ss successes. Then μ^=s/n\hat\mu = s/n and D=−2n[μ^log⁡μ^+(1−μ^)log⁡(1−μ^)]D = -2n[\hat\mu\log\hat\mu + (1 - \hat\mu)\log(1 - \hat\mu)]. For μ^=0.5\hat\mu = 0.5 this is 2nlog⁡2=1.386n2n\log 2 = 1.386n, and for μ^=0.1\hat\mu = 0.1 it is 0.650n0.650n, whatever the order of the zeros and ones. So D/(n−1)D/(n - 1) is about 1.39 or about 0.65 because of the success rate, not because of lack of fit. Fact 6 shows that the same holds for every logistic regression. Grouped binomial data with large mim_i, or a test that compares nested models, avoid the problem.

Non-example: deviances from different data. If one model drops the rows with a missing value of x3x_3 and the other keeps them, the saturated terms differ and D0−D1D_0 - D_1 is not a likelihood ratio statistic. Both models must be fitted to the same rows.

Non-example: deviances of models that are not nested. For a model with log⁡μ=b0+b1x\log\mu = b_0 + b_1x and another with log⁡μ=b0+b1log⁡(1+x)\log\mu = b_0 + b_1\log(1 + x), the difference of deviances is not χ2\chi^2 under either model. Compare such models by an information criterion or by cross-validation.

  • Null deviance. The deviance of the intercept-only model. 1−D1/D01 - D_1/D_0 is the fraction of deviance explained, the analogue of the coefficient of determination; for the eight counts it is 0.9540.954.
  • Akaike information criterion. For a fixed family and ϕ\phi, −2l(μ^;y)=D∗−2l(y;y)-2l(\hat\mu; y) = D^* - 2l(y; y), and the last term depends only on the data. So differences of AIC between models for the same data are differences of D∗D^* plus twice the differences of the numbers of parameters.
  • Pearson statistic. X2=∑wi(yi−μ^i)2/V(μ^i)X^2 = \sum w_i(y_i - \hat\mu_i)^2/V(\hat\mu_i) is the second-order expansion of DD in yiy_i around μ^i\hat\mu_i, since d(y,μ)=(y−μ)2/V(μ)+O((y−μ)3)d(y, \mu) = (y - \mu)^2/V(\mu) + O\bigl((y - \mu)^3\bigr). In a contingency table it is the statistic of the chi-squared goodness-of-fit test, and DD is the likelihood ratio statistic G2G^2.
  • Quasi-deviance. The same integral 2∫μy(y−t)/V(t) dt2\int_\mu^y (y - t)/V(t)\,dt with any variance function VV defines a deviance for quasi-likelihood models that have no full likelihood.
  • Cross-entropy loss. For Bernoulli responses, DD is 2n2n times the average cross-entropy loss of the fitted probabilities.
  • Logistic regression. The usual software output reports the residual and null deviances. Fact 6 says how to read them.

References

  1. J. A. Nelder and R. W. M. Wedderburn, Generalized linear models, Journal of the Royal Statistical Society, Series A 135(3), 1972, pp. 370 to 384, The whole paper (the exponential family model with a link, the fit by iterative weighted least squares, and the deviance as the measure of goodness of fit that extends the residual sum of squares)
  2. Donald A. Pierce and Daniel W. Schafer, Residuals in generalized linear models, Journal of the American Statistical Association 81(396), 1986, pp. 977 to 986, The whole paper (deviance residuals and their near-normal distribution)
  3. P. McCullagh and J. A. Nelder, Generalized Linear Models, second edition, Chapman and Hall, 1989, Chapter 2 (the deviance and the generalized Pearson statistic as measures of discrepancy, and the unit deviances of the common families) and Chapter 4 (binary data, where the deviance does not measure fit)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/0H7XKM/text.yaml