Skip to main content

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.

Metadata

Release files for shrinking 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shrinking 1.0.0
File Size Uploaded Provenance Actions
shrinking-1.0.0.tar.gz 22.2 kB Without provenance Details for shrinking-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for shrinking 1.0.0
File Interpreter ABI Platform Actions
shrinking-1.0.0-py3-none-any.whl Python 3 none any Details for shrinking-1.0.0-py3-none-any.whl

Total release size: 44.5 kB

Release files / shrinking-1.0.0.tar.gz

Download URL shrinking-1.0.0.tar.gz
Size 22.2 kB
Tags Source
SHA-256 checksum
How to use checksums
650681a08d90c90b9788fec304b33bfe58949ffb00e6d27525e48e40c75ee424
BLAKE2b-256 checksum
How to use checksums
baa6516e6459459f1fffe5e6847c04bdf8427c1e07aea83f67f1741a8ee28f48
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Hatch/1.16.5 cpython/3.14.3 HTTPX/0.28.1

Release files / shrinking-1.0.0-py3-none-any.whl

Download URL shrinking-1.0.0-py3-none-any.whl
Size 22.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f3dd0e5617b114b1d30d26931b4d5cf3621cba5a53d578f7c60ca49b049ba1f
BLAKE2b-256 checksum
How to use checksums
c6cc6c54f0c27b1e27331589d935f05fb50dd2c50ba8f98ee49890cfe6978d54
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Hatch/1.16.5 cpython/3.14.3 HTTPX/0.28.1

Security for shrinking 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page