Skip to content
11

Covariance

The covariance is an important measure of risk used in portfolio selection and performance analysis. The classic Markowitz [4] portfolio uses the portfolio variance as its risk measure, which is computed from the covariance matrix and portfolio weights. Here we define the most basic covariance/correlation estimator.

PortfolioOptimisers.GeneralCovariance Type
julia
struct GeneralCovariance{T1, T2, T3} <: AbstractCovarianceEstimator
    ce::T1
    w::T2
    idx::T3
end

A flexible covariance estimator for PortfolioOptimisers.jl supporting arbitrary covariance estimators and optional observation weights.

GeneralCovariance allows users to specify both the covariance estimation method and optional observation weights.

Fields

  • ce: Covariance estimator.

  • w: Optional weights for each observation. If nothing, the estimator is unweighted.

  • idx: Optional indices of the observations to use for estimation. If nothing, all observations are used.

Constructor

julia
GeneralCovariance(;
                  ce::StatsBase.CovarianceEstimator = StatsBase.SimpleCovariance(;
                                                                                 corrected = true),
                  w::Option{<:StatsBase.AbstractWeights} = nothing,
                  idx::Option{<:VecInt} = nothing)

Keyword arguments correspond to the fields above.

Validation

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

  • If idx is not nothing, !isempty(idx) and all indices are positive integers.

Details

  • ce can be used to specify ny subtype of StatsBase.CovarianceEstimator. This allows users to leverage packages such as CovarianceEstimation.jl, which implement custom covariance estimators.

Examples

julia
julia> gwc = GeneralCovariance()
GeneralCovariance
   ce ┼ StatsBase.SimpleCovariance: StatsBase.SimpleCovariance(true)
    w ┼ nothing
  idx ┴ nothing

julia> gwc = GeneralCovariance(; w = StatsBase.Weights([0.1, 0.2, 0.7]))
GeneralCovariance
   ce ┼ StatsBase.SimpleCovariance: StatsBase.SimpleCovariance(true)
    w ┼ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [0.1, 0.2, 0.7]
  idx ┴ nothing

Related

source
PortfolioOptimisers.factory Method
julia
factory(ce::GeneralCovariance, w::StatsBase.AbstractWeights)

Return a new GeneralCovariance estimator with observation weights w.

Arguments

  • ce: A GeneralCovariance estimator.

  • w: Observation weights vector.

Returns

  • ce::GeneralCovariance: A new estimator with the same covariance estimator and observation weights w.

Related

source
Statistics.cov Method
julia
Statistics.cov(ce::GeneralCovariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...)

Compute the covariance matrix using a GeneralCovariance estimator.

This method dispatches to robust_cov, using the specified covariance estimator and optional observation weights stored in ce. If no weights are provided, the unweighted covariance is computed; otherwise, the weighted covariance is used.

Arguments

  • ce: Covariance estimator containing the method and optional weights.

  • X: Data matrix (observations × assets).

  • dims: Dimensions along which to perform the computation.

  • mean: Optional mean vector to use for centering.

  • kwargs...: Additional keyword arguments passed to robust_cov.

Returns

  • sigma::MatNum: Covariance matrix.

Related

source
Statistics.cor Method
julia
Statistics.cor(ce::GeneralCovariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...)

Compute the correlation matrix using a GeneralCovariance estimator.

This method dispatches to robust_cor, using the specified covariance estimator and optional observation weights stored in ce. If no weights are provided, the unweighted correlation is computed; otherwise, the weighted correlation is used.

Arguments

  • ce: Covariance estimator containing the method and optional weights.

  • X: Data matrix (observations × assets).

  • dims: Dimensions along which to perform the computation.

  • mean: Optional mean vector to use for centering.

  • kwargs...: Additional keyword arguments passed to robust_cor.

Returns

  • rho::MatNum: Correlation matrix.

Related

source
PortfolioOptimisers.Covariance Type
julia
struct Covariance{T1, T2, T3} <: AbstractCovarianceEstimator
    me::T1
    ce::T2
    alg::T3
end

A flexible container type for configuring and applying joint expected returns and covariance estimation in PortfolioOptimisers.jl.

Covariance encapsulates all components required for estimating the mean vector and covariance matrix of asset returns, including the expected returns estimator, the covariance estimator, and the moment algorithm.

Fields

  • me: Expected returns estimator.

  • ce: Covariance estimator.

  • alg: Moment algorithm.

Constructor

julia
Covariance(; me::AbstractExpectedReturnsEstimator = SimpleExpectedReturns(),
           ce::StatsBase.CovarianceEstimator = GeneralCovariance(),
           alg::AbstractMomentAlgorithm = Full())

Keyword arguments correspond to the fields above.

Examples

julia
julia> Covariance()
Covariance
   me ┼ SimpleExpectedReturns
      │     w ┼ nothing
      │   idx ┴ nothing
   ce ┼ GeneralCovariance
      │    ce ┼ StatsBase.SimpleCovariance: StatsBase.SimpleCovariance(true)
      │     w ┼ nothing
      │   idx ┴ nothing
  alg ┴ Full()

Related

source
PortfolioOptimisers.factory Method
julia
factory(ce::Covariance, w::StatsBase.AbstractWeights)

Return a new Covariance estimator with observation weights w applied to both the expected returns and covariance estimators.

Arguments

  • ce: Covariance estimator.

  • w: Observation weights vector.

Returns

  • ce::Covariance: New estimator with weights applied to both the mean and covariance estimators.

Details

  • Applies weights to both the expected returns estimator ce.me and the covariance estimator ce.ce.

  • Preserves the moment algorithm ce.alg from the original estimator.

  • Enables weighted estimation for both mean and covariance in portfolio workflows.

Related

Examples

julia
julia> ce = Covariance()
Covariance
   me ┼ SimpleExpectedReturns
      │     w ┼ nothing
      │   idx ┴ nothing
   ce ┼ GeneralCovariance
      │    ce ┼ StatsBase.SimpleCovariance: StatsBase.SimpleCovariance(true)
      │     w ┼ nothing
      │   idx ┴ nothing
  alg ┴ Full()

julia> ce_w = factory(ce, StatsBase.Weights([0.2, 0.3, 0.5]))
Covariance
   me ┼ SimpleExpectedReturns
      │     w ┼ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [0.2, 0.3, 0.5]
      │   idx ┴ nothing
   ce ┼ GeneralCovariance
      │    ce ┼ StatsBase.SimpleCovariance: StatsBase.SimpleCovariance(true)
      │     w ┼ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [0.2, 0.3, 0.5]
      │   idx ┴ nothing
  alg ┴ Full()
source
Statistics.cov Method
julia
Statistics.cov(ce::Covariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...)

Compute the covariance matrix using a Covariance estimator.

Arguments

  • ce: Covariance estimator.

    • ce::Covariance{<:Any, <:Any, <:Full}: Covariance estimator with Full moment algorithm.

    • ce::Covariance{<:Any, <:Any, <:Semi}: Covariance estimator with Semi moment algorithm.

  • X: Data matrix (observations × assets).

  • dims: Dimensions along which to perform the computation.

  • mean: Optional mean vector for centering. If not provided, computed using ce.me.

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

Returns

  • sigma::MatNum: Covariance matrix.

Related

source
Statistics.cor Method
julia
Statistics.cor(ce::Covariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...)

Compute the correlation matrix using a Covariance estimator.

Arguments

  • ce: Covariance estimator.

    • ce::Covariance{<:Any, <:Any, <:Full}: Covariance estimator with Full moment algorithm.

    • ce::Covariance{<:Any, <:Any, <:Semi}: Covariance estimator with Semi moment algorithm.

  • X: Data matrix (observations × assets).

  • dims: Dimensions along which to perform the computation.

  • mean: Optional mean vector for centering. If not provided, computed using ce.me.

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

Returns

  • rho::MatNum: Correlation matrix.

Related

source