Skip to main content

regression_diagnostics/influence/
leverage.rs

1use ndarray::Array1;
2
3use crate::OlsFit;
4
5/// Leverage `hᵢ` (the diagonal of the hat matrix) for each observation.
6///
7/// This is already computed efficiently at fit time from the thin `Q` factor —
8/// the full `n × n` hat matrix is never materialized — and simply surfaced here.
9///
10/// # Interpretation
11///
12/// Leverage measures how unusual an observation's **predictor** values are,
13/// independent of its response. High leverage is *potential* influence, not
14/// influence itself: a high-leverage point that happens to sit on the fitted line
15/// barely moves it. Combine leverage with residual size — that is exactly what
16/// [`cooks_distance`](super::cooks_distance) and [`dffits`](super::dffits) do.
17///
18/// Each `hᵢ ∈ [0, 1]` and `Σ hᵢ = p`, so the average leverage is `p/n`; the
19/// common flags are multiples of that average (`2p/n`, `3p/n`).
20pub fn leverage(fit: &OlsFit) -> Array1<f64> {
21    fit.leverage().to_owned()
22}