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

  1. Fit opt on rd with optimise, giving res.
  2. Predict res over the whole of rd with predict.

Arguments

  • opt: Optimisation estimator or result.
  • rd::ReturnsResult: Returns data.

Returns

Related

source
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) - fee

with 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

  1. For the index methods, view the rows test_idx and the columns cols of rd with port_opt_view.
  2. View the weights and the window at the Investable Mask of res with investable_fold_view. The fee of the result is on the investable universe already, and is not viewed.
  3. Override the clock of the fee with fa through override_fee_amortisation.
  4. Set every non-finite entry of the window to zero with filter_held_gaps, giving Xf. A Held Gap warns, or raises an ArgumentError under strict, through strict_diagnostic.
  5. Compute the net return series from the weights, Xf, the fee and wd with calc_net_returns.
  6. Choose the weights the drift starts from with held_start_weights, from the return code, the weights and w_prev viewed at the mask.
  7. Drift them over Xf under hwd with held_weights_result, giving the record and the ruined members.
  8. Warn about the ruined members with warn_ruined_members, and mark them failed with mark_ruined_members.
  9. Collapse the data aligned to the series with reconstruct_rd.
  10. Expand the record to the caller's universe with expand_held_weights, because the turnover of the next fold reads it, and build the PredictionResult. The index methods record test_idx in its idx, and the whole-sample method records nothing, because it cannot know which rows of a larger returns data rd holds.

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, or nothing for a series formed from the target weights.
  • hwd::Option{<:AbstractWeightDrift} = wd: The drift of the Held Weights record, or nothing for no record. The schemes pass held_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, or nothing to 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 an ArgumentError rather than warning.
  • w_prev::Option{<:VecNum_VecVecNum} = nothing: The previous weights the fold was handed, which a failed fold holds under a drift, or nothing.

Returns

Related

source
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

source