Coskewness: private API

PortfolioOptimisers.negative_spectral_coskewness — Function
negative_spectral_coskewness(cskew::MatNum, X::MatNum,
             mp::Option{<:AbstractMatrixProcessingEstimator}, T::Real = size(X, 1))

Builds the negative spectral skewness matrix of a coskewness tensor.

negative_spectral_coskewness splits the coskewness tensor into its N symmetric blocks of size N x N. It keeps the negative part of the spectrum of each block, and it adds the negated parts into one N x N matrix. The matrix processing estimator runs once, on that sum, and not on each block.

The function reduces the block of the tensor alone. This is the rule that matrix_processing_block! applies to a covariance frame. An available-case fit under a CoveragePolicy returns a frame. The tensor carries NaN across the row and the pair columns of each asset that the policy refused.

The function finds the block from the per-asset diagonal of the tensor, cskew[i, (i - 1) * N + i]. That entry is the third central moment of asset i. It is finite if and only if the fit gives a value for that asset. A non-finite cell inside the block is a triple of assets that the fit estimated, but that share no observation. assert_finite_block refuses that cell with an error that names it, so the cell never reaches eigen. The spectral step and mp run on the block alone. The function then writes the result into a NaN frame of the full width, so the row and the column of each asset that the policy refused hold NaN.

Every site of the library that reduces a coskewness tensor to V calls this function, so the block rule has one definition. The callers include _coskewness, the available-case arm of coskewness, port_opt_view of a HighOrderPrior, the high order factor prior and NegativeSkewness.

Mathematical definition

Write the coskewness tensor as $\mathbf{S} = [\mathbf{S}_{1} \vert \mathbf{S}_{2} \vert \ldots \vert \mathbf{S}_{N}]$. Each block $\mathbf{S}_{i}$ is symmetric, so its eigendecomposition is real. Keep the negative eigenvalues alone and negate the sum:

\[\begin{align} \mathbf{S}_{i} &= \mathbf{Q}_{i} \mathbf{\Lambda}_{i} \mathbf{Q}_{i}^{\intercal}\,, \\ \mathbf{S}_{i}^{-} &= \mathbf{Q}_{i} \mathbf{\Lambda}_{i}^{-} \mathbf{Q}_{i}^{\intercal}\,, \\ \mathbf{V} &= -\sum\limits_{i=1}^{N} \mathbf{S}_{i}^{-}\,. \end{align}\]

Where:

  • $\mathbf{S}_{i}$: The $i$-th $N \times N$ block of the coskewness tensor.
  • $\mathbf{Q}_{i}$: The matrix of the eigenvectors of $\mathbf{S}_{i}$.
  • $\mathbf{\Lambda}_{i}^{-}$: The diagonal matrix of the negative eigenvalues of $\mathbf{S}_{i}$, with each non-negative eigenvalue set to zero.
  • $\mathbf{V}$: The negative spectral skewness matrix. It is positive semidefinite, because it is a sum of negated negative semidefinite matrices.
  • $N$: Number of assets.

The entry $\mathbf{S}_{i,\,aj}$ is the third comoment of the deviations of the assets $a$, $i$ and $j$. That comoment does not change when the order of its three assets changes, so the block is symmetric.

Section 2 of [65] splits each block as $\mathbf{S}_{i} = \mathbf{S}_{i}^{+} + \mathbf{S}_{i}^{-}$ and writes the negative quadratic skewness as $\boldsymbol{x}^\intercal \mathbf{V}^{-} \boldsymbol{x}$ with $\mathbf{V}^{-} = -\sum_{i} \mathbf{S}_{i}^{-}$. That is the $\mathbf{V}$ above. The work gets this form when it replaces each weight $x_i$ of the skewness with $1/N$. The matrix processing of the last step is an addition of this library.

Algorithm

  1. Read N from the row count of cskew. Take the block blk as isfinite.(cskew[i, (i - 1) * N + i]) over i.
  2. When blk holds no true, reduce the whole tensor. The tensor has no block, so no cell is refused here, and LinearAlgebra.eigen raises ArgumentError: matrix contains Infs or NaNs on the first block.
  3. Refuse a non-finite cell inside the block with assert_finite_block. When blk holds no false, the block is the whole tensor, and the refusal covers that case too.
  4. Cut the tensor to the block, at the rows blk and the pair columns of coverage_pair_index. Cut X to the same assets. When blk holds no false, the function does not copy either of them.
  5. Allocate the accumulator V of zeros at the width of the block.
  6. For each block index i, take coskew_jk, the view of the columns (i - 1) * N + 1 to i * N of the cut tensor.
  7. Take the eigenvalues vals and the eigenvectors vecs of coskew_jk. Subtract from V the negative semidefinite part that negative_spectral_part rebuilds from them.
  8. Run sample_matrix_processing! once, on the accumulated V, with the cut X and T. The function does not process a block on its own.
  9. When the block is the whole tensor, return V. Otherwise, write V into the block of a NaN frame of the full width, and return the frame.

Arguments

  • cskew: The coskewness tensor, assets × assets², in N blocks of N columns. It can be a frame, in which each refused asset carries NaN across its row and its pair columns.
  • X: The data matrix (observations × assets). matrix_processing! reads it, and the spectral step does not.
  • mp: Optional matrix processing estimator.
    • ::AbstractMatrixProcessingEstimator: The estimator processes the accumulated V in place.
    • ::Nothing: The function does not process V, which is the sum of the negated negative parts.
  • T: The effective number of observations of the tensor, which the denoising step reads. It is size(X, 1) by default, the count of an unweighted tensor. _coskewness passes the count of its weights.

Validation

  • The block of cskew is finite. An IsNonFiniteError that names the cell is thrown otherwise.

Returns

  • V::MatNum: Negative spectral skewness matrix assets x assets of the coskewness tensor, after the matrix processing. It is the $\mathbf{V}$ above, so a portfolio's negative quadratic skewness is the quadratic form $\boldsymbol{w}^{\intercal} \mathbf{V} \boldsymbol{w}$. An asset outside the block carries NaN across its row and its column.

Related

References

  • [65] D. Cajas. On the Spectral Decomposition of Portfolio Skewness and its Application to Portfolio Optimization. Available at SSRN 4540021 (2023). Section 2.
source
PortfolioOptimisers.negative_spectral_part — Function
negative_spectral_part(vals::AbstractVector{<:Real}, vecs::MatNum, Tf::Type) -> MatNum
negative_spectral_part(vals::AbstractVector{<:Complex}, vecs::MatNum, Tf::Type) -> MatNum

Rebuilds one block of a coskewness tensor from the negative part of its spectrum alone.

This function is the inner step of negative_spectral_coskewness. It has one method for a real spectrum and one for a complex spectrum. LinearAlgebra.eigen of a general real matrix returns a real spectrum or a complex one, and only the values show which. The types of the caller do not set the element type of vals, so dispatch on vals selects the method, and each method body has a concrete element type.

The method sets every non-negative eigenvalue to zero and keeps every negative one, so the reconstruction is the negative semidefinite part of the block. The caller subtracts it, and the sum of these negated parts is positive semidefinite.

The complex method is a fallback and not a second definition. A block of the tensor is symmetric, so its spectrum is real. eigen returns a complex spectrum only when round-off makes the block asymmetric. The fallback clamps the real part and the imaginary part with the same bounds, and it returns the real part of the reconstruction.

Arguments

  • vals: The eigenvalues of the block.
  • vecs: The eigenvectors of the block.
  • Tf: The element type of the tensor, which sets the bounds of the clamp.

Returns

  • S::MatNum: The negative semidefinite part of the block, of the shape of the block.

Related

source
PortfolioOptimisers._coskewness — Function
_coskewness(Y::MatNum, X::MatNum, mp::AbstractMatrixProcessingEstimator, w::Option{<:StatsBase.AbstractWeights}) -> (MatNum, MatNum)

Builds the coskewness tensor of a deviation matrix, and the negative spectral skewness matrix of that tensor.

The matrix processing estimator runs on the negative spectral skewness matrix alone, inside negative_spectral_coskewness. It never runs on the tensor.

Mathematical definition

The $N \times N^{2}$ coskewness tensor is the third comoment matrix of the deviations:

Unweighted:

\[\begin{align} \hat{\mathbf{S}} &= \frac{1}{T} \mathbf{Y}^\intercal \mathbf{Z}\,, \\ \mathbf{Z}_{t,\cdot} &= (\boldsymbol{1}^\intercal \otimes \boldsymbol{y}_t^\intercal) \odot (\boldsymbol{y}_t^\intercal \otimes \boldsymbol{1}^\intercal)\,. \end{align}\]

Weighted:

\[\begin{align} \hat{\mathbf{S}} &= \frac{1}{\sum_{t=1}^{T} w_t} (\boldsymbol{w} \odot \mathbf{Y})^\intercal \mathbf{Z}\,. \end{align}\]

Where:

  • $\hat{\mathbf{S}}$: The $N \times N^{2}$ coskewness tensor. Its entry $\hat{\mathbf{S}}_{a,\,(i-1)N+j}$ is the third comoment of the deviations of the assets $a$, $i$ and $j$, so each of its $N$ blocks of $N$ columns is symmetric.
  • $\mathbf{Y}$: $T \times N$ deviation matrix. FullMoment takes the centred returns, and SemiMoment clips every positive entry of them to zero.
  • $\boldsymbol{y}_t$: $N \times 1$ deviation vector of observation $t$, the $t$-th row of $\mathbf{Y}$. Its $i$-th entry is $y_{t,\,i}$.
  • $\mathbf{Z}$: $T \times N^{2}$ pairwise expansion of $\mathbf{Y}$, whose $t$-th row is $\mathbf{Z}_{t,\cdot}$ and whose entry $\mathbf{Z}_{t,\,(i-1)N+j}$ is the product $y_{t,\,i} \, y_{t,\,j}$.
  • $\boldsymbol{w}$: $T \times 1$ observation weights vector.
  • $w_{t}$: Observation weight of observation $t$.
  • $T$: Number of observations.
  • $N$: Number of assets.
  • $\boldsymbol{1}$: The $N \times 1$ vector of ones.
  • $\otimes$: The Kronecker product.
  • $\odot$: The element-wise product. When the operands differ in shape, it broadcasts along the row axis.

The unweighted tensor is the matrix formula for $\mathbf{M}_3$ in Appendix A of [66]. Its entries are the third comoments of Section 2.1 of [63] and Section 2.3 of [64]. In this layout, the entry of row $j$ and column $(i-1)N+k$ is the third comoment of the assets $i$, $j$ and $k$. The weighted form is an addition of this library.

Algorithm

  1. Build o, the $1 \times N$ row of ones.
  2. Build z, the pairwise expansion kron(o, Y) ⊙ kron(Y, o). Its column (i - 1) * N + j is the element-wise product of the columns i and j of Y.
  3. Without weights, form cskew as transpose(Y) * z / size(Y, 1).
  4. With weights, form cskew as transpose(w .* Y) * z / sum(w). The weights multiply the left factor alone, so each summand carries one weight and not three.
  5. Reduce cskew to V with negative_spectral_coskewness, which runs mp on the reduced matrix. With weights, pass the count of the weights from observation_weight_count, so that the denoising step reads the effective number of observations and not size(X, 1).
  6. Return the pair (cskew, V).

Arguments

  • Y: The deviation matrix (observations × assets). The caller centres it.
  • X: The data matrix (observations × assets). It goes to sample_matrix_processing! through negative_spectral_coskewness.
  • mp: The matrix processing estimator.
  • w: The optional observation weights. The unweighted method takes nothing through its args....

Returns

  • cskew::MatNum: Coskewness tensor assets x assets².
  • V::MatNum: Negative spectral skewness matrix assets x assets of the coskewness tensor, after the matrix processing.

Related

References

  • [63] G. M. de Athayde and R. G. Flôres Jr. Finding a maximum skewness portfolio—a general solution to three-moments portfolio choice. Journal of Economic Dynamics and Control 28, 1335–1352 (2004). Section 2.1, page 1338.
  • [64] E. Jondeau and M. Rockinger. Optimal portfolio allocation under higher moments. Notes d'Études et de Recherche 108, Banque de France (2004). Section 2.3.
  • [66] D. Cajas. Convex Optimization of Portfolio Kurtosis. Available at SSRN 4202967 (2022). Appendix A.
source

References

[63]
G. M. de Athayde and R. G. Flôres Jr. Finding a maximum skewness portfolio—a general solution to three-moments portfolio choice. Journal of Economic Dynamics and Control 28, 1335–1352 (2004).
[64]
E. Jondeau and M. Rockinger. Optimal portfolio allocation under higher moments. Notes d'Études et de Recherche 108 (Banque de France, 2004).
[65]
D. Cajas. On the Spectral Decomposition of Portfolio Skewness and its Application to Portfolio Optimization. Available at SSRN 4540021 (2023).
[66]
D. Cajas. Convex Optimization of Portfolio Kurtosis. Available at SSRN 4202967 (2022).