Denoise

PortfolioOptimisers.SpectralDenoiseType
struct SpectralDenoise <: AbstractDenoiseAlgorithm end

A denoising algorithm that sets the smallest num_factors eigenvalues of a covariance or correlation matrix to zero, effectively removing the principal components relating to random noise according to random matrix theory-based approaches.

Examples

julia> alg = SpectralDenoise()
SpectralDenoise()

Related

source
PortfolioOptimisers.FixedDenoiseType
struct FixedDenoise <: AbstractDenoiseAlgorithm end

A denoising algorithm that replaces the smallest num_factors eigenvalues of a covariance or correlation matrix with their average, effectively averaging the principal components relating to random noise according to random matrix theory-based approaches.

Examples

julia> alg = FixedDenoise()
FixedDenoise()

Related

source
PortfolioOptimisers.ShrunkDenoiseType
struct ShrunkDenoise{T1} <: AbstractDenoiseAlgorithm
    alpha::T1
end

A denoising algorithm that shrinks the smallest num_factors eigenvalues of a covariance or correlation matrix towards their diagonal, controlled by the shrinkage parameter alpha. This approach interpolates between no shrinkage (alpha = 0) and full shrinkage (alpha = 1), providing a flexible way to regularize noisy eigenvalues.

Fields

  • alpha: The shrinkage parameter controlling the degree of shrinkage applied to the smallest eigenvalues.

Constructor

ShrunkDenoise(; alpha::Real = 0.0)

Keyword arguments correspond to the fields above.

Validation

  • 0 <= alpha <= 1.

Examples

julia> alg = ShrunkDenoise(; alpha = 0.5)
ShrunkDenoise
  alpha | Float64: 0.5

Related

source
PortfolioOptimisers.DenoiseType
struct Denoise{T1, T2, T3, T4, T5, T6} <: AbstractDenoiseEstimator
    alg::T1
    args::T2
    kwargs::T3
    kernel::T4
    m::T5
    n::T6
end

A flexible container type for configuring and applying denoising algorithms to covariance or correlation matrices in PortfolioOptimisers.jl.

Denoise encapsulates all parameters required for matrix denoising, including the kernel and its arguments for spectral density estimation, the denoising algorithm, and matrix dimensions. It is the standard estimator type for denoising routines and supports a variety of algorithms (SpectralDenoise, FixedDenoise, ShrunkDenoise).

Fields

Constructor

Denoise(; alg::AbstractDenoiseAlgorithm = ShrunkDenoise(), m::Integer = 10,
        n::Integer = 1000, kernel::Any = AverageShiftedHistograms.Kernels.gaussian,
        args::Tuple = (), kwargs::NamedTuple = (;))

Keyword arguments correspond to the fields above.

Examples

julia> de = Denoise(;)
Denoise
     alg | ShrunkDenoise
         |   alpha | Float64: 0.0
    args | Tuple{}: ()
  kwargs | @NamedTuple{}: NamedTuple()
  kernel | typeof(AverageShiftedHistograms.Kernels.gaussian): AverageShiftedHistograms.Kernels.gaussian
       m | Int64: 10
       n | Int64: 1000

julia> de = Denoise(; alg = SpectralDenoise(), m = 20, n = 500)
Denoise
     alg | SpectralDenoise()
    args | Tuple{}: ()
  kwargs | @NamedTuple{}: NamedTuple()
  kernel | typeof(AverageShiftedHistograms.Kernels.gaussian): AverageShiftedHistograms.Kernels.gaussian
       m | Int64: 20
       n | Int64: 500

Related

source
PortfolioOptimisers.denoise!Function
denoise!(de::Denoise, X::AbstractMatrix, q::Real; pdm::Union{Nothing, <:Posdef} = Posdef())
denoise!(::Nothing, args...)

In-place denoising of a covariance or correlation matrix using a Denoise estimator.

For covariance matrices, the function internally converts to a correlation matrix, applies the algorithm, and then rescales back to covariance.

Arguments

  • de: The estimator specifying the denoising algorithm.

    • de::Denoise: The specified denoising algorithm is applied to X in-place.
    • de::Nothing: No-op.
  • X: The covariance or correlation matrix to be denoised (modified in-place).

  • q: The effective sample ratio (e.g., n_obs / n_assets), used for spectral thresholding.

  • pdm: Optional Positive definite matrix estimator. If provided, ensures the output is positive definite.

Returns

  • nothing. The input matrix X is modified in-place.

Examples

julia> using StableRNGs

julia> rng = StableRNG(123456789);

julia> X = rand(rng, 10, 5);

julia> X = X' * X
5×5 Matrix{Float64}:
 3.29494  2.0765   1.73334  2.01524  1.77493
 2.0765   2.46967  1.39953  1.97242  2.07886
 1.73334  1.39953  1.90712  1.17071  1.30459
 2.01524  1.97242  1.17071  2.24818  1.87091
 1.77493  2.07886  1.30459  1.87091  2.44414

julia> denoise!(Denoise(), X, 10 / 5)

julia> X
5×5 Matrix{Float64}:
 3.29494  2.28883  1.70633  2.12343  2.17377
 2.28883  2.46967  1.59575  1.98583  2.0329
 1.70633  1.59575  1.90712  1.48044  1.51553
 2.12343  1.98583  1.48044  2.24818  1.886
 2.17377  2.0329   1.51553  1.886    2.44414

Related

source
PortfolioOptimisers.AbstractDenoiseEstimatorType
abstract type AbstractDenoiseEstimator <: AbstractEstimator end

Abstract supertype for all denoising estimator types in PortfolioOptimisers.jl.

All concrete types that implement denoising of covariance or correlation matrices (e.g., via spectral, fixed, or shrinkage methods) should subtype AbstractDenoiseEstimator. This enables a consistent interface for denoising routines throughout the package.

Related

source
PortfolioOptimisers.errPDFFunction
errPDF(x::Real, vals::AbstractVector, q::Real;
       kernel::Any = AverageShiftedHistograms.Kernels.gaussian, m::Integer = 10,
       n::Integer = 1000)

Compute the sum of squared errors (SSE) between the theoretical Marčenko–Pastur (MP) eigenvalue density and the empirical eigenvalue density estimated from observed eigenvalues.

This function is used internally to fit the MP distribution to the observed spectrum, as part of the denoising procedure.

Arguments

  • x: Scale parameter for the MP distribution [0, 1].
  • vals: Observed eigenvalues.
  • q: Effective sample ratio (e.g., n_obs / n_assets).
  • kernel: Kernel function for AverageShiftedHistograms.ash.
  • m: Number of adjacent histograms to smooth over.
  • n: Number of points in the range of eigenvalues for density estimation.

Returns

  • sse::Real: The sum of squared errors between the empirical and theoretical densities.

Related

source
PortfolioOptimisers.find_max_evalFunction
find_max_eval(vals::AbstractVector, q::Real;
              kernel::Any = AverageShiftedHistograms.Kernels.gaussian, m::Integer = 10,
              n::Integer = 1000, args::Tuple = (), kwargs::NamedTuple = (;))

Estimate the upper edge of the Marčenko–Pastur (MP) distribution for a set of eigenvalues, used to separate signal from noise in random matrix denoising.

This function fits the MP distribution to the observed spectrum by minimizing the sum of squared errors between the empirical and theoretical densities, and returns the estimated maximum eigenvalue for noise.

Arguments

  • vals: Observed eigenvalues (typically sorted in ascending order).
  • q: Effective sample ratio (e.g., n_obs / n_assets).
  • kernel: Kernel function for AverageShiftedHistograms.ash.
  • m: Number of adjacent histograms to smooth over.
  • n: Number of points in the range of eigenvalues for density estimation.
  • args: Additional positional arguments for Optim.optimize.
  • kwargs: Additional keyword arguments for Optim.optimize.

Returns

  • (e_max::Real, x::Real): Tuple containing the estimated upper edge of the noise eigenvalue spectrum (e_max) and the fitted scale parameter (x).

Related

source
PortfolioOptimisers._denoise!Function
_denoise!(alg::AbstractDenoiseAlgorithm, X::AbstractMatrix, vals::AbstractVector,
          vecs::AbstractMatrix, num_factors::Integer)

In-place denoising of a covariance or correlation matrix using a specific denoising algorithm.

These methods are called internally by denoise! and denoise when a Denoise estimator is used, and should not typically be called directly.

Arguments

  • alg::AbstractDenoiseAlgorithm: The denoising algorithm to apply.

    • alg::SpectralDenoise: Sets the smallest num_factors eigenvalues to zero.
    • alg::FixedDenoise: Replaces the smallest num_factors eigenvalues with their average.
    • alg::ShrunkDenoise: Shrinks the smallest num_factors eigenvalues towards the diagonal, controlled by alg.alpha.
  • X: The matrix to be denoised (modified in-place).

  • vals: Eigenvalues of X, sorted in ascending order.

  • vecs: Corresponding eigenvectors of X.

  • num_factors: Number of eigenvalues to treat as noise.

Returns

  • nothing. The input matrix X is modified in-place.

Related

source