PortfolioOptimisersCovariance
PortfolioOptimisers.PortfolioOptimisersCovariance — Type
struct PortfolioOptimisersCovariance{__T_ce, __T_mp, __T_cache} <: AbstractCovarianceEstimatorRuns any covariance estimator, then applies a matrix post-processing step to its result.
ce computes the raw matrix and mp repairs or filters it — positive-definite repair, denoising, and detoning — so the composite is the estimator the rest of the library takes as its default.
Fields
ce: Covariance estimator.
mp: Matrix processing estimator.
cache: Optional partial-fit state. It isnothinguntilpartial_fit!writes one, and the estimator's read-out verb reads it when the caller gives no data matrix. Each propagation channel does one thing with it:factorycarries it unchanged, because a factory call resolves configuration rather than the sample;port_opt_viewslices it to the selected assets by index copy, so the viewed estimator answers over those assets alone; andobs_weights_viewdrops it, because no slice of a state exists on the observation axis. A family whose state has no exact asset slice drops it on both axes and names the reason.
Constructors
PortfolioOptimisersCovariance(; ce::StatsBase.CovarianceEstimator = Covariance(), mp::AbstractMatrixProcessingEstimator = MatrixProcessing(), cache::Option{<:AbstractPartialFitState} = nothing) -> PortfolioOptimisersCovarianceKeywords correspond to the struct's fields.
The incremental fit
The composite folds by composition, and the state it carries decides which of the two routes it takes.
With cache holding nothing, partial_fit! forwards the observation to ce.ce and keeps nothing of its own, because mp reads no observation that a moment and a count cannot stand in for: pdm and dt read sigma alone, and dn reads the effective sample ratio T / N. The one-argument Statistics.cov then reads the inner estimator's folded matrix and applies mp from observation_count, which is exactly the size(X, 1) the matrix arm would have read. An mp.alg of a caller's own is the one step with no shape substitute, and it is refused by name at the fold.
With cache holding a SampleBufferState — which Online seeds — the composite takes the buffering route instead, and its read-out is the batch verb over the observations the buffer kept. That is the route an mp.alg needs.
Propagated parameters
When factory is called on this type, the following @fprop-tagged fields are automatically propagated:
View parameters
When port_opt_view is called on this type, the following @vprop-tagged fields are automatically subset to the selected indices:
ce: Recursively viewed viaport_opt_view.cache: Sliced to the selected assets viaport_opt_view.
Observation view parameters
When obs_weights_view is called on this type:
cache: Dropped viaobs_weights_view, because no slice of a state exists on the observation axis.
Examples
julia> PortfolioOptimisersCovariance()PortfolioOptimisersCovariance ce ┼ Covariance │ me ┼ SimpleExpectedReturns │ │ w ┴ nothing │ ce ┼ GeneralCovariance │ │ ce ┼ StatsBase.SimpleCovariance: StatsBase.SimpleCovariance(true) │ │ w ┴ nothing │ alg ┼ FullMoment() │ w ┴ nothing mp ┼ MatrixProcessing │ pdm ┼ Posdef │ │ alg ┼ UnionAll: NearestCorrelationMatrix.Newton │ │ kwargs ┴ @NamedTuple{}: NamedTuple() │ dn ┼ nothing │ dt ┼ nothing │ alg ┼ nothing │ order ┴ NTuple{4, Symbol}: (:pdm, :dn, :dt, :alg)Related
Statistics.cov — Method
Statistics.cov(ce::PortfolioOptimisersCovariance, X::MatNum; dims = 1,
active_mask::Option{<:AbstractMatrix{<:Bool}} = nothing, kwargs...)Compute the covariance matrix with post-processing using a PortfolioOptimisersCovariance estimator.
This method computes the covariance matrix for the input data matrix X using the underlying covariance estimator in ce, and then applies the matrix post-processing step specified by ce.mp.
The composite is transparent to a gap: it forwards X and active_mask to ce.ce untouched, so gap_fill_value on it is gap_fill_value on ce.ce. A composite that wraps a gap-aware estimator therefore takes no finiteness refusal of its own, and repairs the finite block rather than the whole frame, which is what the AssetPanel method beside it does.
Algorithm
- Check
dims, and orientXandactive_masktoobservations × assets, transposing them whendims == 2. - Refuse a gapped sample with
assert_finite_samplewhengap_fill_valueonceis finite, becausece.ceis then a plain estimator that has no answer for one. - Compute
sigmawithStatistics.cov(ce.ce, X; kwargs...), addingactive_maskwhen one is given. - When
sigmais immutable, copy it into aMatrix, because step 5 writes in place. - Apply
matrix_processing!withce.mptosigma, in place, ormatrix_processing_block!whence.ceis gap-aware, so an asset outside the Coverage Universe keeps itsNaNrow and column. - Return
sigma.
ce.ce runs before ce.mp, and ce.mp.order fixes the order of the steps inside the post-processing. Step 1 orients X once, so the estimator and the post-processing both read the same orientation and neither takes a dims of its own.
Arguments
ce: Composite covariance estimator with post-processing.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.dims: Dimension along which to perform the computation.active_mask: Optional boolean matrix with the same size asX, forwarded toce.cewhen it is given.kwargs...: Additional keyword arguments passed to the underlying covariance estimator and matrix processing step.
Validation
dims in (1, 2).
Returns
sigma::Matrix{<:Number}: The processed covariance matrix.
Related
Statistics.cor — Method
Statistics.cor(ce::PortfolioOptimisersCovariance, X::MatNum; dims = 1,
active_mask::Option{<:AbstractMatrix{<:Bool}} = nothing, kwargs...)Compute the correlation matrix with post-processing using a PortfolioOptimisersCovariance estimator.
This method computes the correlation matrix for the input data matrix X using the underlying covariance estimator in ce, and then applies the matrix post-processing step specified by ce.mp.
Algorithm
- Check
dimsand orientXtoobservations × assets, transposing it whendims == 2. - Compute
rhowithStatistics.cor(ce.ce, X; kwargs...). - When
rhois immutable, copy it into aMatrix, because step 4 writes in place. - Apply
matrix_processing!withce.mptorho, in place. - Return
rho.
ce.ce runs before ce.mp, and ce.mp.order fixes the order of the steps inside the post-processing. Step 1 orients X once, so the estimator and the post-processing both read the same orientation and neither takes a dims of its own.
Arguments
ce: Composite covariance estimator with post-processing.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.dims: Dimension along which to perform the computation.active_mask: Optional boolean matrix with the same size asX, forwarded toce.cewhen it is given.kwargs...: Additional keyword arguments passed to the underlying covariance estimator and matrix processing step.
Validation
dims in (1, 2).
Returns
rho::Matrix{<:Number}: The processed correlation matrix.
Related
Statistics.cov — Method
Statistics.cov(ce::PortfolioOptimisersCovariance, X::MatNum,
pnl::Option{<:AssetPanel}; dims = 1, kwargs...) -> MatNum
Statistics.cor(ce::PortfolioOptimisersCovariance, X::MatNum,
pnl::Option{<:AssetPanel}; dims = 1, kwargs...) -> MatNumForward the Asset Panel to the estimator that this composite wraps, then repair the finite block of the frame it gets back.
This is the composite's override of the reduce-and-expand root. The inner estimator owns the reduction, because it alone knows whether it is plain or mask-aware, and this method owns the repair. The repair runs on the finite block through matrix_processing_block!, so an asset outside the Coverage Universe keeps its NaN row and column and nothing else is touched.
Algorithm
- Check
dimsand orientXtoobservations × assets. - Compute the matrix with the inner estimator, forwarding
pnlas its third positional argument. - When the matrix is immutable, copy it into a
Matrix, because step 4 writes in place. - Repair its finite block with
matrix_processing_block!, underce.mpand the columns ofXthe block names. - Return the matrix.
Arguments
ce: Composite covariance estimator with post-processing.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.pnl: OptionalAssetPanel, whose active mask the Coverage Universe of the fit is derived from.nothingmakes the rule finiteness alone.dims: Dimension along which to perform the computation.kwargs...: Additional keyword arguments passed to the underlying estimator and to the matrix processing step.
Validation
dims in (1, 2).
Returns
sigma::MatNum: The processed covariance matrix, or correlation matrix, on the full asset universe.
Related
PortfolioOptimisers.partial_fit! — Method
partial_fit!(ce::PortfolioOptimisersCovariance{<:Any, <:Any, Nothing}, X::MatNum;
dims::Int = 1, kwargs...)
partial_fit!(ce::PortfolioOptimisersCovariance{<:Any, <:Any, Nothing}, x::VecNum;
kwargs...)Folds observations into a PortfolioOptimisersCovariance by forwarding them to ce.ce.
The composite is exact by composition and keeps no state of its own: ce.ce folds the raw matrix, and ce.mp is a read-out step that runs on whatever matrix it is given. The composite therefore owns no accumulator, and its cache stays nothing on this route — which is what selects the route, because a composite carrying a SampleBufferState is one Online seeded and takes the buffering fold instead.
The step is O(N²), the inner estimator's own, plus nothing.
ce.mp reads no observation that a moment and a count cannot stand in for, with one exception: an mp.alg of a caller's own is handed the whole sample, and assert_shape_only_matrix_processing refuses it here, at the fold, rather than at the read-out where the rows would already be gone.
Algorithm
- Refuse an
mpcarrying a sample-readingalg. - Rebind
ce.ceto the estimatorpartial_fit!gives, withAccessors.@reset.
Arguments
ce: Covariance estimator.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.x: One observation, whose entries are the assets.dims: Dimension along which to perform the computation.kwargs...: Additional keyword arguments, forwarded toce.ce.
Validation
ce.mp.algisnothing. AnArgumentErroris thrown otherwise.
Returns
ce: The composite, withce.cerebound to the estimator carrying the state after the last observation.
Related
Statistics.cov — Method
Statistics.cov(ce::PortfolioOptimisersCovariance{<:Any, <:Any, Nothing}; kwargs...)
Statistics.cor(ce::PortfolioOptimisersCovariance{<:Any, <:Any, Nothing}; kwargs...)Reads the covariance, or the correlation, a folded PortfolioOptimisersCovariance has accumulated.
The read-out of the composition fold. ce.ce answers its own folded matrix, and ce.mp is applied to it from the shape of the sample: observation_count reads the number of observations folded off the inner state, and it is exactly the size(X, 1) the matrix arm of matrix_processing! would have read, NaN rows included. The substitution is therefore an identity, not an approximation, and this method answers what a batch fit over the same observations answers.
The matrix the inner estimator returns is copied when it is immutable, because the processing writes in place, exactly as the matrix methods beside this one do.
Algorithm
- Read the inner estimator's folded matrix with the one-argument
Statistics.covorStatistics.cor. - Copy it into a
Matrixwhen it is immutable. - Apply
ce.mpin place through the shape arm ofmatrix_processing_block!, withTfromobservation_countandNfrom the matrix. It is the block arm rather than the plain one because a fold over a changing universe answersNaNfor an asset outside the Coverage Universe, exactly as theAssetPanelmethods beside it do; a complete matrix costs nothing extra. - Return the matrix.
Arguments
ce: Covariance estimator.kwargs...: Additional keyword arguments, forwarded toce.ce.
Validation
ce.cecarries a partial-fit state. AnArgumentErroris thrown otherwise.ce.mp.algisnothing. AnArgumentErroris thrown otherwise.
Returns
sigma::MatNum: The processed covariance matrix, orrho, the processed correlation matrix.
Related
Statistics.cov — Method
Statistics.cov(ce::PortfolioOptimisersCovariance{<:Any, <:Any, <:SampleBufferState})
Statistics.cor(ce::PortfolioOptimisersCovariance{<:Any, <:Any, <:SampleBufferState})Reads the covariance, or the correlation, of a buffered PortfolioOptimisersCovariance by refitting over its buffer.
The read-out of the buffering route, which Online seeds and which an mp.alg of a caller's own needs. It is the batch verb over the observations the buffer kept, so it answers exactly what a batch fit over those rows answers, mp.alg included.
Arguments
ce: Covariance estimator.
Validation
cecarries aSampleBufferState. AnArgumentErroris thrown otherwise.
Returns
sigma::MatNum: The processed covariance matrix, orrho, the processed correlation matrix.
Related