Base Cross Validation (b)
PortfolioOptimisers.fit_predict — Method
fit_predict(opt::OptE_Opt, rd::ReturnsResult)Fit optimisation estimator opt on returns data rd, and predict over the same data.
The prediction is in-sample, so it measures the fit and not a backtest. Use fit_and_predict or cross_val_predict for an out-of-sample series.
Algorithm
Arguments
opt: Optimisation estimator or result.rd::ReturnsResult: Returns data.
Returns
Related
StatsAPI.predict — Method
predict(res::NonFiniteAllocationOptimisationResult, rd::ReturnsResult)
predict(res, rd, test_idx, cols = :)
predict(res, rd, test_idxs::VecVecInt, cols = :)Apply an optimisation result res to returns data rd to produce a PredictionResult or a vector of prediction results.
When test_idx is given, only the rows of rd that test_idx indexes, and the columns that cols selects, are used for the prediction.
The fee holds no horizon. charge_fees receives the length of the series it charges, so a fold spreads a one-off cost over its own observations and the whole-sample method spreads it over the whole sample. fees.fa names the clock alone: a nothing or FirstObservationFees charges the two fixed terms on the first observation of the series, and an AmortisedFees spreads them evenly over it. The fa keyword overrides that clock for the series this method builds, and changes nothing that the result holds. A report can then charge a fixed fee as a fund saw it, while the optimiser prices the same fee as its own objective needs.
A test window over a point-in-time universe holds two kinds of gap. The column of a non-investable asset has weight 0, and the view at the Investable Mask removes it before anything reads it. A Held Gap is an (observation, asset) pair at which the weight is not zero and the return is missing, which an asset that delists inside the test window makes. The mask comes from the fit and cannot see it. Nothing is renormalised, so the missing weight is held in cash on that observation. Without a drift, the series of the fold is
returns[t] == sum_i w_i * (isfinite(X[t, i]) ? X[t, i] : 0) - feewith the fee taken over the whole weight vector. Under a drift, w_i is the weight of asset i on the weight path at observation t.
A failed fold carries NaN weights, so its series is NaN and res.w and rd.X show the failure to a scorer. Under a drift the fold still held a book. held_start_weights starts the drift from the previous weights w_prev, member by member under a population, so the next fold reads that book. With no w_prev, which is fold 1 or a scheme whose folds are not a timeline, the record is NaN and nothing throws.
Algorithm
- For the index methods, view the rows
test_idxand the columnscolsofrdwithport_opt_view. - View the weights and the window at the Investable Mask of
reswithinvestable_fold_view. The fee of the result is on the investable universe already, and is not viewed. - Override the clock of the fee with
fathroughoverride_fee_amortisation. - Set every non-finite entry of the window to zero with
filter_held_gaps, givingXf. A Held Gap warns, or raises anArgumentErrorunderstrict, throughstrict_diagnostic. - Compute the net return series from the weights,
Xf, the fee andwdwithcalc_net_returns. - Choose the weights the drift starts from with
held_start_weights, from the return code, the weights andw_prevviewed at the mask. - Drift them over
Xfunderhwdwithheld_weights_result, giving the record and the ruined members. - Warn about the ruined members with
warn_ruined_members, and mark them failed withmark_ruined_members. - Collapse the data aligned to the series with
reconstruct_rd. - Expand the record to the caller's universe with
expand_held_weights, because the turnover of the next fold reads it, and build thePredictionResult. The index methods recordtest_idxin itsidx, and the whole-sample method recordsnothing, because it cannot know which rows of a larger returns datardholds.
The method over test_idxs runs the method over one test_idx for each fold.
Arguments
res::NonFiniteAllocationOptimisationResult: Fitted optimisation result.rd::ReturnsResult: Returns data for the prediction period.test_idx: Observation index or vector of observation indices for the test fold.cols: Column selector. Defaults to:(all assets).
Keyword Arguments
wd::Option{<:AbstractWeightDrift} = nothing: The Weight Drift of the series, ornothingfor a series formed from the target weights.hwd::Option{<:AbstractWeightDrift} = wd: The drift of the Held Weights record, ornothingfor no record. The schemes passheld_weights_drift(wd, pws), so a Previous-Weights Source records the held weights of a series that is not drifted.fa::Option{<:AbstractFeeAmortisation} = nothing: The clock the series charges the two fixed fee terms on, ornothingto inherit the clock the fee itself states.store_weight_path::Bool = false: Whether the record stores the weight path, in place of rebuilding it on demand.strict::Bool = false: Whether a Held Gap raises anArgumentErrorrather than warning.w_prev::Option{<:VecNum_VecVecNum} = nothing: The previous weights the fold was handed, which a failed fold holds under a drift, ornothing.
Returns
PredictionResultor vector ofPredictionResult.
Related
PortfolioOptimisers.folds_are_time_ordered — Function
folds_are_time_ordered(cv)Return true if the fold enumeration of a cross-validation scheme is a timeline.
This is one half of the conjunction fold_loop computes, so a scheme states for itself whether its folds carry history. A walk-forward, a multiple-randomised path, and any scheme with no more specific method enumerate their folds in time order. Fold i has fold i - 1 behind it, so the loop may run the folds in order and thread the previous fold's weights. needs_previous_weights is the other half, and it decides whether the loop does so.
A NonSeqCVER scheme answers false. A k-fold and a combinatorial enumeration are not timelines: each fold is independent of the others, so no fold has a previous fold, and the loop runs them in parallel. A KFold training window holds rows that follow its test window, so a quantity measured against another fold's weights is not a backtest reading.
The method is per type and takes the scheme itself, so inference reads the answer from the type of cv and never needs the value. The fallback over ::Any also answers true for nothing, which is what fold_loop receives from a call site that holds no scheme.
Related