Geodesic shrinkage covariance: private API
These are the helpers of GeodesicShrinkageCovariance. geodesic_point computes the shrunk matrix, and geodesic_shrinkage! runs the repair and the shrinkage on the assets with a finite variance.
PortfolioOptimisers.GeodesicShrinkageTarget — Type
const GeodesicShrinkageTarget = Union{<:AbstractCovarianceShrinkageTarget,
<:DiagonalTarget, <:MatNum}Groups the targets a GeodesicShrinkageCovariance accepts: the rules that build the target from the matrix being shrunk, and a fixed positive definite matrix.
The members share no supertype. DiagonalTarget is also a RegimeAdjustedTarget, and a matrix is data. So this union bounds the tgt field, and shrinkage_target dispatches on its members.
Related
PortfolioOptimisers.assert_shrinkage_target — Function
assert_shrinkage_target(
_::Union{DiagonalTarget, AbstractCovarianceShrinkageTarget}
)
Checks a covariance shrinkage target at construction.
A target rule needs no check, because shrinkage_target builds it from the matrix it shrinks. A fixed matrix must be a valid target for every matrix it meets, so this function checks it once, when the estimator is built.
Arguments
tgt: The target.
Validation
- A matrix
tgtis square. The function throws aDimensionMismatchotherwise. - A matrix
tgtis finite, symmetric within the default tolerance ofisapprox, and positive definite. The function throws aDomainErrorotherwise.
Returns
nothing.
Related
PortfolioOptimisers.geodesic_power — Function
geodesic_power(
vals::AbstractVector{<:Union{var"#s136", var"#s53"} where {var"#s136"<:Number, var"#s53"<:AbstractJuMPScalar}},
p::Number
) -> Any
Raises the eigenvalues of a positive semidefinite matrix to a power, and reads an eigenvalue inside the round-off tolerance as zero.
An eigendecomposition of a singular positive semidefinite matrix returns its zero eigenvalues as small numbers of either sign. A power below one makes such a number larger. For example, (1e-17)^0.5 is about 3e-9. The threshold keeps a zero eigenvalue of the start at zero.
Mathematical definition
\[\begin{align} \lambda_i^{(p)} &= \begin{cases} \lambda_i^{p} & \lambda_i > \tau\,, \\ 0 & \lambda_i \leq \tau\,, \end{cases} \\ \tau &= N \, \epsilon \, \max_{j} \lvert \lambda_j \rvert\,. \end{align}\]
Where:
- $\lambda_i^{(p)}$: $i$-th eigenvalue after the power.
- $\lambda_i$: $i$-th eigenvalue of the input matrix.
- $p$: The power, positive.
- $\tau$: Round-off tolerance of a symmetric eigensolver.
- $\epsilon$: Machine epsilon of the element type of the eigenvalues.
- $N$: Number of assets.
An eigenvalue below $-\tau$ belongs to an indefinite matrix, which no geodesic reaches.
Arguments
vals: Eigenvalues of a symmetric matrix.p: The power, positive.
Validation
minimum(vals)is not below $-\tau$. The function throws aDomainErrorotherwise.
Returns
vals_p::VecNum: The eigenvalues raised top, with every eigenvalue inside the tolerance at zero.
Related
PortfolioOptimisers.geodesic_point — Function
geodesic_point(
_::ScaledIdentityTarget,
sigma::AbstractMatrix{<:Union{var"#s136", var"#s53"} where {var"#s136"<:Number, var"#s53"<:AbstractJuMPScalar}},
alpha::Number
) -> Any
Computes the point at intensity alpha on the geodesic from sigma to the target of tgt.
Algorithm
For ScaledIdentityTarget:
- Take the average variance
vfrom the trace ofsigma. - At
alpha = 1, returnvtimes the identity matrix, as aDiagonal. - Eigendecompose
sigma, givingvalsandvecs. - Raise
valsto1 - alphawithgeodesic_powerand multiply them byv ^ alpha. - Rebuild the matrix from
vecsand the new eigenvalues, and return its symmetric part.
For every other target:
- Build the target matrix
tgt_matwithshrinkage_target. - At
alpha = 1, return a copy oftgt_mat. - Factor
tgt_matby Cholesky intochol, with lower factorL. Refuse atgt_matthat is not finite, or whose factorisation fails. ADiagonaltarget factors intoDiagonalfactors, so its whitening only rescales the rows and columns ofsigma. - Eigendecompose the whitened matrix
L \ sigma / L', givingvalsandvecs. - Take the basis
basis = L * vecs, raisevalsto1 - alphawithgeodesic_power, rebuild the matrix frombasisand the new eigenvalues, and return its symmetric part.
Arguments
tgt: The target.sigma: Covariance-like or correlation-like matrixassets × assets.alpha: Shrinkage intensity, strictly positive.
Validation
- Every check of
shrinkage_targetapplies. - The target matrix is finite and positive definite. The function throws a
DomainErrorotherwise. A zero variance insigmamakes the target ofConstantCorrelationTargetnot finite, and the target ofDiagonalTargetsingular. sigmais positive semidefinite, asgeodesic_powerstates.
Returns
sigma_alpha::MatNum: A new matrix, the point on the geodesic.
Related
PortfolioOptimisers.geodesic_shrinkage! — Function
geodesic_shrinkage!(
ce::GeodesicShrinkageCovariance,
sigma::AbstractMatrix{<:Union{var"#s136", var"#s53"} where {var"#s136"<:Number, var"#s53"<:AbstractJuMPScalar}}
) -> AbstractMatrix{<:Union{var"#s136", var"#s53"} where {var"#s136"<:Number, var"#s53"<:AbstractJuMPScalar}}
Repairs and shrinks the finite block of a covariance matrix in place, and leaves the frame of NaN around it alone.
An inner estimator that reads the gaps of a sample returns NaN for an asset it could not estimate. The repair and the shrinkage cannot take a NaN. So they run on the block of the assets whose variance is finite, and the NaN rows and columns of the frame stay as they are.
Algorithm
- Take the block
blkas the assets whose variance on the diagonal ofsigmais finite. Returnsigmawhenblkholds no asset. - Refuse a non-finite entry inside the block with
assert_finite_block. - Copy the block out as
block, or takesigmaitself whenblkholds every asset. - Repair
blockwithposdef!underce.pdm. - When
ce.alphais positive, write the point atce.alphaon the geodesic intoblock, withgeodesic_point. - Write
blockback intosigma, and returnsigma.
Arguments
ce: The geodesic shrinkage covariance estimator.sigma: Covariance-like or correlation-like matrixassets × assets.
Validation
- The block of
sigmais finite. The function throws anIsNonFiniteErrorotherwise. - Every check of
geodesic_pointapplies to the block.
Returns
sigma::MatNum: The input matrix, with its block repaired and shrunk in place.
Related
GeodesicShrinkageCovariancegeodesic_pointmatrix_processing_block!: the same rule for a matrix processing estimator.
PortfolioOptimisers.frame_cov2cor! — Function
frame_cov2cor!(
sigma::AbstractMatrix{<:Union{var"#s136", var"#s53"} where {var"#s136"<:Number, var"#s53"<:AbstractJuMPScalar}}
) -> AbstractMatrix{<:Union{var"#s136", var"#s53"} where {var"#s136"<:Number, var"#s53"<:AbstractJuMPScalar}}
Rescales a covariance matrix to its correlation matrix in place, and keeps the NaN of an asset whose variance is not finite.
StatsBase.cov2cor! writes one on the whole diagonal, so it turns the NaN variance of an asset outside the Coverage Universe into a correlation of one. The entries off the diagonal of such an asset are already NaN, so this function writes the NaN back on the diagonal alone.
Algorithm
- Take the standard deviations
sfrom the diagonal ofsigma. - Rescale
sigmawithStatsBase.cov2cor!ands. - Write
s[i]back onto the diagonal wherevers[i]is not finite.
Arguments
sigma: Covariance-like or correlation-like matrixassets × assets.
Returns
rho::MatNum: The input matrix, rescaled in place to a correlation matrix.
Related
PortfolioOptimisers.gap_fill_value — Method
gap_fill_value(ce::GeodesicShrinkageCovariance) -> NumberReturns the gap fill value of ce.ce, because the estimator forwards the sample and its keywords unchanged.
The shrinkage reads the matrix that ce.ce returns and no cell of the sample. So the inner estimator alone sets what a gap becomes.
Arguments
ce: The geodesic shrinkage covariance estimator.
Returns
fv::Number:gap_fill_valueofce.ce.
Related
PortfolioOptimisers.supports_partial_fit — Method
supports_partial_fit(ce::GeodesicShrinkageCovariance) -> Any
GeodesicShrinkageCovariance method of supports_partial_fit.
The estimator folds through ce.ce, so it folds when ce.ce folds.
Arguments
ce: The geodesic shrinkage covariance estimator.
Returns
folds::Bool:truewhenpartial_fit!foldsce.ce.
Related