Distance Covariance
PortfolioOptimisers.DistanceCovariance Type
struct DistanceCovariance{__T_metric, __T_args, __T_kwargs, __T_w, __T_ex} <: AbstractCovarianceEstimatorA flexible container type for configuring and applying distance-based covariance estimators in PortfolioOptimisers.jl.
DistanceCovariance encapsulates all components required for distance covariance or correlation estimation, including the distance metric, additional arguments and keyword arguments for the metric, optional weights, and parallel execution strategy.
Fields
metric:metric: Distance metric used for pairwise computations.args:args: Additional positional arguments for the distance metric.kwargs:kwargs: Additional keyword arguments for the distance metric.w:w: Optional observation weights vectorobservations × 1, or a concrete subtype ofDynamicAbstractWeights. Ifnothing, the computation is unweighted.ex:ex: Parallel execution strategy.
Constructors
DistanceCovariance(;
metric::Distances.Metric = Distances.Euclidean(),
args::Tuple = (),
kwargs::NamedTuple = (;),
w::Option{<:ObsWeights} = nothing,
ex::FLoops.Transducers.Executor = ThreadedEx()
) -> DistanceCovarianceKeywords correspond to the struct's fields.
Propagated parameters
When factory is called on this type, the following @fprop-tagged fields are automatically propagated:
w: Replaced with the incomingObsWeights.
Examples
julia> DistanceCovariance()
DistanceCovariance
metric ┼ Distances.Euclidean: Distances.Euclidean(0.0)
args ┼ Tuple{}: ()
kwargs ┼ @NamedTuple{}: NamedTuple()
w ┼ nothing
ex ┴ Transducers.ThreadedEx{@NamedTuple{}}: Transducers.ThreadedEx()Related
Statistics.cov Method
Statistics.cov(ce::DistanceCovariance, X::MatNum; dims::Int = 1, kwargs...)Compute the pairwise distance covariance matrix for all columns in a data matrix using a configured DistanceCovariance estimator.
Arguments
ce: Distance covariance estimator.X: Data matrix (observations × assets).dims: Dimension along which to perform the computation.kwargs...: Additional keyword arguments (currently unused).
Returns
sigma::Matrix{<:Number}: Symmetric matrix of pairwise distance covariances.
Validation
dimsis either1or2.
Examples
julia> ce = DistanceCovariance()
DistanceCovariance
metric ┼ Distances.Euclidean: Distances.Euclidean(0.0)
args ┼ Tuple{}: ()
kwargs ┼ @NamedTuple{}: NamedTuple()
w ┼ nothing
ex ┴ Transducers.ThreadedEx{@NamedTuple{}}: Transducers.ThreadedEx()
julia> X = [1.0 2.0; 2.0 4.0; 3.0 6.0];
julia> cov(ce, X)
2×2 Matrix{Float64}:
0.702728 0.993808
0.993808 1.40546Related
sourceStatistics.cor Method
Statistics.cor(ce::DistanceCovariance, X::MatNum; dims::Int = 1, kwargs...)Compute the pairwise distance correlation matrix for all columns in a data matrix using a configured DistanceCovariance estimator.
Arguments
ce: Distance covariance estimator.X: Data matrix (observations × assets).dims: Dimension along which to perform the computation.kwargs...: Additional keyword arguments (currently unused).
Returns
rho::Matrix{<:Number}: Symmetric matrix of pairwise distance correlations.
Validation
dimsis either1or2.
Examples
julia> ce = DistanceCovariance()
DistanceCovariance
metric ┼ Distances.Euclidean: Distances.Euclidean(0.0)
args ┼ Tuple{}: ()
kwargs ┼ @NamedTuple{}: NamedTuple()
w ┼ nothing
ex ┴ Transducers.ThreadedEx{@NamedTuple{}}: Transducers.ThreadedEx()
julia> X = [1.0 2.0; 2.0 4.0; 3.0 6.0];
julia> cor(ce, X)
2×2 Matrix{Float64}:
1.0 1.0
1.0 1.0Related
sourcePortfolioOptimisers.calc_pairwise_dists Method
calc_pairwise_dists(ce::DistanceCovariance, v1::VecNum, v2::VecNum, w::Option{<:StatsBase.AbstractWeights}) -> (MatNum, MatNum)Compute pairwise distance matrices between two vectors using the configured metric.
Internal helper used in distance correlation computation. Handles weighted and unweighted cases.
Arguments
ce:DistanceCovarianceestimator with metric configuration.v1,v2: Data vectors.w: Observation weights (nothingfor unweighted,StatsBase.AbstractWeightsfor weighted).
Returns
- Tuple of pairwise distance matrices
(D1, D2).
Related
sourcePortfolioOptimisers.cor_distance Function
cor_distance(ce::DistanceCovariance, v1::VecNum, v2::VecNum)Compute the distance correlation between two vectors using a configured DistanceCovariance estimator.
This function computes the distance correlation between v1 and v2 using the specified distance metric, optional weights, and any additional arguments or keyword arguments provided in the estimator. The computation follows the standard distance correlation procedure, centering the pairwise distance matrices and normalizing the result.
Mathematical definition
Let
Where:
, : Pairwise distances between observations and . : -th row mean of . : -th column mean of . : Grand mean of .
The squared distance covariances and distance correlation are:
Where:
: Number of observations. : Frobenius inner product.
Where:
: Distance correlation between and .
Arguments
ce: Distance covariance estimator.v1: First data vector.v2: Second data vector.
Returns
rho::Float64: The computed distance correlation betweenv1andv2.
Details
Computes pairwise distance matrices for
v1andv2using the estimator's metric and configuration.Centers the distance matrices by subtracting row and column means and adding the grand mean.
Computes the squared distance covariance and normalizes to obtain the distance correlation.
Validation
length(v1) == length(v2).length(v1) > 1.
Related
sourcePortfolioOptimisers.cov_distance Function
cov_distance(ce::DistanceCovariance, v1::VecNum, v2::VecNum)Compute the distance covariance between two vectors using a configured DistanceCovariance estimator.
This function computes the distance covariance between v1 and v2 using the specified distance metric, optional weights, and any additional arguments or keyword arguments provided in the estimator. The computation follows the standard distance covariance procedure, centering the pairwise distance matrices and aggregating the result.
Mathematical definition
Using the same doubly-centered matrices cor_distance:
Where:
: Distance covariance between and . : Number of observations. : Frobenius inner product of doubly-centered distance matrices.
Arguments
ce: Distance covariance estimator.v1: First data vector.v2: Second data vector.
Returns
rho::Number: The computed distance covariance betweenv1andv2.
Details
Computes pairwise distance matrices for
v1andv2using the estimator's metric and configuration.Centers the distance matrices by subtracting row and column means and adding the grand mean.
Computes the squared distance covariance and returns its square root.
Validation
length(v1) == length(v2).length(v1) > 1.
Related
sourcePortfolioOptimisers.cor_distance Function
cor_distance(ce::DistanceCovariance, X::MatNum)Compute the pairwise distance correlation matrix for all columns in a data matrix using a configured DistanceCovariance estimator.
This function computes the distance correlation between each pair of columns in X, using the specified distance metric, optional weights, and parallel execution strategy. The resulting matrix is symmetric, with each entry representing the distance correlation between two assets.
Arguments
ce: Distance covariance estimator.X: Data matrix (observations × assets).
Returns
rho::Matrix{<:Number}: Distance correlation matrix.
Details
Iterates over all pairs of columns in
X, computing the distance correlation for each pair usingcor_distance(ce, v1, v2).Parallelizes computation using the estimator's
exfield.
Related
sourcePortfolioOptimisers.cov_distance Function
cov_distance(ce::DistanceCovariance, X::MatNum)Compute the pairwise distance covariance matrix for all columns in a data matrix using a configured DistanceCovariance estimator.
This function computes the distance covariance between each pair of columns in X, using the specified distance metric, optional weights, and parallel execution strategy. The resulting matrix is symmetric, with each entry representing the distance covariance between two assets.
Arguments
ce: Distance covariance estimator.X: Data matrix (observations × assets).
Returns
sigma::Matrix{<:Number}: Symmetric matrix of pairwise distance covariances.
Details
Iterates over all pairs of columns in
X, computing the distance covariance for each pair usingcov_distance(ce, v1, v2).Parallelizes computation using the estimator's
exfield.
Related
source