Skip to main content

Hat matrix and leverage diagnostics for linear regression.

Project description

matrix-hat

CI PyPI Python License: MIT

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 full n × n hat 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

matrix_hat-0.1.0.tar.gz (141.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

matrix_hat-0.1.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

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

Hashes for matrix_hat-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4990cb8b733725c6038a83e4adb29e019a68896d75b69dcae9c82e4c6efe464f
MD5 35b65e42d3132ea216508554ee4293ea
BLAKE2b-256 878445a9c635fe3d150a987217c0abb87ce867419a974e5b5c53ea53ca142d5b

See more details on using hashes here.

Provenance

The following attestation bundles were made for matrix_hat-0.1.0.tar.gz:

Publisher: publish.yml on aokienz/matrix-hat

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for matrix_hat-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 777051674ab1eb01103188b41a9929999771832e47e3a3294f371cbfdf383639
MD5 8bfb7250e903eaf6a349b96e81b033a2
BLAKE2b-256 7b82ecbc958a93133c52afd5ca124193718f39179154200b14eedca5a5619c63

See more details on using hashes here.

Provenance

The following attestation bundles were made for matrix_hat-0.1.0-py3-none-any.whl:

Publisher: publish.yml on aokienz/matrix-hat

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page