Skip to content
13

Simple 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.

General covariance

PortfolioOptimisers.GeneralCovariance Type
julia
struct GeneralCovariance{__T_ce, __T_w} <: AbstractCovarianceEstimator

A simple wrapper around a StatsBase.CovarianceEstimator, optional StatsBase.AbstractWeights, and an optional index. It uses ideas from SCIML to simplify the standard API of StatsBase.cov.

Fields

  • ce: Covariance estimator.

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

Constructors

julia
GeneralCovariance(;
    ce::StatsBase.CovarianceEstimator = StatsBase.SimpleCovariance(;
        corrected = true),
    w::Option{<:ObsWeights} = nothing
) -> GeneralCovariance

Keywords correspond to the struct's fields.

Validation

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

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> GeneralCovariance()
GeneralCovariance
  ce ┼ StatsBase.SimpleCovariance: StatsBase.SimpleCovariance(true)
   w ┴ nothing

julia> 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]

Related

source
PortfolioOptimisers.factory Method
julia
factory(
    ce::StatsBase.CovarianceEstimator,
    args...;
    kwargs...
) -> StatsBase.CovarianceEstimator

Fallback for covariance estimator factory methods.

Arguments

  • ce: Covariance estimator.

  • args...: Optional arguments (ignored).

  • kwargs...: Optional keyword arguments (ignored).

Returns

  • ce::StatsBase.CovarianceEstimator: The original covariance estimator.

Related

source
julia
factory(
    ce::GeneralCovariance,
    w::ObsWeights
) -> GeneralCovariance

Return a new GeneralCovariance estimator with observation weights w.

Arguments

  • ce: Covariance estimator.

  • w: Observation weights vector observations × 1.

Returns

  • ce: New covariance estimator of the same type as the argument, with the new weights applied.

Examples

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

julia> factory(ce, 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]

Related

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

Compute the covariance matrix using a GeneralCovariance estimator.

This method dispatches to the appropriate robust_cov depending on ce.w, which computes the covariance matrix using ce.ce.

Arguments

  • ce: Covariance 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 passed to robust_cov.

Returns

  • sigma::MatNum: Covariance matrix features x features.

Details

  • Calls robust_cov with the appropriate covariance estimator.

Related

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

Compute the correlation matrix using a GeneralCovariance estimator.

This method dispatches to the appropriate robust_cor depending on ce.w, which computes the correlation matrix using ce.ce.

Arguments

  • ce: Covariance 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 passed to robust_cor.

Returns

  • rho::MatNum: Correlation matrix features x features.

Details

  • Calls robust_cor with the appropriate covariance estimator.

Related

source

Covariance

PortfolioOptimisers.Covariance Type
julia
struct Covariance{__T_me, __T_ce, __T_alg} <: AbstractCovarianceEstimator

A flexible container type for covariance estimation in PortfolioOptimisers.jl.

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

Fields

  • me: Expected returns estimator.

  • ce: Covariance estimator.

  • alg: Moment algorithm.

Constructors

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

Keywords correspond to the struct's fields.

Examples

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

Related

source
PortfolioOptimisers.factory Method
julia
factory(
    ce::StatsBase.CovarianceEstimator,
    args...;
    kwargs...
) -> StatsBase.CovarianceEstimator

Fallback for covariance estimator factory methods.

Arguments

  • ce: Covariance estimator.

  • args...: Optional arguments (ignored).

  • kwargs...: Optional keyword arguments (ignored).

Returns

  • ce::StatsBase.CovarianceEstimator: The original covariance estimator.

Related

source
julia
factory(
    ce::Covariance,
    w::ObsWeights
) -> Covariance

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 observations × 1.

Returns

  • ce: New covariance estimator of the same type as the argument, with the new weights applied.

Details

  • Calls factory(ce.me, w) and factory(ce.ce, w) to propagate the weights to the mean and covariance estimators.

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

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

Examples

julia
julia> ce = Covariance()
Covariance
   me ┼ SimpleExpectedReturns
      │   w ┴ nothing
   ce ┼ GeneralCovariance
      │   ce ┼ StatsBase.SimpleCovariance: StatsBase.SimpleCovariance(true)
      │    w ┴ 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]
   ce ┼ GeneralCovariance
      │   ce ┼ StatsBase.SimpleCovariance: StatsBase.SimpleCovariance(true)
      │    w ┴ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [0.2, 0.3, 0.5]
  alg ┴ Full()

Related

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

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 × 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. If not provided, computed using ce.me.

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

Returns

  • sigma::MatNum: Covariance matrix features x features.

Related

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

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 × 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. If not provided, computed using ce.me.

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

Returns

  • rho::MatNum: Correlation matrix features x features.

Related

source