Mirror descent: private API
PortfolioOptimisers.MirrorDescentState — Type
struct MirrorDescentState{__T_n, __T_u, __T_w0, __T_s, __T_gs} <: AbstractPartialFitStateHolds the state of a MirrorDescent rule between its updates.
The state holds the unmixed iterate, the Start Allocation that a restart returns to, the statistic of the schedule and the averages of the Gradient Transform. The mirror step reads and writes the iterate. The head plays the uniform mix of the iterate when alpha is positive, and the iterate itself otherwise.
Fields
n: Number of observations folded into the state.
u: The unmixed iterate $\boldsymbol{w}_t$, over the full pinned universe. It sums to one.
w0: The Start Allocation projected onto the Allocation Set, which a restart returns to.
s: The schedule's statistic, ornothing.
gs: The state of the Gradient Transform, ornothing.
Related
PortfolioOptimisers.doubling_stage — Function
doubling_stage(
sched::DoublingTrickRate,
t::Integer
) -> Tuple{Any, Any}
Finds the stage of a DoublingTrickRate that holds period t.
Algorithm
- Compute
base = N^2 * log(N). - Set the length
lenof stage 0 toceil(2 * base), and its last periodfintolen. - While
t > fin, go to the next stagei. Setlentoceil(2^i * base), and addlentofin.
Returns
(len, fin)::Tuple{Int, Int}: The length of the stage that holdst, and its last period.
Related
PortfolioOptimisers.WindowedBestRateState — Type
struct WindowedBestRateState{__T_n, __T_B, __T_L}Holds the state of a WindowedBestRate inside the state of the rule.
The state holds one expert allocation per rate, each an exponentiated-gradient run, and a ring of the experts' log period returns. The schedule sums the ring over the window.
Fields
n: Number of observations folded into the state.
B: The experts' allocations,assets × rates. Columnkis the exponentiated-gradient run at thek-th rate, from the Start Allocation.
L: The experts' log period returns,window × rates. The row of periodtoverwrites rowmod1(t, window)of this ring. Under the whole history the matrix is1 × ratesand holds a running sum.
Related
PortfolioOptimisers.ring_row — Function
ring_row(window::Nothing, n::Integer)
ring_row(window::Integer, n::Integer)Finds the row of a WindowedBestRateState's ring that period n writes.
Under a window the row is mod1(n, window). Under the whole history it is row 1, the one row of the running sum.
Related
PortfolioOptimisers.played_allocation — Function
played_allocation(u::AbstractVector, alpha::Real) -> Any
Forms the allocation that a MirrorDescent rule plays from its iterate.
At alpha = 0 the function returns a copy of the iterate u.
Mathematical definition
\[\begin{align} \tilde{\boldsymbol{w}} &= (1 - \alpha) \boldsymbol{w} + \frac{\alpha}{N} \boldsymbol{1}\,. \end{align}\]
Where:
- $\tilde{\boldsymbol{w}}$: Played allocation.
- $\boldsymbol{w}$: Iterate of the rule.
- $\alpha$: Uniform-mix share, in $[0, 1)$.
- $N$: Number of assets.
- $\boldsymbol{1}$: Vector of ones.
Related
PortfolioOptimisers.mixed_relatives — Function
mixed_relatives(x::AbstractVector, alpha::Real) -> Any
Mixes the price relatives that a MirrorDescent rule's update reads under the uniform mix alpha.
At alpha = 0 the function returns x itself.
Theorem 4.2 of [249] states the mix for price relatives with a period maximum of one, $(1 - \alpha / N) \boldsymbol{x} / \max_i x_i + (\alpha / N) \boldsymbol{1}$. Every reader of the mixed vector, the gradient of the log-wealth loss and the hint residual, gives the same result at every scale of the vector. So the function scales the floor to the period's maximum, and does not scale the price relatives down to one.
The mix of the raw price relatives with an unscaled floor is a different step, outside the proof of the theorem. Take the exponentiated gradient at the default rate and $\alpha = 0.2$, over the price relatives 1 .+ 0.02 .* randn(StableRNG(7), 60, 4). The two steps then give played allocations that differ by at most $2.6 \times 10^{-6}$. From the uniform allocation, over one period in which the first of four assets rises by half and the others stay flat, they differ by $7.0 \times 10^{-5}$.
Mathematical definition
\[\begin{align} \tilde{\boldsymbol{x}} &= \left(1 - \frac{\alpha}{N}\right) \boldsymbol{x} + \frac{\alpha}{N} \max_i x_i \boldsymbol{1}\,. \end{align}\]
Where:
- $\tilde{\boldsymbol{x}}$: Mixed price relative vector.
- $\boldsymbol{x}$: Price relative vector of the period.
- $x_i$: Price relative of asset $i$.
- $\alpha$: Uniform-mix share, in $[0, 1)$.
- $N$: Number of assets.
- $\boldsymbol{1}$: Vector of ones.
The result is $\max_i x_i$ times the mix of [249].
Related
References
- [249] D. P. Helmbold, R. E. Schapire, Y. Singer and M. K. Warmuth. On-line portfolio selection using multiplicative updates. Mathematical Finance 8, 325–347 (1998). Section 4, Theorem 4.2.
PortfolioOptimisers.reprojection — Function
reprojection(proj::AbstractProjectionGeometry, set::BoundedAllocationSet, q::AbstractVector, w::AbstractVector)
reprojection(proj::AbstractProjectionGeometry, set::AbstractAllocationSet, q::AbstractVector, w::AbstractVector)Projects onto the head's Allocation Set an allocation that a MirrorDescent rule plays without a step to it.
There are two such allocations. One is the uniform mix of the iterate under alpha. The other is the Start Allocation that a restart of a Learning-Rate Schedule returns to. Either can lie outside the set. The mix does when the set excludes the uniform allocation. The start does when a turnover ceiling or a MIP kind excludes it from the book the fund now holds. So the head projects them again, in the rule's own geometry, as an ExpertMixture projects its blend with blend_projection.
On a BoundedAllocationSet, the function returns an allocation inside the bounds unchanged, so the default set costs nothing. On every other set the projection is a programme, and a rule with a positive alpha solves two programmes per period.
Arguments
proj: The rule's geometry.set: The set, resolved.q: The allocation to play.w: The Price-Adjusted Allocation the step trades from.
Returns
w'::AbstractVector:qitself on a bounded set it lies in, its projection otherwise.
Related
PortfolioOptimisers.investable_risk_gradient — Function
investable_risk_gradient(obj::RiskLoss, u::AbstractVector, pr::AbstractPriorResult, imsk::Nothing, rd::ReturnsResult)
investable_risk_gradient(obj::RiskLoss, u::AbstractVector, pr::AbstractPriorResult, imsk::BitVector, rd::ReturnsResult)Computes the gradient of a Risk Loss on the Investable Mask of its prior result.
A leg that the window does not cover gets a zero gradient, so the loss does not move it.
Algorithm
The measure reads the rows rd that the prior is fitted on: each DataBenchmarkTracking of the measure binds to their benchmark with bind_data_benchmark, after the view at the mask. The constructor of the loss refuses a data benchmark under a prior that states x_src = ExcessSource(), so a data benchmark meets only a prior of the raw rows.
When the mask is nothing, every asset is priced, and the method returns risk_gradient of the bound measure at u. Otherwise it runs these steps.
- Find the indices
idxof the assets on the mask. - Make the zero vector
gof the length ofu. - View the measure at
idxwithport_opt_view. The view cuts a benchmark, a turnover reference or a moment that the measure holds to the legs on the mask. - Bind the viewed measure to the rows viewed at
idx. - Write
risk_gradientof the bound measure atu[idx], against the prior viewed atidx, intog[idx]. The slice ofuis not renormalised.
Related
PortfolioOptimisers.reader_frames — Method
reader_frames(obj::RiskLoss, path::AbstractString, refit::Bool) -> ReturnsFramesWalk the readers of a RiskLoss for the group of the head that holds it.
The loss holds no source of its own. Its prior pe is the outermost reader of the window, so the walk counts it, and the readers inside the measures r, such as the prior of an uncertainty set, count too. The walk reads the refit rule of the loss's own prior, because a regression reader inside the loss reads that prior, see refits_regression.
Arguments
obj: The loss.path: The path of the value from the head, for example"pe","cle"or"r[2].pe". A warning names a reader by its path.refit:truewhen a regression reader atx_src = nothingin the group fits its regression on the raw returns data.refits_regressionfinds the value. The loss ignores it and reads the rule of its own prior.
Returns
fr::ReturnsFrames: The frames that the loss adds to the group.
Related
References
- [249]
- D. P. Helmbold, R. E. Schapire, Y. Singer and M. K. Warmuth. On-line portfolio selection using multiplicative updates. Mathematical Finance 8, 325–347 (1998).