Covariance

PortfolioOptimisers.GeneralWeightedCovarianceType
struct GeneralWeightedCovariance{T1, T2} <: AbstractCovarianceEstimator
    ce::T1
    w::T2
end

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

GeneralWeightedCovariance allows users to specify both the covariance estimation method and optional observation weights. This enables robust and extensible covariance estimation workflows.

Fields

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

Constructor

GeneralWeightedCovariance(;
                          ce::StatsBase.CovarianceEstimator = StatsBase.SimpleCovariance(;
                                                                                         corrected = true),
                          w::Union{Nothing, <:AbstractWeights} = nothing)

Keyword arguments correspond to the fields above.

Validation

  • If w is provided, !isempty(w).

Examples

julia> using StatsBase

julia> gwc = GeneralWeightedCovariance()
GeneralWeightedCovariance
  ce | StatsBase.SimpleCovariance: StatsBase.SimpleCovariance(true)
   w | nothing

julia> w = Weights([0.1, 0.2, 0.7]);

julia> gwc = GeneralWeightedCovariance(; w = w)
GeneralWeightedCovariance
  ce | StatsBase.SimpleCovariance: StatsBase.SimpleCovariance(true)
   w | StatsBase.Weights{Float64, Float64, Vector{Float64}}: [0.1, 0.2, 0.7]

Related

source
Statistics.covMethod
cov(ce::GeneralWeightedCovariance, X::AbstractMatrix; dims::Int = 1, mean = nothing,
    kwargs...)

Compute the covariance matrix using a GeneralWeightedCovariance 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: Dimension along which to compute the covariance.
  • mean: Optional mean vector to use for centering.
  • kwargs...: Additional keyword arguments passed to robust_cov.

Returns

  • sigma::AbstractMatrix{<:Real}: Covariance matrix.

Related

source
Statistics.corMethod
cor(ce::GeneralWeightedCovariance, X::AbstractMatrix; dims::Int = 1, mean = nothing,
    kwargs...)

Compute the correlation matrix using a GeneralWeightedCovariance 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: Dimension along which to compute the correlation.
  • mean: Optional mean vector to use for centering.
  • kwargs...: Additional keyword arguments passed to robust_cor.

Returns

  • rho::AbstractMatrix{<:Real}: Correlation matrix.

Related

source
PortfolioOptimisers.CovarianceType
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. This enables modular and extensible workflows for portfolio optimization and risk modeling.

Fields

  • me: Expected returns estimator.
  • ce: Covariance estimator.
  • alg: Moment algorithm.

Constructor

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

Keyword arguments correspond to the fields above.

Examples

julia> cov_est = Covariance()
Covariance
   me | SimpleExpectedReturns
      |   w | nothing
   ce | GeneralWeightedCovariance
      |   ce | StatsBase.SimpleCovariance: StatsBase.SimpleCovariance(true)
      |    w | nothing
  alg | Full()

Related

source
Statistics.covMethod
cov(ce::Covariance, X::AbstractMatrix; 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: Dimension along which to compute the covariance.

  • 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::AbstractMatrix{<:Real}: Covariance matrix.

Related

source
Statistics.corMethod
cor(ce::Covariance, X::AbstractMatrix; 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: Dimension along which to compute the correlation.

  • 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::AbstractMatrix{<:Real}: Correlation matrix.

Related

source