Base meta optimisation
A meta-optimiser solves one inner problem for each sub-portfolio, and gives the outer optimiser a synthetic universe with one asset for each sub-portfolio. NestedClustered enumerates cluster index sets, and Stacking enumerates inner optimisers. A new enumeration subtypes SubPortfolioUniverse and adds a method of sub_portfolio_count, sub_portfolio_predict, sub_portfolio_view and fold_weight_matrix.
PortfolioOptimisers.SubPortfolioUniverse — Type
abstract type SubPortfolioUniverse <: AbstractAlgorithmAbstract supertype for the sub-portfolio enumeration of a meta-optimiser, which states what a sub-portfolio is and which assets it sees.
A meta-optimiser solves one inner problem for each sub-portfolio, predicts the returns of each sub-portfolio, and gives the outer optimiser a synthetic universe with one asset for each sub-portfolio. One module does this for every meta-optimiser that owns an outer optimiser. The two meta-optimisers of the library differ in one respect only, and this type names it.
NestedClusteredenumerates cluster index sets. It views its one inner optimiser onto each cluster, and it views every full-universe quantity, the Prior Result and the Fees, onto that cluster too.Stackingenumerates inner optimisers. Each one sees the whole universe, so the module views nothing.
FullUniverse and ClusterUniverse declare the two. The module reads them through the four methods below, so a third meta-optimiser needs a third subtype and no copy of the module.
Interfaces
To implement a new sub-portfolio enumeration, subtype SubPortfolioUniverse and implement the following methods:
sub_portfolio_count(u::SubPortfolioUniverse, opti) -> Integer: The number of sub-portfolios.optiis the inner optimiser field of the meta-optimiser.sub_portfolio_predict(u::SubPortfolioUniverse, opti, i::Integer, rd::ReturnsResult, cv, ex) -> MultiPeriodPredictionResult: The cross-validation prediction of sub-portfolioi, onecross_val_predictcall. On the combinatorial path the call returns aPopulationPredictionResultinstead.sub_portfolio_view(u::SubPortfolioUniverse, x, i::Integer): The full-universe quantityx, a Prior Result or Fees ornothing, viewed onto sub-portfolioi.fold_weight_matrix(predictions::VecMPredRes, u::SubPortfolioUniverse, f::Integer, na::Integer) -> Matrix: The weights of every sub-portfolio in foldf, as anassets × sub-portfoliosmatrix over allnaassets.
Arguments
u: The sub-portfolio enumeration.opti: The inner optimiser field of the meta-optimiser.i: The index of the sub-portfolio.rd: The returns data of the meta-optimiser.cv: The cross-validation scheme, a copy that belongs to sub-portfolioi.ex: The FLoops executor.x: A Prior Result, Fees, ornothing.predictions: One prediction result for each sub-portfolio.f: The index of the fold.na: The number of real assets.
Returns
sub_portfolio_count: The number of sub-portfolios.sub_portfolio_predict: The prediction result of sub-portfolioi.sub_portfolio_view:xviewed onto sub-portfolioi.fold_weight_matrix: Theassets × sub-portfoliosweight matrix of foldf.
Examples
An enumeration that splits the assets into two halves. The fold-less prediction then returns the net return of each half's equal-weight portfolio. It reads the rule of the panel collapse off the pcol field of its second argument, and the prior estimator pe of the meta-optimiser off the same argument, which here is a named tuple in place of a meta-optimiser.
julia> struct Halves <: PortfolioOptimisers.SubPortfolioUniverse n::Int endjulia> halves(u::Halves) = [1:(u.n ÷ 2), (u.n ÷ 2 + 1):(u.n)];julia> PortfolioOptimisers.sub_portfolio_count(::Halves, opti) = 2julia> function PortfolioOptimisers.sub_portfolio_view(u::Halves, x, i::Integer) return PortfolioOptimisers.port_opt_view(x, halves(u)[i]) endjulia> function PortfolioOptimisers.sub_portfolio_predict(u::Halves, opti, i::Integer, rd::ReturnsResult, cv, ex) return cross_val_predict(opti, rd, cv; cols = halves(u)[i], ex = ex) endjulia> function PortfolioOptimisers.fold_weight_matrix(predictions, u::Halves, f::Integer, na::Integer) W = zeros(na, 2) for (i, cl) in enumerate(halves(u)) W[cl, i] = predictions[i].pred[f].res.w end return W endjulia> rd = ReturnsResult(; nx = ["A", "B", "C", "D"], X = [0.5 0.25 -0.25 0.125; -0.5 0.75 0.25 0.375]);julia> res = [optimise(EqualWeighted(), PortfolioOptimisers.port_opt_view(rd, cl)) for cl in halves(Halves(4))];julia> W = [0.5 0.0; 0.5 0.0; 0.0 0.5; 0.0 0.5];julia> pr = prior(EmpiricalPrior(), rd);julia> PortfolioOptimisers.predict_outer_returns(nothing, (; pcol = RenormaliseActive(), pe = EmpiricalPrior()), Halves(4), rd, pr, nothing, W, res).X2×2 Matrix{Float64}: 0.375 -0.0625 0.125 0.3125Related
PortfolioOptimisers.sub_portfolio_count — Function
sub_portfolio_count(u::FullUniverse, opti)
sub_portfolio_count(u::ClusterUniverse, opti)Count the sub-portfolios.
A FullUniverse enumerates the inner optimisers, so it has one sub-portfolio for each optimiser in opti. A ClusterUniverse enumerates the clusters, and one inner optimiser serves all of them.
Arguments
u: Sub-portfolio enumeration.opti: The inner optimiser field of the meta-optimiser. It is a vector of optimisers for aFullUniverse, and one optimiser for aClusterUniverse, which does not read it.
Returns
- The number of sub-portfolios.
Related
PortfolioOptimisers.sub_portfolio_predict — Function
sub_portfolio_predict(u::FullUniverse, opti, i, rd, cv, ex)
sub_portfolio_predict(u::ClusterUniverse, opti, i, rd, cv, ex)Cross-validate sub-portfolio i.
The method makes one cross_val_predict call, and the enumeration selects the optimiser and the assets. A FullUniverse runs opti[i] and passes no cols. The sub-portfolio is the whole universe, and the method of cross_val_predict for a precomputed OptimisationResult has no cols keyword, so a colon there raises a MethodError. A ClusterUniverse runs the one inner optimiser on u.cls[i].
Arguments
u: Sub-portfolio enumeration.opti: The inner optimiser field of the meta-optimiser.i: Sub-portfolio index.rd: Returns data.cv: Cross-validation scheme, the copy that belongs to this sub-portfolio.ex: FLoops executor that controls parallelism.
Returns
- The cross-validation prediction result of sub-portfolio
i.
Related
PortfolioOptimisers.sub_portfolio_view — Function
sub_portfolio_view(u::FullUniverse, x, i::Integer)
sub_portfolio_view(u::ClusterUniverse, x, i::Integer)View a full-universe quantity onto sub-portfolio i.
The quantities are the two that the predicted returns of a sub-portfolio read: the Prior Result and the Fees. A FullUniverse sub-portfolio holds the whole universe, so the method returns x unchanged. A ClusterUniverse sub-portfolio restricts x to its cluster through port_opt_view.
Arguments
u: Sub-portfolio enumeration.x: Full-universe quantity, ornothing.i: Sub-portfolio index.
Returns
x, viewed onto sub-portfolioi.
Related
PortfolioOptimisers.fold_weight_matrix — Function
fold_weight_matrix(predictions, u::FullUniverse, f, na)
fold_weight_matrix(predictions, u::ClusterUniverse, f, na)Write the sub-portfolio weights of fold f as the assets × sub-portfolios matrix that the collapse of the Asset Panel reads.
Mathematical definition
\[\begin{align} W_{ik} &= \begin{cases} w^{(f)}_{k,i} & \text{if } i \in \mathcal{C}_k\,,\\ 0 & \text{otherwise}\,. \end{cases} \end{align}\]
Where:
- $W_{ik}$: Entry of the weight matrix of fold $f$, the weight of sub-portfolio $k$ on asset $i$.
- $w^{(f)}_{k,i}$: Weight of sub-portfolio $k$ on asset $i$ in fold $f$.
- $\mathcal{C}_k$: Assets of sub-portfolio $k$. It is every asset for a
FullUniverse, and cluster $k$ for aClusterUniverse.
The clusters of a ClusterUniverse partition the assets, so a column is the real weight of its sub-portfolio over every asset.
Arguments
predictions: OneMultiPeriodPredictionResultfor each sub-portfolio.u: Sub-portfolio enumeration, aSubPortfolioUniverse.f: Fold index.na: Number of real assets.
Returns
- The
assets × sub-portfoliosweight matrix of foldf.
Related