Skip to main content

➕ nncg — Non-Negative Conjugate Gradients

CI Coverage Python License: MIT CodeQL CodeFactor Rhiza Paper DOI


Quick Links: 📄 Paper • 🐛 Report Bug • 💡 Request Feature


📋 Overview

nncg solves the strictly convex non-negative quadratic program

$$\min_{x \geq 0}\ \tfrac{1}{2} x^\top A x - b^\top x, \qquad A \succ 0,$$

and its equality-augmented variant with a general linear system $Bx = c$, by wrapping matrix-free conjugate gradients in a primal-dual active-set loop. The working-set toggles are the principal pivots of the linear complementarity problem $\mathrm{LCP}(A, -b)$; guarding the fast block-pivot path with a least-index Bland fallback gives unconditional finite termination at the unique global minimiser — no non-degeneracy assumption.

This is the reference implementation of the paper Non-Negative Conjugate Gradients (Schmelzer & Stoll), developed in Jebel-Quant/mean_variance_solvers. The paper's numerical study doubles as this package's test suite: planted-optimum recovery across condition numbers, the equality-augmented solve for $p \in {1, 3, 8}$, CG-vs-exact free-set trajectory agreement (the inexactness lemma), warm-started parameter sweeps, and the adversarial anti-correlated family on which the unguarded batch path provably cycles and the fallback terminates.

The quadratic term enters as a cvx.linalg.SymmetricOperator: wrap an explicit SPD array in DenseOperator. When $A = M^\top M$ is a Gram matrix, pass GramOperator(M, ridge) and the inner solves need only products with $M$ — the $n \times n$ matrix is never formed and working memory is $O(n)$.

Each free-block solve is delegated to a pluggable inner solver — plain CG (CG), Jacobi- or randomized-Nyström-preconditioned CG (Jacobi, Nystrom), a Nyström sketch built once on the full operator and reused across every free block (GlobalNystrom — pays off on repeated solves of the same operator), or a direct factorisation (Exact) — so you match the inner solve to the operator's structure without touching the outer loop. ActiveSetSolver owns the loop and knows nothing about preconditioning; new inner solvers plug in by implementing a one-method InnerSolver interface.

📦 Installation

pip install nncg

🚀 Quickstart

The one-call solve_nnqp / solve_nnqp_eq wrappers cover the common case — pass a plain SPD array and name the inner solver as a string:

>>> import numpy as np
>>> from nncg import solve_nnqp, solve_nnqp_eq

>>> # a random SPD problem with condition number 1e4
>>> rng = np.random.default_rng(0)
>>> Q, _ = np.linalg.qr(rng.standard_normal((200, 200)))
>>> A = (Q * np.geomspace(1.0, 1e4, 200)) @ Q.T
>>> b = rng.standard_normal(200)

>>> res = solve_nnqp(A, b, inner="cg")  # inner solver: "cg" / "jacobi" / "nystrom" / "global_nystrom" / "exact"
>>> assert res.converged  # sign tests passed and the natural residual is small
>>> assert res.residual <= 1e-8 * max(1.0, abs(b).max())  # max|min(x, Ax - b)|

>>> # equality-augmented: minimise subject to x >= 0 and B x = c
>>> B = np.ones((1, 200))  # p = 1: the budget 1'x = 1
>>> res_eq = solve_nnqp_eq(A, b, B, np.array([1.0]), inner="jacobi")
>>> assert res_eq.lam.shape == (1,)  # multiplier, via a p-by-p Schur solve

For reuse across a parametric sweep, a matrix-free Gram operator, or a tuned inner solver, build the ActiveSetSolver and its operator directly — the wrappers are logic-free shortcuts to exactly this:

>>> from cvx.linalg import DenseOperator, GramOperator
>>> from nncg import ActiveSetSolver, CG, GlobalNystrom, Jacobi, Nystrom, NystromConfig, kkt_violation

>>> op = DenseOperator(A)  # kkt_violation takes a SymmetricOperator too
>>> solver = ActiveSetSolver(inner=CG())  # configure once, reuse across problems
>>> res = solver.solve(op, b)
>>> assert kkt_violation(op, b, res.x) < 1e-6  # zero certifies the global minimiser

>>> # warm-start a parametric sweep: support-stable steps take ONE outer step.
>>> # GlobalNystrom sketches `A` once (on the FIRST solve) and masks that one sketch to
>>> # each free block on every later solve — Nystrom would resketch A[F, F] every time.
>>> sweep_solver = ActiveSetSolver(inner=GlobalNystrom(nystrom=NystromConfig(rank=20)))
>>> res2 = sweep_solver.solve(op, b + 1e-4, warm=(res.free, res.x))

>>> # Gram-structured: A = M'M + I only through products with M — never formed.
>>> # Swap the inner solver freely — here Jacobi to strip the diagonal scaling.
>>> M = rng.standard_normal((50, 200))
>>> res_g = ActiveSetSolver(inner=Jacobi()).solve(GramOperator(M, ridge=1.0), M.T @ np.ones(50))
>>> assert res_g.converged

>>> # tuned inner solver: pass the instance (the string shortcut takes defaults only)
>>> res_n = ActiveSetSolver(inner=Nystrom(nystrom=NystromConfig(rank=20))).solve(op, b)

The package also ships MPRGP (Dostál & Schöberl) as a first-order alternative for the bound-constrained problem: conjugate-gradient, expansion and proportioning steps under the proportioning test, no factorisation and no active-set combinatorics. It takes the same operator and returns the same kind of certificate, so the two are directly comparable — but it carries no finite-termination guarantee and handles bound constraints only (no Bx = c).

>>> from nncg import solve_nnqp_mprgp

>>> res_m = solve_nnqp_mprgp(A, b)  # or MPRGP(...).solve(op, b) for a reusable solver
>>> assert kkt_violation(op, b, res_m.x) < 1e-6  # same certificate as the active-set path

🔬 The algorithm in one paragraph

Fix a working set of free variables and solve the unconstrained reduced SPD system by CG (matrix-free, $O(\sqrt{\kappa})$ Krylov rate). Push any free variable that returns negative to its bound (primal step); release any bound variable whose reduced gradient is negative (dual step); repeat. Batch exchanges are fast but can cycle; a patience counter falls back to Murty's least-index single pivot, which cannot — hence finite termination without any non-degeneracy hypothesis, and the fallback is provably necessary: on anti-correlated designs (the make_adversarial family in the test suite's tests/problems.py) the unguarded batch path revisits a previously seen working set and loops forever.

📖 Citation

If you use this package in academic work, please cite both the software and the paper. The software is archived on Zenodo; the DOI below is the concept DOI, which always resolves to the latest release:

@software{nncg,
  title     = {nncg: Non-Negative Conjugate Gradients},
  author    = {Schmelzer, Thomas and Stoll, Martin},
  doi       = {10.5281/zenodo.22096407},
  url       = {https://doi.org/10.5281/zenodo.22096407},
  publisher = {Zenodo},
}

and the paper:

@techreport{schmelzer2026nncg,
  title       = {Non-Negative Conjugate Gradients},
  author      = {Schmelzer, Thomas and Stoll, Martin},
  year        = {2026},
  institution = {Jebel Quant Research and TU Chemnitz},
  url         = {https://github.com/Jebel-Quant/mean_variance_solvers},
}

⚖️ License

MIT — see LICENSE.

Metadata

Release files for nncg 0.6.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 nncg 0.6.0
File Size Uploaded Provenance Actions
nncg-0.6.0.tar.gz 241.4 kB With provenance Details for nncg-0.6.0.tar.gz

Built distribution (wheel)

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

Total release size: 284.2 kB

Release files / nncg-0.6.0.tar.gz

Download URL nncg-0.6.0.tar.gz
Size 241.4 kB
Tags Source
SHA-256 checksum
How to use checksums
0c1cc4c705dc4f1eea37c7dc18228803e21424b13f746e5a13585d76a532cb49
BLAKE2b-256 checksum
How to use checksums
802bea18ed5e86e2cde89f8edb119a57a84e3198b8e0a5a6ceed4d256f120f09
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

Release files / nncg-0.6.0-py3-none-any.whl

Download URL nncg-0.6.0-py3-none-any.whl
Size 42.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1925c494fca4733a8e4ce8f38b090e5ec7e9fa3cddbdea37c5c0f260c042b9e2
BLAKE2b-256 checksum
How to use checksums
bf83075643dc1ee8db42f095f6891688f0f9f0cf5027adf97d2ef4a0e8a99a23
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

Security for nncg 0.6.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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for nncg-0.6.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

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.

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