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 ny 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::StatsBase.CovarianceEstimator,
args...;
kwargs...
) -> StatsBase.CovarianceEstimatorFallback 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
sourcefactory(
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
sourceStatistics.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
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::StatsBase.CovarianceEstimator,
args...;
kwargs...
) -> StatsBase.CovarianceEstimatorFallback 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
sourcefactory(
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.
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
sourceStatistics.cor Method
Statistics.cor(
ce::Covariance,
X::MatNum;
dims::Int = 1,
mean = nothing,
kwargs...
) -> MatNumCompute the correlation matrix using a Covariance estimator.
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
source