Skip to content
13

Coskewness

PortfolioOptimisers.Coskewness Type
julia
struct Coskewness{__T_me, __T_mp, __T_alg, __T_w} <: CoskewnessEstimator

Container type for coskewness estimators.

Coskewness encapsulates the mean estimator, matrix processing estimator, and moment algorithm for coskewness estimation.

Fields

  • me: Expected returns estimator.

  • mp: Matrix processing estimator.

  • alg: Moment algorithm.

  • w: Optional observation weights vector observations × 1, or a concrete subtype of DynamicAbstractWeights. If nothing, the computation is unweighted.

Constructors

julia
Coskewness(;
    me::AbstractExpectedReturnsEstimator = SimpleExpectedReturns(),
    mp::AbstractMatrixProcessingEstimator = MatrixProcessing(),
    alg::AbstractMomentAlgorithm = Full(),
    w::Option{<:ObsWeights} = nothing
) -> Coskewness

Keywords correspond to the struct's fields.

Propagated parameters

When factory is called on this type, the following @fprop-tagged fields are automatically propagated:

View parameters

When port_opt_view is called on this type, the following @vprop-tagged fields are automatically subset to the selected indices:

Validation

  • If w is not nothing, !isempty(w).

Examples

julia
julia> Coskewness()
Coskewness
   me ┼ SimpleExpectedReturns
      │   w ┴ nothing
   mp ┼ MatrixProcessing
      │     pdm ┼ Posdef
      │         │      alg ┼ UnionAll: NearestCorrelationMatrix.Newton
      │         │   kwargs ┴ @NamedTuple{}: NamedTuple()
      │      dn ┼ nothing
      │      dt ┼ nothing
      │     alg ┼ nothing
      │   order ┴ NTuple{4, Symbol}: (:pdm, :dn, :dt, :alg)
  alg ┼ Full()
    w ┴ nothing

Related

source
PortfolioOptimisers.coskewness Function
julia
coskewness(ske::Option{<:Coskewness}, X::MatNum; dims::Int = 1,
           mean = nothing, kwargs...)

Compute the full coskewness tensor and processed matrix for a dataset. Observation weights in ske.w are applied if set. For Full, it uses all centered data; for Semi, it uses only negative deviations. If the estimator is nothing, returns (nothing, nothing).

Arguments

  • ske: Coskewness estimator.

    • ske::Coskewness{<:Any, <:Any, <:Full}: Coskewness estimator with Full moment algorithm.

    • ske::Coskewness{<:Any, <:Any, <:Semi}: Coskewness estimator with Semi moment algorithm.

    • ske::Nothing: No-op, returns (nothing, nothing).

  • X: Data matrix (observations × assets).

  • dims: Dimension along which to perform the computation.

  • mean: Optional mean vector. If not provided, computed using the estimator's mean estimator.

  • kwargs...: Additional keyword arguments passed to the mean estimator.

Validation

  • dims is either 1 or 2.

Returns

  • cskew::Matrix{<:Number}: Coskewness tensor (observations × assets^2).

  • V::Matrix{<:Number}: Processed coskewness matrix (assets × assets).

Examples

julia
julia> using StableRNGs

julia> rng = StableRNG(123456789);

julia> X = randn(rng, 10, 3);

julia> cskew, V = coskewness(Coskewness(), X);

julia> cskew
3×9 Matrix{Float64}:
 -0.329646    0.0782455   0.325842   0.325842  -0.250881   0.16769
  0.0782455  -0.236104   -0.250881     -0.250881   0.266005   0.144546
  0.325842   -0.250881    0.16769       0.16769    0.144546  -0.605589

julia> V
3×3 Matrix{Float64}:
  0.513743   -0.0452078  -0.290893
 -0.0452078   0.402765   -0.0372996
 -0.290893   -0.0372996   0.837701

Related

source
julia
coskewness(ske::WindowedCoskewness, X::MatNum; dims::Int = 1, iv::Option{<:MatNum} = nothing, kwargs...)

Compute the coskewness tensor and processed matrix using a rolling or indexed observation window.

This method selects a window of observations from X (and applies observation weights if specified), then delegates to the underlying coskewness estimator.

Arguments

  • ske: Windowed coskewness estimator.

  • X: Data matrix of asset returns (observations × assets).

  • dims: Dimension along which to perform the computation.

  • iv: Optional implied volatility matrix. Used if any internal covariance estimator is an instance of ImpliedVolatility.

  • kwargs...: Additional keyword arguments passed to the underlying estimator.

Returns

  • cskew::Matrix{<:Number}: Coskewness tensor (assets × assets²).

  • V::Matrix{<:Number}: Processed coskewness matrix (assets × assets).

Related

source
PortfolioOptimisers.CoskewnessEstimator Type
julia
abstract type CoskewnessEstimator <: AbstractEstimator

Abstract supertype for all coskewness estimators in PortfolioOptimisers.jl.

All concrete and/or abstract types implementing coskewness estimation algorithms should be subtypes of CoskewnessEstimator.

Interfaces

In order to implement a new coskewness estimator which will work seamlessly with the library, subtype CoskewnessEstimator with all necessary parameters–-including observation weights–-as part of the struct, and implement the following methods:

Coskewness

  • PortfolioOptimisers.coskewness(ske::CoskewnessEstimator, X::MatNum; dims::Int = 1, mean = nothing, kwargs...) -> (MatNum, MatNum): Computes the coskewness tensor and processed matrix.

Arguments

  • ske: Coskewness estimator.

  • X: Data matrix observations × features if the dims keyword does not exist or dims = 1, features × observations when dims = 2.

  • dims: Dimension along which to perform the computation.

  • mean: Optional mean value to use for centering.

  • kwargs...: Additional keyword arguments.

Returns

  • cskew::MatNum: Coskewness tensor features × features^2.

  • V::MatNum: Processed coskewness matrix features × features.

Factory

  • PortfolioOptimisers.factory(ske::CoskewnessEstimator, w::PortfolioOptimisers.ObsWeights) -> CoskewnessEstimator: Factory method for creating instances of the estimator with new observation weights.

Arguments

  • ske: Coskewness estimator.

  • w: Observation weights vector observations × 1.

Returns

  • ske::CoskewnessEstimator: New coskewness estimator of the same type, with the new weights applied.

View

  • PortfolioOptimisers.port_opt_view(ske::CoskewnessEstimator, i) -> CoskewnessEstimator: Returns a view of the estimator for the i-th element(s).

Arguments

  • ske: Coskewness estimator.

  • i: Index or indices.

Returns

  • skev: New coskewness estimator of the same type as the argument, for the new view.

Examples

We can create a dummy coskewness estimator as follows:

julia
julia> struct MyCoskewnessEstimator{T1} <: PortfolioOptimisers.CoskewnessEstimator
           w::T1
           function MyCoskewnessEstimator(w::PortfolioOptimisers.Option{<:PortfolioOptimisers.ObsWeights})
               PortfolioOptimisers.assert_nonempty_nonneg_finite_val(w, :w)
               return new{typeof(w)}(w)
           end
       end

julia> function MyCoskewnessEstimator(;
                                      w::PortfolioOptimisers.Option{<:PortfolioOptimisers.ObsWeights} = nothing)
           return MyCoskewnessEstimator(w)
       end
MyCoskewnessEstimator

julia> function PortfolioOptimisers.factory(::MyCoskewnessEstimator,
                                            w::PortfolioOptimisers.ObsWeights)
           return MyCoskewnessEstimator(; w = w)
       end

julia> function PortfolioOptimisers.port_opt_view(ske::MyCoskewnessEstimator, i)
           return ske
       end

julia> function PortfolioOptimisers.coskewness(ske::MyCoskewnessEstimator,
                                               X::PortfolioOptimisers.MatNum; dims::Int = 1,
                                               mean = nothing, kwargs...)
           N = size(X, 2)
           return zeros(N, N^2), zeros(N, N)
       end

julia> cskew, V = coskewness(MyCoskewnessEstimator(), [1.0 2.0; 0.3 0.7; 0.5 1.1]);

julia> cskew
2×4 Matrix{Float64}:
 0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0

julia> V
2×2 Matrix{Float64}:
 0.0  0.0
 0.0  0.0

julia> PortfolioOptimisers.factory(MyCoskewnessEstimator(), StatsBase.Weights([1, 2, 3]))
MyCoskewnessEstimator
  w ┴ StatsBase.Weights{Int64, Int64, Vector{Int64}}: [1, 2, 3]

Related

source
PortfolioOptimisers.negative_spectral_coskewness Function
julia
negative_spectral_coskewness(cskew::MatNum, X::MatNum,
             mp::AbstractMatrixProcessingEstimator)

Internal helper for coskewness matrix processing.

negative_spectral_coskewness processes the coskewness tensor by applying the matrix processing estimator to each block, then projects the result using eigenvalue decomposition and clamps negative values. Used internally for robust coskewness estimation.

Arguments

  • cskew: Coskewness tensor (flattened or block matrix).

  • X: Data matrix (observations × assets).

  • mp: Matrix processing estimator.

Returns

  • V::Matrix{<:Number}: Processed coskewness matrix.

Related

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

Internal helper for coskewness computation.

_coskewness computes the coskewness tensor and applies matrix processing. Used internally by coskewness estimators.

Mathematical definition

Let Y be the T×N matrix of demeaned returns. Define zt=ytyt (Kronecker-style element-wise product). The N×N2 coskewness tensor is:

Unweighted:

S^=1TYZ,Zt,=(1yt)(yt1).

Weighted:

S^=1t=1Twt(wY)Z.

Where:

  • S^: N×N2 coskewness tensor.

  • T: Number of observations.

  • Y: T×N matrix of demeaned returns.

  • Z: Row-wise outer product expansion matrix.

  • yt: Demeaned return vector at time t.

  • w: Observation weights vector T×1.

  • wt: Observation weight at time t.

  • : Kronecker product.

  • : Element-wise (row-wise broadcast) multiplication.

Arguments

  • Y: Centered data vector (e.g., X .- mean).

  • X: Data matrix (observations × assets).

  • mp: Matrix processing estimator.

  • w: Optional observation weights.

Returns

  • cskew::Matrix{<:Number}: Coskewness tensor.

  • V::Matrix{<:Number}: Processed coskewness matrix.

Related

source