Online optimisation

An optimiser updates with two calls. partial_fit!(opt, rd) adds the observations of a ReturnsResult to the prior only, and stores the rest of the ReturnsResult in a PortfolioOptimisers.ReturnsBufferState. optimise(opt) with no returns rebuilds the ReturnsResult from the state, replaces the prior with its current result, and runs the ordinary batch optimisation. Every part that uses the prior is then fitted as in a batch run, such as the clustering estimator, the constraint estimators, each uncertainty set and the inner optimisers of a meta-optimiser. Reading the prior does not change its state, so a fallback optimiser, which runs when the first one fails, reads the same prior.

PortfolioOptimisers.partial_fit! — Method
partial_fit!(opt::Union{JuMPOptimisationEstimator, HierarchicalRiskParity, HierarchicalEqualRiskContribution, SchurComplementHierarchicalRiskParity}, rd::ReturnsResult)
partial_fit!(opt::Union{JuMPOptimiser, BaseClusteringOptimisationEstimator, InverseVolatility, NestedClustered, BaseStackingOptimisationEstimator, BaseSubsetResamplingOptimisationEstimator}, rd::ReturnsResult)
partial_fit!(opt::Union{EqualWeighted, RandomWeighted, BestConstantRebalancedPortfolio}, rd::ReturnsResult)
partial_fit!(opt::PreviousWeights, rd::ReturnsResult)
partial_fit!(opt::FiniteAllocationOptimisationEstimator, rd::ReturnsResult)
partial_fit!(td::TD_OptE_Opt, rd::ReturnsResult)

Folds observations into an optimiser, and solves nothing.

The online step of an optimiser has two functions. partial_fit!(opt, rd) folds the observations and returns the estimator, and optimise(opt) with no returns reads the state out and solves once. The step forwards the observations to the prior alone, through fold_prior, and records the rest of rd in a ReturnsBufferState, so optimise(opt) with no returns can rebuild the ReturnsResult that the batch path reads. The step does not change the members above the prior: the clustering estimator, the constraint estimators, the uncertainty sets, and the inner optimisers of a meta-optimiser. optimise(opt) with no returns fits each of them as a batch run does.

The step takes a ReturnsResult, as optimise(opt, rd) does. rd holds one observation or a block of them, and the step unpacks it into the arity of the prior's step. A JuMP head forwards to the JuMPOptimiser it holds, and a hierarchical head to its HierarchicalOptimiser. The optimiser that holds the prior and the context takes the step. The three meta-optimisers and InverseVolatility hold their prior directly, so each takes its own step. EqualWeighted, RandomWeighted and BestConstantRebalancedPortfolio hold no prior but read the observations, so their context keeps the rows itself. PreviousWeights reads no observation and carries no context, but its fallback can read them. So its step folds rd into its fallback through step_fallback, and optimise(opt) with no returns reads that fallback out when w is nothing.

After t observations, optimise(opt) equals optimise(opt, rd[1:t]). The ReturnsResult that optimise(opt) with no returns rebuilds equals rd[1:t] field by field, in every frame that the readers of the optimiser state. The weights of a family that solves no programme agree to rounding, and the weights of a JuMP family agree to the tolerance of its solver.

Algorithm

  1. An optimiser that holds a prior refuses a ReturnsTracking anywhere in its tree, through assert_step_tracks_no_series.
  2. A JuMP head or a hierarchical head steps the bundle it holds in opt through fold_returns, under the source of its own group, see group_source. It rebuilds itself around the result.
  3. An optimiser that holds a prior steps through fold_returns.
  4. A head that holds no prior checks that rd.X is present. It then folds rd into its own context with fold_context, and the context keeps the returns and the factor column.
  5. PreviousWeights steps its fallback through step_fallback, and rebuilds itself around the result. It keeps w.

Arguments

  • opt: The optimiser to fold into.
  • rd: The returns data to fold, observations × assets. One row is one step of a walk-forward, and a block is a warm-up.

Validation

  • opt is not a finite allocation. An ArgumentError is thrown otherwise, because a finite allocation converts a weight vector and prices into share counts and reads no returns window.
  • opt is not a TimeDependent schedule of optimisers. An ArgumentError is thrown otherwise, because a schedule swaps the optimiser that carries the state.
  • No field in the tree of an optimiser that holds a prior holds a ReturnsTracking. An ArgumentError that names the field is thrown otherwise.
  • rd.X is not nothing. An IsNothingError is thrown otherwise.
  • Everything fold_prior and the step of ReturnsBufferState refuse.

Returns

  • opt: The optimiser, with its prior folded and its context recorded.

Related

source
PortfolioOptimisers.optimise — Method
optimise(opt::OptimisationEstimator; kwargs...)

Reads out a folded optimiser. It rebuilds the ReturnsResult from the state and solves once over it.

This method reads the state of the online step, and it is also the entry of every optimiser that takes no returns. The optimiser answers from what it holds. That is a prior that is already a result, which this entry has always served, or the state that its partial_fit! steps wrote. The ordinary optimise(opt, rd) then runs over the rebuilt ReturnsResult, and it fits the constraint estimators, the clustering, the uncertainty sets and the inner optimisers as a batch run does. A failed solve changes no state, so the fallback chain runs as in batch.

mr = MeanRisk(; opt = JuMPOptimiser(; pe = EmpiricalPrior(), slv = slv))for t in 1:warmup    mr = partial_fit!(mr, port_opt_view(rd, t:t, :))   # folds; solves nothingendres = optimise(mr)                                     # reads out; solves once

Algorithm

  1. Turn opt into a batch estimator and its rd with batch_from_state.
  2. Run optimise(opt, rd; kwargs...) on them.

Arguments

  • opt: The optimiser to read out.
  • kwargs...: The keyword arguments of the batch function, dims, str_names and save, passed on unchanged.

Validation

Returns

  • res::OptimisationResult: The result that the batch function gives over the observations folded so far.

Related

source
PortfolioOptimisers.optimise — Method
optimise(pw::PreviousWeights; kwargs...)

Reads out a PreviousWeights with no returns data, with or without a fallback.

The head holds w when w is set, as the batch verb does. With w unset the head fails, and the batch verb hands its returns data to the fallback. Here no returns data exists to hand on, so the fallback reads itself out from the rows that partial_fit! folded into it through step_fallback. So the head answers online as it answers in batch, also when it is the fallback of an OnlinePortfolioSelection head. A head with no fallback takes this method too, and read_out_fallback returns its Result as it is: the held weights, or a failure when w is nothing.

Algorithm

  1. Set every TimeDependent field of pw to its static default, with reset_time_dependent_estimator.
  2. Hold w with _optimise, giving res.
  3. Walk the fallback chain after res with read_out_fallback.

Arguments

  • pw: The hold-only head.
  • kwargs: The keyword arguments of optimise with no data on the fallback, passed on unchanged.

Validation

  • Everything that optimise(fb) with no data refuses on the fallback fb, when w is nothing.

Returns

  • res::OptimisationResult: The held weights, or the Result of the fallback chain when w is nothing.

Related

source