Skip to main content

Repair indefinite covariance and correlation matrices via shrinking methods.

Project description

shrinking

Dedicated to Nick Higham.

shrinking implements the algorithms from Higham, Strabić, Šego, Restoring Definiteness via Shrinking, with an Application to Correlation Matrices with a Fixed Block.

The package repairs indefinite covariance and correlation matrices by shrinking them toward a positive definite target.

The algorithms were created in 2014. The code was written by Vedran Šego, under the supervision of Nick Higham. In 2026, the code was refactored, restyled, and adapted for PyPI deployment, mostly by OpenAI's Codex using the GPT-5.4 model, guided and reviewed by Vedran Šego.

Scope

This implementation focuses on positive definite targets. The paper also treats positive semidefinite targets, but this package keeps the public API to the positive definite case in order to keep the implementation and contract smaller.

Installation

pip install shrinking

Public API

The primary API is the snake_case package interface:

Validation and Helpers

  • check_pos_def: Check whether a matrix is positive definite.
  • blocks_to_target: Build the fixed-block target matrix from fixed block sizes.

S(alpha) Helpers

  • s: Compute S(alpha) from one of the supported target specifications.
  • s_with_target: Compute S(alpha) from an explicit target matrix.
  • s_with_difference: Compute S(alpha) from a precomputed target difference.
  • s_with_fixed_blocks: Compute S(alpha) for a fixed-block target.
  • s_with_identity: Compute S(alpha) when the target is the identity matrix.

Algorithms

  • bisection: Compute the shrinking parameter by the bisection method.
  • bisection_with_fixed_block: Run the fixed-block bisection variant.
  • newton: Compute the shrinking parameter by Newton's method.
  • gep: Compute the shrinking parameter by solving a generalized eigenvalue problem.
  • gep_with_fixed_block: Run the fixed-block generalized eigenvalue variant.

Each algorithm also has a _meta variant with the same name plus _meta, which returns an AlgorithmResult containing the shrinking parameter alpha (the same one that the ordinary function returns) and iteration count.

Compatibility

The legacy API is available under shrinking.backwards_compatibility.

The package accepts numpy.ndarray, numpy.matrix, and plain nested sequence inputs. Support for numpy.matrix is kept for compatibility with older numerical code; for new code, numpy.ndarray is the natural default. Plain nested sequences are normalized to numpy.ndarray, and mixed inputs use array semantics unless every input is explicitly a numpy.matrix.

Example

import numpy as np

from shrinking import bisection

matrix0 = np.array([[1.0, 1.2], [1.2, 1.0]])
matrix1 = np.identity(2)
alpha = bisection(matrix0, matrix1=matrix1)
print(alpha)

Development

The commands in this section are for a repository checkout, not for a normal installed package.

Run the test suite from the repository root with:

./run_tests.sh

Locally, the test runner imports the checkout from src/. In CI, the workflow sets USE_INSTALLED_PACKAGE=1, so the tests run against the installed package instead.

Remove repository-generated artifacts with:

./clean.sh

For the full development tool set used in this repository, including coverage and the optional demo dependencies, install the package with the development extra:

pip install ".[dev]"

Note: installing the sdist in an isolated build environment may download the build backend (hatchling). For offline checks, preinstall the build backend and use --no-build-isolation.

For an interactive Python session in the repository with the package import path configured:

./try_me.sh

To run the demo script from the repository through the same wrapper:

./try_me.sh demo_shrinking.py 17 17

The repository also includes a GitHub Actions workflow that installs the package and runs the test suite on supported Python versions.

License

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

shrinking-1.0.0.tar.gz (22.2 kB view details)

Uploaded Source

Built Distribution

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

shrinking-1.0.0-py3-none-any.whl (22.3 kB view details)

Uploaded Python 3

File details

Details for the file shrinking-1.0.0.tar.gz.

File metadata

  • Download URL: shrinking-1.0.0.tar.gz
  • Upload date:
  • Size: 22.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.5 cpython/3.14.3 HTTPX/0.28.1

File hashes

Hashes for shrinking-1.0.0.tar.gz
Algorithm Hash digest
SHA256 650681a08d90c90b9788fec304b33bfe58949ffb00e6d27525e48e40c75ee424
MD5 096423a6244d3bb032bc2f1851b44081
BLAKE2b-256 baa6516e6459459f1fffe5e6847c04bdf8427c1e07aea83f67f1741a8ee28f48

See more details on using hashes here.

File details

Details for the file shrinking-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: shrinking-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 22.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.5 cpython/3.14.3 HTTPX/0.28.1

File hashes

Hashes for shrinking-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f3dd0e5617b114b1d30d26931b4d5cf3621cba5a53d578f7c60ca49b049ba1f
MD5 2fc719949fd5ac4a0d148b8d3a215df5
BLAKE2b-256 c6cc6c54f0c27b1e27331589d935f05fb50dd2c50ba8f98ee49890cfe6978d54

See more details on using hashes here.

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