Distance Covariance: private API
PortfolioOptimisers.calc_pairwise_dists — Method
calc_pairwise_dists(ce::DistanceCovariance, v1::VecNum, v2::VecNum) -> (MatNum, MatNum)Compute pairwise distance matrices between two vectors using the configured metric.
Internal helper used in distance correlation and distance covariance computation. The observation weights never reach it. They weight the statistic that calc_centred_dists and calc_dcov2 build, and not the data that the metric measures.
Algorithm
- Apply the estimator's
metrictov1and tov2as the caller gave them, and passce.argsandce.kwargstoDistances.pairwise. - Return the two matrices in the order
(D1, D2). Each is $T \times T$ for a pair of series of $T$ observations, andD1[t, s]is the distance between observations $t$ and $s$ of the same series.
Arguments
ce:DistanceCovarianceestimator with metric configuration.v1,v2: Data vectors.
Returns
- Tuple of pairwise distance matrices
(D1, D2).
Related
PortfolioOptimisers.calc_centred_dists — Method
calc_centred_dists(a::MatNum, w::Option{<:StatsBase.AbstractWeights}) -> MatNumDoubly centre a pairwise distance matrix, weighting every observation by w.
Internal helper used in distance correlation and distance covariance computation. The weights enter the three means, so a constant weight vector cancels from each of them and returns the unweighted matrix.
Algorithm
- Without weights, take the ordinary row means, column means and grand mean of
a. - With weights, take the same three means weighted by
w. Each is normalised by the sum of the weights that it uses, so the scale ofwcancels. - Subtract the row means and the column means from
a, then add the grand mean back, givingA.
Arguments
a: Pairwise distance matrix.w: Observation weights, ornothingfor the unweighted statistic.
Returns
A::MatNum: Doubly centred distance matrix.
Related
PortfolioOptimisers.calc_dcov2 — Method
calc_dcov2(A::MatNum, B::MatNum, w::Option{<:StatsBase.AbstractWeights}) -> NumberContract two doubly centred distance matrices into a squared distance covariance.
Internal helper used in distance correlation and distance covariance computation. The weights enter both indices of the contraction, so a constant weight vector cancels and returns the unweighted value.
Algorithm
- Without weights, take the Frobenius inner product of
AandB, and divide it by the number of entries ofA. - With weights, take the element-wise product of
AandB, contract it withwon both of its indices, and divide by the square of the sum of the weights.
Arguments
A,B: Doubly centred distance matrices, ascalc_centred_distsreturns them.w: Observation weights, ornothingfor the unweighted statistic.
Returns
dcov2::Number: Squared distance covariance of the two matrices.
Related
PortfolioOptimisers.cor_distance — Function
cor_distance(ce::DistanceCovariance, v1::VecNum, v2::VecNum,
w::Option{<:StatsBase.AbstractWeights} = nothing)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 $a_{ts} = d(v_{1t}, v_{1s})$ and $b_{ts} = d(v_{2t}, v_{2s})$ be pairwise distance matrices. Define doubly-centered versions, whose three means carry the observation weights:
\[\begin{align} \bar{a}_{t\cdot} &= \frac{\sum\limits_{s=1}^{T} w_{s} a_{ts}}{\sum\limits_{s=1}^{T} w_{s}}\,, \\ \bar{a}_{\cdot s} &= \frac{\sum\limits_{t=1}^{T} w_{t} a_{ts}}{\sum\limits_{t=1}^{T} w_{t}}\,, \\ \bar{a}_{\cdot\cdot} &= \frac{\sum\limits_{t=1}^{T} \sum\limits_{s=1}^{T} w_{t} w_{s} a_{ts}}{\left(\sum\limits_{t=1}^{T} w_{t}\right)^{2}}\,, \\ A_{ts} &= a_{ts} - \bar{a}_{t\cdot} - \bar{a}_{\cdot s} + \bar{a}_{\cdot\cdot}\,, \\ B_{ts} &= b_{ts} - \bar{b}_{t\cdot} - \bar{b}_{\cdot s} + \bar{b}_{\cdot\cdot}\,. \end{align}\]
Where:
- $a_{ts}$, $b_{ts}$: Pairwise distances between observations $t$ and $s$.
- $\bar{a}_{t\cdot}$: $t$-th weighted row mean of $\mathbf{a}$.
- $\bar{a}_{\cdot s}$: $s$-th weighted column mean of $\mathbf{a}$.
- $\bar{a}_{\cdot\cdot}$: Weighted grand mean of $\mathbf{a}$.
- $A_{ts}$, $B_{ts}$: Doubly centred pairwise distances.
- $w_{t}$: Observation weight of observation $t$.
- $T$: Number of observations.
The three means of $\mathbf{b}$ take the form that the three means of $\mathbf{a}$ take.
The squared distance covariances and distance correlation are:
\[\begin{align} \widehat{\mathrm{dCov}}^2(X,X) &= \frac{\sum\limits_{t=1}^{T} \sum\limits_{s=1}^{T} w_{t} w_{s} A_{ts} A_{ts}}{\left(\sum\limits_{t=1}^{T} w_{t}\right)^{2}}\,, \\ \widehat{\mathrm{dCov}}^2(X,Y) &= \frac{\sum\limits_{t=1}^{T} \sum\limits_{s=1}^{T} w_{t} w_{s} A_{ts} B_{ts}}{\left(\sum\limits_{t=1}^{T} w_{t}\right)^{2}}\,, \\ \widehat{\mathrm{dCov}}^2(Y,Y) &= \frac{\sum\limits_{t=1}^{T} \sum\limits_{s=1}^{T} w_{t} w_{s} B_{ts} B_{ts}}{\left(\sum\limits_{t=1}^{T} w_{t}\right)^{2}}\,. \end{align}\]
Where:
- $\widehat{\mathrm{dCov}}^2(X,Y)$: Squared distance covariance of $X$ and $Y$.
\[\begin{align} \hat{R}_{\mathrm{dist}}(X, Y) &= \frac{\sqrt{\widehat{\mathrm{dCov}}^2(X,Y)}}{\sqrt{\sqrt{\widehat{\mathrm{dCov}}^2(X,X)} \cdot \sqrt{\widehat{\mathrm{dCov}}^2(Y,Y)}}}\,. \end{align}\]
Where:
- $\hat{R}_{\mathrm{dist}}(X, Y)$: Distance correlation between $X$ and $Y$.
Each quotient above divides two forms of one degree in the weights, so a constant weight cancels from all of them. The value $w_{t} = 1$ reduces the three squared distance covariances to $\mathbf{A}:\mathbf{B} / T^{2}$, the Frobenius inner product $\sum_{t,\,s} A_{ts} B_{ts}$ over $T^{2}$, which is the unweighted statistic.
Algorithm
- Build the two pairwise distance matrices $\mathbf{a}$ and $\mathbf{b}$ with
calc_pairwise_dists, which carries the estimator's metric, itsargsand itskwargs. - Doubly centre each matrix with
calc_centred_dists, which weights the three means it subtracts and adds, giving $\mathbf{A}$ and $\mathbf{B}$. - Contract the three pairs $(\mathbf{A}, \mathbf{A})$, $(\mathbf{A}, \mathbf{B})$ and $(\mathbf{B}, \mathbf{B})$ with
calc_dcov2, giving the three squared distance covariances. - Return $\sqrt{\widehat{\mathrm{dCov}}^2(X,Y)}$ divided by the square root of the product of the two remaining square roots.
Arguments
ce: Distance covariance estimator.v1: First data vector.v2: Second data vector.w: Observation weights, ornothingfor the unweighted statistic.
Validation
length(v1) == length(v2).length(v1) > 1.
Returns
rho::Float64: The computed distance correlation betweenv1andv2. A series against itself gives exactly1.0.
Related
PortfolioOptimisers.cov_distance — Function
cov_distance(ce::DistanceCovariance, v1::VecNum, v2::VecNum,
w::Option{<:StatsBase.AbstractWeights} = nothing)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 $\mathbf{A}$ and $\mathbf{B}$ as in cor_distance:
\[\begin{align} \widehat{\mathrm{dCov}}(X, Y) &= \sqrt{\frac{\sum\limits_{t=1}^{T} \sum\limits_{s=1}^{T} w_{t} w_{s} A_{ts} B_{ts}}{\left(\sum\limits_{t=1}^{T} w_{t}\right)^{2}}}\,. \end{align}\]
Where:
- $\widehat{\mathrm{dCov}}(X, Y)$: Distance covariance between $X$ and $Y$.
- $A_{ts}$, $B_{ts}$: Doubly centred pairwise distances.
- $w_{t}$: Observation weight of observation $t$.
- $T$: Number of observations.
The value $w_{t} = 1$ reduces the quotient to $\mathbf{A}:\mathbf{B} / T^{2}$, the Frobenius inner product $\sum_{t,\,s} A_{ts} B_{ts}$ over $T^{2}$, which is the unweighted statistic.
The square root takes no absolute value. It needs none: the doubly-centred V-statistic is non-negative for a metric of strong negative type, which the Euclidean default is.
Algorithm
- Build the two pairwise distance matrices $\mathbf{a}$ and $\mathbf{b}$ with
calc_pairwise_dists, which carries the estimator's metric, itsargsand itskwargs. - Doubly centre each matrix with
calc_centred_dists, which weights the three means it subtracts and adds, giving $\mathbf{A}$ and $\mathbf{B}$. - Contract the pair $(\mathbf{A}, \mathbf{B})$ with
calc_dcov2, giving the squared distance covariance. - Return the square root of that value. Steps 1 and 2 are those of
cor_distance; this method takes one of the three contractions and takes no ratio.
Arguments
ce: Distance covariance estimator.v1: First data vector.v2: Second data vector.w: Observation weights, ornothingfor the unweighted statistic.
Validation
length(v1) == length(v2).length(v1) > 1.
Returns
rho::Number: The computed distance covariance betweenv1andv2. A series against itself gives the distance standard deviation $\widehat{\mathrm{dVar}}^{1/2}(X)$, which is not the sample standard deviation.
Related
PortfolioOptimisers.cor_distance — Function
cor_distance(ce::DistanceCovariance, X::MatNum,
w::Option{<:StatsBase.AbstractWeights} = nothing)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.
Algorithm
- Allocate an $N \times N$ matrix, where $N$ is the number of columns of
X. The result is indexed by asset and never by observation, so a non-squareXcannot hide a transposed index. - For each column
j, and for each columniat or belowj, callcor_distance(ce::DistanceCovariance, v1::VecNum, v2::VecNum)on the two columns and write the value into bothrho[i, j]andrho[j, i]. The estimator'sexfield runs the outer loop. - Return the symmetric matrix. Its diagonal is exactly
1.0, because the pair(j, j)is one of the pairs step 2 computes.
Arguments
ce: Distance covariance estimator.X: Data matrix (observations × assets).w: Observation weights, ornothingfor the unweighted statistic.
Returns
rho::Matrix{<:Number}: Distance correlation matrix.
Related
PortfolioOptimisers.cov_distance — Function
cov_distance(ce::DistanceCovariance, X::MatNum,
w::Option{<:StatsBase.AbstractWeights} = nothing)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.
Algorithm
- Allocate an $N \times N$ matrix, where $N$ is the number of columns of
X. The result is indexed by asset and never by observation, so a non-squareXcannot hide a transposed index. - For each column
j, and for each columniat or belowj, callcov_distance(ce::DistanceCovariance, v1::VecNum, v2::VecNum)on the two columns and write the value into bothsigma[i, j]andsigma[j, i]. The estimator'sexfield runs the outer loop. - Return the symmetric matrix. Its diagonal is the distance standard deviation of each asset, because the pair
(j, j)is one of the pairs step 2 computes.
Arguments
ce: Distance covariance estimator.X: Data matrix (observations × assets).w: Observation weights, ornothingfor the unweighted statistic.
Returns
sigma::Matrix{<:Number}: Symmetric matrix of pairwise distance covariances.
Related