Result investable view: private API
PortfolioOptimisers.extract_pr — Function
extract_pr(res::OptimisationResult) -> Any
extract_pr(
res::OptimisationResult,
pr::Union{Nothing, AbstractPriorResult, ReturnsResult}
) -> Any
Returns the prior result for a risk calculation from an optimisation result.
A pr that the caller gives has priority. Else the function tests hasproperty(res, :pr), which property forwarding answers for a nested result. For example, a JuMP result holds its prior at res.jr.jor.pr, and res.pr resolves to it.
The prior 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 prior with res.w reads both through result_investable_view, and not through this function alone.
Arguments
res: The optimisation result, which holds a prior result as its propertypr, directly or by property forwarding.pr: A prior result that the caller gives, ornothing.
Validation
pris notnothing, orreshas the propertypr, else anArgumentErrorthat names the type ofresis thrown.
Returns
pr::Pr_RR: The prior result for the risk calculation.
Related
PortfolioOptimisers.result_investable_returns — Function
result_investable_returns(imsk, res::OptimisationResult, pr::Nothing)
result_investable_returns(imsk::Nothing, res::OptimisationResult, pr::MatNum_Pr)
result_investable_returns(imsk::BitVector, res::OptimisationResult, pr::Pr_RR)
result_investable_returns(imsk::BitVector, res::OptimisationResult, X::MatNum)Returns the returns data that a consumer of a result reads: the prior of the result, or the returns data of the caller viewed at the Investable Mask of the result.
This is the half of result_investable_view for the returns data. The prior of the result is on the universe that the fit solved, so the function reads it through extract_pr and keeps it as it is. The same holds when the caller gives that prior back, as in expected_risk(r, res, res.pr). It is the only returns data that is already on the investable universe. The function finds it by identity and not by width, because reduced data and data on the full universe can have the same width by chance.
Every other returns data of the caller is on the universe of res.w. Under a BitVector mask, the function views it at findall(imsk). A prior result or a ReturnsResult goes through its own port_opt_view, which is the rule that fold_factor_returns applies to the rd of a caller on a fold. A matrix is viewed by its columns. Under a mask nothing, the fit left out no asset, so the returns data of the caller does not change.
Algorithm
The method that Julia selects is the algorithm.
- When
prisnothing, return the prior ofreswithextract_pr. - When
imskisnothing, returnprunchanged. - When
pris the prior ofres, by identity, return it unchanged. - Else return the view of
pratimsk.
Arguments
imsk: The Investable Mask ofres, ornothing.res::OptimisationResult: The optimisation result.pr: Returns data of the caller on the universe ofres.w, ornothing.
Returns
- The returns data on the investable universe of
res.
Related
PortfolioOptimisers.result_investable_fees — Function
result_investable_fees(imsk, res::OptimisationResult, fees::Nothing)
result_investable_fees(imsk::Nothing, res::OptimisationResult, fees::Fees)
result_investable_fees(imsk::BitVector, res::OptimisationResult, fees::Fees)Returns the fee that a consumer of a result charges: the fee of the result, or the fee of the caller viewed at the Investable Mask of the result.
This is the half of result_investable_view for the fee, and it is the rule that fold_fees applies on a fold. At the fit, investable_fees_view reduced the fee of the result. It sliced the five per-asset fields to the mask and the two liquidation charges, lq and flq, to the complement, and it marked the fee with the mask. So the function reads that fee through extract_fees and keeps it as it is.
A fee of the caller is on the universe of the caller, so it goes through the same function as a fee at the fit, investable_fees_view. That function slices the per-asset fields to the mask and lq and flq to the complement. Under a mask nothing, the fit left out no asset, so it removes lq and flq. The function needs the width of the full universe to find the complement. A result holds no matrix of that width, so the width is the length of the mask.
Algorithm
The method that Julia selects is the algorithm.
- When
feesisnothing, return the fee ofreswithextract_fees. - When
imskisnothing, returninvestable_fees_view(fees, nothing, nothing). - Else return
investable_fees_view(fees, imsk, length(imsk)).
Arguments
imsk: The Investable Mask ofres, ornothing.res::OptimisationResult: The optimisation result.fees: AFeesof the caller on the universe ofres.w, ornothing.
Returns
fees::Option{<:Fees}: The fee on the two axes that the mask of the result gives, ornothing.
Related
PortfolioOptimisers.result_investable_view — Function
result_investable_view(res::OptimisationResult, pr, fees, nx, imsk::Nothing)
result_investable_view(res::OptimisationResult, pr = nothing, fees = nothing, nx = nothing,
imsk::BitVector = result_investable_mask(res))Puts the weights, the returns data, the fee and the asset names that a consumer of a result reads on the investable universe of the result.
A result holds three values on two universes. An optimisation reduces its universe to the Investable Mask at its entry, and expands the solved weights back. So res.w is on the full universe of the caller, and res.pr is the prior of the universe that the fit solved. The entry also reduced res.fees, with its five per-asset fields on the mask and its two liquidation charges, lq and flq, on the complement.
A consumer that reads two of the three values separately puts a full vector with a reduced one. A per-asset fee indexed at a mask of full length throws a BoundsError. A reduced returns matrix with a full weight vector throws a DimensionMismatch. A reduced mu under the full names of the caller draws each bar after the gap under the name of the asset before it. So this function is the one place that puts the values together. Every method of expected_risk, calc_net_returns, factor_attribution and performance_summary that takes a result reads the values through it, and so does the plotting extension.
The answers are on the investable universe of res, where its prior and its fee already are. The weights are viewed at the mask through investable_weights_view. result_investable_returns gives the returns data, and result_investable_fees gives the fee. Each keeps the value of the result as it is, and views a value of the caller at the mask. The asset names are on the asset axis, so they take the mask directly, and a figure labels each bar with its own asset.
A result whose mask is nothing left out no asset, so the weights and the names do not change. A consumer that forms a per-asset answer on the investable universe expands it back through expand_investable_weights, with the mask that this function returns first.
Algorithm
- Take the Investable Mask
imsk. By default,result_investable_maskreads it offres, and the call dispatches on its type. - For a mask
nothing, returnnothing,res.w, the returns data fromresult_investable_returns, the fee fromresult_investable_fees, andnxunchanged. - For a
BitVectormask, return the mask, the view ofres.wat it, the returns data and the fee from the same two functions, and the view ofnxat the mask.
Arguments
res::OptimisationResult: The optimisation result.pr: Returns data of the caller on the universe ofres.w: a prior result, aReturnsResultor a returns matrix. Withnothing, the function reads the prior of the result.fees: AFeesof the caller on the universe ofres.w. Withnothing, the function reads the fee of the result.nx: Asset names on the universe ofres.w, ornothing.imsk: The Investable Mask ofres, ornothing.
Returns
(imsk, w, pr, fees, nx): The Investable Mask, and the weights, the returns data, the fee and the names on the investable universe ofres.
Related