Denoise
PortfolioOptimisers.SpectralDenoise
— TypeSpectralDenoise <: AbstractDenoiseAlgorithm
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
PortfolioOptimisers.FixedDenoise
— TypeFixedDenoise <: AbstractDenoiseAlgorithm
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
PortfolioOptimisers.ShrunkDenoise
— Typestruct 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.
Constructor
ShrunkDenoise(; alpha::Real = 0.0)
Creates a new ShrunkDenoise
with the specified shrinkage parameter alpha
.
Related
PortfolioOptimisers.ShrunkDenoise
— MethodShrunkDenoise(; alpha::Real = 0.0)
Constructor for ShrunkDenoise
.
Arguments
alpha
: The shrinkage parameter controlling the degree of shrinkage applied to the smallest eigenvalues. Must be in the range[0, 1]
, where0
means no shrinkage and1
means full shrinkage.
Validation
- Throws an error if
alpha
is not in[0, 1]
.
Examples
julia> alg = ShrunkDenoise(; alpha = 0.5)
ShrunkDenoise
alpha | Float64: 0.5
PortfolioOptimisers.Denoise
— Typestruct 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
alg::AbstractDenoiseAlgorithm
: Denoising algorithm (SpectralDenoise
,FixedDenoise
,ShrunkDenoise
).args::Tuple
: Positional arguments for the univariate Optim.optimize.kwargs::NamedTuple
: Keyword arguments for the univariate Optim.optimize.kernel::Any
: Kernel function for AverageShiftedHistograms.ash.m::Integer
: Number of adjacent histograms to smooth over in AverageShiftedHistograms.ash.n::Integer
: Number of points in the range of eigenvalues used in the average shifted histogram density estimation.
Constructor
Denoise(; alg::AbstractDenoiseAlgorithm = ShrunkDenoise(),
m::Integer = 10,
n::Integer = 1000,
kernel = AverageShiftedHistograms.Kernels.gaussian,
args::Tuple = (),
kwargs::NamedTuple = (;))
Keyword arguments correspond to the fields above. The constructor infers types and sets defaults for robust denoising.
Related
PortfolioOptimisers.Denoise
— MethodDenoise(; kernel = AverageShiftedHistograms.Kernels.gaussian,
args::Tuple = (),
kwargs::NamedTuple = (;),
alg::AbstractDenoiseAlgorithm = ShrunkDenoise(),
m::Integer = 10,
n::Integer = 1000)
Construct a Denoise
object, configuring all parameters for matrix denoising in PortfolioOptimisers.jl.
Arguments
alg::AbstractDenoiseAlgorithm
: Denoising algorithm to use (e.g.,SpectralDenoise
,FixedDenoise
,ShrunkDenoise
).args::Tuple
: Positional arguments for the univariate Optim.optimize.kwargs::NamedTuple
: Keyword arguments for the univariate Optim.optimize.kernel
: Kernel function for AverageShiftedHistograms.ash.m::Integer
: Number of adjacent histograms to smooth over in AverageShiftedHistograms.ash.n::Integer
: Number of points in the range of eigenvalues used in the average shifted histogram density estimation.
Validation
- Types are inferred from provided arguments.
- All keyword arguments correspond to fields in
Denoise
.
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
PortfolioOptimisers.denoise!
— Functiondenoise!(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.
- If
de
isnothing
, this is a no-op and returnsnothing
. - If
de
is aDenoise
object, the specified denoising algorithm is applied toX
in-place. Optionally, aPosdef
can be provided to ensure the output is positive definite.
Arguments
de::Denoise
: The denoising estimator specifying the algorithm and kernel parameters.pdm::Union{Nothing, <:Posdef}
: Optional positive definite matrix estimator. If provided, ensures the output is positive definite.X::AbstractMatrix
: The covariance or correlation matrix to be denoised (modified in-place).q::Real
: The effective sample ratio (e.g.,n_obs / n_assets
), used for spectral thresholding.
ReturnsResult
nothing
. The input matrixX
is modified in-place.
Validation
- If
X
is a covariance matrix, it is internally converted to a correlation matrix for denoising and then rescaled. - The number of factors is determined automatically from the spectrum and kernel parameters.
- If
pdm
is provided, the result is projected to the nearest positive definite matrix.
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
PortfolioOptimisers.denoise
— Functiondenoise(de::Denoise, X::AbstractMatrix, q::Real, pdm::Union{Nothing, <:Posdef} = Posdef())
denoise(::Nothing, args...)
Same as denoise!
, but returns a new matrix instead of modifying X
in-place.
- If
de
isnothing
, this is a no-op and returnsnothing
.
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> Xd = denoise(Denoise(), X, 10 / 5)
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
PortfolioOptimisers.AbstractDenoiseEstimator
— TypeAbstractDenoiseEstimator <: AbstractEstimator
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
PortfolioOptimisers.AbstractDenoiseAlgorithm
— TypeAbstractDenoiseAlgorithm <: AbstractAlgorithm
Abstract supertype for all denoising algorithm types in PortfolioOptimisers.jl.
All concrete types that implement a specific denoising algorithm (e.g., spectral, fixed, shrinkage) should subtype AbstractDenoiseAlgorithm
. This enables flexible extension and dispatch of denoising routines.
Related
PortfolioOptimisers._denoise!
— Function_denoise!(alg::SpectralDenoise, X::AbstractMatrix, vals::AbstractVector, vecs::AbstractMatrix, num_factors::Integer)
_denoise!(alg::FixedDenoise, X::AbstractMatrix, vals::AbstractVector, vecs::AbstractMatrix, num_factors::Integer)
_denoise!(alg::ShrunkDenoise, 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::SpectralDenoise
: Sets the smallestnum_factors
eigenvalues to zero.alg::FixedDenoise
: Replaces the smallestnum_factors
eigenvalues with their average.alg::ShrunkDenoise
: Shrinks the smallestnum_factors
eigenvalues towards the diagonal, controlled byalg.alpha
.X::AbstractMatrix
: The matrix to be denoised (modified in-place).vals::AbstractVector
: Eigenvalues ofX
, sorted in ascending order.vecs::AbstractMatrix
: Corresponding eigenvectors ofX
.num_factors::Integer
: Number of eigenvalues to treat as noise.
ReturnsResult
nothing
. The input matrixX
is modified in-place.
Validation
- Assumes
vals
andvecs
are from the eigendecomposition of a correlation matrix. - The denoised matrix is projected back to correlation space.
Related
PortfolioOptimisers.errPDF
— FunctionerrPDF(x, vals; kernel = AverageShiftedHistograms.Kernels.gaussian, m = 10, n = 1000, q = 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::AbstractVector
: Observed eigenvalues.kernel
: Kernel function for AverageShiftedHistograms.ash.m::Integer
: Number of adjacent histograms to smooth over.n::Integer
: Number of points in the range of eigenvalues for density estimation.q::Real
: Effective sample ratio (e.g.,n_obs / n_assets
).
ReturnsResult
sse::Real
: The sum of squared errors between the empirical and theoretical densities.
Validation
- Assumes
vals
are eigenvalues of a correlation or covariance matrix. - The empirical density is estimated using average shifted histograms.
Related
PortfolioOptimisers.find_max_eval
— Functionfind_max_eval(vals, q; kernel = AverageShiftedHistograms.Kernels.gaussian, m::Integer = 10, n::Integer = 1000, args = (), kwargs = (;))
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::AbstractVector
: Observed eigenvalues (typically sorted in ascending order).q::Real
: Effective sample ratio (e.g.,n_obs / n_assets
).kernel
: Kernel function for AverageShiftedHistograms.ash.m::Integer
: Number of adjacent histograms to smooth over.n::Integer
: 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.
ReturnsResult
(e_max, x)
: Tuple containing the estimated upper edge of the noise eigenvalue spectrum (e_max
) and the fitted scale parameter (x
).
Validation
- Uses Optim.optimize to fit the MP distribution.
- Assumes
vals
are eigenvalues of a correlation or covariance matrix.
Related