Rank covariance
PortfolioOptimisers.KendallCovariance — Type
struct KendallCovariance{__T_ve, __T_cache} <: RankCovarianceEstimatorMeasures monotonic association with Kendall's tau, which counts concordant pairs against discordant pairs.
Kendall's tau reads only the ranks of the returns. An outlier changes it only through its rank, and the statistic does not assume Gaussian data. The generic covariance fallback rescales the correlation matrix by the marginal standard deviations of ve. Entry $(i, j)$ of the covariance is $\hat{\tau}^b_{ij}\,\hat{\sigma}_i\,\hat{\sigma}_j$, and the diagonal is the variance.
Equation 1 of [41] states the coefficient as the difference of the counts of concordant and discordant pairs, divided by the number of pairs. Section 6.1.3 of [12] states it again. The library computes the tie-corrected form of Equations 10 and 12 of [42]. The two forms differ when the data have ties.
Fields
ve: Variance estimator.
cache: Optional partial-fit state. It isnothinguntilpartial_fit!writes one. When the caller gives no data matrix, the batch verb of the estimator reads it. Each propagation channel does one thing with it.factorykeeps it unchanged, because a factory call resolves the configuration, not the sample.port_opt_viewcopies the entries of the selected assets into a new state, so the viewed estimator holds the state of those assets alone.obs_weights_viewremoves it, because a state has no slice on the observation axis. Only a type with an observation-weights field has that channel. A type with no such field falls through to the identity method, so it keeps its state. A family whose state has no exact asset slice removes it on both axes and names the reason.
Constructors
KendallCovariance(; ve::AbstractVarianceEstimator = SimpleVariance(), cache::Option{<:AbstractPartialFitState} = nothing) -> KendallCovarianceKeywords correspond to the struct's fields.
Propagated parameters
factory passes its arguments on to these @fprop fields:
View parameters
port_opt_view takes the view of these @vprop fields at the selected indices:
ve: Recursively viewed throughport_opt_view.cache: Sliced to the selected assets throughport_opt_view.
Examples
julia> KendallCovariance()KendallCovariance ve ┼ SimpleVariance │ me ┼ SimpleExpectedReturns │ │ w ┴ nothing │ w ┼ nothing │ corrected ┴ Bool: trueRelated
RankCovarianceEstimatorSpearmanCovarianceAbstractVarianceEstimatorSimpleVariancefactoryport_opt_view
References
- [41] M. G. Kendall. A new measure of rank correlation. Biometrika 30, 81–93 (1938). Section 2, Equation 1.
- [42] M. G. Kendall. The treatment of ties in ranking problems. Biometrika 33, 239–251 (1945). Sections 10 to 11, Equations 10 and 12.
- [12] D. Cajas. Advanced Portfolio Optimization: A Cutting-edge Quantitative Approach (Springer Nature Switzerland, 2025). Section 6.1.3, Equation 6.3.
PortfolioOptimisers.SpearmanCovariance — Type
struct SpearmanCovariance{__T_ve, __T_cache} <: RankCovarianceEstimatorMeasures monotonic association with Spearman's rho, the Pearson correlation of the rank-transformed returns.
Spearman's rho reads only the ranks of the returns. An outlier changes it only through its rank, and the statistic does not assume Gaussian data. The generic covariance fallback rescales the correlation matrix by the marginal standard deviations of ve. Entry $(i, j)$ of the covariance is $\hat{\rho}^S_{ij}\,\hat{\sigma}_i\,\hat{\sigma}_j$, and the diagonal is the variance.
Part I, Section 3 of [43] computes the correlation of the ranks by the method of product moments. It gives the reason for the ranks. A rank does not depend on the form of the marginal distributions, and an extreme value changes it little. Section 4(d) of that part gives a different statistic, $R = 1 - 3 S_d / (n^{2} - 1)$, from the sum of absolute rank differences $S_d$. This estimator does not compute that statistic.
Fields
ve: Variance estimator.
cache: Optional partial-fit state. It isnothinguntilpartial_fit!writes one. When the caller gives no data matrix, the batch verb of the estimator reads it. Each propagation channel does one thing with it.factorykeeps it unchanged, because a factory call resolves the configuration, not the sample.port_opt_viewcopies the entries of the selected assets into a new state, so the viewed estimator holds the state of those assets alone.obs_weights_viewremoves it, because a state has no slice on the observation axis. Only a type with an observation-weights field has that channel. A type with no such field falls through to the identity method, so it keeps its state. A family whose state has no exact asset slice removes it on both axes and names the reason.
Constructors
SpearmanCovariance(; ve::AbstractVarianceEstimator = SimpleVariance(), cache::Option{<:AbstractPartialFitState} = nothing) -> SpearmanCovarianceKeywords correspond to the struct's fields.
Propagated parameters
factory passes its arguments on to these @fprop fields:
View parameters
port_opt_view takes the view of these @vprop fields at the selected indices:
ve: Recursively viewed throughport_opt_view.cache: Sliced to the selected assets throughport_opt_view.
Examples
julia> SpearmanCovariance()SpearmanCovariance ve ┼ SimpleVariance │ me ┼ SimpleExpectedReturns │ │ w ┴ nothing │ w ┼ nothing │ corrected ┴ Bool: trueRelated
References
- [43] C. Spearman. The proof and measurement of association between two things. The American Journal of Psychology 15, 72–101 (1904). Part I, Sections 3 and 4(d), pages 79 to 87.
Statistics.cor — Method
Statistics.cor(::KendallCovariance, X::MatNum; dims::Int = 1, kwargs...)Compute the Kendall's tau rank correlation matrix with a KendallCovariance estimator.
Entry $(i, j)$ is the Kendall's tau of the returns of assets $i$ and $j$, which measures their monotonic association.
Mathematical definition
For two asset return series $(x_1, \ldots, x_T)$ and $(y_1, \ldots, y_T)$, StatsBase.corkendall computes the tie-corrected $\tau_b$:
\[\begin{align} \hat{\tau}^b_{ij} &= \frac{C - D}{\sqrt{(n_0 - n_x)(n_0 - n_y)}}\,, \\ n_0 &= \binom{T}{2}\,, \quad n_x = \sum_{g} \binom{t_g}{2}\,, \quad n_y = \sum_{h} \binom{u_h}{2}\,. \end{align}\]
Where:
- $\hat{\tau}^b_{ij}$: The Kendall's $\tau_b$ rank correlation of assets $i$ and $j$.
- $C$: The number of concordant pairs. A pair $(t, s)$ is concordant if $(x_t - x_s)(y_t - y_s) > 0$.
- $D$: The number of discordant pairs. A pair $(t, s)$ is discordant if $(x_t - x_s)(y_t - y_s) < 0$.
- $n_0$: The total number of pairs.
- $t_g$, $u_h$: The sizes of the $g$-th group of tied $x$ values and of the $h$-th group of tied $y$ values.
- $T$: Number of observations.
Without ties, $n_x = n_y = 0$ and $\hat{\tau}^b$ reduces to $\tau_a = (C - D) / \binom{T}{2}$, which is Equation 1 of [41]. With ties, the two differ, because the tie counts make the denominator smaller. Equation 10 of [42] gives a tied pair the score zero, and Equation 12 then gives the denominator of the formula above. On the ten ranks of Example 3 of that work, the library returns $33 / \sqrt{41 \cdot 36} \approx 0.85896$. The work truncates this value to 0.8589.
Algorithm
- Orient
Xwithdims_oriented, which transposes it whendimsis2and refuses any other value. - Refuse a gapped sample with
assert_finite_sample. - Return
StatsBase.corkendall(X), the tie-corrected $\tau_b$ of each pair of columns. The method does not scale or clamp the result. The entries of a constant column off the diagonal areNaN. - Write
NaNon the row and the column of each constant column withconstant_column_correlation!, and return the matrix. A constant column has no ranks to correlate, so its correlation with itself is0 / 0too.
Arguments
ce: The Kendall's tau covariance estimator.X: The data matrix of asset returns,observations × assets.dims: Dimension along which the computation runs.kwargs...: More keyword arguments, which the method ignores.
Validation
dimsis either1or2.
Returns
rho::Matrix{<:Number}: The symmetric matrix of Kendall's tau rank correlation coefficients.
Examples
julia> X = [0.01 0.02; 0.03 0.04; 0.02 0.03];julia> cor(KendallCovariance(), X)2×2 Matrix{Float64}: 1.0 1.0 1.0 1.0Related
Statistics.cor — Method
Statistics.cor(::SpearmanCovariance, X::MatNum; dims::Int = 1, kwargs...)Compute the Spearman's rho rank correlation matrix with a SpearmanCovariance estimator.
Entry $(i, j)$ is the Spearman's rho of the returns of assets $i$ and $j$, which measures their monotonic association.
Mathematical definition
Spearman's $\rho$ is the Pearson correlation of the ranks of the data. Let $\mathrm{rk}(x_t)$ be the mid-rank of observation $x_t$ among $x_1, \ldots, x_T$. Each value of a group of tied values gets the average rank of the group:
\[\begin{align} \hat{\rho}^S_{ij} &= \frac{\mathrm{cov}\!\left(\mathrm{rk}(x_{\cdot i}),\, \mathrm{rk}(x_{\cdot j})\right)}{\sigma_{\mathrm{rk}(x_{\cdot i})} \, \sigma_{\mathrm{rk}(x_{\cdot j})}}\,. \end{align}\]
Where:
- $\hat{\rho}^S_{ij}$: The Spearman's $\rho$ rank correlation of assets $i$ and $j$.
- $T$: Number of observations.
- $x_{ti}$: The return of asset $i$ at time $t$.
- $\mathrm{rk}(\cdot)$: The mid-rank function.
- $\sigma_{\mathrm{rk}(\cdot)}$: The standard deviation of the ranks.
Without ties, this is equal to the closed form $1 - 6 \sum_t d_t^2 / (T(T^2 - 1))$, with $d_t = \mathrm{rk}(x_{ti}) - \mathrm{rk}(x_{tj})$. With ties, the two differ.
Algorithm
- Orient
Xwithdims_oriented, which transposes it whendimsis2and refuses any other value. - Refuse a gapped sample with
assert_finite_sample. - Return
StatsBase.corspearman(X), the Pearson correlation of the mid-ranked columns. The method does not scale or clamp the result. The entries of a constant column off the diagonal areNaN. - Write
NaNon the row and the column of each constant column withconstant_column_correlation!, and return the matrix. A constant column has no ranks to correlate, so its correlation with itself is0 / 0too.
Arguments
ce: The Spearman's rho covariance estimator.X: The data matrix of asset returns,observations × assets.dims: Dimension along which the computation runs.kwargs...: More keyword arguments, which the method ignores.
Validation
dimsis either1or2.
Returns
rho::Matrix{<:Number}: The symmetric matrix of Spearman's rho rank correlation coefficients.
Examples
julia> X = [0.01 0.02; 0.03 0.04; 0.02 0.03];julia> cor(SpearmanCovariance(), X)2×2 Matrix{Float64}: 1.0 1.0 1.0 1.0Related
References
- [12]
- D. Cajas. Advanced Portfolio Optimization: A Cutting-edge Quantitative Approach (Springer Nature Switzerland, 2025).
- [41]
- M. G. Kendall. A new measure of rank correlation. Biometrika 30, 81–93 (1938).
- [42]
- M. G. Kendall. The treatment of ties in ranking problems. Biometrika 33, 239–251 (1945).
- [43]
- C. Spearman. The proof and measurement of association between two things. The American Journal of Psychology 15, 72–101 (1904).