Resume
The MultiPeriodPredictionResult of an online walk-forward stores the estimator as it was after the last training window. To continue the run, pass Resume(res) to cross_val_predict in place of the estimator, with the same walk-forward and the full history with the new rows at its end. The walk-forward skips every fold up to the one whose training window ends at the last row of the stored state. It adds the rows after that point to a copy of the state, and continues from the next fold. Each fold then gets the same weights that one run over the longer history gives. The result holds the new folds only, and vcat stacks a run and its resumes, so you can score them together.
PortfolioOptimisers.Resume — Type
struct Resume{__T_res, __T_opt} <: AbstractResultContinues an online walk-forward from the folds that its Result holds, over a longer history.
Resume is a transient declaration in the estimator slot, like Online and TimeDependent. The MultiPeriodPredictionResult of an online run carries in opt the estimator that the fold loop threaded, folded through the last training end. Resume(res) hands that Result back to cross_val_predict, with the same scheme and the full history extended, which is the old returns data with the new rows appended. The loop skips the folds that the Result holds, folds the new rows into a copy of res.opt, and runs the folds after them. No warm-up runs. The resumed Result holds the new folds only and carries opt again, so it can be resumed in turn, and vcat stacks the two Results for scoring.
The oracle is the one-shot run. For an online scheme cv, a history rd_T and its extension rd_{T+k},
res = cross_val_predict(mr, rd_T, cv)res2 = cross_val_predict(Resume(res), rd_{T+k}, cv)vcat(res.pred, res2.pred) ≈ cross_val_predict(mr, rd_{T+k}, cv).pred # fold for foldThe weights agree to the tolerance of the moment layer and of the solver. The training returns that the call with no data rebuilds from the state agree exactly, under a cap too. Resume copies every state at entry, so res is never written. One Result can be resumed any number of times, and a terminal view does not change a later continuation.
cross_val_predict refuses a resume in five cases, each with an ArgumentError that names the cause.
- The scheme is not an Online Scheme.
rdadds no fold.rdor the state holds no timestamps, or the timestamps that the state holds differ from their rows ofrd. An index-only caller attaches a synthetic calendar,ts = Date(1) .+ Day.(0:(T - 1)).- The last fold of the Result was partial. With
reduce_test = truethat fold predicted the rows left at the end of the old history, and in the longer history those rows are the first part of a full window. The fold can be neither skipped nor completed. Resume the same Result withreduce_test = truefor the live view of the leftover rows, and resume it again withreduce_test = falsewhen the rows arrive. - The head is an
OnlinePortfolioSelectionwhose fees carry a turnover term, and the scheme has no Previous-Weights Source. The one-shot online arm refuses the same head.
The timestamp check reads the training ends of the scheme. It refuses a scheme with a different window, step or purge, because such a scheme moves the training ends. A change of the Weight Drift or of the Previous-Weights Source moves no training end, so the check does not see it. The new folds then run under the scheme handed, and the identity above does not hold. Under a cap the state holds its last rows only, and the check pins those rows and nothing before them.
The previous weights come from the Result alone. When no fold of res passes its weights on, for example because every fold of a short resumed Result failed, the first new fold gets no previous weights. The one-shot run hands it the last threadable fold of the earlier run instead. Resume vcat(old, res) in that case, because the stacked Result holds the earlier folds too.
Every state is an immutable struct of arrays, names, integers and a timestamp vector. The Result of an online run therefore round-trips through the stdlib Serialization as it stands, and the deserialised Result resumes to the same weights. That format is bound to the Julia version that wrote it, and the package gives no other format.
The constructor refuses a batch Result and a PopulationPredictionResult from a MultipleRandomised run. cross_val_predict refuses a scheme that is not a walk-forward, a walk-forward that is not an Online Scheme, and a search. A Pipeline resumes on the same terms, through the state that its row owner keeps.
Fields
res: The Result of the online run to continue, carrying the threaded estimator inopt.
opt: The estimator thatrescarries inopt, the same object. The Result does not carry the type of its estimator in a type parameter, soResumecarries it here, and the methods for an optimiser and for aPipelinedispatch on it.
Constructors
Resume(res::MultiPeriodPredictionResult)Resume(; res::MultiPeriodPredictionResult)opt is a derived field, so Accessors.set reads it again from the new res, and it refuses a patch of opt.
Validation
res.optis notnothing. AnArgumentErroris thrown otherwise, because a batch run threads no estimator.resis not aPopulationPredictionResult. AnArgumentErroris thrown otherwise.
Related
PortfolioOptimisers.cross_val_predict — Method
cross_val_predict(r::OptimiserResume, rd::ReturnsResult, cv::CVER; cols = :, ex = FLoops.ThreadedEx())
fit_and_predict(r::OptimiserResume, rd::ReturnsResult, cv::CVER, cv_res = split(cv, rd); cols = :, ex = FLoops.ThreadedEx(), id = nothing)Continues an online walk-forward from a Result, over the full history extended.
The methods that take an optimiser in the estimator slot take a Resume too. cross_val_predict views rd by cols as the one-shot method views it, but it leaves the estimator of the Result as it is. That estimator was threaded over the view of the old run already, and its state refuses a different view at the first step. The Result that the methods return holds the new folds only.
Algorithm
- For
cross_val_predictonly, runassert_internal_optimiserandassert_external_optimiseronr.opt, and viewrdbycolswhencolsis not:. - Refuse a scheme that a
Resumecannot re-enter, throughassert_resume_scheme. - Read
train_idxandtest_idxfrom the split ofrdbycv, whichfit_and_predicttakes ascv_res, and refuse shuffled folds throughassert_unshuffled_folds. - Read the evaluation settings of
cvthroughfold_evaluation, givingwd,pws,fa,store_weight_pathandstrict, and the drift of the held weights throughheld_weights_drift, givinghwd. - Run
fold_looponr, which takes the resumed arm ofonline_folds. Each fold calls the one-foldfit_and_predictwith its estimator, its test window, its previous weights and the settings of step 4, givingpredictionsand the threadedest. - Return a
MultiPeriodPredictionResultofpredictions, withid, and withestasopt.
Arguments
r: The declaration, holding the Result to continue.rd: The returns result to use.cv: The scheme that the Result came from, an Online Scheme.cv_res: The split of the data bycv, aCrossValidationResult. The default splits the data argument bycv. A caller that runs many estimators over one scheme splits once and passes the split to each call. Then every estimator scores the same folds, and a scheme that draws at random draws once. The method still reads the evaluation settings and the fold predicates ofcv.cross_val_predicttakes nocv_res, because it viewsrdbycolsbefore the split.cols: The asset view.cross_val_predictviewsrdby it.fit_and_predicthands it to every fold, which views its estimator and its prediction by it.ex: Not read, because the resumed arm runs the folds in order.id: The identifier that the Result carries.
Validation
- Everything that
assert_resume_schemeand the resumed arm ofonline_foldsrefuse. - For
cross_val_predict, everything thatassert_internal_optimiserandassert_external_optimiserrefuse onr.opt.
Returns
res::MultiPeriodPredictionResult: The new folds, carrying the threaded estimator.
Related
PortfolioOptimisers.search_cross_validation — Method
search_cross_validation(::Resume, ::AbstractSearchCrossValidationEstimator, ::Any)Refuses a Resume in a search, by name.
A search scores every candidate through one fold loop. A resumed search would need one Result per candidate, and the Result of a search does not carry them. The message names the resume of the walk-forward of the chosen candidate instead.
Validation
- A search never takes a
Resume. AnArgumentErroris always thrown.
Related
Base.vcat — Method
Base.vcat(a::MultiPeriodPredictionResult, b::MultiPeriodPredictionResult)Stacks the folds of a run and of its resume into one Result, for scoring.
The stacked Result carries the id of a and the opt of b, which is the estimator that the later run threaded, so it can be resumed too. The two Results must abut in time. The check reads the timestamps of the stacked return series, so a pair that overlaps and a pair from runs without timestamps are refused with an error that names the cause.
Algorithm
- Read the timestamps of the stacked return series, giving
ta = a.mrd.tsandtb = b.mrd.ts. Refuse when either isnothing. - Refuse unless the last entry of
tais before the first entry oftb. - Build a
MultiPeriodPredictionResultfromvcat(a.pred, b.pred), theidofaand theoptofb. Its constructor stacksmrdagain from the folds.
Arguments
a: The earlier Result.b: The later Result, a resume ofa.
Validation
- Both carry timestamps, and
aends beforebstarts. AnArgumentErroris thrown otherwise.
Returns
res::MultiPeriodPredictionResult: The stacked Result.
Related