Hat matrix and leverage diagnostics for linear regression.
Project description
matrix-hat
Small, dependency-light toolkit for the hat matrix and leverage diagnostics used in linear regression.
The hat matrix of a design matrix X is the orthogonal projection onto its
column space:
$$H = X (X^\top X)^{-1} X^\top$$
It earns its name because it turns observed responses into fitted values ("y-hat"): $\hat{y} = Hy$. Its diagonal entries $h_{ii}$ are the leverages, which measure how far each observation sits from the centre of the predictor space — a key tool for spotting influential points and outliers.
Full documentation (user guide, math background, API reference) lives in
docs/. Build locally with pip install -e ".[docs]" then
sphinx-build -W -b html docs docs/_build/html.
Why this package
- Numerically stable. Rank-revealing SVD instead of an explicit
(XᵀX)⁻¹, so it stays well-behaved on ill-conditioned or rank-deficient designs. - Memory-aware.
leverage()returns just the diagonal without ever forming the fulln × nhat matrix. - Tiny footprint. NumPy is the only runtime dependency.
Installation
pip install matrix-hat
From source (tests + docs):
pip install -e ".[dev]"
Quickstart
import numpy as np
from matrix_hat import leverage, hat_matrix, high_leverage_points
X = np.array([[1.0], [2.0], [3.0], [4.0], [50.0]])
# Leverages (diagonal of the hat matrix), with an intercept column added.
h = leverage(X, add_intercept=True)
# array([0.267, 0.255, 0.245, 0.235, 0.998]) approx.
# Flag high-leverage observations (default rule of thumb: 2 * p / n).
high_leverage_points(X, add_intercept=True)
# array([4])
# The full projection matrix, when you actually need it.
H = hat_matrix(X, add_intercept=True)
API
| Function | Description |
|---|---|
leverage(X, add_intercept=False) |
Leverages h_ii (diagonal of H) as a length-n vector. |
hat_matrix(X, add_intercept=False) |
The full n × n projection matrix H. |
high_leverage_points(X, threshold=None, factor=2.0) |
Indices of observations above a leverage cut-off. |
orthonormal_basis(X, add_intercept=False) |
Orthonormal basis Q for the column space (H = Q Qᵀ). |
effective_rank(X, add_intercept=False) |
Numerical rank of the design matrix. |
All functions accept a tol argument to control the singular-value cut-off
used for rank determination. See the Sphinx docs for full numpydoc reference
pages.
Development
pip install -e ".[dev]"
pytest
sphinx-build -W -b html docs docs/_build/html
See CONTRIBUTING.md for details.
Citation
If you use this package in research, see CITATION.cff.
Changelog
See CHANGELOG.md.
License
MIT — see LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file matrix_hat-0.1.0.tar.gz.
File metadata
- Download URL: matrix_hat-0.1.0.tar.gz
- Upload date:
- Size: 141.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4990cb8b733725c6038a83e4adb29e019a68896d75b69dcae9c82e4c6efe464f
|
|
| MD5 |
35b65e42d3132ea216508554ee4293ea
|
|
| BLAKE2b-256 |
878445a9c635fe3d150a987217c0abb87ce867419a974e5b5c53ea53ca142d5b
|
Provenance
The following attestation bundles were made for matrix_hat-0.1.0.tar.gz:
Publisher:
publish.yml on aokienz/matrix-hat
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
matrix_hat-0.1.0.tar.gz -
Subject digest:
4990cb8b733725c6038a83e4adb29e019a68896d75b69dcae9c82e4c6efe464f - Sigstore transparency entry: 2196507028
- Sigstore integration time:
-
Permalink:
aokienz/matrix-hat@359d4184a5453e2555aa34c699db4677b6173c38 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/aokienz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@359d4184a5453e2555aa34c699db4677b6173c38 -
Trigger Event:
release
-
Statement type:
File details
Details for the file matrix_hat-0.1.0-py3-none-any.whl.
File metadata
- Download URL: matrix_hat-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
777051674ab1eb01103188b41a9929999771832e47e3a3294f371cbfdf383639
|
|
| MD5 |
8bfb7250e903eaf6a349b96e81b033a2
|
|
| BLAKE2b-256 |
7b82ecbc958a93133c52afd5ca124193718f39179154200b14eedca5a5619c63
|
Provenance
The following attestation bundles were made for matrix_hat-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on aokienz/matrix-hat
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
matrix_hat-0.1.0-py3-none-any.whl -
Subject digest:
777051674ab1eb01103188b41a9929999771832e47e3a3294f371cbfdf383639 - Sigstore transparency entry: 2196507227
- Sigstore integration time:
-
Permalink:
aokienz/matrix-hat@359d4184a5453e2555aa34c699db4677b6173c38 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/aokienz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@359d4184a5453e2555aa34c699db4677b6173c38 -
Trigger Event:
release
-
Statement type: