Skip to content
13

Distances of Distances

PortfolioOptimisers.DistanceDistance Type
julia
struct DistanceDistance{__T_metric, __T_args, __T_kwargs, __T_power, __T_alg} <: AbstractDistanceEstimator

Distance-of-distances estimator for portfolio optimization.

DistanceDistance wraps a distance metric from Distances.jl and a distance algorithm, allowing you to compute a "distance of distances" matrix. If power is not nothing, it computes the generalised distance matrix, which is then used to compute the distances of distances matrix.

gd~i,j=g\bmDig\bmDj,

where gd~ is the general distance of distances, g\bmDi is the row corresponding to asset i of the general distance matrix computed using the specified distance algorithm AbstractDistanceAlgorithm, is the metric used to compute the distance of distances.

Fields

  • metric: Distance metric used for the distances of distances computations.

  • args: Additional positional arguments for the distances of distances metric.

  • kwargs: Additional keyword arguments for the distances of distances metric.

  • power: Optional matrix exponent.

  • alg: Distance algorithm.

Constructors

julia
DistanceDistance(;
    metric::Distances.Metric = Distances.Euclidean(),
    args::Tuple = (),
    kwargs::NamedTuple = (;),
    power::Option{<:Integer} = 1,
    alg::AbstractDistanceAlgorithm = SimpleDistance()
) -> DistanceDistance

Keywords correspond to the struct's fields.

Validation

  • If power is not nothing, power >= 1.

Examples

julia
julia> DistanceDistance()
DistanceDistance
  metric ┼ Distances.Euclidean: Distances.Euclidean(0.0)
    args ┼ Tuple{}: ()
  kwargs ┼ @NamedTuple{}: NamedTuple()
   power ┼ nothing
     alg ┴ SimpleDistance()

Related

source
PortfolioOptimisers.distance Method
julia
distance(de::DistanceDistance, ce::StatsBase.CovarianceEstimator, X::MatNum;
         dims::Int = 1, kwargs...)

Compute the distance-of-distances matrix from a covariance estimator and data matrix.

This method first computes a base distance matrix using Distance with the specified power and algorithm, then applies the provided metric to compute a second-level distance matrix.

Arguments

  • de: Distance-of-distances estimator.

  • ce: Covariance estimator.

  • X: Data matrix (observations × features).

  • dims: Dimension along which to perform the computation.

  • kwargs...: Additional keyword arguments passed to the base distance computation.

Returns

  • D::Matrix{<:Number}: Matrix of pairwise distances of distances.

Related

source
PortfolioOptimisers.distance Method
julia
distance(de::DistanceDistance, rho::MatNum, args...; kwargs...)

Compute the distance-of-distances matrix from a correlation or covariance matrix.

This method first computes a base distance matrix using Distance with the specified power and algorithm, then applies the provided metric to compute a second-level distance matrix.

Arguments

  • de: Distance-of-distances estimator.

  • rho: Correlation or covariance matrix.

  • args...: Additional arguments (ignored).

  • kwargs...: Additional keyword arguments passed to the base distance computation.

Returns

  • D::Matrix{<:Number}: Matrix of pairwise distances of distances.

Related

julia
source
PortfolioOptimisers.cor_and_dist Method
julia
cor_and_dist(de::DistanceDistance, ce::StatsBase.CovarianceEstimator, X::MatNum;
             dims::Int = 1, kwargs...)

Compute both the correlation matrix and the distance-of-distances matrix from a covariance estimator and data matrix.

This method first computes the correlation and base distance matrices using Distance, then applies the provided metric to the base distance matrix.

Arguments

  • de: Distance-of-distances estimator.

  • ce: Covariance estimator.

  • X: Data matrix (observations × features).

  • dims: Dimension along which to perform the computation.

  • kwargs...: Additional keyword arguments passed to the base distance computation.

Returns

  • (rho::Matrix{<:Number}, D::Matrix{<:Number}): Tuple of correlation matrix and distance-of-distances matrix.

Related

source