Distance: private API
PortfolioOptimisers.LTDCov_AllInternalLTDCov — Type
const LTDCov_AllInternalLTDCov = Union{<:LowerTailDependenceCovariance,
<:PortfolioOptimisersCovariance{<:LowerTailDependenceCovariance}}Alias for all internal lower tail dependence covariance estimator types.
Matches LowerTailDependenceCovariance or any PortfolioOptimisersCovariance wrapping it. Used internally for dispatch in distance computation.
Related
PortfolioOptimisers.AllInternalMutualInfoCov — Type
const AllInternalMutualInfoCov = Union{<:PortfolioOptimisersCovariance{<:MutualInfoCovariance}}Alias for all internal mutual information covariance wrapper types.
Matches any PortfolioOptimisersCovariance wrapping a MutualInfoCovariance. Used internally for dispatch in canonical distance computation.
Related
PortfolioOptimisers.DistCov_AllInternalDistCov — Type
const DistCov_AllInternalDistCov = Union{<:DistanceCovariance,
<:PortfolioOptimisersCovariance{<:DistanceCovariance}}Alias for all internal distance covariance estimator types.
Matches DistanceCovariance or any PortfolioOptimisersCovariance wrapping it. Used internally for dispatch in canonical distance computation.
Related
PortfolioOptimisers.RhoDistanceAlgorithm — Type
const RhoDistanceAlgorithm = Union{SimpleDistance, SimpleAbsoluteDistance,
LogDistance, CorrelationDistance}Union of the correlation-based distance algorithms: those whose distance matrix is a pure function of a correlation matrix via _dist_from_cor. Excludes VariationInfoDistance (information-theoretic, computed from the data matrix) and CanonicalDistance (a redirect that selects one of the others from the covariance estimator).
Related
PortfolioOptimisers._as_correlation — Function
_as_correlation(rho::MatNum, sym::Symbol = :rho) -> MatNumCoerce a square matrix to a correlation matrix, converting it from a covariance matrix when its diagonal says it is one.
The value of the diagonal decides, never the type. A matrix whose diagonal is all ones is already a correlation matrix and is returned as the same object; any other diagonal is read as the variances of a covariance matrix. This is the same test the matrix processing pipeline applies, so the two layers agree on what a correlation matrix is. The square-matrix check runs here, once, for every correlation-based algorithm's matrix entry point. The conversion round-trips: the correlation of a covariance matrix built from a correlation matrix and a vector of standard deviations is that correlation matrix again.
Algorithm
- Check that
rhois square, reporting the failure under the namesym. - Read the diagonal of
rhointos.LinearAlgebra.diagallocates, sorhois never written to. - When every entry of
sis one, returnrhoitself. Steps 4 and 5 do not run. - Otherwise replace
swith its square roots, giving the standard deviations. - Divide
rhoby the outer product ofswithStatsBase.cov2cor, giving a new correlation matrix.
Arguments
rho: Correlation matrixassets × assets, or the covariance matrix to convert. It is never mutated on either route: step 2 copies the diagonal, and step 5 builds a new matrix.sym: Name to report the square-matrix failure under.
Validation
rhois square.
Returns
rho::MatNum: Correlation matrixassets x assets.
Related
PortfolioOptimisers._absguard — Function
_absguard(rho::MatNum) -> MatNumSupply the magnitude of rho to the two algorithms that are defined on it, without allocating when the magnitude is already rho.
This is an allocation guard, not a branch in the mathematics. abs.(rho) equals rho entry for entry whenever no entry of rho is negative, so both arms return the same numbers for every input, -0.0 included; the guard only decides whether a second matrix is built. Shared by SimpleAbsoluteDistance and LogDistance. A NaN compares false against zero, so a matrix holding one takes the allocating arm; abs(NaN) is NaN, so that entry is NaN on either arm.
Algorithm
- Test every entry of
rhoagainst zero. The test reads the whole matrix, which is the intended reading of the two algorithms: both take the magnitude of every entry. - When no entry is negative, return
rhoitself, the same object the caller passed. - Otherwise return
abs.(rho), a new matrix. One negative entry allocates the copy for all of them.
Arguments
rho: Correlation matrixassets × assets.
Returns
rho::MatNum: The magnitude of the argument. It is the argument itself when the argument holds no negative entry.
Related
PortfolioOptimisers._dist_from_cor — Function
_dist_from_cor(alg::RhoDistanceAlgorithm, power::Option{<:Integer}, rho::MatNum) -> MatNumTurn a correlation matrix into a distance matrix, for one of the four correlation-based algorithms.
This is the shared kernel behind the distance and cor_and_dist entry points: they differ only in how they obtain rho, never in the transform they apply to it. Eight methods cover the four algorithms of RhoDistanceAlgorithm at each of the two power cases. Every method allocates its own result, and clamp! writes only into that allocation, so rho is never mutated.
Mathematical definition
Distance states the eight closed forms and the scaling $s$. Each algorithm's own docstring states the base case and the range it is defined on.
Algorithm
power selects the method, so the base case never raises rho to a power.
SimpleAbsoluteDistanceandLogDistancereplacerhowith its magnitude through_absguard.SimpleDistanceandCorrelationDistancedo not, and read the signed correlation.- When
poweris anInteger, raiserhoto it entry by entry. Whenpowerisnothing, leaverhoas it is. SimpleDistancescales $1 - \rho$ by1//2for an oddpowerand by1//1for an even one, and by1//2in the base case. The scale is aRational, so the element type ofrhois carried through: aFloat32correlation matrix gives aFloat32distance matrix, as it does under the other three algorithms. The other three apply no scaling.- The three square-root algorithms clamp the radicand into $[0,\,1]$ with
clamp!and take its square root.LogDistanceinstead takes $-\log$ and floors the result at zero withmax.
Arguments
alg: Distance algorithm.power: Optional matrix exponent.nothingand1both give the base distance, so onlypower >= 2changes the result.rho: Correlation matrixassets × assets.
Returns
D::MatNum: Distance matrixassets x assets, in the units the distance algorithm defines.
Related
RhoDistanceAlgorithm_absguard_as_correlationDistancedistancecor_and_distCanonicalDistance: never reaches this kernel. It is a redirect that resolves to one of the four before the call.VariationInfoDistance: never reaches this kernel. It reads the data matrix and holds no correlation.
PortfolioOptimisers.assert_dims — Function
assert_dims(dims::Integer)
assert_dims(
dims::Integer,
sym::Union{AbstractString, Symbol}
)
Assert that dims selects a valid matrix dimension (dims in (1, 2)).
Arguments
dims: Dimension selector to check.sym: Symbolic name used in the error message.
Validation
dims in (1, 2), which raises aDomainErrornamingsymanddims.
Returns
nothing.
Related
PortfolioOptimisers.dims_oriented — Function
dims_oriented(
dims::Integer,
A::Union{Nothing, AbstractMatrix}
) -> Any
Validate dims and return the matrices with the observations along the rows.
The guard and the orientation are one call, so a caller cannot orient a matrix without validating dims. This is the single decision point: a leaf that spelled the guard and the transpose by hand could omit the guard and answer a dims of 3 with the raw input.
Algorithm
- Validate
dimswithassert_dims. - Return each matrix untouched when
dimsis1, because the observations already lie along the rows. - Return the
transposeof each matrix whendimsis2. Anothingpasses through unchanged.
Arguments
dims: Dimension along which the observations lie.A,B,Cs...: Matrices to orient. Anothingpasses through unchanged, so an optional matrix needs no branch of its own.
Validation
dims in (1, 2), byassert_dims.
Returns
A: The oriented matrix, when one matrix is given.(A, B, Cs...): A tuple of the oriented matrices, when more than one is given.
Related