Centering matrix (source code)

= Centering matrix
{title2=$P=I-d^{-1}\mathbf1\mathbf1^{\mathsf T}$}
{wiki}

The \b[centering matrix] subtracts the <arithmetic mean> from every component of a real <vector>. For a positive integer $d$, let $\mathbf1\in\mathbb R^d$ have every component equal to one and define
$$
P=I-\frac1d\mathbf1\mathbf1^{\mathsf T}.
$$
For $x\in\mathbb R^d$, write $\bar x=d^{-1}\mathbf1^{\mathsf T}x$. Then $Px=x-\bar x\mathbf1$, so $\mathbf1^{\mathsf T}Px=0$. The output lies in the subspace of vectors whose components sum to zero.

Its <orthogonal projection> properties follow directly. Set $J=\mathbf1\mathbf1^{\mathsf T}/d$. Since $\mathbf1^{\mathsf T}\mathbf1=d$, we have $J^2=J$ and hence
$$
P^{\mathsf T}=P,\qquad P^2=(I-J)^2=I-J=P.
$$
Thus $P$ is a symmetric <idempotent matrix>, the orthogonal projector onto $\mathbf1^\perp$. It kills constant vectors and leaves zero-sum vectors unchanged. Its <kernel of a linear map> is $\operatorname{span}\{\mathbf1\}$ and its <rank> is $d-1$; for $d=1$ it is the zero matrix. The eigenvalues are zero on the constant direction and one on its orthogonal complement.

For an original example, $x=(1,2,4,9)^{\mathsf T}$ has mean $4$, giving $Px=(-3,-2,0,5)^{\mathsf T}$. The centered sum of squares is
$$
x^{\mathsf T}Px=\|Px\|^2=9+4+0+25=38.
$$
For these four observations, the usual sample <variance> is $38/(4-1)=38/3$. Centering removes the mean; it does not itself divide by a standard deviation.

For a data <matrix> $X$ with observations in its $d$ rows, $PX$ centers each column. When $d>1$, its sample <covariance matrix> is $X^{\mathsf T}PX/(d-1)$. Also $P(x+c\mathbf1)=Px$ for any scalar $c$, which proves invariance to adding the same offset to every observation. These identities make the centering matrix useful in <least squares> and <principal component analysis>.