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
struct GeneralCovariance{T1, T2, T3} <: AbstractCovarianceEstimator
ce::T1
w::T2
idx::T3
endA 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. Ifnothing, the estimator is unweighted.idx: Optional indices of the observations to use for estimation. Ifnothing, all observations are used.
Constructor
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
wis notnothing,!isempty(w).If
idxis notnothing,!isempty(idx)and all indices are positive integers.
Details
cecan be used to specify ny subtype ofStatsBase.CovarianceEstimator. This allows users to leverage packages such asCovarianceEstimation.jl, which implement custom covariance estimators.
Examples
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 ┴ nothingRelated
sourcePortfolioOptimisers.factory Method
factory(ce::GeneralCovariance, w::StatsBase.AbstractWeights)Return a new GeneralCovariance estimator with observation weights w.
Arguments
ce: AGeneralCovarianceestimator.w: Observation weights vector.
Returns
ce::GeneralCovariance: A new estimator with the same covariance estimator and observation weightsw.
Related
sourceStatistics.cov Method
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 torobust_cov.
Returns
sigma::MatNum: Covariance matrix.
Related
sourceStatistics.cor Method
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 torobust_cor.
Returns
rho::MatNum: Correlation matrix.
Related
sourcePortfolioOptimisers.Covariance Type
struct Covariance{T1, T2, T3} <: AbstractCovarianceEstimator
me::T1
ce::T2
alg::T3
endA 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
Covariance(; me::AbstractExpectedReturnsEstimator = SimpleExpectedReturns(),
ce::StatsBase.CovarianceEstimator = GeneralCovariance(),
alg::AbstractMomentAlgorithm = Full())Keyword arguments correspond to the fields above.
Examples
julia> Covariance()
Covariance
me ┼ SimpleExpectedReturns
│ w ┼ nothing
│ idx ┴ nothing
ce ┼ GeneralCovariance
│ ce ┼ StatsBase.SimpleCovariance: StatsBase.SimpleCovariance(true)
│ w ┼ nothing
│ idx ┴ nothing
alg ┴ Full()Related
sourcePortfolioOptimisers.factory Method
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.meand the covariance estimatorce.ce.Preserves the moment algorithm
ce.algfrom the original estimator.Enables weighted estimation for both mean and covariance in portfolio workflows.
Related
Examples
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()Statistics.cov Method
Statistics.cov(ce::Covariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...)Compute the covariance matrix using a Covariance estimator.
Arguments
ce: Covariance estimator.X: Data matrix (observations × assets).dims: Dimensions along which to perform the computation.mean: Optional mean vector for centering. If not provided, computed usingce.me.kwargs...: Additional keyword arguments passed to the underlying covariance estimator.
Returns
sigma::MatNum: Covariance matrix.
Related
sourceStatistics.cor Method
Statistics.cor(ce::Covariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...)Compute the correlation matrix using a Covariance estimator.
Arguments
ce: Covariance estimator.X: Data matrix (observations × assets).dims: Dimensions along which to perform the computation.mean: Optional mean vector for centering. If not provided, computed usingce.me.kwargs...: Additional keyword arguments passed to the underlying correlation estimator.
Returns
rho::MatNum: Correlation matrix.
Related
source