S covariance: private API
PortfolioOptimisers.S_MAX_ITER — Constant
S_MAX_ITERThe largest number of iterations of one fixed point of an S or MM fit, 1000.
The I-steps of s_isteps, the steps of the M-scale of m_scale, and the steps of the M-step of bisquare_mstep each stop at this bound. A fit of real data converges in a few tens of steps.
Related
PortfolioOptimisers.show_fields — Method
show_fields(_::SCovariance) -> NTuple{5, Symbol}
Renders every field of an SCovariance except cache.
A cache holds the state of an incremental fit, not a part of the configuration. Set set_show_nothing_fields!(:SCovariance, true) to render it.
Arguments
::SCovariance: The estimator, read for its type alone.
Returns
fields::Tuple: The names of the fields to render,(:me, :rho, :bdp, :alg, :q).
Related
PortfolioOptimisers.srho_fit — Function
srho_fit(rho::AbstractSRho, X::MatNum, mu::VecNum, sigma::MatNum, k::Number, q::Number)
-> RobustCovarianceResultBuilds the fit of an S or MM estimate from its location and its covariance.
The weight of an observation is srho_weight at its squared distance in the metric of sigma, from squared_distances. The covariance of the fit is sigma times the factor of s_consistency, and the squared distances of the fit are in its metric. The flag of an observation is the one of robust_outliers at q.
Arguments
rho: The rho function of the weights.X: The data matrix,observations × assets.mu: The location.sigma: The covariance, the scale times the shape, before the consistency factor.k: The tuning constant of the rho function.q: The probability of the cut-off of the outliers.
Returns
res::RobustCovarianceResult: The fit.
Related
PortfolioOptimisers.s_search — Function
s_search(alg::AbstractSAlgorithm, rho::AbstractSRho, X::MatNum, centre::Option{<:VecNum},
h::Integer, k::Number, b::Number, maxsteps::Integer) -> Tuple{VecNum, MatNum, Number}Returns the location, the shape of determinant one and the M-scale of the best estimate that the search alg finds.
Algorithm
- Take the starts with
s_starts, and keep the best of a search with many starts withs_refine. - Do at most
maxstepsI-steps from each start withs_isteps, until the estimate does not change. - Return the estimate with the smallest M-scale. The first start wins a tie. Warn when that estimate did not converge.
Arguments
alg: The search algorithm.rho: The rho function.X: The data matrix,observations × assets.centre: The fixed centre, ornothing.h: The size of a start subset.k: The tuning constant of the rho function.b: The right side of the equation of the M-scale.maxsteps: The largest number of I-steps from one start,S_MAX_ITERin a fit.
Returns
(mu, V, s): The location, the shape and the scale.
Related
PortfolioOptimisers.fasts_random_start — Function
fasts_random_start(rng::Random.AbstractRNG, X::MatNum, centre::Option{<:VecNum})
-> Tuple{VecNum, MatNum}Draws one start of FastS.
The function draws $p + 1$ observations with no repetition. While their covariance matrix is singular, it adds one more observation that it draws from the rest, as fastmcd_random_start does. The start is the location and the scatter of the drawn observations, from mcd_fit_rows.
Arguments
rng: The random number generator.X: The data matrix,observations × assets.centre: The fixed centre, ornothing.
Returns
(mu, S): The location and the scatter of the start.
Related
PortfolioOptimisers.s_isteps — Function
s_isteps(rho::AbstractSRho, X::MatNum, mu::VecNum, S::MatNum, fixed::Bool, k::Number,
b::Number, maxsteps::Integer) -> Tuple{VecNum, MatNum, Number, Bool}Does I-steps of an S estimate from the location mu and the scatter S.
An I-step is a step of the fixed-point equations of [129], which state that an S estimate is an M-estimate with the weights of its own rho function. For a rho function that is concave in $t$, such as BisquareRho, the step does not increase the M-scale. RockeRho is convex below $t = 1$, so the step has no such bound, and the search keeps the start with the smallest M-scale. It stops when the relative change of the shape and the change of the location in the metric of the scatter are both below sqrt(eps), or after maxsteps steps.
Algorithm
- Scale
Sto the determinant one withunit_determinant!. Compute the squared distancestwithsquared_distances, and their M-scaleswithm_scale, from the start $\mathrm{med}(t)$. - Repeat at most
maxstepstimes:- Take the weight of each observation,
srho_weightatt / s. - Take the weighted mean and the weighted scatter of determinant one with
weighted_shape. - Compute the new distances, and their M-scale from the start
s.
- Take the weight of each observation,
Arguments
rho: The rho function.X: The data matrix,observations × assets.mu: The start location.S: The start scatter. The method does not change it.fixed: Whether the centre is fixed atmu.k: The tuning constant of the rho function.b: The right side of the equation of the M-scale.maxsteps: The largest number of steps.
Returns
(mu, V, s, done): The location, the shape of determinant one, the M-scale, and whether the steps converged.
Related
References
- [129] H. P. Lopuhaä. On the relation between S-estimators and M-estimators of multivariate location and covariance. The Annals of Statistics 17, 1662–1683 (1989). Not read. The fixed point agrees with the R package
rrcov, version 1.7-7.
PortfolioOptimisers.shape_converged — Function
shape_converged(mu::VecNum, V::MatNum, mun::VecNum, Vn::MatNum, s::Number, tol::Number) -> BoolReturns whether a step of a fixed point moved the location and the shape by less than tol.
The relative change of the shape is in the Frobenius norm. The change of the location is its distance in the metric of the old scatter $s \mathbf{V}$, so the test does not change when the data are multiplied by a constant.
Arguments
mu,V: The old location and shape.mun,Vn: The new location and shape.s: The scale of the shape, so that $s \mathbf{V}$ is the scatter.tol: The tolerance.
Returns
done::Bool: Whether both changes are belowtol.
Related
PortfolioOptimisers.weighted_shape — Function
weighted_shape(X::MatNum, w::VecNum, mu::VecNum, fixed::Bool) -> Tuple{VecNum, MatNum}Returns the weighted mean of the observations and their weighted scatter about it, scaled to the determinant one.
With a fixed centre, the location stays at mu, and the scatter is about mu. A common factor of the weights does not change the result.
Arguments
X: The data matrix,observations × assets.w: The weight of each observation.mu: The location, used whenfixedistrue.fixed: Whether the centre is fixed atmu.
Returns
(mu, V): The location and the shape.
Related
PortfolioOptimisers.unit_determinant! — Function
unit_determinant!(S::MatNum) -> MatNumScales a scatter matrix in place to the determinant one.
Arguments
S: The scatter matrix.
Validation
Sis positive definite, asscatter_choleskyreads it. Otherwise, the function raises aDomainErrorthat states that the weighted observations lie on a hyperplane.
Returns
S::MatNum: The input matrix, scaled.
Related
References
- [129]
- H. P. Lopuhaä. On the relation between S-estimators and M-estimators of multivariate location and covariance. The Annals of Statistics 17, 1662–1683 (1989).