Coverage Universe: private API
The coverage universe of a fit is the set of assets whose returns are finite, and whose entries in the active mask of the AssetPanel are true, on every row of the window. A prior fits each ordinary moment estimator on the returns of those assets alone. It then writes the results into arrays over all the assets, where the entries of every other asset are NaN.
Every moment function takes the asset panel as its third positional argument. Its panel method does this reduction and expansion. An estimator that reads the active mask itself replaces that method, and takes the whole window.
A matrix call can give the active mask as the active_mask keyword, with no panel. The panel method reads that mask as it reads the mask of a panel, so both calls reach the same coverage universe.
The universe and the reduction
PortfolioOptimisers.coverage_mask — Function
coverage_mask(X::MatNum, pnl::Nothing; dims::Int = 1) -> Option{BitVector}
coverage_mask(X::MatNum, pnl::AssetPanel{<:Any, Nothing, Nothing};
dims::Int = 1) -> Option{BitVector}
coverage_mask(X::MatNum, pnl::AssetPanel; dims::Int = 1) -> Option{BitVector}
coverage_mask(X::MatNum, amsk::AbstractMatrix{<:Bool}; dims::Int = 1) -> Option{BitVector}Derive the Coverage Universe of one fit, the mask that is true at each asset a plain moment estimator can fit.
An asset is in the Coverage Universe when its return is finite, and the active mask of the panel is true, at every row of the window. The method does not read the estimation mask. That mask names the assets of a cross-sectional estimate, and a moment is not a cross-sectional estimate. A static panel carries no mask, so there the rule is finiteness alone, as it is with no panel.
The active mask can come without a panel, as the active_mask keyword of a matrix call does. The last method takes that mask, observations × assets, and the panel method reads its own mask through it. coverage_mask_source chooses between the keyword and the panel.
When every asset is covered, the method returns nothing, not a mask of every true. The caller then skips the slice and the expansion, as the nothing of investable_mask lets the optimiser skip its reduction and its expansion.
The rule has a cost. One non-finite return, or one inactive row, in the window puts the asset outside the Coverage Universe of that fit. A caller with a holiday in the window can fill the price gap with PriceGapFill, or use a mask-aware estimator, which takes the whole window and writes its own frame.
The method scans X and the active mask separately, and only their asset axes must agree. A prior that reweights observations fits on the observations that its nested prior returned, and a nested prior can drop rows. So X and the panel can have different observation counts, and no row of one pairs with a row of the other. A pairing by position reads the wrong date, and a pairing by the last rows assumes a warm-up that no contract states. The method reads each input over its own rows, so it assumes neither. Where the two row counts differ, the result is conservative: an asset that the panel marks inactive at a row that the sample no longer holds is outside the Coverage Universe of that fit.
Mathematical definition
\[\begin{align} \mathcal{C} &= \left\{ i \in \{1, \ldots, N\} : x_{t,\,i} \in \mathbb{R} \text{ for } t = 1, \ldots, T \text{ and } \tilde{a}_{s,\,i} = 1 \text{ for } s = 1, \ldots, T_{a} \right\}\,. \end{align}\]
Where:
- $\mathcal{C}$: Coverage Universe of the fit, the assets whose returns are finite, and whose active mask entries are $1$, at every observation of the window.
- $x_{t,\,i}$: Return of asset $i$ at observation $t$.
- $\tilde{a}_{t,\,i}$: Active mask entry of asset $i$ at observation $t$ of the returns clock, $1$ when both prices of the return lie inside the listing of the asset.
- $T$: Number of observations.
- $T_{a}$: Number of rows of the active mask, $0$ when the panel carries no mask.
- $N$: Number of assets.
Algorithm
- Orient
Xtoobservations × assetswithdims_oriented. - Scan each column of
X. An asset stays in while its return is finite at every row. - Scan each column of the active mask, where a panel carries one. An asset stays in while the mask is
trueat every row. - Throw an
IsEmptyErrorwhen no asset is covered, because a moment over no asset has no value. - Return
nothingwhen every asset is covered, and the mask otherwise.
Arguments
X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.pnl: OptionalAssetPanel. The Coverage Universe of the fit comes from its active mask. If it isnothing, the rule reads only the finiteness of the returns.amsk: The active mask of the window,observations × assetswhateverdimssays.dims: Dimension along which the computation runs.
Validation
dims in (1, 2).- The asset axis of the active mask must be the asset axis of the oriented
X. - At least one asset must be covered.
Returns
cmsk::Option{BitVector}:trueat every covered asset, ornothingwhen every asset is covered.
Related
coverage_mask(X::MatNum, iv::MatNum,
pnl::Union{Nothing, AssetPanel, AbstractMatrix{<:Bool}};
dims::Int = 1) -> Option{BitVector}
coverage_mask(X::MatNum, iv::Nothing,
pnl::Union{Nothing, AssetPanel, AbstractMatrix{<:Bool}};
dims::Int = 1) -> Option{BitVector}Derive the Coverage Universe of one ImpliedVolatility fit, which reads an implied volatility surface beside its returns.
A fit that reads no surface passes iv = nothing, and the method on Nothing derives the Coverage Universe from the returns and pnl alone, as the two-argument form does. So coverage_keyword_reduction makes one call whether the fit reads a surface or not.
The estimator fits on the assets whose returns pass coverage_mask, and whose implied volatilities are finite at every row of the window. An implied volatility series holds NaN where it has no quote, as a return series does. So one rule names an asset that the estimator cannot price and an asset that it cannot estimate, and the reduce-and-expand writes a NaN row and column for both.
The second input narrows the asset universe. It adds no second mask and no second Coverage Universe. The result names no entry of the implied volatility axis, and each consumer of the expanded moment reads a NaN of this method as it reads the NaN of an absent return.
The narrowing is in the reduce-and-expand of the estimator, not in the ReturnsResult that holds iv. A caller of Statistics.cov(ce, X; iv = …) with its own surface passes through no ReturnsResult, and a nested estimator that receives the forwarded iv passes through none either. Both come after this reduction, so both see a block whose implied volatilities are complete.
Mathematical definition
\[\begin{align} \mathcal{C}_{\sigma} &= \mathcal{C} \cap \left\{ i : \sigma_{t,\,i} \in \mathbb{R} \text{ for } t = 1, \ldots, T_{\sigma} \right\}\,. \end{align}\]
Where:
- $\mathcal{C}_{\sigma}$: Coverage Universe of the implied volatility fit.
- $\mathcal{C}$: Coverage Universe of the fit, the assets whose returns are finite, and whose active mask entries are $1$, at every observation of the window.
- $\sigma_{t,\,i}$: Implied volatility of asset $i$ at observation $t$.
- $T_{\sigma}$: Number of rows of the implied volatility surface.
Algorithm
- Orient
Xandivtoobservations × assetswithdims_oriented. - Check that
ivhas as many columns asX. - Flag the assets that
Xand the mask cover withcoverage_flags. - Scan each column of
iv. An asset stays in while its implied volatility is finite at every row. - Hand the flags to
coverage_sentinelwith a refusal that names the implied volatilities.
Arguments
X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.iv: Implied volatility surfaceobservations × assets.pnl: The Asset Panel, its active mask alone,observations × assets, ornothing.coverage_mask_sourcegives it.dims: Dimension along which the computation runs.
Validation
dims in (1, 2).- The asset axis of the oriented
ivmust be the asset axis of the orientedX. - The asset axis of the active mask must be the asset axis of the oriented
X. - At least one asset must be covered.
Returns
cmsk::Option{BitVector}:trueat every covered asset, ornothingwhen every asset is covered.
Related
PortfolioOptimisers.coverage_flags — Function
coverage_flags(X::MatNum, pnl::Nothing; dims::Int = 1) -> BitVector
coverage_flags(X::MatNum, pnl::AssetPanel{<:Any, Nothing, Nothing};
dims::Int = 1) -> BitVector
coverage_flags(X::MatNum, pnl::AssetPanel; dims::Int = 1) -> BitVector
coverage_flags(X::MatNum, amsk::AbstractMatrix{<:Bool}; dims::Int = 1) -> BitVector
coverage_flags(X::MatNum, iv::MatNum,
pnl::Union{Nothing, AssetPanel, AbstractMatrix{<:Bool}};
dims::Int = 1) -> BitVector
coverage_flags(X::MatNum, iv::Nothing,
pnl::Union{Nothing, AssetPanel, AbstractMatrix{<:Bool}};
dims::Int = 1) -> BitVectorFlag each asset that is in the Coverage Universe of one fit, and refuse nothing.
coverage_mask owns the rule, and it calls this function before coverage_sentinel. This function returns the flags of every asset, all false when no asset is covered. So a caller that needs to know whether a window covers an asset, and must not raise, reads the same rule. coverage_keyword_flags is that caller.
Algorithm
- Orient
X, andivwhen it is given, toobservations × assetswithdims_oriented. - Start with one
trueflag for each asset, and set it tofalseat the first row where the return of the asset is not finite. - Do the same for the active mask, where the call gives one, at the first row where the mask is
false. - Do the same for
iv, where the call gives one, at the first row where the implied volatility is not finite. - Return the flags.
Arguments
X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.pnl: OptionalAssetPanel. The Coverage Universe of the fit comes from its active mask. If it isnothing, the rule reads only the finiteness of the returns.amsk: The active mask of the window,observations × assetswhateverdimssays.iv: Implied volatility surfaceobservations × assets, ornothing.dims: Dimension along which the computation runs.
Validation
dims in (1, 2).- The asset axis of the active mask, and of the oriented
iv, must be the asset axis of the orientedX.
Returns
flags::BitVector:trueat every covered asset.
Related
PortfolioOptimisers.coverage_mask_source — Function
coverage_mask_source(pnl::Option{<:AssetPanel}, active_mask::Nothing, dims::Int)
coverage_mask_source(pnl::Option{<:AssetPanel}, active_mask::AbstractMatrix{<:Bool},
dims::Int)Choose the active mask that one fit reads to derive its Coverage Universe.
A caller gives the active mask in one of two forms. An Asset Panel holds it, observations × assets whatever dims says. A matrix call gives it as the active_mask keyword, of the shape of X. Both forms state the same thing, so a plain moment estimator must reach the same Coverage Universe from each. The keyword takes the place of the mask of the panel when the caller gives both, as it does on the route of a mask-aware estimator in coverage_panel_moment, where the keyword comes after the mask of the panel.
Arguments
pnl: OptionalAssetPanel. The Coverage Universe of the fit comes from its active mask. If it isnothing, the rule reads only the finiteness of the returns.active_mask: The active mask ofX, of the shape ofX, ornothing. When it is notnothing, it takes the place of the active mask ofpnl.dims: Dimension along which the computation runs.
Returns
pnl, whenactive_maskisnothing.amsk::AbstractMatrix{<:Bool}:active_maskturned toobservations × assets, otherwise.
Related
PortfolioOptimisers.coverage_sentinel — Function
coverage_sentinel(cmsk::BitVector, msg::AbstractString = "…") -> Option{BitVector}Refuse an empty Coverage Universe, and collapse a complete one onto the nothing sentinel.
Every coverage_mask method ends with this call, so the file holds one copy of the refusal and of the sentinel. The default msg names the returns and the Asset Panel. A method that reads one more input passes its own msg, because this function cannot know why an asset left the Coverage Universe. The implied volatility method of coverage_mask reads a surface too, and a message that names only the returns and the Asset Panel sends its caller to the wrong input.
Algorithm
- Throw an
IsEmptyErrorcarryingmsgwhencmskholds notrue. - Return
nothingwhencmskholds nofalse. - Return
cmskotherwise.
Arguments
cmsk: The raw coverage mask, one entry per asset.msg: The refusal message, which names every input that the caller read to derive the mask. The default names the returns and the Asset Panel.
Validation
- At least one asset must be covered.
Returns
cmsk::Option{BitVector}:cmskitself, ornothingwhen every asset is covered.
Related
PortfolioOptimisers.coverage_reduction — Method
coverage_reduction(X::MatNum, pnl::Option{<:AssetPanel}; dims::Int = 1,
active_mask::Option{<:AbstractMatrix{<:Bool}} = nothing)
-> Tuple{Option{BitVector}, MatNum}Reduce a returns matrix to its Coverage Universe, and return the mask beside the clean block.
The reduction keeps the orientation of the caller. The plain moment verb that receives the block sees a matrix of the same shape as a sample with no Asset Panel, and its own dims still describes it. The nothing sentinel returns X itself, so a complete window allocates nothing.
The active mask of the fit is active_mask when the caller gives one, and the active mask of pnl otherwise. A matrix call of a prior gives its masks as keywords, so this is how such a call reaches the Coverage Universe that the same call with an Asset Panel reaches. Every column of the clean block is active at every row, so the caller does not pass the mask on to the plain estimator.
Algorithm
- Choose the active mask with
coverage_mask_source, and derive the Coverage Universe withcoverage_mask. - Return
(nothing, X)on the sentinel. - Slice the asset axis of
Xotherwise: its columns whendims = 1, and its rows whendims = 2.
Arguments
X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.pnl: OptionalAssetPanel. The Coverage Universe of the fit comes from its active mask. If it isnothing, the rule reads only the finiteness of the returns.dims: Dimension along which the computation runs.active_mask: The active mask ofX, of the shape ofX, ornothing. When it is notnothing, it takes the place of the active mask ofpnl.
Validation
dims in (1, 2).
Returns
(cmsk, Xc)::Tuple{Option{BitVector}, MatNum}: The Coverage Universe, andXreduced to it.
Related
PortfolioOptimisers.coverage_reduction — Method
coverage_reduction(rd::AbstractReturnsResult) -> Tuple{Option{BitVector}, AbstractReturnsResult}
coverage_reduction(X::MatNum, rd::AbstractReturnsResult) -> Tuple{Option{BitVector}, AbstractReturnsResult}
coverage_reduction(cmsk::Nothing,
rd::AbstractReturnsResult) -> Tuple{Nothing, AbstractReturnsResult}
coverage_reduction(cmsk::BitVector,
rd::AbstractReturnsResult) -> Tuple{BitVector, AbstractReturnsResult}Reduce a returns result to the Coverage Universe of its own window.
coverage_reduction(X::MatNum, pnl::Option{<:AssetPanel}) gives a moment estimator a clean block. This method gives a consumer of the whole returns result a clean returns result. port_opt_view slices nx, X, the benchmark, the implied volatility surface and the AssetPanel at the same asset index. The caller is pre-selection. fit_preprocessing over an AbstractAssetSelector reduces the returns result here, so each selector ranks the live assets alone.
The four methods choose each branch by dispatch, not by a condition, as coverage_reduction(opt::AbstractOptimisationEstimator, rd::ReturnsResult) does. The first method passes the returns matrix of the returns result as the first argument of the second. The second method derives the mask. The nothing method is the path where every asset is covered, and it gives back the returns result unchanged. The BitVector method takes the view.
The split between the first two methods is the refusal of the family. A returns result whose X is not an observations × assets matrix has no asset axis to reduce. PredictionReturnsResult holds one portfolio return series, for example. Such a returns result matches no method, and the call throws a MethodError that names the returns result. The refusal comes before the method reads the panel. So the error names the returns matrix of a returns result that has neither the fields nx and X nor the fields nx, X and pnl, not a missing field.
A window with no covered asset throws an IsEmptyError in coverage_mask, which derives the mask, so every caller gets the same refusal.
Algorithm
- Pass
rd.Xas the first argument of the second method, which refuses a returns result with no asset axis. - Derive the Coverage Universe of
Xandrd.pnlwithcoverage_mask, withdims = 1, because a returns result holds its observations along the rows. - Return the mask and the returns result unchanged when the mask is
nothing. - Otherwise return the mask beside a
port_opt_viewof the returns result atfindall(cmsk).
Arguments
rd: The returns result to use.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.cmsk: The Coverage Universe, ornothing.
Validation
- The returns result must hold an
observations × assetsreturns matrix. - At least one asset must be in the Coverage Universe.
Returns
(cmsk, rdc)::Tuple{Option{BitVector}, AbstractReturnsResult}: The Coverage Universe, and the returns result reduced to it.
Related
PortfolioOptimisers.coverage_reduced_pair — Function
coverage_reduced_pair(A::MatNum, B::MatNum, cmsk::Nothing) -> Tuple{MatNum, MatNum}
coverage_reduced_pair(A::MatNum, B::MatNum, cmsk::BitVector) -> Tuple{MatNum, MatNum}Slice two observations × assets matrices onto the same Coverage Universe.
An estimator that reads a second per-asset panel beside its returns, as ImpliedVolatility reads an implied volatility surface, must slice both, so that the two describe the same assets. The nothing sentinel returns both unchanged.
Arguments
A,B: Two matrices, bothobservations × assets.cmsk: The Coverage Universe, ornothing.
Returns
(A, B)::Tuple{MatNum, MatNum}: The two matrices on the Coverage Universe.
Related
PortfolioOptimisers.panel_moment_masks — Function
panel_moment_masks(pnl::Nothing) -> Tuple{Nothing, Nothing}
panel_moment_masks(pnl::AssetPanel) -> TupleRead the two universe masks that a mask-aware moment estimator takes out of an optional Asset Panel.
A mask-aware estimator overrides the panel method of its verb and takes the whole window. So it needs the masks, not the Coverage Universe that coverage_mask derives. No panel and a static panel both carry no mask, so both give two nothing, which select the unmasked path of the estimator.
Arguments
pnl: OptionalAssetPanel. The Coverage Universe of the fit comes from its active mask. If it isnothing, the rule reads only the finiteness of the returns.
Returns
(amsk, emsk)::Tuple: The active mask and the estimation mask, or twonothing.
Related
The expansion
PortfolioOptimisers.coverage_nan_frame — Function
coverage_nan_frame(frame::AbstractArray) -> AbstractArrayFill the NaN frame that a moment expands into.
The caller allocates the frame. A moment of a block allocates it as similar(A, sz), where A is the block that the caller writes into the frame and sz is the size of the frame. coverage_series_frame allocates it with the element type of a variance, because the frame of a variance series takes the element type of a variance, not the element type of the sample.
The frame of a moment takes its element type from the block through similar, not from eltype(A). MatNum admits a JuMP scalar as well as a number, so at inference eltype of a MatNum splits into two branches, and a moment only takes the numeric one. similar keeps the frame in the element type of the block and leaves no branch that no call reaches.
The element type of the frame must hold NaN. An Integer and a Rational have no NaN, and a moment of a Rational sample stays Rational, so the function refuses both before it writes the frame. The function does not widen the type, because a sentinel is not an arithmetic operation, and the data sets the type. A caller with a Rational sample and a gap converts the sample to a floating-point type, or fills the gap so that every asset is in the Coverage Universe.
Algorithm
- Refuse the frame when its element type is an
Integeror aRational. - Fill it with
NaN, and return it.
Arguments
frame: A frame that the caller allocated.
Validation
- When an asset is outside the Coverage Universe, the element type of the moment holds
NaN. AnIntegeror aRationalelement type raises anArgumentError.
Returns
frame::AbstractArray:frame, filled withNaN.
Related
PortfolioOptimisers.coverage_pair_index — Function
coverage_pair_index(cmsk::BitVector) -> Vector{Int}Map the columns of a reduced co-moment tensor onto the columns of the full-universe one.
A coskewness tensor is assets × assets² and a cokurtosis matrix is assets² × assets². Both index an asset pair (a, b) at (a - 1) * N + b, because the estimators build both from kron(o, Y) ⊙ kron(Y, o), whose column (a - 1) * N + b is the elementwise product of column b and column a. The reduced tensor uses the same rule over the reduced width, with a outermost, so the returned index is in the order of the reduced columns.
Mathematical definition
\[\begin{align} \mathcal{P} &= \left( (a - 1) N + b \right)_{a \in \mathcal{C},\, b \in \mathcal{C}}\,. \end{align}\]
Where:
- $\mathcal{P}$: Pair index, a sequence of $\lvert \mathcal{C} \rvert^{2}$ full-universe columns, ordered with $a$ outermost and each of $a$ and $b$ ascending.
- $\mathcal{C}$: Coverage Universe of the fit, the assets whose returns are finite, and whose active mask entries are $1$, at every observation of the window.
- $N$: Number of assets.
Algorithm
- Take the positions of the covered assets.
- Return
(a - 1) * N + bover every ordered pair of them, withaoutermost.
Arguments
cmsk: The Coverage Universe.
Returns
idx::Vector{Int}: The full-universe pair columns of the reduced ones, in the reduced order.
Related
PortfolioOptimisers.expand_columns — Function
expand_columns(A::MatNum, cmsk::Nothing) -> A
expand_columns(A::MatNum, cmsk::BitVector) -> MatNumWrite a block whose columns are the asset axis back into a NaN frame of the full width.
The expansion of a block has two basic functions, this one and expand_rows. The moment verbs call them through expand_moment, and a prior that carries a block of its own calls them directly. A per-asset vector is a method of expand_moment. A reconstructed returns matrix is observations × assets and a Cholesky factor is factors × assets, so both expand along their columns.
Arguments
A: The block that the reduced fit produced,anything × assets.cmsk: The Coverage Universe, ornothing.
Returns
- The block on the full asset universe, carrying
NaNoutside the Coverage Universe.
Related
PortfolioOptimisers.reduce_columns — Function
reduce_columns(A::MatNum, cmsk::Nothing) -> A
reduce_columns(A::MatNum, cmsk::BitVector) -> MatNumTake the columns of an observations × assets block that a mask keeps.
This is the inverse of expand_columns, and the file keeps the two side by side so that they stay consistent. coverage_reduction takes the same slice for a mask that it derives itself. This function is for a caller that already holds a mask. An AugmentedBlackLittermanPrior reduces its returns by the Investable Mask of its asset prior. That mask can differ from the mask of the returns alone, because a column can be quoted over the whole window and still have no estimate.
The result is a copy, not a view. The block goes on to a regression, and the compiler cannot infer through a SubArray of an array whose element type is not concrete.
nothing is the path where every asset is kept, and it returns the block unchanged.
Arguments
A: The block to reduce,observations × assets.cmsk: The mask to keep, ornothing.
Returns
- The block over the kept columns.
Related
PortfolioOptimisers.expand_rows — Function
expand_rows(A::MatNum, cmsk::Nothing) -> A
expand_rows(A::MatNum, cmsk::BitVector) -> MatNumWrite a block whose rows are the asset axis back into a NaN frame of the full width.
This is expand_columns along the other axis. A loadings matrix is assets × factors, so it expands along its rows.
Arguments
A: The block that the reduced fit produced,assets × anything.cmsk: The Coverage Universe, ornothing.
Returns
- The block on the full asset universe, carrying
NaNoutside the Coverage Universe.
Related
PortfolioOptimisers.expand_moment — Function
expand_moment(m, cmsk::Nothing) -> m
expand_moment(m, cmsk::Nothing, dims::Int) -> m
expand_moment(m, cmsk::Nothing, ::Val{:kt}) -> m
expand_moment(m::Nothing, cmsk::BitVector) -> nothing
expand_moment(sigma::MatNum, cmsk::BitVector) -> MatNum
expand_moment(v::VecNum, cmsk::BitVector, dims::Int = 1) -> VecNum
expand_moment(mu::MatNum, cmsk::BitVector, dims::Int) -> MatNum
expand_moment(skV::Tuple{MatNum, MatNum}, cmsk::BitVector) -> Tuple{MatNum, MatNum}
expand_moment(kt::MatNum, cmsk::BitVector, ::Val{:kt}) -> MatNumWrite a moment estimated on the Coverage Universe back into a NaN frame of the full width.
A Prior Result is on the full asset universe, and an asset that the prior cannot estimate carries NaN. So the prior expands every block of a reduced fit before it stores the block. coverage_reduction is the first half of the reduce-and-expand, and this function is the second half. The nothing sentinel returns the argument unchanged, and a moment that is nothing stays nothing.
The four shapes need four methods, and their types do not tell them apart. A per-asset vector, such as a mean, an intercept or the variances of an idiosyncratic covariance, is the fifth shape, and its type tells it apart. The idiosyncratic covariance of a regression is a vector or an assets × assets matrix, and each takes its own method. A covariance matrix and a cokurtosis matrix are both square, and a mean and a coskewness tensor are both rectangular. The number of arguments and the Val marker tell them apart.
Mathematical definition
For $i, a, b, c, d \in \mathcal{C}$,
\[\begin{align} \tilde{m}_{i} &= m_{k(i)}\,, \\ \tilde{\mathbf{B}}_{i,\,j} &= \mathbf{B}_{k(i),\,k(j)}\,, \\ \tilde{\mathbf{S}}_{i,\,(a - 1) N + b} &= \mathbf{S}_{k(i),\,(k(a) - 1) n + k(b)}\,, \\ \tilde{\mathbf{K}}_{(a - 1) N + b,\,(c - 1) N + d} &= \mathbf{K}_{(k(a) - 1) n + k(b),\,(k(c) - 1) n + k(d)}\,, \end{align}\]
and every other entry of the four frames is NaN.
Where:
- $\mathcal{C}$: Coverage Universe of the fit, the assets whose returns are finite, and whose active mask entries are $1$, at every observation of the window.
- $n = \lvert \mathcal{C} \rvert$: Number of assets in the Coverage Universe.
- $k(i)$: Position of asset $i$ in $\mathcal{C}$ in ascending order, so that $k(i) \in \{1, \ldots, n\}$.
- $\boldsymbol{m}$, $\tilde{\boldsymbol{m}}$: A marginal, of $n$ entries on the Coverage Universe and of $N$ entries on the full universe.
- $\mathbf{B}$, $\tilde{\mathbf{B}}$: A covariance-like matrix, $n \times n$ and $N \times N$.
- $\mathbf{S}$, $\tilde{\mathbf{S}}$: A coskewness tensor, $n \times n^{2}$ and $N \times N^{2}$.
- $\mathbf{K}$, $\tilde{\mathbf{K}}$: A cokurtosis matrix, $n^{2} \times n^{2}$ and $N^{2} \times N^{2}$.
- $N$: Number of assets.
Algorithm
Dispatch selects one step of this list for each call.
- Return the moment unchanged when the mask is
nothing, at every arity, and returnnothingfor a moment that isnothing. - Write a covariance-like matrix,
assets × assets, intoframe, aNaNframe ofN × N, at(cmsk, cmsk). - Write a marginal into
framealong its asset axis. A matrix marginal is1 × assetsunderdims = 1andassets × 1underdims = 2, and write a vector into aNaNframe ofNentries atcmsk, with or withoutdims. - Write the coskewness tensor of a pair into
frame, aNaNframe ofN × N², at the rowscmskand the columns ofcoverage_pair_index. Expand the negative spectral skewness matrix of the pair as in step 2. - Write a cokurtosis matrix into
frame, aNaNframe ofN² × N², at the pair index on both axes.
Arguments
m,sigma,v,mu,skV,kt: The moment that the reduced fit produced.cmsk: The Coverage Universe, ornothing.dims: The dimension that the marginal was computed along. A vector reads no dimension.::Val{:kt}: The marker that names a cokurtosis matrix, which a covariance matrix cannot be told from by type.
Validation
- When an asset is outside the Coverage Universe, the element type of the moment holds
NaN. AnIntegeror aRationalelement type raises anArgumentError.
Returns
- The moment on the full asset universe, carrying
NaNoutside the Coverage Universe.
Related
PortfolioOptimisers.expand_count — Function
expand_count(x::Nothing, cmsk::BitVector) -> nothing
expand_count(x::VecNum, cmsk::BitVector) -> VecNumWrite a per-asset count of a loadings block, edof or ediv, back onto the full asset universe.
A count differs from a moment at an absent asset. No observation of an asset outside the Coverage Universe entered the fit, so its count is zero. Zero is a true count, not a gap. Every number type has a zero, so an integer count expands in its own type, and a NaN frame needs a type that holds NaN.
Arguments
x: The count on the Coverage Universe, ornothing.cmsk: The Coverage Universe.
Returns
x::Option{<:VecNum}: The count on the full asset universe, zero outside the Coverage Universe, ornothing.
Related
PortfolioOptimisers.expand_regression — Function
expand_regression(re, cmsk::Nothing) -> re
expand_regression(re::Regression, cmsk::BitVector) -> RegressionWrite a regression result fitted on the Coverage Universe back onto the full asset universe.
A Prior Result has no mask field, so every block that it carries is on the full asset universe, and its regression result is one of those blocks. The loadings, the intercepts, the idiosyncratic covariance and the counts edof and ediv of that covariance are all per asset, so each expands along its asset axis. An asset outside the Coverage Universe reads zero in each count, through expand_count. The row key idx and ts names observations, not assets, so it passes through unchanged.
The method reads L with getfield, as port_opt_view does. The property rule swap(L, M) makes re.L return re.M when L is nothing. A read through the property writes a copy of M into the L of the expanded result, and the result no longer shows that L is unset.
Arguments
re: The regression result the reduced fit produced.cmsk: The Coverage Universe, ornothing.
Returns
re::Regression: The regression result on the full asset universe.
Related
The check for missing returns
An ordinary estimator reads a cell that the active mask turns off as a missing return. Its matrix method refuses such a sample, and its incremental fit writes NaN in that cell.
PortfolioOptimisers.assert_finite_sample — Function
assert_finite_sample(X::ArrNum, name::AbstractString = "X") -> nothingRefuse a non-finite sample in a plain moment estimator, and name the two ways out.
A plain moment estimator has no correct value for an asset whose window has a gap. So it refuses the whole sample, and does not return a number that looks like an estimate. The message names the two paths that a caller has. The caller can fit through a prior, which reduces the sample to the Coverage Universe and expands the result. Or the caller can use a mask-aware estimator, which takes the whole window and writes its own frame.
This is assert_all_finite with a message for this refusal. The message of assert_all_finite names the count and the position of the non-finite entries alone. It tells a caller what is wrong, and not what to do.
Arguments
X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.name: The name of the sample in the message.
Validation
- Every entry of
Xmust be finite.
Returns
nothing.
Related
assert_finite_sample(X::ArrNum, active_mask::Nothing) -> nothing
assert_finite_sample(X::ArrNum, active_mask::AbstractArray{<:Bool}) -> nothingRefuse a gapped sample in a plain moment estimator that takes the active mask as a keyword.
A cell is a gap when its return is non-finite or when the active mask is false there. That is the rule of the Coverage Universe, which coverage_mask applies to each cell. So a plain estimator reads an inactive cell as a non-finite return, and refuses it with the same error. The Asset Panel method of the verb reduces the same sample to its Coverage Universe and expands, and the fold writes NaN in each gap with inactive_as_nonfinite. So the three forms of one plain estimator agree on every cell.
Arguments
X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.active_mask: The active mask ofX, of the shape ofX, ornothing.
Validation
- Every entry of
Xmust be finite. active_maskmust have the size ofX, and betrueat every entry.
Returns
nothing.
Related
PortfolioOptimisers.inactive_as_nonfinite — Function
inactive_as_nonfinite(x::ArrNum, active_mask::Nothing) -> ArrNum
inactive_as_nonfinite(x::ArrNum, active_mask::AbstractArray{<:Bool}) -> ArrNumWrite NaN in each cell of a sample that the active mask turns off, for the fold of a plain moment estimator.
A plain fold has one observation count for every asset, so it cannot skip one cell of an observation. It reads a non-finite return as it reads any return, and the NaN stays in the running moments of that asset from then on. The asset then carries NaN in the answer, as the Asset Panel method gives for an asset that leaves the Coverage Universe. An inactive cell is a gap by the same rule, so this method writes NaN there before the fold reads it. A plain fold then answers each asset as the Asset Panel method answers the same rows.
Arguments
x: One observation, or a block of observations.active_mask: The active mask ofx, of the shape ofx, ornothing.
Validation
active_maskmust have the size ofx.- The element type of
xmust holdNaN. AnIntegeror aRationalraises anArgumentError.
Returns
xitself whenactive_maskisnothing, and otherwise a copy ofxthat carriesNaNat each inactive cell. The copy takes the element type ofx.
Related
The panel method of variance_series
PortfolioOptimisers.variance_series — Method
variance_series(
ce::AbstractCovarianceEstimator,
X::AbstractMatrix{<:Union{Number, AbstractJuMPScalar}},
pnl::Union{Nothing, AssetPanel};
dims,
kwargs...
) -> Matrix
Fit a point-in-time variance series on the Coverage Universe of each expanding window, and write it into a NaN frame of the full width.
variance_series refits its estimator once per observation, so each window meets the refusal of assert_finite_sample on its own. Each window is reduced to its own Coverage Universe, not to the Coverage Universe of the whole sample. Every window starts at the first row, so an asset that lists after the first row is outside the Coverage Universe of every window, and its column is NaN throughout.
A mask-aware estimator overrides this method and takes the whole window, as it overrides Statistics.cov(ce::AbstractCovarianceEstimator, X::MatNum, pnl::Option{<:AssetPanel}; dims::Int = 1, kwargs...). A Covariance or a SimpleVariance with a CoveragePolicy takes the available-case method of variance_series, which follows this method in the source. Either of them with no policy takes this method. The series refits the estimator at each observation and writes the frame one row at a time, so it does not go through coverage_panel_moment, which writes the frame once at the end.
A PortfolioOptimisersCovariance overrides this method, so that its inner estimator chooses the route.
Algorithm
- Orient
Xtoobservations × assetswithdims_oriented. - Allocate
val, theNaNframe, and readamsk, the active mask, withcoverage_series_frame. - For each observation
t, takeXt, rows1totofX. - Set
cmskto the assets whose returns are finite at every row ofXtand, whenamskis notnothing, whose entries ofamskaretrueat every one of those rows. - Skip row
twhencmskholds no asset, so the whole row keeps itsNaN. - Otherwise fit the variance of the estimator on the columns
cmskofXt, and write it into the entriescmskof rowtofval. The fit reads the observation weights of the estimator at rows1tot, cut withobs_weights_view. - Return
val, transposed whendims == 2.
Mathematical definition
\[\begin{align} v_{t,\,i} &= \begin{cases} \left[ \hat{\boldsymbol{\sigma}}^{2}\left( \mathbf{X}_{1:t,\,\mathcal{C}_{t}} \right) \right]_{i} & i \in \mathcal{C}_{t} \\ \mathrm{NaN} & i \notin \mathcal{C}_{t} \end{cases}\,. \end{align}\]
Where:
- $v_{t,\,i}$: Entry of the series for asset $i$ at observation $t$.
- $\mathcal{C}_{t}$: Coverage Universe of the window of rows $1$ to $t$, the assets whose returns are finite, and whose active mask entries are $1$, at each of those rows.
- $\mathbf{X}_{1:t,\,\mathcal{C}_{t}}$: Rows $1$ to $t$ of the returns matrix, over the columns of $\mathcal{C}_{t}$.
- $\hat{\boldsymbol{\sigma}}^{2}(\cdot)$: Variance vector that the estimator fits on a block, indexed by asset.
Arguments
ce: Covariance estimator.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.pnl: OptionalAssetPanel. The Coverage Universe of the fit comes from its active mask. If it isnothing, the rule reads only the finiteness of the returns.dims: Dimension along which the computation runs.kwargs...: Additional keyword arguments passed to the estimator.
Validation
dims in (1, 2).- When an asset is outside the Coverage Universe, the element type of the moment holds
NaN. AnIntegeror aRationalelement type raises anArgumentError. The series writes its frame before its first row, so it needs this type for every sample, with or without a gap. - The active mask of
pnlmust have the size of the orientedX.
Returns
val::Matrix{<:Number}: Variance series on the full asset universe, shaped as(T, N)ifdims == 1or(N, T)ifdims == 2, carryingNaNoutside each window's Coverage Universe.
Related
PortfolioOptimisers.coverage_variance_series — Function
coverage_variance_series(
ce::AbstractCovarianceEstimator,
X::AbstractMatrix{<:Union{Number, AbstractJuMPScalar}},
pnl::Union{Nothing, AssetPanel};
dims,
kwargs...
) -> Any
Run the reduce-and-expand variance series over an Asset Panel, the body of the root method of variance_series(ce::AbstractCovarianceEstimator, X::MatNum, pnl::Option{<:AssetPanel}; dims::Int = 1, kwargs...).
The root method states the rule and the # Algorithm. The body is a function of its own, because a PortfolioOptimisersCovariance over a plain leaf estimator, by reduces_each_window, takes this series too. Its own panel method of variance_series overrides the root, so it calls this function and does not refit through panel_series_row, which costs more for each row and gives the same values.
Arguments
ce: Covariance estimator.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.pnl: OptionalAssetPanel. The Coverage Universe of the fit comes from its active mask. If it isnothing, the rule reads only the finiteness of the returns.dims: Dimension along which the computation runs.kwargs...: Additional keyword arguments passed to the estimator.
Returns
val::Matrix{<:Number}: The variance series, as the root method returns it.
Related
PortfolioOptimisers.covers_an_asset — Function
covers_an_asset(est::AbstractCovarianceEstimator, X::MatNum, pnl::Option{<:AssetPanel};
dims::Int = 1, active_mask::Option{<:AbstractMatrix{<:Bool}} = nothing,
kwargs...) -> Bool
covers_an_asset(est::Union{WindowedVariance, WindowedCovariance}, X::MatNum,
pnl::Option{<:AssetPanel}; dims::Int = 1, iv::Option{<:MatNum} = nothing,
kwargs...) -> Bool
covers_an_asset(est::PortfolioOptimisersCovariance, X::MatNum, pnl::Option{<:AssetPanel};
kwargs...) -> BoolAnswer whether the panel method of Statistics.var of est can fit the window X, where a plain estimator refuses a window that covers no asset.
The method of panel_series_row for a wrapper reads the answer, so a variance series writes a NaN row for such a window and does not raise. The answer follows the panel method of var, so it is the rule of coverage_mask on the window that the innermost plain estimator fits:
- A mask-aware estimator, by
reads_active_mask, reads the window itself, so the answer istrue. - A plain estimator reads the window as
coverage_keyword_reductiondoes. The answer istruewhencoverage_keyword_flagsflags an asset. - A
WindowedVarianceor aWindowedCovariancecutsX,pnl,ivand the masks to its window, as its panel method ofvardoes, and asks its inner estimator. - A
PortfolioOptimisersCovariancegives the window to its inner estimator without change, so it asks it.
Arguments
est: The variance estimator.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.pnl: OptionalAssetPanel. The Coverage Universe of the fit comes from its active mask. If it isnothing, the rule reads only the finiteness of the returns.dims: Dimension along which the computation runs.active_mask: The active mask ofX, of the shape ofX, ornothing. When it is notnothing, it takes the place of the active mask ofpnl.iv: Implied volatility surface of the window, ornothing.kwargs...: The other keywords of the fit.
Returns
flag::Bool:truewhen the fit has an asset to fit.
Related
The available-case fit
An estimator with a CoveragePolicy fits each asset, or each pair of assets, on the rows where its returns are finite and active. Its panel method therefore passes the whole window and the active mask to the estimator, and does not reduce the window to the coverage universe.
PortfolioOptimisers.coverage_panel_moment — Function
coverage_panel_moment(f, est, cvg, X, pnl, expand; dims::Int = 1, kwargs...)Choose the Coverage Universe route or the available-case route for the Asset Panel method of a moment verb.
An estimator that carries a CoveragePolicy is a mask-aware estimator, in the sense of the root method of cov in this file. It takes the whole window and reads pnl.amsk itself. So the panel method must give it the mask, and must not reduce the window to its Coverage Universe. The caller passes the cvg field as the third argument, so dispatch chooses the route. The Nothing method is the reduce-and-expand of the root methods.
expand is the framing that the verb needs, because the four moment shapes frame differently. A covariance frames as a matrix, a marginal along its asset axis alone, and a co-moment at the pair index. The verb passes the framing, so the file holds one copy of this routing for every verb.
Arguments
f: The plain, panel-free method of the verb.est: The estimator.cvg: The policy the estimator carries, which selects the route.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.pnl: OptionalAssetPanel. The Coverage Universe of the fit comes from its active mask. If it isnothing, the rule reads only the finiteness of the returns.expand: The framing, called asexpand(val, cmsk)on the Coverage Universe route and unused on the other.dims: Dimension along which the computation runs.kwargs...: Additional keyword arguments passed to the estimator.
Returns
val: The moment on the full asset universe.
Related
PortfolioOptimisers.coverage_panel_moment — Method
coverage_panel_moment(
f,
est,
::Nothing,
X::AbstractMatrix{<:Union{Number, AbstractJuMPScalar}},
pnl::Union{Nothing, AssetPanel},
expand;
dims,
active_mask,
kwargs...
) -> Any
Nothing method of coverage_panel_moment, the Coverage Universe route. It reduces the window with coverage_reduction, fits the plain estimator on the clean block, and frames the result with expand. The reduction reads the active_mask keyword in the place of the active mask of pnl when the caller gives one, and the clean block is active at every row, so the keyword stops here.
Related
PortfolioOptimisers.coverage_panel_moment — Method
coverage_panel_moment(
f,
est,
::CoveragePolicy,
X::AbstractMatrix{<:Union{Number, AbstractJuMPScalar}},
pnl::Union{Nothing, AssetPanel},
;
dims,
kwargs...
) -> Any
CoveragePolicy method of coverage_panel_moment, the available-case route. It gives the estimator the whole window and the active mask of the panel, turned to the orientation of X, and the estimator writes its own frame.
Related
PortfolioOptimisers.coverage_series_frame — Function
coverage_series_frame(X::MatNum, pnl::Option{<:AssetPanel}) -> TupleAllocate the NaN frame of a point-in-time variance series, and read the active mask that the series pairs with X row by row.
Row t of the series is a fit on rows 1 to t of X and of the active mask, so the series pairs the two by position, and the mask must have the size of X. coverage_mask pairs no rows, and it lets the two observation axes differ.
A variance divides, so the frame takes the element type of X. A Float32 sample gives a Float32 frame. A Rational sample keeps its type, which has no NaN, so coverage_nan_frame refuses it.
Algorithm
- Read
amsk, the active mask ofpnl, withpanel_moment_masks. - Check that
amskhas the size ofXwhen the panel carries one. - Allocate an array of the size of
Xwith the element typeeltype(X), and give it tocoverage_nan_frame, which refuses a type with noNaNand returnsval, the array filled withNaN.
Arguments
X: Returns matrix, already oriented toobservations × assets.pnl: OptionalAssetPanel. The Coverage Universe of the fit comes from its active mask. If it isnothing, the rule reads only the finiteness of the returns.
Validation
- The active mask of
pnlmust have the size ofX. - When an asset is outside the Coverage Universe, the element type of the moment holds
NaN. AnIntegeror aRationalelement type raises anArgumentError.
Returns
(val, amsk)::Tuple: TheNaNframe of the series, and the active mask ornothing.
Related
PortfolioOptimisers.variance_series — Method
variance_series(
ce::Union{Covariance{<:Any, <:Any, <:Any, <:Any, <:CoveragePolicy}, SimpleVariance{<:Any, <:Any, <:Any, <:CoveragePolicy}},
X::AbstractMatrix{<:Union{Number, AbstractJuMPScalar}},
pnl::Union{Nothing, AssetPanel};
dims,
kwargs...
) -> Any
Asset Panel method of variance_series for a Covariance or a SimpleVariance with a CoveragePolicy, the available-case route.
It fits each window over the whole universe, and does not reduce it to its own Coverage Universe. An asset that lists after the first row therefore carries a number once the estimator admits it, where the Coverage Universe route of variance_series(ce::AbstractCovarianceEstimator, X::MatNum, pnl::Option{<:AssetPanel}; dims::Int = 1, kwargs...) leaves its column at NaN. The type of the cvg field selects the route, so no branch reads the value of the policy.
Algorithm
- Orient
Xtoobservations × assetswithdims_oriented. - Allocate
val, theNaNframe, and readamsk, the active mask, withcoverage_series_frame. The active mask of an Asset Panel isobservations × assetsalready. - For each observation
t, fit the variance of the estimator on rows1totofX, with rows1totofamskas its active mask, and write it into rowtofval. The fit reads the observation weights of the estimator at the same rows, cut withobs_weights_view. - Return
val, transposed whendims == 2.
Arguments
ce: Covariance estimator.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.pnl: OptionalAssetPanel. The Coverage Universe of the fit comes from its active mask. If it isnothing, the rule reads only the finiteness of the returns.dims: Dimension along which the computation runs.kwargs...: Additional keyword arguments passed to the estimator.
Validation
dims in (1, 2).- The active mask of
pnlmust have the size of the orientedX.
Returns
val::Matrix{<:Number}: Variance series on the full asset universe.
Related
PortfolioOptimisers.coverage_floor — Function
coverage_floor(cvg::Nothing)
coverage_floor(cvg::CoveragePolicy)
coverage_floor(me::AbstractExpectedReturnsEstimator)
coverage_floor(ce::StatsBase.CovarianceEstimator)
coverage_floor(me::SimpleExpectedReturns)
coverage_floor(ve::SimpleVariance)
coverage_floor(ce::Covariance)
coverage_floor(ce::PortfolioOptimisersCovariance)
coverage_floor(a::Nothing, b::Nothing)
coverage_floor(a::Nothing, b::Real)
coverage_floor(a::Real, b::Nothing)
coverage_floor(a::Real, b::Real)Read the coverage floor at which an estimator admits an asset, and take the tighter floor of two arms.
A CoveragePolicy is a field of one estimator, so a fit has no single floor until a caller reads the floor of each arm. The one-argument methods do that read. An estimator that carries a policy returns its own cvg, a wrapper forwards to the estimator it holds, and every other estimator returns nothing. nothing means that the arm states no floor, not a floor of zero. The two-argument methods take the maximum of two arms, because an asset must pass both arms. The Investable Mask needs mu and the diagonal of sigma finite, and coverage_admission reads the same per-asset count for both. So a floor on either arm bounds every investable column, whatever the other arm does.
resolve_fill_limit reads the floor, and turns it into the share of a scenario_fill that fills without a warning. An estimator with no method of its own returns nothing through the method of its root type. So a policy inside a wrapper that this function does not forward through gives a warning for every fill, not for none, which is the conservative result.
The two-argument methods choose by dispatch, not by a branch, so a call site that holds two Option values finds a method for each member of its union split.
Arguments
cvg: ACoveragePolicyornothing.me,ve,ce: The moment estimator whose floor the method reads.a,b: The floors of two arms, each aRealornothing.
Returns
floor::Option{<:Real}: The coverage floor, ornothingwhen none is stated.
Related
coverage_floor(pe::EmpiricalPrior) -> Any
EmpiricalPrior method of coverage_floor, the binding floor of its two arms.
pe.me and pe.ce can each carry a CoveragePolicy. The Investable Mask is the conjunction of the two admissions, and both admissions read the same count of observations per asset. So each floor bounds every investable column, and the binding floor is the maximum of the floors that the arms state. A configuration where only one arm states a floor needs no rule of its own.
Arguments
pe: The empirical prior estimator.
Returns
floor::Option{<:Real}: The binding coverage floor, ornothingwhen neither arm states one.
Related
PortfolioOptimisers.coverage_keyword_reduction — Function
coverage_keyword_reduction(est, X::MatNum, pnl::Option{<:AssetPanel}, dims::Int,
active_mask::Option{<:AbstractMatrix{<:Bool}}; kwargs...)Reduce X and its per-asset keywords to the Coverage Universe of one fit.
The reduce-and-expand root of a moment verb forwards its keywords to the estimator. A prior gives every moment the implied volatility surface iv and the premium ivpa as keywords, and both carry one entry for each asset. The root reduces X, so it must reduce them too, or a wrapped ImpliedVolatility reads N columns of iv against fewer columns of X.
A gap in iv puts an asset outside the Coverage Universe only of a fit that reads the surface. So the function narrows the universe by iv when reads_implied_volatility answers true for est, through the surface method of coverage_mask.
Algorithm
- Choose the active mask with
coverage_mask_source. - Derive the Coverage Universe from
Xand the mask, and also fromivwhenestreads it and the keywords carry it. - Slice
X,ivandivpaonto the Coverage Universe withcoverage_keyword_columns.
Arguments
est: The moment estimator.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.pnl: OptionalAssetPanel. The Coverage Universe of the fit comes from its active mask. If it isnothing, the rule reads only the finiteness of the returns.dims: Dimension along which the computation runs.active_mask: The active mask ofX, of the shape ofX, ornothing. When it is notnothing, it takes the place of the active mask ofpnl.kwargs...: The keywords of the call, which can carryivandivpa.
Validation
- The rules of
coverage_mask.
Returns
(cmsk, Xc, kw): The Coverage Universe ornothing, the reducedX, and the keywords withivandivpareduced.
Related
PortfolioOptimisers.coverage_keyword_columns — Function
coverage_keyword_columns(
cmsk::Union{Nothing, BitVector},
X::AbstractMatrix{<:Union{Number, AbstractJuMPScalar}},
dims::Int64;
kwargs...
) -> Union{Tuple{BitVector, Any, @NamedTuple{}}, Tuple{Nothing, AbstractMatrix{<:Union{Number, AbstractJuMPScalar}}, @NamedTuple{}}}
Slice X and its per-asset keywords iv and ivpa onto a Coverage Universe that the caller derived.
coverage_keyword_reduction derives the Coverage Universe and refuses one with no asset, then calls this function. A caller that derived the flags itself with coverage_keyword_flags, and handled a window with no asset, calls it directly, so the window is scanned one time. panel_series_row is that caller.
Algorithm
- Slice
ivalong the asset axis ofdimswithcoverage_columns, andivpawithcoverage_reduced_ivpa. A keyword that the caller did not give stays absent. - Slice
Xthe same way.
Arguments
cmsk: The Coverage Universe, ornothingwhen every asset is in it.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.dims: Dimension along which the computation runs.kwargs...: The keywords of the call, which can carryivandivpa.
Returns
(cmsk, Xc, kw): The Coverage Universe, the reducedX, and the keywords withivandivpareduced.
Related
PortfolioOptimisers.coverage_keyword_flags — Function
coverage_keyword_flags(
est,
X::AbstractMatrix{<:Union{Number, AbstractJuMPScalar}},
pnl::Union{Nothing, AssetPanel},
dims::Int64,
active_mask::Union{Nothing, AbstractMatrix{<:Bool}};
kwargs...
) -> BitVector
Flag each asset in the Coverage Universe of one fit of a plain estimator, by the rule of coverage_keyword_reduction, and refuse nothing.
The flags read the active mask of active_mask or of pnl, as coverage_mask_source chooses, and the implied volatility iv when reads_implied_volatility answers true for est. All flags are false for a window that covers no asset. panel_series_row and covers_an_asset read them.
Arguments
est: The moment estimator.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.pnl: OptionalAssetPanel. The Coverage Universe of the fit comes from its active mask. If it isnothing, the rule reads only the finiteness of the returns.dims: Dimension along which the computation runs.active_mask: The active mask ofX, of the shape ofX, ornothing. When it is notnothing, it takes the place of the active mask ofpnl.kwargs...: The keywords of the call, which can carryiv.
Returns
flags::BitVector:trueat every covered asset, fromcoverage_flags.
Related
PortfolioOptimisers.coverage_columns — Function
coverage_columns(A::Nothing, cmsk, dims::Int) -> Nothing
coverage_columns(A::MatNum, cmsk::Nothing, dims::Int) -> MatNum
coverage_columns(A::MatNum, cmsk::BitVector, dims::Int) -> MatNumSlice a matrix onto the Coverage Universe along the asset axis that dims names. The asset axis is the second axis when dims is 1, and the first axis otherwise.
Related