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
sourcePortfolioOptimisers.factory Method
factory(
ce::GeneralCovariance,
w::ObsWeights
) -> GeneralCovarianceReturn a new GeneralCovariance estimator with observation weights w.
Arguments
ce: Covariance estimator.w: Observation weights vectorobservations × 1.
Returns
ce: New covariance estimator of the same type as the argument, with the new weights applied.
Examples
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
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.0PortfolioOptimisers.port_opt_view Method
port_opt_view(
ce::GeneralCovariance,
i,
args...
) -> GeneralCovariance{<:CovarianceEstimator}Gets the view of the covariance estimator for the i-th element(s).
Arguments
ce: Covariance estimator.i: Index or indices to view.
Returns
ce: New covariance estimator of the same type as the argument, for the new view.
Related
sourceCovariance
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 = Full()
) -> 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 ┴ Full()Related
sourcePortfolioOptimisers.factory Method
factory(
ce::Covariance,
w::ObsWeights
) -> CovarianceReturn a new Covariance estimator with observation weights w applied to both the expected returns and covariance estimators.
Arguments
ce: Covariance estimator.w: Observation weights vectorobservations × 1.
Returns
ce: New covariance estimator of the same type as the argument, with the new weights applied.
Details
Calls
factory(ce.me, w)andfactory(ce.ce, w)to propagate the weights to the mean and covariance estimators.Preserves the moment algorithm
ce.algfrom the original estimator.Enables weighted estimation for both mean and covariance in portfolio workflows.
Examples
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
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
Full covariance:
Where:
: Estimated covariance between assets and . : Return of asset at time . : Estimated mean of asset . : Number of observations.
Semi (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.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, <:Semi},
X::AbstractMatrix{<:Union{var"#s20", var"#s19"} where {var"#s20"<:Number, var"#s19"<:AbstractJuMPScalar}};
dims,
mean,
kwargs...
) -> AnySemi 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.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, <:Semi},
X::AbstractMatrix{<:Union{var"#s20", var"#s19"} where {var"#s20"<:Number, var"#s19"<:AbstractJuMPScalar}};
dims,
mean,
kwargs...
) -> AnySemi 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.
PortfolioOptimisers.port_opt_view Method
port_opt_view(
ce::Covariance,
i,
args...
) -> Covariance{<:AbstractExpectedReturnsEstimator, <:CovarianceEstimator, <:AbstractMomentAlgorithm}Gets the view of the covariance estimator for the i-th element(s).
Arguments
ce: Covariance estimator.i: Index or indices to view.
Returns
ce: New covariance estimator of the same type as the argument, for the new view.
Related
source