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

  • nothing when T <= window, because no candidate exists.
  • (rows, cands, latest) otherwise. rows is (window + 1):T. The entry of cands for the candidate row i is vec(X[(i - window):(i - 1), :]). latest is vec(X[(T - window + 1):T, :]).

Related

source
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}: Entry j is the number of edges at or below v[j].

Related

source
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, an Integer, or the fraction, a real in (0, 1).
  • n: The number of candidates.

Returns

  • l::Integer: min(neighbours, n) for a count, and floor(Int, neighbours * n) for a fraction.

Related

source
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

source
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 of 2 for a tree that holds a second moment, 1 otherwise, and fit_min_rows(opt).

Related

source
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: The AllocationSetConstraint to append.

Returns

  • c alone when ccnt is nothing, the vector [ccnt, c] for one constraint, and vcat(ccnt, c) for a vector.

Related

source
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:

  1. Build bcrp, a copy of opt whose wb and sets are those of set.
  2. Optimise factory(bcrp, w) on rd, giving res.
  3. When the fixed point stopped at iters before its certificate met tol, warn once and name the certificate.
  4. Return res.w.

The JuMP method:

  1. Pass w into opt with factory.
  2. Append an AllocationSetConstraint over set, w and X to the ccnt of the estimator.
  3. Optimise on rd, giving res. When its retcode is an OptimisationSuccess, return res.w.
  4. Otherwise record a Held Step through record_held_step! with the trials of the solver, and return nothing.

Arguments

  • opt: The held estimator.
  • rd: The head's rows carrier viewed at the sample. It holds the returns as they are, NaN where 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, a BoundedAllocationSet for the solver-free method.
  • X: The head's rows carrier over every row that it holds, or nothing. 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, or nothing on a Held Step.

Related

source
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

source
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, a Turnover fee for the rule.
  • slv: The solver of the programme.
  • pe: The prior estimator of the programme.

Returns

  • opt::MeanRisk: The programme.

Related

source
PortfolioOptimisers.FollowTheLeadingHistoryState — Type
struct FollowTheLeadingHistoryState{__T_n, __T_st, __T_h, __T_p, __T_born} <: AbstractPartialFitState

Holds 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, nothing when 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

source
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, with j the number of trailing zeros of born.

Related

source