Panel collapse: private API
PortfolioOptimisers.extract_fees — Function
extract_fees(res::OptimisationResult) -> Any
extract_fees(
res::OptimisationResult,
fees::Union{Nothing, Fees}
) -> Any
Returns the fees for a net return calculation from an optimisation result.
A fees that the caller gives has priority. Else the function reads the property fees of res, and a result with no such property gives nothing.
The fee of a result is on the universe that the fit solved, and res.w is on the universe of the caller. So a consumer that reads the fee with res.w reads both through result_investable_view, and not through this function alone.
Arguments
res: The optimisation result, which can have a propertyfees.fees: Fees that the caller gives, ornothing.
Returns
fees::Option{<:Fees}: The fees for the net return calculation, ornothingwhen none is found.
Related
PortfolioOptimisers.synthetic_asset_weights — Function
synthetic_asset_weights(
w::AbstractVector{<:Union{Number, AbstractJuMPScalar}}
) -> Any
Normalises inner weights into the convex weights that collapse the real assets onto the sub-portfolios of a meta-optimiser.
A quantity beside the returns is extensive or intensive. An extensive quantity, such as a return or a benchmark return, collapses as the weighted sum $\mathbf{W}^\intercal \boldsymbol{x}$. An intensive quantity, such as the rates rd.iv and rd.ivpa, collapses as a weighted mean. A weighted sum multiplies an intensive quantity by the gross exposure $s_k$, so a portfolio with short positions or leverage makes the rate larger. These weights make each product a convex combination, so a caller that collapses an intensive quantity passes its weights through this function.
Mathematical definition
\[\begin{align} s_{k} &= \sum_{i=1}^{N} \lvert W_{ik} \rvert\,,\\ \tilde{W}_{ik} &= \begin{cases} \lvert W_{ik} \rvert / s_{k} & s_{k} > 0\,,\\ 0 & s_{k} = 0\,. \end{cases} \end{align}\]
Where:
- $\mathbf{W}$: Inner weight matrix
assets × sub-portfolios. Column $k$ holds the weights of sub-portfolio $k$ over all $N$ assets, and zero at an asset outside the sub-portfolio. - $\tilde{\mathbf{W}}$: Normalised inner weight matrix, $\tilde{W}_{ik} = |W_{ik}| / \sum_{j=1}^{N} |W_{jk}|$. A column of zeros stays a column of zeros.
- $s_{k}$: Gross exposure of sub-portfolio $k$.
- $N$: Number of assets.
Each column of $\tilde{\mathbf{W}}$ sums to one, except a column of zeros, which stays a column of zeros.
Arguments
w: The inner weights. A vector collapses onto one sub-portfolio. A matrix,assets × sub-portfolios, collapses each column separately.
Returns
w: The normalised weights, with the shape of the input.
Related
PortfolioOptimisers.active_divide! — Function
active_divide!(C::AbstractArray, d::Nothing, sq::Bool = false) -> C
active_divide!(C::AbstractMatrix, d::MatNum, sq::Bool = false) -> C
active_divide!(C::AbstractArray{<:Any, 3}, d::MatNum, sq::Bool = false) -> CDivides the collapsed values of a time-varying Panel Field by the divisors of active_weight_divisor, in place.
The divisor of row t and sub-portfolio k divides every label of that cell. When sq is true, the label axis is the sub-portfolio axis too, so the cell of labels k and l also divides by the divisor of l.
Algorithm
The method that Julia selects is the algorithm.
nothingleavesCas it is.- A matrix,
observations × sub-portfolios, divides bydcell by cell. - An array of rank three,
sub-portfolios × labels × observations, divides each cell(k, l, t)byd[t, k], and, whensqistrue, byd[t, l]too.
Arguments
C: The collapsed values,observations × sub-portfolios, orsub-portfolios × labels × observations.d: The divisors,observations × sub-portfolios, ornothing.sq:truewhen the label axis is the sub-portfolio axis.
Returns
C, divided.
Related
PortfolioOptimisers.collapse_asset_panel — Function
collapse_asset_panel(pnl::Nothing, wi::MatNum, nx, alg) -> nothing
collapse_asset_panel(pnl::AssetPanel, wi::MatNum, nx::Option{<:VecStr},
alg::AbstractPanelCollapseAlgorithm) -> AssetPanelCollapses an AssetPanel onto the sub-portfolios that a meta-optimiser builds for its outer problem.
The outer problem of a meta-optimiser allocates over sub-portfolios, which are the clusters of NestedClustered and the inner portfolios of Stacking. Each of them is a weighted combination of the real assets. The outer ReturnsResult must state each quantity on that universe, and the panel too. With no collapse, the outer optimiser has no panel, and a FeatureDistance in it throws and does not cluster.
A feature is intensive, as iv and ivpa are. So the collapse starts from the weights of synthetic_asset_weights, which divide out the gross exposure $s_k$ of each sub-portfolio. A weighted sum with no normalisation multiplies the feature vector of a sub-portfolio by its gross exposure, which makes it larger under leverage or short positions. Under the default AngularDist, the normalisation does not change a rectangular field, because a scale of one row changes no cosine. In the square case it does change the field, because the product on both sides scales the label axis too. The normalisation also keeps the collapse bounded for every gross exposure larger than zero.
At an observation where a member is outside the universe, the panel stores a finite value that has no meaning, so the collapse reads only the active members. A cell that a fill did not reach holds a placeholder too, and its placeholder mask is true. So for each Panel Field the collapse reads only the members that are active and hold data. A cell that a fill policy wrote holds data, and the collapse reads it. The rule alg states what the weight of an inactive member becomes. The default, RenormaliseActive, divides by the weight of the active members, as the normalisation divides out the gross exposure, and each value stays a convex combination. InactiveAsCash reads the missing weight as cash with a zero feature. It matches the outer returns for an additive feature, because those returns read the weight of an inactive asset as cash.
The collapse does not support an extensive feature, such as a market capitalisation or a headcount, that needs a weighted sum. The divisor comes from the inner solve, so a caller cannot scale the feature before the solve.
A sub-portfolio whose weights are all zero has a gross exposure of zero. synthetic_asset_weights keeps its column of zeros and does not divide, so the collapse gives that sub-portfolio a feature vector of zeros and does not throw. The distance kernel already handles a feature vector of zeros, and the same weights give a returns column of zeros, and zero iv and ivpa. A sub-portfolio with no active member at an observation gets the same zeros there, and it is inactive there.
Mathematical definition
At each observation $t$ of a panel that changes over time:
\[\begin{align} D_{t,ik} &= \frac{\tilde{W}_{ik} r_{ti}}{d_{tk}}\,, \quad r_{ti} = m_{ti} u_{ti}\,,\\ \boldsymbol{a}^{o}_{t} &= \mathbf{D}_{t}^\intercal \boldsymbol{a}_{t}\,,\\ \mathbf{C}^{o}_{t} &= \mathbf{D}_{t}^\intercal \mathbf{C}_{t}\,,\\ \mathbf{S}^{o}_{t} &= \mathbf{D}_{t}^\intercal \mathbf{S}_{t} \mathbf{D}_{t}\,,\\ \mathbf{F}^{o}_{t} &= \mathbf{D}_{t}^\intercal \mathbf{H}_{t}\,,\\ m^{o}_{tk} &= \mathbf{1}\left[\left(\tilde{\mathbf{W}}^\intercal \boldsymbol{m}_{t}\right)_{k} > 0\right]\,,\\ o^{o}_{tk} &= \mathbf{1}\left[\left(\tilde{\mathbf{W}}^\intercal (\boldsymbol{m}_{t} \odot \boldsymbol{o}_{t})\right)_{k} > 0\right]\,,\\ p^{o}_{tk} &= 1 - \mathbf{1}\left[\left(\tilde{\mathbf{W}}^\intercal (\boldsymbol{m}_{t} \odot \boldsymbol{u}_{t})\right)_{k} > 0\right]\,. \end{align}\]
The divisor and the restricted weights belong to one Panel Field, because each field has its own data mask. Under RenormaliseActive, $d_{tk} = \sum_{i} \tilde{W}_{ik} r_{ti}$, so a numeric value is
\[\begin{align} a^{o}_{tk} &= \frac{\sum_{i=1}^{N} \tilde{W}_{ik} r_{ti} a_{ti}}{\sum_{i=1}^{N} \tilde{W}_{ik} r_{ti}}\,. \end{align}\]
Under InactiveAsCash, $d_{tk} = 1$, so $a^{o}_{tk} = \sum_{i} \tilde{W}_{ik} r_{ti} a_{ti}$. A static panel has no active mask, so $m_{ti} = 1$ on its one row, and $r_{ti} = u_{ti}$. A tensor field reads the data mask of each label, and a square field reads a pair of members, see collapse_read_tensor. Where every active cell holds data, $r_{ti} = m_{ti}$.
Where:
- $\mathbf{D}_{t}$: Restricted weights of observation $t$,
assets × sub-portfolios. - $d_{tk}$: Divisor of sub-portfolio $k$ at observation $t$, see
active_weight_divisor. It is $1$ where the sum is zero, and where every member with weight is active. - $\boldsymbol{a}_{t}$, $\boldsymbol{a}^{o}_{t}$: Values of a numeric field at observation $t$, one entry for each asset and one for each sub-portfolio.
- $\mathbf{C}_{t}$, $\mathbf{C}^{o}_{t}$: Values of a tensor field whose labels are not the asset names,
assets × labelsandsub-portfolios × labels. - $\mathbf{S}_{t}$, $\mathbf{S}^{o}_{t}$: Values of a tensor field whose labels are the asset names,
assets × assetsandsub-portfolios × sub-portfolios. - $\mathbf{H}_{t}$: One-hot matrix of a categorical field,
assets × levels. - $\mathbf{F}^{o}_{t}$: Membership fractions,
sub-portfolios × levels. - $m_{ti}$: Active mask of the Asset Panel, $1$ when asset $i$ is in the universe at observation $t$ and $0$ otherwise. $\boldsymbol{m}_{t}$ is its row $t$.
- $m^{o}_{tk}$: Active mask of the collapsed panel. The estimation mask collapses in the same way.
- $\boldsymbol{o}_{t}$, $o^{o}_{tk}$: Observed mask of a Panel Field at observation $t$, and its collapse. $o_{ti}$ is $1$ for a Panel Field that cannot blank.
- $\boldsymbol{u}_{t}$: Data mask of a Panel Field at observation $t$: $u_{ti}$ is $1$ where the cell of member $i$ holds a value that the raw input carried or that a fill policy wrote, and $0$ where it holds a placeholder. See
panel_data_mask. - $p^{o}_{tk}$: Placeholder mask of the collapsed Panel Field, $1$ where no active member with weight holds data.
- $r_{ti}$: Mask of the cells that the collapse reads: $1$ where member $i$ is active and holds data at observation $t$.
- $\mathbf{1}[\cdot]$: Indicator, $1$ when the condition holds and $0$ otherwise.
- $\odot$: Element-wise product.
- $\tilde{\mathbf{W}}$: Normalised inner weight matrix, $\tilde{W}_{ik} = |W_{ik}| / \sum_{j=1}^{N} |W_{jk}|$. A column of zeros stays a column of zeros.
- $N$: Number of assets.
Under RenormaliseActive, each column of $\mathbf{D}_{t}$ sums to one or is a column of zeros. So each collapsed value is a convex combination of the values of the active members that hold data, or zero. A row of $\mathbf{F}^{o}_{t}$ sums to one when the sub-portfolio has an active member with weight and every member has a level.
Algorithm
- Return
nothingwhenpnlisnothing. - Normalise the inner weights with
synthetic_asset_weights, givingW. - Name the sub-portfolios
"_1","_2", …, givingsyn. - Collapse each Panel Field with
collapse_panel_field, on the active mask of the panel and the rulealg. Each field computes its divisors on its own active cells that hold data. - Collapse the active mask and the estimation mask with
collapse_panel_mask. They staynothingfor a static panel.
Arguments
pnl: The Asset Panel, ornothing.wi: The inner weights,assets × sub-portfolios.nx: The asset names of the returns data, ornothing. Only a tensor field reads it, to find the square case.alg: The rule of the panel collapse, thepcolfield of the meta-optimiser.
Returns
pnl::Option{AssetPanel}: The Asset Panel on the sub-portfolios, ornothing.
Related
PortfolioOptimisers.collapse_rate — Function
collapse_rate(a::Nothing, W::MatNum, m, alg) -> nothing
collapse_rate(a::Number, W::MatNum, m, alg) -> Number
collapse_rate(a::MatNum, W::MatNum, m::Option{<:AbstractMatrix{Bool}}, alg) -> Matrix
collapse_rate(a::VecNum, W::MatNum, m::Nothing, alg) -> Vector
collapse_rate(a::VecNum, W::MatNum, m::AbstractMatrix{Bool}, alg) -> VectorCollapses one implied volatility quantity of the returns data onto the sub-portfolios, with the rule of the panel collapse.
The implied volatilities iv and the implied volatility risk premium adjustment ivpa are rates, so they collapse as the values of a numeric Panel Field do, see collapse_asset_panel. iv holds observations × assets and collapses row by row against the active mask of the panel. A vector ivpa holds one value for each asset, and it collapses over the members that are active at the last row of the mask, the row at which the outer fit stands. With no panel, or a static one, m is nothing, and every member counts.
Algorithm
The method that Julia selects is the algorithm.
nothingand a scalar stay as they are.- A matrix collapses through
collapse_panel_numeric, with the divisors ofactive_weight_divisor. It reads the cells offinite_read_mask: an implied volatility that is not finite counts as an inactive member of its row, so a gap inivdoes not poison the row. - A vector collapses as
transpose(W) * awith no mask. With a mask, the value of each member that is inactive at the last row ofmbecomes zero, the vector collapses in the same way, and the answer divides by the divisors of that row. A sub-portfolio with no member with weight active at that row has no value to read there, so it collapses astranspose(W) * a: a zero is not a rate, and a Returns Result refuses anivpaof zero.
Arguments
a:iv,ivpa, ornothing.W: The normalised inner weights,assets × sub-portfolios.m: The active mask of the panel,observations × assets, ornothing.alg: The rule of the panel collapse.
Returns
- The collapsed quantity.
Related
PortfolioOptimisers.collapse_panel_field — Function
collapse_panel_field(f::NumericPanelField, W, nx, syn, m, alg) -> NumericPanelField
collapse_panel_field(f::CategoricalPanelField, W, nx, syn, m, alg) -> TensorPanelField
collapse_panel_field(f::TensorPanelField, W, nx, syn, m, alg) -> TensorPanelFieldCollapses one Panel Field onto the sub-portfolios of a meta-optimiser.
The collapse acts on one field at a time and returns a field. So the collapsed panel is an ordinary panel, and a selector that the caller wrote for the inner problem resolves on it with no change.
- A numeric field stays numeric.
- A tensor field stays a tensor field with the same name and labels. When its labels are the asset names, the contraction acts on both axes. Then the sub-portfolios name the labels, and the groups are dropped, so the field is square on the sub-portfolios too.
- A categorical field becomes a tensor field of membership fractions. It has the same name, the axis
"level", the levels as labels, and the combination of its one-hot block as values. A one-hot column of the Feature Matrix holds0and1. So its combination is the fraction of the weight of the sub-portfolio in that level, and the Feature Matrix of the collapsed panel is the collapse of the Feature Matrix of the panel. A combination of integer codes has no meaning, and a majority level loses the fractions and needs a rule for ties.
A field that a time-varying panel lifts from a static input, see RepeatedObservations, holds the same value at every row, and its collapse reads the active members of each row. So the collapsed field changes over time where the active members change.
The collapse reads a member only where it is active and its cell holds data: a value that the raw input carried, or that a fill policy wrote. A cell that holds the placeholder of a blank that no fill reached holds no data, so the rule alg divides by the weight of the active members that hold data. The collapsed value is then the same for every placeholder. A collapsed cell is observed where an active member with weight is observed, and it holds a placeholder where no active member with weight holds data.
Algorithm
The method that Julia selects is the algorithm. Each kind contracts its values over the active cells that hold data, through panel_data_mask, read_cells, collapse_read_numeric, collapse_read_onehot and collapse_read_tensor. It contracts its observed mask and its data mask through collapse_panel_mask, collapse_panel_tensor and collapse_categorical_mask. Its placeholder mask is the negation of the collapsed data mask, through collapse_placeholder_mask.
Arguments
f: The Panel Field.W: The normalised inner weights,assets × sub-portfolios.nx: The asset names of the returns data, ornothing. Only a tensor field reads it, to find the square case.syn: The names of the sub-portfolios.m: The active mask of the panel, ornothingfor a static panel.alg: The rule of the panel collapse.
Returns
- The collapsed Panel Field.
Related
PortfolioOptimisers.read_cells — Function
read_cells(m, o)Give the mask of the cells that the panel collapse reads for a numeric or a categorical Panel Field: a member is read where it is active and its cell holds data.
A cell that holds the placeholder of a blank that no fill reached holds no data, so the collapse reads it as it reads an inactive member. nothing reads every cell. A static panel has no active mask, so its mask is the data mask, and nothing when every cell holds data. Then the collapse keeps the static contraction bit for bit.
Arguments
m: The active mask of the panel,observations × assets, ornothingfor a static panel.o: The data mask of the Panel Field, seepanel_data_mask, ornothing.
Returns
- The mask,
observations × assetsorassets, ornothing.
Related
PortfolioOptimisers.collapse_read_numeric — Function
collapse_read_numeric(A::AbstractArray, W::MatNum, r, alg::AbstractPanelCollapseAlgorithm) -> ArrayCollapse the values of a numeric Panel Field over the cells that the mask r of read_cells marks.
The rule alg divides by the weight of the read members, through active_weight_divisor on r. Where every cell holds data, r is the active mask, and the collapse is the one that reads the active members alone. A static mask is a window of one row: the values and the mask take an observation axis of length one, collapse as a time-varying field does, and drop it after.
Arguments
A: The values,assetsorobservations × assets.W: The normalised inner weights,assets × sub-portfolios.r: The mask of the read cells, ornothing.alg: The rule of the panel collapse.
Returns
- The collapsed values.
Related
PortfolioOptimisers.collapse_read_onehot — Function
collapse_read_onehot(H::AbstractArray, W::MatNum, r, alg::AbstractPanelCollapseAlgorithm) -> ArrayCollapse the one-hot block H of a categorical Panel Field, assets × levels or assets × levels × observations, over the cells that the mask r of read_cells marks. A cell holds data or not for every level at once, so one mask serves each level. It is collapse_read_numeric with a level axis, through collapse_panel_tensor. A static block takes an observation axis of length one, and drops it after.
Related
PortfolioOptimisers.collapse_read_tensor — Function
collapse_read_tensor(A::AbstractArray, W::MatNum, sq::Bool, m, o, alg::AbstractPanelCollapseAlgorithm) -> ArrayCollapse the values of a tensor Panel Field over the cells that are active and hold data.
The data mask o of a tensor Panel Field has a label axis, so a member can hold data at one label and not at another. Where each active member holds data at every label, the cells to read are the active members, and collapse_panel_tensor collapses the field as it does with no data mask. Otherwise each collapsed value is a weighted mean over its own read cells.
- A rectangular field collapses each label as a numeric field, through
collapse_read_numeric, on the active members that hold data at that label. - A square field reads a pair of members, the asset and the label, with the weight $\tilde{W}_{ik} \tilde{W}_{jl}$. A pair is read where both are active and the cell holds data. The pairs collapse as the members of a numeric field, with the weights
kron(W, W), so the rule divides by the weight of the read pairs. This keeps a symmetric field symmetric, and a separable mask gives the collapse of the active members.
A static field takes an observation axis of length one, and drops it after.
Arguments
A: The values,assets × labelsorassets × labels × observations.W: The normalised inner weights,assets × sub-portfolios.sq:truewhen the label axis is the asset axis.m: The active mask of the panel,observations × assets, ornothingfor a static panel.o: The data mask of the Panel Field, with the shape ofA, ornothing. Seepanel_data_mask.alg: The rule of the panel collapse.
Returns
- The collapsed values.
Related
PortfolioOptimisers.collapse_panel_numeric — Function
collapse_panel_numeric(A::AbstractVector, W::MatNum) -> Vector
collapse_panel_numeric(A::AbstractMatrix, W::MatNum) -> Matrix
collapse_panel_numeric(A::AbstractArray, W::MatNum, m::Nothing, d) -> Array
collapse_panel_numeric(A::AbstractMatrix, W::MatNum, m::AbstractMatrix{Bool}, d) -> MatrixCollapses the values of one numeric Panel Field onto the sub-portfolios.
W is the normalised weight matrix that synthetic_asset_weights returns, assets × sub-portfolios. A static field holds one value for each asset, and a field that changes over time holds observations × assets. So the asset axis is the one axis that both forms contract. The two methods that take m read the active mask: a static panel passes nothing, contracts every member and has no divisor to read, and a time-varying panel contracts the active members of each row alone, then divides by d. collapse_asset_panel states the mathematics.
Algorithm
The method that Julia selects is the algorithm.
- A vector contracts as
transpose(W) * A, and a matrix asA * W. - With a mask, the value of each inactive cell becomes zero, the matrix contracts, and the answer divides by
dthroughactive_divide!.
Arguments
A: The values,assetsorobservations × assets.W: The normalised inner weights,assets × sub-portfolios.m: The active mask of the panel,observations × assets, ornothingfor a static panel.d: The divisors ofactive_weight_divisor, ornothing.
Returns
- The collapsed values,
sub-portfoliosorobservations × sub-portfolios.
Related
PortfolioOptimisers.collapse_panel_tensor — Function
collapse_panel_tensor(A::AbstractMatrix, W::MatNum, sq::Bool) -> Matrix
collapse_panel_tensor(A::AbstractArray{<:Any, 3}, W::MatNum, sq::Bool) -> Array
collapse_panel_tensor(A::AbstractArray, W::MatNum, sq::Bool, m::Nothing, d) -> Array
collapse_panel_tensor(A::AbstractArray{<:Any, 3}, W::MatNum, sq::Bool, m::AbstractMatrix{Bool}, d) -> ArrayCollapses the values of one tensor Panel Field onto the sub-portfolios.
A tensor array is assets × labels when it is static, and assets × labels × observations when it changes over time. The function always contracts the asset axis. When sq is true, the label axis is the asset axis, see features_are_assets. Then the function contracts the label axis too, and the answer is square on the sub-portfolios. The two methods that take m read the active mask, as collapse_panel_numeric does, and a square field restricts both axes to the active members. collapse_asset_panel states the mathematics.
Algorithm
The method that Julia selects is the algorithm.
- A matrix contracts as
transpose(W) * A. Whensqistrue, the answer is multiplied byWon the right. - A three-dimensional array does the same for the contiguous slice of each observation, into a result that the method makes before the loop.
- With a mask, the value of each inactive cell becomes zero, on the asset axis and, when
sqistrue, on the label axis, throughpanel_tensor_mask. The array contracts by step 2, and the answer divides bydthroughactive_divide!.
Arguments
A: The values.W: The normalised inner weights,assets × sub-portfolios.sq:truewhen the label axis is the asset axis.m: The active mask of the panel,observations × assets, ornothingfor a static panel.d: The divisors ofactive_weight_divisor, ornothing.
Returns
- The collapsed values. The sub-portfolios replace the asset axis, and also the label axis when
sqistrue.
Related
PortfolioOptimisers.panel_tensor_mask — Function
panel_tensor_mask(m::AbstractMatrix{Bool}, sq::Bool) -> BitArray{3}Lay the active mask of a time-varying panel out against the values of a tensor Panel Field.
The active mask is observations × assets, and a tensor Panel Field is assets × labels × observations. A rectangular field reads an asset at every label, so its mask is assets × 1 × observations, and it broadcasts over the labels. A square field holds the members on its label axis too, see features_are_assets, so an inactive member leaves both axes, and its mask is assets × assets × observations.
Algorithm
- Transpose
m, and reshape it toassets × 1 × observations. - When
sqistrue, take the conjunction with the same mask reshaped to1 × assets × observations.
Arguments
m: The active mask of the panel,observations × assets.sq:truewhen the label axis is the asset axis.
Returns
ma::BitArray{3}: The mask,assets × 1 × observations, orassets × assets × observationswhensqistrue.
Related
PortfolioOptimisers.collapse_panel_mask — Function
collapse_panel_mask(m::Nothing, W::MatNum) -> nothing
collapse_panel_mask(m::AbstractArray{Bool}, W::MatNum) -> BitArray
collapse_panel_mask(o::Nothing, W::MatNum, m) -> nothing
collapse_panel_mask(o::AbstractArray{Bool}, W::MatNum, m::Nothing) -> BitArray
collapse_panel_mask(o::AbstractMatrix{Bool}, W::MatNum, m::AbstractMatrix{Bool}) -> BitMatrixCollapses one mask onto the sub-portfolios, as the support of its combination.
A sub-portfolio is active or in estimation at an observation when one member with weight is. So the values and the masks use one kernel, and the mask stays Bool by its type. The estimation mask is a subset of the active mask, and the collapse keeps that relation with no second check. The two methods with two arguments collapse the universe masks. The methods with three arguments collapse a mask o of the cells of a Panel Field, its observed mask or its data mask: a sub-portfolio is observed, or holds data, where one member with weight is active and is observed, or holds data. So a member that is inactive there does not count, as its value does not. collapse_asset_panel states the mathematics.
Algorithm
The method that Julia selects is the algorithm. nothing stays nothing. Else the mask collapses through collapse_panel_numeric, with the active mask m when it is given, and an entry of the answer is true where the collapsed value is larger than zero.
Arguments
m: A universe mask, ornothing, in the methods with two arguments. The active mask of the panel, ornothingfor a static panel, in the methods with three arguments.o: The observed mask or the data mask of a Panel Field, ornothing.W: The normalised inner weights,assets × sub-portfolios.
Returns
- The collapsed mask, or
nothing.
Related
PortfolioOptimisers.collapse_categorical_mask — Function
collapse_categorical_mask(o::Nothing, W::MatNum, nl::Integer, m) -> nothing
collapse_categorical_mask(o::AbstractVector{Bool}, W::MatNum, nl::Integer, m::Nothing) -> BitMatrix
collapse_categorical_mask(o::AbstractMatrix{Bool}, W::MatNum, nl::Integer, m::AbstractMatrix{Bool}) -> BitArrayCollapses the observed mask or the data mask of a categorical Panel Field onto the tensor field that its collapse returns.
The collapsed field has one label for each level, so its mask needs a level axis, which the categorical mask does not have. A cell is observed, or holds data, for the whole label, never for one level. So the function collapses the asset mask once, and repeats it for each level.
Algorithm
The method that Julia selects is the algorithm. nothing stays nothing. Else the asset mask collapses through collapse_panel_mask, with the active mask m, and the answer repeats it nl times on the level axis, the second axis. A time-varying answer holds the observations last.
Arguments
o: The observed mask or the data mask of the categorical field,assetsorobservations × assets, ornothing.W: The normalised inner weights,assets × sub-portfolios.nl: The number of levels.m: The active mask of the panel, ornothingfor a static panel.
Returns
- The collapsed mask,
sub-portfolios × levelsorsub-portfolios × levels × observations, ornothing.
Related
PortfolioOptimisers.collapse_tensor_mask — Function
collapse_tensor_mask(o::Nothing, W::MatNum, sq::Bool, m) -> nothing
collapse_tensor_mask(o::AbstractArray{Bool}, W::MatNum, sq::Bool, m) -> BitArrayCollapses the observed mask or the data mask o of a tensor Panel Field, as the support of its combination through collapse_panel_tensor. nothing stays nothing.
Related
PortfolioOptimisers.collapse_placeholder_mask — Function
collapse_placeholder_mask(d::Nothing) -> nothing
collapse_placeholder_mask(d::AbstractArray{Bool}) -> BitArrayGive the placeholder mask of a collapsed Panel Field from its collapsed data mask d: a collapsed cell holds a placeholder where no active member with weight holds data. nothing stays nothing, because a Panel Field that holds no placeholder collapses to one that holds none.
Related
PortfolioOptimisers.finite_read_mask — Function
finite_read_mask(a::MatNum, m::Nothing) -> Option{BitMatrix}
finite_read_mask(a::MatNum, m::AbstractMatrix{Bool}) -> AbstractMatrix{Bool}Return the cells of a per-asset matrix that a collapse reads: the active cells of m at which a is finite.
A gap in a per-asset series acts as a gap in the returns of that asset, so a collapse reads it as an inactive cell. A complete matrix returns m itself, so a panel with no gap collapses as it did before.
Arguments
a: The per-asset matrix,observations × assets.m: The active mask, ornothing.
Returns
- The read mask, or
nothingwhenmisnothingandais finite at every cell.
Related
PortfolioOptimisers.benchmark_cells — Function
benchmark_cells(rd::ReturnsResult) -> Option{<:VecNum_MatNum}
benchmark_cells(B::Option{<:VecNum}, X, m) -> Option{<:VecNum}
benchmark_cells(B::MatNum, X::MatNum, m::Option{<:AbstractMatrix{Bool}}) -> MatNumReturn the benchmark of rd that a product with weights reads, with every cell that the portfolio does not read set to zero.
A matrix benchmark holds one series for each asset, and a product with weights forms the benchmark of a portfolio. The portfolio series reads an asset at the cells where its return is finite: a fold zeroes every other cell once, with filter_held_gaps. So the benchmark reads the same cells: the active cells at which both the return and the benchmark are finite. A gap in B[t, i] then gives zero, as a gap in X[t, i] does, and not NaN. NaN * 0 is NaN, so without this function a gap at an asset of zero weight poisons the observation. A vector benchmark is a series of the whole portfolio, so it passes through.
Arguments
rd: The returns result to use.B: The benchmark.X: The asset returns.m: The active mask of the Asset Panel, ornothing.
Returns
- The benchmark with the cells that are not read set to zero, or the benchmark itself when every cell is read.
Related
PortfolioOptimisers.collapse_fold_rate — Function
collapse_fold_rate(a::MatNum, w::VecNum, pnl::Option{<:AssetPanel}) -> Vector
collapse_fold_rate(a::VecNum, w::VecNum, pnl::Option{<:AssetPanel}) -> NumberCollapse an implied volatility quantity of one fold onto its portfolio, as collapse_rate collapses it onto a sub-portfolio of a meta-optimiser.
A fold of a cross-validation forms one portfolio, so it collapses iv and a vector ivpa against one weight vector. It reads the active mask of pnl and the rule RenormaliseActive, the default of a meta-optimiser, so the two collapses agree under the default. A fold of a meta-optimiser does not know its rule, so rebuild_fold_rates collapses its rates again under the rule. iv gives one rate per observation, and ivpa gives one rate.
Arguments
a:iv,observations × assets, or a vectorivpa.w: The normalised weights of the fold, seesynthetic_asset_weights.pnl: The Asset Panel of the fold, ornothing.
Returns
- The collapsed rate.
Related