Skip to content
13

Cokurtosis

PortfolioOptimisers.Cokurtosis Type
julia
struct Cokurtosis{__T_me, __T_mp, __T_alg, __T_w} <: CokurtosisEstimator

Container type for cokurtosis estimators.

Cokurtosis encapsulates the mean estimator, matrix processing estimator, and moment algorithm for cokurtosis 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
Cokurtosis(;
    me::AbstractExpectedReturnsEstimator = SimpleExpectedReturns(),
    mp::AbstractMatrixProcessingEstimator = MatrixProcessing(),
    alg::AbstractMomentAlgorithm = Full(),
    w::Option{<:ObsWeights} = nothing
) -> Cokurtosis

Keywords correspond to the struct's fields.

Validation

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

Examples

julia
julia> Cokurtosis()
Cokurtosis
   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.factory Method
julia
factory(kte::Cokurtosis, w::ObsWeights) -> Cokurtosis

Return a new Cokurtosis estimator with observation weights w applied to the underlying mean estimator.

Arguments

  • kte: Cokurtosis estimator.

  • w: Observation weights vector observations × 1.

Returns

  • kte::Cokurtosis: Updated estimator with weights applied.

Examples

julia
julia> kte = Cokurtosis();

julia> factory(kte, StatsBase.Weights([0.2, 0.3, 0.5]))
Cokurtosis
   me ┼ SimpleExpectedReturns
      │   w ┴ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [0.2, 0.3, 0.5]
   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 ┴ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [0.2, 0.3, 0.5]

Related

source
PortfolioOptimisers.cokurtosis Function
julia
cokurtosis(kte::Option{<:Cokurtosis}, X::MatNum; dims::Int = 1,
           mean = nothing, kwargs...)

Compute the cokurtosis tensor for a dataset.

This method computes the cokurtosis tensor using the estimator's mean and matrix processing algorithm. Observation weights in kte.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.

Arguments

  • kte: Cokurtosis estimator.

    • kte::Cokurtosis{<:Any, <:Any, <:Full}: Cokurtosis estimator with Full moment algorithm.

    • kte::Cokurtosis{<:Any, <:Any, <:Semi}: Cokurtosis estimator with Semi moment algorithm.

    • kte::Nothing: No-op, returns 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

  • ckurt::Matrix{<:Number}: Cokurtosis tensor (assets^2 × assets^2).

Examples

julia
julia> using StableRNGs

julia> rng = StableRNG(123456789);

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

julia> cokurtosis(Cokurtosis(), X)
4×4 Matrix{Float64}:
  1.33947   -0.246726  -0.246726   0.493008
 -0.246726   0.493008   0.493008  -0.201444
 -0.246726   0.493008   0.493008  -0.201444
  0.493008  -0.201444  -0.201444   0.300335

Related

source
julia
cokurtosis(ke::WindowedCokurtosis, X::MatNum; dims::Int = 1, iv::Option{<:MatNum} = nothing, kwargs...)

Compute the cokurtosis tensor 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 cokurtosis estimator.

Arguments

  • ke: Windowed cokurtosis 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

  • ckurt::Matrix{<:Number}: Cokurtosis tensor (assets² × assets²).

Related

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

Abstract supertype for all cokurtosis estimators in PortfolioOptimisers.jl.

All concrete and/or abstract types implementing cokurtosis estimation algorithms should be subtypes of CokurtosisEstimator.

Interfaces

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

Cokurtosis

  • PortfolioOptimisers.cokurtosis(kte::CokurtosisEstimator, X::MatNum; dims::Int = 1, mean = nothing, kwargs...) -> MatNum: Computes the cokurtosis tensor.

Arguments

  • kte: Cokurtosis 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

  • ckurt::MatNum: Cokurtosis tensor features^2 × features^2.

Factory

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

Arguments

  • kte: Cokurtosis estimator.

  • w: Observation weights vector observations × 1.

Returns

  • kte::CokurtosisEstimator: New cokurtosis estimator of the same type, with the new weights applied.

View

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

Arguments

  • kte: Cokurtosis estimator.

  • i: Index or indices.

Returns

  • kev: New cokurtosis estimator of the same type as the argument, for the new view.

Examples

We can create a dummy cokurtosis estimator as follows:

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

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

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

julia> function PortfolioOptimisers.port_opt_view(kte::MyCokurtosisEstimator, i)
           return kte
       end

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

julia> cokurtosis(MyCokurtosisEstimator(), [1.0 2.0; 0.3 0.7; 0.5 1.1])
4×4 Matrix{Float64}:
 0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0

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

Related

source
PortfolioOptimisers._cokurtosis Function
julia
_cokurtosis(X::MatNum, mp::AbstractMatrixProcessingEstimator, w::Option{<:ObsWeights}) -> MatNum

Internal helper for cokurtosis computation.

_cokurtosis computes the cokurtosis tensor for the input data matrix and applies matrix processing using the specified estimator.

Mathematical definition

Let X be the T×N matrix of demeaned returns. Define the T×N2 matrix Z with rows:

Zt,=(1xt)(xt1).

Where:

  • Zt,: t-th row of the auxiliary matrix Z.

  • xt: t-th row of demeaned returns.

  • : Kronecker product.

  • : Element-wise (Hadamard) product.

The N2×N2 cokurtosis tensor is:

Unweighted:

K^=1TZZ.

Weighted:

K^=1t=1Twt(wZ)Z.

Where:

  • K^: N2×N2 cokurtosis tensor.

  • Z: T×N2 auxiliary matrix of pairwise return products.

  • T: Number of observations.

  • w: Observation weights vector T×1.

  • wt: Observation weight at time t.

Arguments

  • X: Data matrix (observations × assets).

  • mp: Matrix processing estimator.

  • w: Optional observation weights.

Returns

  • ckurt::Matrix{<:Number}: Cokurtosis tensor after matrix processing.

Related

source
PortfolioOptimisers.port_opt_view Method
julia
port_opt_view(
    kte::Cokurtosis,
    i,
    args...
) -> Cokurtosis{<:AbstractExpectedReturnsEstimator, <:AbstractMatrixProcessingEstimator, <:AbstractMomentAlgorithm}

Gets the view of the cokurtosis estimator for the i-th element(s).

Arguments

  • kte: Cokurtosis estimator.

  • i: Index or indices to view.

Returns

  • kev: New cokurtosis estimator of the same type as the argument, for the new view.

Related

source