Online selection rules: the third set: private API
PortfolioOptimisers.pattern_candidates — Function
pattern_candidates(
window::Integer,
X::AbstractMatrix
) -> Union{Nothing, Tuple{Any, Any, Any}}
Forms the candidate periods of a pattern-matching selector, their flattened windows and the flattened latest window.
A window flattens column by column, so the entries of one asset stay together.
Arguments
window: The number of rows in each window.X: The price relatives,T × N, in time order.
Returns
nothingwhenT <= window, because no candidate exists.(rows, cands, latest)otherwise.rowsis(window + 1):T. The entry ofcandsfor the candidate rowiisvec(X[(i - window):(i - 1), :]).latestisvec(X[(T - window + 1):T, :]).
Related
PortfolioOptimisers.assert_pattern_window — Function
assert_pattern_window(window::Integer)
Checks that the window of a pattern-matching selector holds at least one row.
Arguments
window: The number of rows in each window.
Validation
window >= 1. ADomainErroris thrown otherwise.
Returns
nothing.
Related
PortfolioOptimisers.histogram_cell — Function
histogram_cell(
edges::AbstractVector,
v::AbstractVector
) -> Any
Returns the histogram cell of a flattened window, the bin index of each of its entries.
Arguments
edges: The cell edges, increasing.v: The flattened window.
Returns
cell::AbstractVector{<:Integer}: Entryjis the number ofedgesat or belowv[j].
Related
PortfolioOptimisers.neighbour_count — Function
neighbour_count(neighbours::Integer, n::Integer)
neighbour_count(neighbours::Real, n::Integer)Returns the number of neighbours that a nearest-neighbour selector keeps out of n candidates.
Arguments
neighbours: The count, anInteger, or the fraction, a real in(0, 1).n: The number of candidates.
Returns
l::Integer:min(neighbours, n)for a count, andfloor(Int, neighbours * n)for a fraction.
Related
PortfolioOptimisers.LeaderOptimiser — Type
const LeaderOptimiser = Union{<:BestConstantRebalancedPortfolio, <:JuMPOptimisationEstimator}Groups the optimisation estimators that a follow-the-leader rule can re-solve under the head's Allocation Set.
A leader of a FollowTheLeader rule must honour the set. The solver-free BestConstantRebalancedPortfolio takes a bounded set on its own wb and sets slots. A JuMP estimator takes any set through the AllocationSetConstraint on its ccnt. No other estimator has a slot for the set, so the alias excludes it.
Related
PortfolioOptimisers.leader_min_rows — Function
leader_min_rows(opt::LeaderOptimiser)Returns the fewest sample rows on which the held estimator can re-solve.
An estimator whose tree holds a second moment needs two rows, because the covariance of one observation does not exist. Any estimator in the tree can state a larger floor through fit_min_rows.
Arguments
opt: The held estimator.
Returns
n::Integer: The larger of2for a tree that holds a second moment,1otherwise, andfit_min_rows(opt).
Related
PortfolioOptimisers.append_custom_constraint — Function
append_custom_constraint(ccnt::Nothing, c::CustomJuMPConstraint)
append_custom_constraint(ccnt::CustomJuMPConstraint, c::CustomJuMPConstraint)
append_custom_constraint(ccnt::AbstractVector{<:CustomJuMPConstraint}, c::CustomJuMPConstraint)Appends the Allocation Set Constraint to the custom constraints of the held estimator.
Arguments
ccnt: The custom constraints of the held estimator,nothing, one constraint, or a vector of them.c: TheAllocationSetConstraintto append.
Returns
calone whenccntisnothing, the vector[ccnt, c]for one constraint, andvcat(ccnt, c)for a vector.
Related
PortfolioOptimisers.leader_allocation — Function
leader_allocation(opt::BestConstantRebalancedPortfolio, rd::ReturnsResult, w::AbstractVector, set::BoundedAllocationSet, X)
leader_allocation(opt::JuMPOptimisationEstimator, rd::ReturnsResult, w::AbstractVector, set::AbstractAllocationSet, X)Re-solves the held estimator on the sample under the head's Allocation Set, and returns the leader, or nothing on a Held Step.
The estimator runs the batch path that it runs on any carrier. An estimator with no prior reduces to the Coverage Universe of the sample, an estimator with a prior reduces to the Investable Mask of its prior, and both return a zero at every asset outside it. A leader that stopped short of its certificate still trades, so the solver-free method warns and plays the fixed point.
Algorithm
The solver-free method:
- Build
bcrp, a copy ofoptwhosewbandsetsare those ofset. - Optimise
factory(bcrp, w)onrd, givingres. - When the fixed point stopped at
itersbefore its certificate mettol, warn once and name the certificate. - Return
res.w.
The JuMP method:
- Pass
wintooptwithfactory. - Append an
AllocationSetConstraintoverset,wandXto theccntof the estimator. - Optimise on
rd, givingres. When its retcode is anOptimisationSuccess, returnres.w. - Otherwise record a Held Step through
record_held_step!with the trials of the solver, and returnnothing.
Arguments
opt: The held estimator.rd: The head's rows carrier viewed at the sample. It holds the returns as they are,NaNwhere no return exists, under the pinned names and the Asset Panel of the buffer.w: The Price-Adjusted Allocation of the step.set: The resolved Allocation Set of the head, aBoundedAllocationSetfor the solver-free method.X: The head's rows carrier over every row that it holds, ornothing. The JuMP method passes it to the set's builders, and the solver-free method does not read it.
Returns
w::Union{Nothing, AbstractVector}: The leader, ornothingon a Held Step.
Related
PortfolioOptimisers.loss_control_programme — Function
loss_control_programme(
gamma::Real,
slv::Union{var"#s1802", var"#s1801"} where {var"#s1802"<:Solver, var"#s1801"<:(AbstractVector{<:Solver})},
pe::AbstractPriorEstimator
) -> MeanRisk{__T_opt, __T_r, MinimumRisk, Nothing, Nothing} where {__T_opt, __T_r}
Builds the programme of the short-term portfolio optimisation with loss control.
The programme is MeanRisk under MinimumRisk over WorstRealisation plus Variance scaled by gamma under QuadRiskExpr. The method is typed on the solver and the prior, so the optimiser that it builds is concrete for them.
Arguments
gamma: The weight of the variance.slv: The solver of the programme.pe: The prior estimator of the programme.
Returns
opt::MeanRisk: The programme.
Related
PortfolioOptimisers.ensemble_programme — Function
ensemble_programme(
gamma::Real,
fees::Fees,
slv::Union{var"#s1802", var"#s1801"} where {var"#s1802"<:Solver, var"#s1801"<:(AbstractVector{<:Solver})},
pe::AbstractPriorEstimator
) -> MeanRisk{JuMPOptimiser{__T_pe, __T_slv, __T_wb, __T_bgt, __T_sbgt, __T_gbgt, __T_xbgt, __T_lt, __T_st, __T_lcse, __T_cte, __T_gcarde, __T_sgcarde, __T_smtx, __T_sgmtx, __T_slt, __T_sst, __T_sglt, __T_sgst, __T_tn, __T_fees, __T_sets, __T_tr, __T_ple, __T_ret, __T_sca, __T_ccnt, __T_cobj, __T_sc, __T_so, __T_ss, __T_card, __T_scard, __T_l2c, __T_lpc, __T_linfc, __T_l1, __T_l2, __T_lp, __T_linf, __T_brt, __T_x_src, __T_strict, __T_cache}, Variance{RiskMeasureSettings{Float64, Nothing, Bool}, Nothing, Nothing, Nothing, QuadRiskExpr}, MaximumUtility{var"#s185"}, Nothing, Nothing} where {__T_pe, __T_slv, __T_wb, __T_bgt, __T_sbgt, __T_gbgt, __T_xbgt, __T_lt, __T_st, __T_lcse, __T_cte, __T_gcarde, __T_sgcarde, __T_smtx, __T_sgmtx, __T_slt, __T_sst, __T_sglt, __T_sgst, __T_tn, __T_fees, __T_sets, __T_tr, __T_ple, __T_ret, __T_sca, __T_ccnt, __T_cobj, __T_sc, __T_so, __T_ss, __T_card, __T_scard, __T_l2c, __T_lpc, __T_linfc, __T_l1, __T_l2, __T_lp, __T_linf, __T_brt, __T_x_src, __T_strict, __T_cache, var"#s185"<:Real}
Builds the programme of the online low-dimension ensemble method.
The programme is MeanRisk under MaximumUtility at the risk aversion gamma over Variance under QuadRiskExpr, and it charges fees. The method is typed on the solver and the prior, so the optimiser that it builds is concrete for them.
Arguments
gamma: The risk aversion over the predictive variance.fees: The fees of the programme, aTurnoverfee for the rule.slv: The solver of the programme.pe: The prior estimator of the programme.
Returns
opt::MeanRisk: The programme.
Related
PortfolioOptimisers.FollowTheLeadingHistoryState — Type
struct FollowTheLeadingHistoryState{__T_n, __T_st, __T_h, __T_p, __T_born} <: AbstractPartialFitStateHolds the live experts of a follow-the-leading-history rule, with their Rule States, start periods and weights.
It is the carrier of FollowTheLeadingHistory. The rule refuses a merge of two carriers, because the head's allocation depends on the order of the rows.
Fields
n: Number of observations folded into the state.
st: The carrier of each live expert,nothingwhen the base rule carries nothing.
h: The allocation that each live expert holds during the current period.
p: The weight over the live experts during the current period.
born: The start period of each live expert, from which its lifetime counts.
Related
PortfolioOptimisers.expert_alive — Function
expert_alive(born::Integer, t::Integer) -> Any
Checks whether the copy started at period born is alive at period t, under the lifetime of Hazan and Seshadhri (2009).
The function reads t >= born as given.
Mathematical definition
\[\begin{align} k &= r\, 2^{j}\,, \quad r \text{ odd}\,, \\ k &\in S_t \iff k \leq t \leq k + 2^{j + 2} + 1\,. \end{align}\]
Where:
- $k$: Start period of the copy,
born. - $r$, $j$: Odd part of $k$ and the exponent of the largest power of two that divides $k$.
- $S_t$: Working set of period $t$.
- $t$: Period index, counted from one.
The paper calls $2^{j + 2} + 1$ the lifetime of $k$, so the copy is alive in $2^{j + 2} + 2$ periods.
Arguments
born: The start period of the copy.t: The period.
Returns
alive::Bool:t <= born + 2^(j + 2) + 1, withjthe number of trailing zeros ofborn.
Related
FollowTheLeadingHistory: the rule whose pruning reads the lifetime.