Weighted centering matrix (source code)

= Weighted centering matrix
{title2=$P_a=I-\mathbf1a^T$}

A \b[weighted centering matrix] subtracts the same weighted mean from every component of a <vector>. Let $a\in\mathbb R^n$ satisfy $a_i\ge0$ and $a^\mathsf T\mathbf1=1$, where $\mathbf1$ has every component equal to one. Define
$$
P_a=I-\mathbf1a^\mathsf T.
$$
Then $P_ax=x-(a^\mathsf Tx)\mathbf1$ and $a^\mathsf TP_ax=0$. The centered vector has zero \i[weighted] mean; its unweighted component sum need not vanish.

The <projection (linear algebra)> properties follow directly from normalization:
$$
P_a^2=I-2\mathbf1a^\mathsf T+
\mathbf1(a^\mathsf T\mathbf1)a^\mathsf T=P_a.
$$
Also $P_a\mathbf1=0$, so its <kernel of a linear map> is $\operatorname{span}\{\mathbf1\}$, its range is $\ker a^\mathsf T$ and its <rank> is $n-1$. It is an <idempotent matrix>. For unequal weights it is generally not symmetric: it projects onto the zero-weighted-mean subspace along the constant-vector direction, an <oblique projection> in ordinary Euclidean geometry.

For an original example, $a=(1/4,3/4)^\mathsf T$ and $x=(4,2)^\mathsf T$ give $a^\mathsf Tx=2.5$ and $P_ax=(1.5,-0.5)^\mathsf T$. The weighted mean is zero, while the ordinary mean is $0.5$. Euclidean <orthogonal projection> of $x$ onto the same line instead gives $(3,-1)^\mathsf T$.

\Image[/weighted-centering-oblique-projection.png]
{title=Weighted centering and Euclidean orthogonal projection reach different points on the same zero-weighted-mean line}
{description=Original two-coordinate example with $a=(1/4,3/4)^\mathsf T$ and $x=(4,2)^\mathsf T$. The orange arrow subtracts $2.5$ from both components; the dashed arrow is perpendicular to the green line in the Euclidean metric.}

If every $a_i>0$, put $D=\operatorname{diag}(a)$. Then $DP_a=P_a^\mathsf TD=D-aa^\mathsf T$, so this same map is orthogonal for the <inner product> $\langle u,v\rangle_D=u^\mathsf TDv$. It is the residual-making map for minimizing $\sum_i a_i(x_i-c)^2$ over a fitted constant $c$, the intercept-only case of <weighted least squares>.<Weighted Least Squares by NIST>[§4.4.3.2, General WLS Criterion; centering derivation given here]{ref} Zero weights preserve the algebraic projection but make this proposed inner product degenerate. When all weights equal $1/n$, the construction reduces to the usual <centering matrix>.