covarianceShrinkage

package module
v0.0.0-...-18db3c1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 24, 2025 License: MIT Imports: 6 Imported by: 0

README

covarianceShrinkage

A Go package implementing Ledoit-Wolf linear and nonlinear shrinkage for improving the estimation of large covariance matrices. Particularly useful in high-dimensional statistics and portfolio optimization, where the number of assets exceeds the number of observations.


Installation

go get github.com/dgelody/covarianceShrinkage

Functions

CovCor

Signature:

func CovCor(X *mat.Dense, S *mat.SymDense) (FMatrix *mat.SymDense, norm float64, err error) 

Description:

Linear shrinkage towards constant-correlation matrix; the target preserves the diagonal of the sample covariance matrix and all correlation coefficients are the same. See Ledoit and Wolf (2004a).

Parameters:

  • X - matrix of n random variables by t observations. The data used to create the covariance matrix. Should be differenced if the covariance matrix is the covariance of the differences.
  • S - n by n covariance matrix. If nil, S will be the sample covariance matrix

Returns:

  • F - n by n shrinkage estimator of the covariance matrix
  • norm - maximum absolute difference between the covariance matrix S and the shrinkage estimator of the covariance matrix F
  • err - error
QuadraticInverseShrinkage

Signature:

func QuadraticInverseShrinkage(X *mat.Dense, S *mat.SymDense, demeanData bool) (F *mat.Dense, norm float64, err error)

Description:

Nonlinear shrinkage derived under Frobenius loss and its two cousins, Inverse Stein’s loss and Minimum Variance loss, called quadratic-inverse shrinkage (QIS). Preserves the variance of the original covariance matrix. See Ledoit and Wolf (2022, Section 4.5).

Parameters:

  • X - matrix of t observations by n random variables. The data used to create the covariance matrix. Should be differenced if the covariance matrix is the covariance of the differences.
  • S - n by n covariance matrix. If nil, S will be the sample covariance matrix
  • demeanData - if true then demean the data, otherwise no demeaning takes place

Returns:

  • F - n by n shrinkage estimator of the covariance matrix
  • norm - maximum absolute difference between the covariance matrix S and the shrinkage estimator of the covariance matrix F
  • err - error

Testing

Run all unit tests with coverage:

go test -v -cover ./...

References

(a) Ledoit, O. and Wolf, M. (2004a). Honey, I shrunk the sample covariance matrix. Journal of Portfolio Management, 30(4):110–119.

(b) Ledoit, O. and Wolf, M. (2004b). A well-conditioned estimator for large-dimensional covariance matrices. Journal of Multivariate Analysis, 88(2):365–411.

(c) Ledoit, O. and Wolf, M. (2022). Quadratic shrinkage for large covariance matrices. Bernoulli, 28(3): 1519-1547.


License

MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CovCor

func CovCor(X *mat.Dense, S *mat.SymDense) (F *mat.SymDense, norm float64, err error)

Returns a covariance estimate using linear shrinkage. The target preserves the diagonal of the sample covariance matrix and all correlation coefficients are the same.

Arguments: X *mat.Dense : matrix of n random variables by t observations. The data used to create the covariance matrix. Should be differenced if the covariance matrix is the covariance of the differences.

S *mat.SymDense: nxn covariance matrix. If nil, S will be the sample covariance matrix

Returns: F *mat.SymDense: nxn shrinkage estimator of the covariance matrix norm float64: maximum absolute difference between covariance and shrinkage matrix err error

func FrobeniusNorm

func FrobeniusNorm(A, B []float64) float64

Computes the Frobenius norm between matrices A and B

Arguments: A []float64: matrix A B []float64: matrix B

Returns: float64: Frobenius norm

func MaxAbsDiff

func MaxAbsDiff(A, B []float64) float64

Computes the maximum absolute difference between elements of matrices A and B

Arguments: A []float64: matrix A B []float64: matrix B

Returns: float64: maximum absolute difference between elements

func QuadraticInverseShrinkage

func QuadraticInverseShrinkage(X *mat.Dense, S *mat.SymDense, demeanData bool) (F *mat.Dense, norm float64, err error)

deameanData bool: if true then demean the data, otherwise no demeaning takes place

Returns: F *mat.Dense: the Quadratic-Inverse-Shrinkage covariance estimator norm float64: maximum absolute difference between covariance and shrinkage matrix err error

func SampleCovarianceMatrix

func SampleCovarianceMatrix(n int, t int, X []float64) []float64

Computes the sample covariance matrix

Arguments: n int: number of assets t int: number of periods X []float64: matrix of n assets by t periods

Returns: []float64: nxn sample covariance matrix

Types

type Target

type Target int

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL