glass.algorithm — General purpose algorithms

This module contains general implementations of algorithms which are used by GLASS, but are otherwise unrelated to GLASS functionality.

This module should be imported manually if used outside of GLASS:

import glass.algorithm

Non-negative least squares

glass.algorithm.nnls(a, b, *, tol=0.0, maxiter=None)[source]

Compute a non-negative least squares solution.

Implementation of the algorithm due to [Lawson95] as described by [Bro97].

Parameters:
Returns:

The non-negative least squares solution.

Raises:
Return type:

ndarray[tuple[int, ...], dtype[float64]]

Nearest correlation matrix

glass.algorithm.nearcorr(a, *, tol=None, niter=100)[source]

Compute the nearest correlation matrix.

Returns the nearest correlation matrix using the alternating projections algorithm of [Higham02].

Parameters:
  • a (ndarray[tuple[int, ...], dtype[float64]]) – Square matrix (or a stack of square matrices).

  • tol (float | None) – Tolerance for convergence. Default is dimension times machine epsilon.

  • niter (int) – Maximum number of iterations.

Returns:

Nearest correlation matrix.

Return type:

ndarray[tuple[int, ...], dtype[float64]]

Covariance matrix regularisation

glass.algorithm.cov_clip(cov, rtol=None)[source]

Covariance matrix from clipping non-positive eigenvalues.

The relative tolerance rtol is defined as for matrix_rank().

Return type:

ndarray[tuple[int, ...], dtype[float64]]

Parameters:

Parameter

cov

A symmetric matrix (or a stack of matrices).

rtol

An optional relative tolerance for eigenvalues to be considered positive.

Returns:

Covariance matrix with negative eigenvalues clipped.

glass.algorithm.cov_nearest(cov, tol=None, niter=100)[source]

Covariance matrix from nearest correlation matrix.

Divides cov along rows and columns by the square root of the diagonal, then computes the nearest valid correlation matrix using nearcorr(), before scaling rows and columns back. The diagonal of the input is hence unchanged.

Parameters:
Returns:

Covariance matrix from nearest correlation matrix.

Return type:

ndarray[tuple[int, ...], dtype[float64]]