MCD search: private API

PortfolioOptimisers.MCD_MAX_CSTEPS — Constant
MCD_MAX_CSTEPS

The largest number of concentration steps of one start of an MCD search, 200.

The concentration steps of mcd_csteps stop when the subset does not change. Theorem 1 of [120] states that the determinant does not increase in a step, and the number of subsets is finite, so the steps converge. A start of real data converges in a few steps. The bound is the one of the R package robustbase, version 0.99-7.

Related

source
PortfolioOptimisers.mcd_standardise — Function
mcd_standardise(X::MatNum, centre::Option{<:VecNum}) -> Tuple{MatNum, VecNum}

Centres each column of the data and divides it by its robust scale.

The search of the MCD subset is affine equivariant, so the standardisation does not change the subset that a concentration step selects. It keeps the numbers near one, and the six starts of DetMCD read the standardised data.

A value that is further than eps^(-1/4) scales from the median of its column, 8192 scales in double precision, moves to that bound. Such a value is an outlier at either place. But the covariance matrix of a subset that holds an outlier larger than about eps^(-1/2) scales is singular in the arithmetic, and the search then stops although the rest of the data are regular. The search alone reads the standardised data, so the bound does not enter the estimate.

Algorithm

  1. Refuse a constant column.
  2. Take the centre c of each column: the median m when centre is nothing, and centre otherwise.
  3. Take the scale s of each centred column with robust_column_scale.
  4. Clamp each column to m ± s * eps^(-1/4). Subtract c, and divide by s.

Arguments

  • X: The data matrix, observations × assets.
  • centre: A fixed centre, or nothing for the median of each column.

Validation

  • No column of X is constant. Otherwise, the function raises a DomainError, because the covariance matrix of each subset is singular.

Returns

  • (Z, s): The standardised data, and the scale of each column.

Related

source
PortfolioOptimisers.mcd_subset_fit — Function
mcd_subset_fit(Z::MatNum, idx::AbstractVector{<:Integer}, fixed::Bool) -> Tuple{VecNum, MatNum}

Returns the mean and the covariance matrix of the observations idx of Z.

When fixed is false, the centre is the mean of the subset and the divisor is $h - 1$, as the code of the R package robustbase, version 0.99-7, has it. When fixed is true, the centre is zero and the divisor is $h$, because the subset estimates no centre.

Arguments

  • Z: The standardised data, observations × assets.
  • idx: The positions of the observations of the subset.
  • fixed: Whether the centre is fixed at zero.

Returns

  • (m, S): The centre and the covariance matrix of the subset.

Related

source
PortfolioOptimisers.SubsetShrinkage — Type
struct SubsetShrinkage{__T_rho, __T_c, __T_tgt}

Holds the blend of the scatter of a subset with the target of an MCDShrinkage.

Fields

  • rho: Shrinkage intensity of the fit, the weight of the target.
  • c: Factor of the scatter of the subset.
  • tgt: Target of the shrinkage, a positive definite matrix.

Constructors

SubsetShrinkage(    rho::Number,    c::Number,    tgt::MatNum) -> SubsetShrinkage

Related

source
PortfolioOptimisers.shrink_scatter! — Function
shrink_scatter!(S::MatNum, reg::Nothing) -> MatNum
shrink_scatter!(S::MatNum, c::Number) -> MatNum
shrink_scatter!(S::MatNum, reg::SubsetShrinkage) -> MatNum

Scales the scatter of a subset in place, and blends it with the target.

The nothing method returns the scatter of a search with no shrinkage, where a common factor does not change the subset. The number method multiplies the scatter by the factor c. The SubsetShrinkage method gives $\rho \mathbf{T} + (1 - \rho) c \mathbf{S}$, Equation 7 of [112].

Arguments

  • S: The scatter of the subset.
  • reg, c: The shrinkage, the factor, or nothing.

Returns

  • S::MatNum: The input matrix.

Related

References

  • [112] K. Boudt, P. J. Rousseeuw, S. Vanduffel and T. Verdonck. The minimum regularized covariance determinant estimator. Statistics and Computing 30, 113–128 (2020). Section 2.2, Equation 7.
source
PortfolioOptimisers.scatter_cholesky — Function
scatter_cholesky(S::MatNum) -> Option{<:LinearAlgebra.Cholesky}

Returns the Cholesky factorisation of a scatter matrix, or nothing when the matrix is singular.

A matrix of rank p - 1 can pass the factorisation in the arithmetic, with a pivot of the size of the rounding error. A pivot over its diagonal entry is the share of the variance of a variable that the variables before it do not explain. So the function reads a share of at most p * eps as zero. The rule does not depend on the units of the variables.

Arguments

  • S: The scatter matrix.

Returns

  • C: The factorisation, or nothing.

Related

source
PortfolioOptimisers.squared_distances — Function
squared_distances(Z::MatNum, m::VecNum, S::MatNum) -> VecNum

Returns the squared Mahalanobis distance of each observation of Z from the centre m in the metric of S.

Mathematical definition

\[\begin{align} d_t^2 &= (\boldsymbol{z}_t - \boldsymbol{m})^\intercal \mathbf{S}^{-1} (\boldsymbol{z}_t - \boldsymbol{m})\,. \end{align}\]

Arguments

  • Z: The data, observations × assets.
  • m: The centre.
  • S: The scatter matrix. It must be positive definite.

Validation

  • S is positive definite, as scatter_cholesky reads it. Otherwise, the function raises a DomainError that states that the observations lie on a hyperplane.

Returns

  • d2::VecNum: The squared distance of each observation.

Related

source
PortfolioOptimisers.nearest_positions — Function
nearest_positions(d::VecNum, h::Integer) -> Vector{Int}

Returns the positions of the h smallest values of d, in increasing order of the values.

The function sorts the positions by their values, only up to the position h. The order of two equal values is not fixed.

Arguments

  • d: The values, such as the squared distances of the observations.
  • h: The number of positions.

Returns

  • idx::Vector{Int}: The positions.

Related

source
PortfolioOptimisers.mcd_csteps — Function
mcd_csteps(Z::MatNum, idx::AbstractVector{<:Integer}, h::Integer, fixed::Bool,
           reg::Option{<:SubsetShrinkage}, maxsteps::Integer) -> Tuple{Vector{Int}, Number}

Does concentration steps from the subset idx and returns the last subset and the logarithm of the determinant of its covariance matrix.

A concentration step fits the mean and the covariance matrix of the subset, computes the distance of every observation, and takes the h nearest observations as the new subset. Theorem 1 of [120] states that the determinant of the new covariance matrix is not larger than the old one. Theorem 1 of [112] states the same for a shrunk scatter. The steps stop when the subset does not change, or after maxsteps steps.

Algorithm

  1. Fit the subset with mcd_subset_fit, and shrink its scatter with shrink_scatter!.
  2. Repeat at most maxsteps times. Compute the distances with squared_distances. Take the h nearest observations with a partial sort. Stop if this subset is the old subset. Otherwise, fit the new subset.
  3. Return the subset and the logarithm of the determinant of its covariance matrix. The logarithm is -Inf for a singular matrix, see scatter_cholesky.

Arguments

  • Z: The standardised data, observations × assets.
  • idx: The positions of the start subset, h of them.
  • h: The size of the subset.
  • fixed: Whether the centre is fixed at zero.
  • reg: The SubsetShrinkage of the subset scatter, or nothing.
  • maxsteps: The largest number of steps.

Returns

  • (idx, obj): The sorted positions of the last subset, and the logarithm of the determinant of its covariance matrix.

Related

References

  • [120] P. J. Rousseeuw and K. Van Driessen. A fast algorithm for the minimum covariance determinant estimator. Technometrics 41, 212–223 (1999). Section 2, Theorem 1.
  • [112] K. Boudt, P. J. Rousseeuw, S. Vanduffel and T. Verdonck. The minimum regularized covariance determinant estimator. Statistics and Computing 30, 113–128 (2020). Section 3, Theorem 1.
source
PortfolioOptimisers.mcd_univariate_subset — Function
mcd_univariate_subset(z::VecNum, h::Integer, fixed::Bool) -> Vector{Int}

Returns the exact MCD subset of one variable.

The covariance matrix of one variable is its variance. The subset of h values with the smallest variance is a run of h consecutive values of the sorted data, so a pass over the n - h + 1 runs finds it. Each run holds the value of rank h, because 2h > n. So the sums of a run are a sum to the left of that value plus a sum to its right, and an outlier outside the run does not enter them. When the centre is fixed at zero, the subset is the h values with the smallest absolute value.

Arguments

  • z: The standardised values of the variable.
  • h: The size of the subset.
  • fixed: Whether the centre is fixed at zero.

Returns

  • idx::Vector{Int}: The positions of the subset.

Related

source
PortfolioOptimisers.mcd_search — Function
mcd_search(alg::AbstractMCDAlgorithm, smp::MCDSample, h::Integer, c::Number) -> Tuple{Vector{Int}, Option{<:Number}}

Returns the positions of the best subset of h observations that the search alg finds, and the shrinkage intensity.

The data of one variable have an exact subset, which mcd_univariate_subset finds for every search. A shrinkage does not change it. A subset of every observation needs no search.

Algorithm

  1. When h is the number of observations, or the data have one column, take every position, or the exact subset of mcd_univariate_subset. The intensity is the one of mcd_intensity for this subset.
  2. Otherwise, take the starts of the search with mcd_starts, and the intensity and the starts that it permits with pooled_intensity.
  3. Keep the best starts of a search with many starts with mcd_refine.
  4. Do concentration steps from each start with mcd_csteps until the subset does not change, and return the subset with the smallest determinant. The first start wins a tie.

Arguments

  • alg: The search algorithm.
  • smp: The data of the fit.
  • h: The size of the subset.
  • c: The consistency factor of the scatter of the subset.

Returns

  • (idx, rho): The sorted positions of the best subset, and the intensity, or nothing with no shrinkage.

Related

References

  • [112] K. Boudt, P. J. Rousseeuw, S. Vanduffel and T. Verdonck. The minimum regularized covariance determinant estimator. Statistics and Computing 30, 113–128 (2020). Section 3, step 3 of the algorithm.
source
PortfolioOptimisers.best_mcd_subset — Function
best_mcd_subset(Z::MatNum, starts, h::Integer, fixed::Bool,
                reg::Option{<:SubsetShrinkage}) -> Vector{Int}

Does concentration steps from each start until the subset does not change, and returns the subset with the smallest determinant.

Arguments

  • Z: The standardised data.
  • starts: The start subsets.
  • h: The size of the subset.
  • fixed: Whether the centre is fixed at zero.
  • reg: The SubsetShrinkage of the subset scatter, or nothing.

Returns

  • idx::Vector{Int}: The sorted positions of the best subset. The first start wins a tie.

Related

source
PortfolioOptimisers.fastmcd_random_start — Function
fastmcd_random_start(rng::Random.AbstractRNG, Z::MatNum, h::Integer, fixed::Bool,
                     intensity::Nothing) -> Vector{Int}
fastmcd_random_start(rng::Random.AbstractRNG, Z::MatNum, h::Integer, fixed::Bool,
                     intensity::Union{Number, AbstractSubsetShrinkageIntensity}) -> Vector{Int}

Draws one start subset of FastMCD.

The method with no shrinkage draws $p + 1$ observations with no repetition. While their covariance matrix is singular, it adds one more observation that it draws from the rest. Section 3.1 of [120] gives this rule. The start is then the h observations nearest to the mean of the drawn observations in the metric of their covariance matrix.

With a shrinkage, the scatter of the drawn observations is not necessary, because the target gives a metric. The method draws two observations, the fewest that have a spread, so a start with no outlier is likely. The start is the h observations nearest to their mean in the metric of the target. With a fixed centre, that metric gives the same start for every draw. So the metric is then the sum of the target and of the covariance matrix of the two observations about the centre.

Arguments

  • rng: The random number generator.
  • Z: The standardised data.
  • h: The size of the subset.
  • fixed: Whether the centre is fixed at zero.
  • intensity: The shrinkage intensity or its rule, or nothing.

Validation

  • With no shrinkage, the covariance matrix of the data is not singular. Otherwise, the method raises the DomainError of squared_distances.

Returns

  • idx::Vector{Int}: The positions of the start subset.

Related

References

  • [120] P. J. Rousseeuw and K. Van Driessen. A fast algorithm for the minimum covariance determinant estimator. Technometrics 41, 212–223 (1999). Section 3.1.
source

References

[112]
K. Boudt, P. J. Rousseeuw, S. Vanduffel and T. Verdonck. The minimum regularized covariance determinant estimator. Statistics and Computing 30, 113–128 (2020).
[120]
P. J. Rousseeuw and K. Van Driessen. A fast algorithm for the minimum covariance determinant estimator. Technometrics 41, 212–223 (1999).