# Cook's distance

In statistics, Cook's distance or Cook's D is a commonly used estimate of the influence of a data point when performing a least-squares regression analysis. In a practical ordinary least squares analysis, Cook's distance can be used in several ways: to indicate influential data points that are particularly worth checking for validity; or to indicate regions of the design space where it would be good to be able to obtain more data points. It is named after the American statistician R. Dennis Cook, who introduced the concept in 1977.

## Definition

Data points with large residuals (outliers) and/or high leverage may distort the outcome and accuracy of a regression. Cook's distance measures the effect of deleting a given observation. Points with a large Cook's distance are considered to merit closer examination in the analysis.
For the algebraic expression, first define
    ${\underset {n\times 1}{\mathbf {y} }}={\underset {n\times p}{\mathbf {X} }}\quad {\underset {p\times 1}{\boldsymbol {\beta }}}\quad +\quad {\underset {n\times 1}{\boldsymbol {\varepsilon }}}$
where ${\boldsymbol {\varepsilon }}\sim {\mathcal {N}}\left(0,\sigma ^{2}\mathbf {I} \right)$ is the error term, ${\boldsymbol {\beta }}=\left[\beta _{0}\,\beta _{1}\dots \beta _{p-1}\right]^{\mathsf {T}}$ is the coefficient matrix, $p$ is the number of covariates or predictors for each observation, and $\mathbf {X}$ is the design matrix including a constant. The least squares estimator then is $\mathbf {b} =\left(\mathbf {X} ^{\mathsf {T}}\mathbf {X} \right)^{-1}\mathbf {X} ^{\mathsf {T}}\mathbf {y}$, and consequently the fitted (predicted) values for the mean of $\mathbf {y}$ are
    $\mathbf {\widehat {y}} =\mathbf {X} \mathbf {b} =\mathbf {X} \left(\mathbf {X} ^{\mathsf {T}}\mathbf {X} \right)^{-1}\mathbf {X} ^{\mathsf {T}}\mathbf {y} =\mathbf {H} \mathbf {y}$
where $\mathbf {H} \equiv \mathbf {X} (\mathbf {X} ^{\mathsf {T}}\mathbf {X} )^{-1}\mathbf {X} ^{\mathsf {T}}$ is the projection matrix (or hat matrix). The $i$-th diagonal element of $\mathbf {H} \,$, given by $h_{ii}\equiv \mathbf {x} _{i}^{\mathsf {T}}(\mathbf {X} ^{\mathsf {T}}\mathbf {X} )^{-1}\mathbf {x} _{i}$, is known as the leverage of the $i$-th observation. Similarly, the $i$-th element of the residual vector $\mathbf {e} =\mathbf {y} -\mathbf {\widehat {y\,}} =\left(\mathbf {I} -\mathbf {H} \right)\mathbf {y}$ is denoted by $e_{i}$.
Cook's distance $D_{i}$ of observation $i\;({\text{for }}i=1,\dots ,n)$ is defined as the sum of all the changes in the regression model when observation $i$ is removed from it
    $D_{i}={\frac {\sum _{j=1}^{n}\left({\widehat {y\,}}_{j}-{\widehat {y\,}}_{j(i)}\right)^{2}}{ps^{2}}}$
where p is the rank of the model (i.e., number of independent variables in the design matrix) and ${\widehat {y\,}}_{j(i)}$ is the fitted response value obtained when excluding $i$, and $s^{2}={\frac {\mathbf {e} ^{\top }\mathbf {e} }{n-p}}$ is the mean squared error of the regression model.
Equivalently, it can be expressed using the leverage ($h_{ii}$):
    $D_{i}={\frac {e_{i}^{2}}{ps^{2}}}\left[{\frac {h_{ii}}{(1-h_{ii})^{2}}}\right].$

## Detecting highly influential observations

There are different opinions regarding what cut-off values to use for spotting highly influential points. Since Cook's distance is in the metric of an F distribution with $p$ and $n-p$ (as defined for the design matrix $\mathbf {X}$ above) degrees of freedom, the median point (i.e., $F_{0.5}(p,n-p)$) can be used as a cut-off. Since this value is close to 1 for large $n$, a simple operational guideline of $D_{i}>1$ has been suggested.
The $p$-dimensional random vector $\mathbf {b} -\mathbf {b\,} _{(i)}$, which is the change of $\mathbf {b}$ due to a deletion of the $i$-th observation, has a covariance matrix of rank one and therefore it is distributed entirely over one dimensional subspace (a line, say $L$) of the $p$-dimensional space. The distributional property of $\mathbf {b} -\mathbf {b\,} _{(i)}$ mentioned above implies that information about the influence of the $i$-th observation provided by $\mathbf {b} -\mathbf {b\,} _{(i)}$ should be obtained not from outside of the line $L$ but from the line $L$ itself. However, in the introduction of Cook’s distance, a scaling matrix of full rank $p$ is chosen and as a result $\mathbf {b} -\mathbf {b\,} _{(i)}$ is treated as if it is a random vector distributed over the whole space of $p$ dimensions. This means that information about the influence of the $i$-th observation provided by $\mathbf {b} -\mathbf {b\,} _{(i)}$ through the Cook’s distance comes from the whole space of $p$ dimensions. Hence the Cook's distance measure is likely to distort the real influence of observations, misleading the right identification of influential observations.

## Relationship to other influence measures (and interpretation)

$D_{i}$ can be expressed using the leverage ($0\leq h_{ii}\leq 1$) and the square of the internally Studentized residual ($0\leq t_{i}^{2}$), as follows:
    ${\begin{aligned}D_{i}&={\frac {e_{i}^{2}}{ps^{2}}}\cdot {\frac {h_{ii}}{(1-h_{ii})^{2}}}={\frac {1}{p}}\cdot {\frac {e_{i}^{2}}{{1 \over n-p}\sum _{j=1}^{n}{\widehat {\varepsilon \,}}_{j}^{\,2}(1-h_{ii})}}\cdot {\frac {h_{ii}}{1-h_{ii}}}\\[5pt]&={\frac {1}{p}}\cdot t_{i}^{2}\cdot {\frac {h_{ii}}{1-h_{ii}}}.\end{aligned}}$
The benefit in the last formulation is that it clearly shows the relationship between $t_{i}^{2}$ and $h_{ii}$ to $D_{i}$ (while p and n are the same for all observations). If $t_{i}^{2}$ is large then it (for non-extreme values of $h_{ii}$) will increase $D_{i}$. If $h_{ii}$ is close to 0 then $D_{i}$ will be small, while if $h_{ii}$ is close to 1 then $D_{i}$ will become very large (as long as $t_{i}^{2}>0$, i.e.: that the observation $i$ is not exactly on the regression line that was fitted without observation $i$).
$D_{i}$ is related to DFFITS through the following relationship (note that ${{\widehat {\sigma }} \over {\widehat {\sigma }}_{(i)}}t_{i}=t_{i(i)}$ is the externally studentized residual, and ${\widehat {\sigma }},{\widehat {\sigma }}_{(i)}$ are defined here):
    ${\begin{aligned}D_{i}&={\frac {1}{p}}\cdot t_{i}^{2}\cdot {\frac {h_{ii}}{1-h_{ii}}}\\&={\frac {1}{p}}\cdot {\frac {{\widehat {\sigma }}_{(i)}^{2}}{{\widehat {\sigma }}^{2}}}\cdot {\frac {{\widehat {\sigma }}^{2}}{{\widehat {\sigma }}_{(i)}^{2}}}\cdot t_{i}^{2}\cdot {\frac {h_{ii}}{1-h_{ii}}}={\frac {1}{p}}\cdot {\frac {{\widehat {\sigma }}_{(i)}^{2}}{{\widehat {\sigma }}^{2}}}\cdot \left(t_{i(i)}{\sqrt {\frac {h_{ii}}{1-h_{ii}}}}\right)^{2}\\&={\frac {1}{p}}\cdot {\frac {{\widehat {\sigma }}_{(i)}^{2}}{{\widehat {\sigma }}^{2}}}\cdot {\text{DFFITS}}^{2}\end{aligned}}$
$D_{i}$ can be interpreted as the distance one's estimates move within the confidence ellipsoid that represents a region of plausible values for the parameters. This is shown by an alternative but equivalent representation of Cook's distance in terms of changes to the estimates of the regression parameters between the cases, where the particular observation is either included or excluded from the regression analysis.
An alternative to $D_{i}$ has been proposed. Instead of considering the influence a single observation has on the overall model, the statistics $S_{i}$ serves as a measure of how sensitive the prediction of the $i$-th observation is to the deletion of each observation in the original data set. It can be formulated as a weighted linear combination of the $D_{j}$'s of all data points. Again, the projection matrix is involved in the calculation to obtain the required weights:
    $S_{i}={\frac {\sum _{j=1}^{n}\left({\widehat {y}}_{i}-{{\widehat {y}}_{i}}_{(j)}\right)^{2}}{ps^{2}h_{ii}}}=\sum _{j=1}^{n}{\frac {h_{ij}^{2}\cdot D_{j}}{h_{ii}\cdot h_{jj}}}=\sum _{j=1}^{n}\rho _{ij}^{2}\cdot D_{j}$
In this context, $\rho _{ij}$ ($\leq 1$) resembles the correlation between the predictions ${\widehat {y\,}}_{i}$ and ${\widehat {y\,}}_{j}$. In contrast to $D_{i}$, the distribution of $S_{i}$ is asymptotically normal for large sample sizes and models with many predictors. In absence of outliers the expected value of $S_{i}$ is approximately $p^{-1}$. An influential observation can be identified if
    $\left|S_{i}-\operatorname {med} (S)\right|\geq 4.5\cdot \operatorname {MAD} (S)$
with $\operatorname {med} (S)$ as the median and $\operatorname {MAD} (S)$ as the median absolute deviation of all $S$-values within the original data set, i.e., a robust measure of location and a robust measure of scale for the distribution of $S_{i}$. The factor 4.5 covers approx. 3 standard deviations of $S$ around its centre. When compared to Cook's distance, $S_{i}$ was found to perform well for high- and intermediate-leverage outliers, even in presence of masking effects for which $D_{i}$ failed. Interestingly, $D_{i}$ and $S_{i}$ are closely related because they can both be expressed in terms of the matrix $\mathbf {T}$ which holds the effects of the deletion of the $j$-th data point on the $i$-th prediction:
    ${\begin{aligned}&\mathbf {T} =\left[{\begin{matrix}{\widehat {y}}_{1}-{{\widehat {y}}_{1}}_{\left(1\right)}&{\widehat {y}}_{1}-{{\widehat {y}}_{1}}_{\left(2\right)}&{\widehat {y}}_{1}-{{\widehat {y}}_{1}}_{\left(3\right)}&\cdots &{\widehat {y}}_{1}-{{\widehat {y}}_{1}}_{\left(n-1\right)}&{\widehat {y}}_{1}-{{\widehat {y}}_{1}}_{\left(n\right)}\\{\widehat {y}}_{2}-{{\widehat {y}}_{2}}_{\left(1\right)}&{\widehat {y}}_{2}-{{\widehat {y}}_{2}}_{\left(2\right)}&{\widehat {y}}_{2}-{{\widehat {y}}_{2}}_{\left(3\right)}&\cdots &{\widehat {y}}_{2}-{{\widehat {y}}_{2}}_{\left(n-1\right)}&{\widehat {y}}_{2}-{{\widehat {y}}_{2}}_{\left(n\right)}\\\vdots &\vdots &\vdots &\ddots &\vdots &\vdots \\{\widehat {y}}_{n-1}-{{\widehat {y}}_{n-1}}_{\left(1\right)}&{\widehat {y}}_{n-1}-{{\widehat {y}}_{n-1}}_{\left(2\right)}&{\widehat {y}}_{n-1}-{{\widehat {y}}_{n-1}}_{\left(3\right)}&\cdots &{\widehat {y}}_{n-1}-{{\widehat {y}}_{n-1}}_{\left(n-1\right)}&{\widehat {y}}_{n-1}-{{\widehat {y}}_{n-1}}_{\left(n\right)}\\{\widehat {y}}_{n}-{{\widehat {y}}_{n}}_{\left(1\right)}&{\widehat {y}}_{n}-{{\widehat {y}}_{n}}_{\left(2\right)}&{\widehat {y}}_{n}-{{\widehat {y}}_{n}}_{\left(3\right)}&\cdots &{\widehat {y}}_{n}-{{\widehat {y}}_{n}}_{\left(n-1\right)}&{\widehat {y}}_{n}-{{\widehat {y}}_{n}}_{\left(n\right)}\end{matrix}}\right]\\\\&\ \ =\mathbf {H} \mathbf {E} \mathbf {G} =\mathbf {H} \left[{\begin{matrix}e_{1}&0&0&\cdots &0&0\\0&e_{2}&0&\cdots &0&0\\\vdots &\vdots &\vdots &\ddots &\vdots &\vdots \\0&0&0&\cdots &e_{n-1}&0\\0&0&0&\cdots &0&e_{n}\end{matrix}}\right]\left[{\begin{matrix}{\frac {1}{1-h_{11}}}&0&0&\cdots &0&0\\0&{\frac {1}{1-h_{22}}}&0&\cdots &0&0\\\vdots &\vdots &\vdots &\ddots &\vdots &\vdots \\0&0&0&\cdots &{\frac {1}{1-h_{n-1,n-1}}}&0\\0&0&0&\cdots &0&{\frac {1}{1-h_{nn}}}\end{matrix}}\right]\end{aligned}}$
With $\mathbf {T}$ at hand, $\mathbf {D}$ is given by:
    $\mathbf {D} =\left[{\begin{matrix}D_{1}\\D_{2}\\\vdots \\D_{n-1}\\D_{n}\end{matrix}}\right]={\frac {1}{ps^{2}}}\operatorname {diag} \left(\mathbf {T} ^{\mathsf {T}}\mathbf {T} \right)={\frac {1}{ps^{2}}}\operatorname {diag} \left(\mathbf {G} \mathbf {E} \mathbf {H} ^{\mathsf {T}}\mathbf {H} \mathbf {E} \mathbf {G} \right)=\operatorname {diag} (\mathbf {M} )$
where $\mathbf {H} ^{\mathsf {T}}\mathbf {H} =\mathbf {H}$ if $\mathbf {H}$ is symmetric and idempotent, which is not necessarily the case. In contrast, $\mathbf {S}$ can be calculated as:
    ${\begin{aligned}&\mathbf {S} =\left[{\begin{matrix}S_{1}\\S_{2}\\\vdots \\S_{n-1}\\S_{n}\end{matrix}}\right]={\frac {1}{ps^{2}}}\mathbf {F} \operatorname {diag} \left(\mathbf {T} \mathbf {T} ^{\mathsf {T}}\right)={\frac {1}{ps^{2}}}\left[{\begin{matrix}{\frac {1}{h_{11}}}&0&0&\cdots &0&0\\0&{\frac {1}{h_{22}}}&0&\cdots &0&0\\\vdots &\vdots &\vdots &\ddots &\vdots &\vdots \\0&0&0&\cdots &{\frac {1}{h_{n-1n-1}}}&0\\0&0&0&\cdots &0&{\frac {1}{h_{nn}}}\end{matrix}}\right]\operatorname {diag} \left(\mathbf {T} \mathbf {T} ^{\mathsf {T}}\right)\\\\&\ \ ={\frac {1}{ps^{2}}}\mathbf {F} \operatorname {diag} \left(\mathbf {H} \mathbf {E} \mathbf {G} \mathbf {G} \mathbf {E} \mathbf {H} ^{\mathsf {T}}\right)=\mathbf {F} \operatorname {diag} (\mathbf {P} )\end{aligned}}$
where $\operatorname {diag} (\mathbf {A} )$ extracts the main diagonal of a square matrix $\mathbf {A}$. In this context, $\mathbf {M} =p^{-1}s^{-2}\mathbf {G} \mathbf {E} \mathbf {H} ^{\mathsf {T}}\mathbf {H} \mathbf {E} \mathbf {G}$ is referred to as the influence matrix whereas $\mathbf {P} =p^{-1}s^{-2}\mathbf {H} \mathbf {E} \mathbf {G} \mathbf {G} \mathbf {E} \mathbf {H} ^{\mathsf {T}}$ resembles the so-called sensitivity matrix. An eigenvector analysis of $\mathbf {M}$ and $\mathbf {P}$ - which both share the same eigenvalues – serves as a tool in outlier detection, although the eigenvectors of the sensitivity matrix are more powerful.

## Software implementations

Many programs and statistics packages, such as R, Python, Julia, etc., include implementations of Cook's distance.
| Language/Program | Function | Notes |
| Stata | predict, cooksd | See |
| R | cooks.distance(model, ...) | See |
| Python | CooksDistance.fit(X, y) | See |
| Julia | cooksdistance(model, ...) | See |

## Extensions

High-dimensional Influence Measure (HIM) is an alternative to Cook's distance for when $p>n$ (i.e., when there are more predictors than observations). While the Cook's distance quantifies the individual observation's influence on the least squares regression coefficient estimate, the HIM measures the influence of an observation on the marginal correlations.