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
struct GeneralCovariance{__T_ce, __T_w} <: AbstractCovarianceEstimatorA 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 vectorobservations × 1, or a concrete subtype ofDynamicAbstractWeights. Ifnothing, the computation is unweighted.
Constructors
GeneralCovariance(;
ce::StatsBase.CovarianceEstimator = StatsBase.SimpleCovariance(;
corrected = true),
w::Option{<:ObsWeights} = nothing
) -> GeneralCovarianceKeywords correspond to the struct's fields.
Validation
- If
wis notnothing,!isempty(w).
Details
cecan be used to specify any subtype ofStatsBase.CovarianceEstimator. This allows users to leverage packages such asCovarianceEstimation.jl, which implement custom covariance estimators.
Examples
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
sourceStatistics.cov Method
Statistics.cov(
ce::GeneralCovariance,
X::MatNum;
dims::Int = 1,
mean = nothing,
kwargs...
) -> MatNumCompute 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 matrixobservations × featuresif thedimskeyword does not exist ordims = 1,features × observationswhendims = 2.dims: Dimension along which to perform the computation.mean: Optional mean value to use for centering.kwargs...: Additional keyword arguments passed torobust_cov.
Returns
sigma::MatNum: Covariance matrixfeatures x features.
Details
- Calls
robust_covwith the appropriate covariance estimator.
Related
Examples
julia> X = [0.01 0.02; 0.03 0.04; 0.02 0.03];
julia> cov(GeneralCovariance(), X)
2×2 Matrix{Float64}:
0.0001 0.0001
0.0001 0.0001Statistics.cor Method
Statistics.cor(
ce::GeneralCovariance,
X::MatNum;
dims::Int = 1,
mean = nothing,
kwargs...
) -> MatNumCompute 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 matrixobservations × featuresif thedimskeyword does not exist ordims = 1,features × observationswhendims = 2.dims: Dimension along which to perform the computation.mean: Optional mean value to use for centering.kwargs...: Additional keyword arguments passed torobust_cor.
Returns
rho::MatNum: Correlation matrixfeatures x features.
Details
- Calls
robust_corwith the appropriate covariance estimator.
Related
Examples
julia> X = [0.01 0.02; 0.03 0.04; 0.02 0.03];
julia> cor(GeneralCovariance(), X)
2×2 Matrix{Float64}:
1.0 1.0
1.0 1.0Covariance
PortfolioOptimisers.Covariance Type
struct Covariance{__T_me, __T_ce, __T_alg} <: AbstractCovarianceEstimatorA 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
Covariance(;
me::AbstractExpectedReturnsEstimator = SimpleExpectedReturns(),
ce::StatsBase.CovarianceEstimator = GeneralCovariance(),
alg::AbstractMomentAlgorithm = FullMoment()
) -> CovarianceKeywords correspond to the struct's fields.
Examples
julia> Covariance()
Covariance
me ┼ SimpleExpectedReturns
│ w ┴ nothing
ce ┼ GeneralCovariance
│ ce ┼ StatsBase.SimpleCovariance: StatsBase.SimpleCovariance(true)
│ w ┴ nothing
alg ┴ FullMoment()Related
sourceStatistics.cov Method
Statistics.cov(
ce::Covariance,
X::MatNum;
dims::Int = 1,
mean = nothing,
kwargs...
) -> MatNumCompute the covariance matrix using a Covariance estimator.
Mathematical definition
FullMoment covariance:
Where:
: Estimated covariance between assets and . : Return of asset at time . : Estimated mean of asset . : Number of observations.
SemiMoment (downside) covariance — clip de-meaned returns to zero before computing:
Where:
: Clipped de-meaned return of asset at time . : Return of asset at time . : Estimated mean of asset .
Where:
: Estimated semi-covariance between assets and . , : Clipped de-meaned returns of assets and . : Number of observations.
Arguments
ce: Covariance estimator.ce::Covariance{<:Any, <:Any, <:FullMoment}: Covariance estimator withFullMomentmoment algorithm.ce::Covariance{<:Any, <:Any, <:SemiMoment}: Covariance estimator withSemiMomentmoment algorithm.
X: Data matrixobservations × featuresif thedimskeyword does not exist ordims = 1,features × observationswhendims = 2.dims: Dimension along which to perform the computation.mean: Optional mean value to use for centering. If not provided, computed usingce.me.kwargs...: Additional keyword arguments passed to the underlying covariance estimator.
Returns
sigma::MatNum: Covariance matrixfeatures x features.
Related
Examples
julia> X = [0.01 0.02; 0.03 0.04; 0.02 0.03];
julia> cov(Covariance(), X)
2×2 Matrix{Float64}:
0.0001 0.0001
0.0001 0.0001Statistics.cov Method
cov(
ce::Covariance{<:Any, <:Any, <:SemiMoment},
X::AbstractMatrix{<:Union{var"#s29", var"#s28"} where {var"#s29"<:Number, var"#s28"<:AbstractJuMPScalar}};
dims,
mean,
kwargs...
) -> AnySemiMoment variant of cov(ce::Covariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...). Clips de-meaned returns to zero before computing the covariance matrix, capturing only downside co-movements.
Statistics.cor Method
Statistics.cor(
ce::Covariance,
X::MatNum;
dims::Int = 1,
mean = nothing,
kwargs...
) -> MatNumCompute the correlation matrix using a Covariance estimator.
Mathematical definition
Where:
: Estimated correlation between assets and . : Estimated covariance between assets and . : Estimated standard deviation of asset .
Arguments
ce: Covariance estimator.ce::Covariance{<:Any, <:Any, <:FullMoment}: Covariance estimator withFullMomentmoment algorithm.ce::Covariance{<:Any, <:Any, <:SemiMoment}: Covariance estimator withSemiMomentmoment algorithm.
X: Data matrixobservations × featuresif thedimskeyword does not exist ordims = 1,features × observationswhendims = 2.dims: Dimension along which to perform the computation.mean: Optional mean value to use for centering. If not provided, computed usingce.me.kwargs...: Additional keyword arguments passed to the underlying correlation estimator.
Returns
rho::MatNum: Correlation matrixfeatures x features.
Related
Examples
julia> X = [0.01 0.02; 0.03 0.04; 0.02 0.03];
julia> cor(Covariance(), X)
2×2 Matrix{Float64}:
1.0 1.0
1.0 1.0Statistics.cor Method
cor(
ce::Covariance{<:Any, <:Any, <:SemiMoment},
X::AbstractMatrix{<:Union{var"#s29", var"#s28"} where {var"#s29"<:Number, var"#s28"<:AbstractJuMPScalar}};
dims,
mean,
kwargs...
) -> AnySemiMoment variant of cor(ce::Covariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...). Clips de-meaned returns to zero before computing the correlation matrix, capturing only downside co-movements.