Tyler covariance: private API
PortfolioOptimisers.show_fields — Method
show_fields(_::TylerCovariance) -> NTuple{5, Symbol}
Renders every field of a TylerCovariance except cache.
A cache holds the state of an incremental fit, not a part of the configuration. Set set_show_nothing_fields!(:TylerCovariance, true) to render it.
Arguments
::TylerCovariance: The estimator, read for its type alone.
Returns
fields::Tuple: The names of the fields to render,(:me, :ve, :shr, :w, :q).
Related
PortfolioOptimisers.TYLER_MAX_ITER — Constant
PortfolioOptimisers.away_from_centre — Function
away_from_centre(d2::VecNum) -> BitVectorMarks the observations that have a direction from the centre in a fit of TylerCovariance.
An observation at the centre has the squared distance zero, so its term in the equations of the fixed point is not defined. The function reads a distance of at most eps times the median of the distances as zero.
Arguments
d2: The squared distance of each observation.
Returns
pos::BitVector: The observations away from the centre.
Related
PortfolioOptimisers.tyler_fixed_point — Function
tyler_fixed_point(X::MatNum, w::Option{<:StatsBase.AbstractWeights},
centre::Option{<:VecNum}, reg::Option{<:ShapeShrinkage})
-> Tuple{VecNum, MatNum}Solves the fixed-point equations of TylerCovariance for the shape and, with no fixed centre, for the location.
Algorithm
- Start the location at the coordinatewise weighted median, or at
centre. Start the shape at the diagonal matrix of the squares of the robust scales of the centred columns, fromrobust_column_scale, scaled to the tracep. The start does not move the fixed point, and an outlier of any size does not make it singular. - Repeat at most
TYLER_MAX_ITERtimes:- Compute the squared distances
d2in the metric of the shape withsquared_distances. An observation thataway_from_centredoes not mark leaves both sums. - Update the shape with
tyler_shape_step, and scale it to the tracep, or shrink it withshrink_shape!. - With no fixed centre, update the location with
spatial_median_step. - Stop when the relative change of the shape and the change of the location in the metric of the shape are both below
sqrt(eps).
- Compute the squared distances
- Warn when the iterations do not converge, and return the last iterate.
Arguments
X: The data matrix,observations × assets.w: The observation weights, ornothing.centre: The fixed centre, ornothing.reg: The shrinkage to the identity matrix, ornothing.
Returns
(mu, V): The location and the shape. The shape has the tracep, unless the form ofregisPenalisedShrinkage.
Related
References
PortfolioOptimisers.observation_median — Function
observation_median(x::VecNum, w::Option{<:StatsBase.AbstractWeights}) -> NumberReturns the median of x, weighted by the observation weights w when they are not nothing.
The weighted median is the one of StatsBase. Under StatsBase.FrequencyWeights it is the median of the data with each value repeated by its weight.
Arguments
x: The values.w: The observation weights, ornothing.
Returns
m::Number: The median.
Related
PortfolioOptimisers.trace_normalise! — Function
trace_normalise!(V::MatNum) -> MatNumScales the shape matrix V in place to the trace p, its number of rows.
Arguments
V: The shape matrix.
Returns
V::MatNum: The input matrix, scaled.
Related
PortfolioOptimisers.tyler_shape_step — Function
tyler_shape_step(D::MatNum, d2::VecNum, wt::VecNum, pos::AbstractVector{Bool}) -> MatrixReturns one step of the shape equation of TylerCovariance: $p \sum_t w_t \boldsymbol{x}_t \boldsymbol{x}_t^\intercal / d_t^2 \, / \sum_t w_t$ over the observations of pos.
The step multiplies each row by the square root of its weight and takes one product of the result with itself, so the matrix is exactly symmetric.
Arguments
D: The data minus the location,observations × assets.d2: The squared distance of each observation.wt: The weight of each observation.pos: The observations away from the centre.
Returns
V::Matrix: The new shape, before the normalisation.
Related
PortfolioOptimisers.spatial_median_step — Function
spatial_median_step(X::MatNum, mu::VecNum, V::MatNum, d2::VecNum, wt::VecNum,
pos::AbstractVector{Bool}) -> VectorReturns one step of the weighted spatial median in the metric of the shape V, the location equation of TylerCovariance.
The step is the Weiszfeld step $\tilde{\boldsymbol{\mu}} = \sum_t (w_t/d_t) \boldsymbol{x}_t / \sum_t (w_t/d_t)$ over the observations away from the centre. When some observations are at the centre, with the total weight $\eta$, the step is the modification of Vardi and Zhang: $\max(0, 1 - \eta/r) \tilde{\boldsymbol{\mu}} + \min(1, \eta/r) \boldsymbol{\mu}$, where $r$ is the length of $\sum_t w_t (\boldsymbol{x}_t - \boldsymbol{\mu})/d_t$ in the metric of V. The R package ICSNP, version 1.2-0, takes the same step in its spatial median.
Arguments
X: The data matrix,observations × assets.mu: The location.V: The shape.d2: The squared distance of each observation.wt: The weight of each observation.pos: The observations away from the centre.
Returns
mu::Vector: The new location.
Related
References
- [123] T. P. Hettmansperger and R. H. Randles. A practical affine equivariant multivariate median. Biometrika 89, 851–860 (2002).
PortfolioOptimisers.tyler_scale — Function
tyler_scale(ve::Nothing, X::MatNum, mu::VecNum, V::MatNum, w::Option{<:StatsBase.AbstractWeights},
v::Nothing; kwargs...) -> Matrix
tyler_scale(ve::Nothing, X::MatNum, mu::VecNum, V::MatNum, w::Option{<:StatsBase.AbstractWeights},
v::VecNum; kwargs...) -> Matrix
tyler_scale(ve::StatsBase.CovarianceEstimator, X::MatNum, mu::VecNum, V::MatNum,
w::Option{<:StatsBase.AbstractWeights}, v::Option{<:VecNum}; kwargs...) -> MatrixScales the shape of TylerCovariance to a covariance matrix.
The first nothing method multiplies the shape by the weighted median of the squared distances in its metric, divided by the median of the $\chi^2_p$ distribution. At the normal model, half of the squared distances in the metric of the covariance are below that median, so the scale is consistent. The second nothing method is the one of a shrunk shape. It scales the trace to the sum of the robust variances v. The estimator method keeps the correlation of the shape and takes the standard deviations from std(ve, X), through library_covariance_estimator for an estimator of another package.
Arguments
ve: The variance estimator, ornothing.X: The data matrix,observations × assets.mu: The location.V: The shape.w: The observation weights, ornothing.v: The robust variance of each asset for a shrunk shape, ornothing.kwargs...: More keyword arguments forstd(ve, X).
Returns
sigma::Matrix: The covariance matrix.
Related
References
- [122]
- D. E. Tyler. A distribution-free M-estimator of multivariate scatter. The Annals of Statistics 15, 234–251 (1987).
- [123]
- T. P. Hettmansperger and R. H. Randles. A practical affine equivariant multivariate median. Biometrika 89, 851–860 (2002).