Skip to content
13

WalkForward

PortfolioOptimisers.WalkForwardEstimator Type
julia
abstract type WalkForwardEstimator <: SequentialCrossValidationEstimator

Abstract supertype for all walk-forward cross-validation estimators in PortfolioOptimisers.jl.

Walk-forward estimators split time series data into sequential training and testing windows, advancing the test window forward at each step. Subtypes implement index-based or date-based walk-forward schemes.

Related

source
PortfolioOptimisers.WalkForwardResult Type
julia
struct WalkForwardResult{__T_train_idx, __T_test_idx} <: SequentialCrossValidationResult

Result type produced by WalkForwardEstimator subtypes after splitting time series data.

Stores the train and test index vectors for each fold of the walk-forward cross-validation.

Fields

  • train_idx: Vector of training index ranges for each fold.

  • test_idx: Vector of testing index ranges for each fold.

Related

source
PortfolioOptimisers.IndexWalkForward Type
julia
struct IndexWalkForward{__T_train_size, __T_test_size, __T_purged_size, __T_expand_train, __T_reduce_test} <: WalkForwardEstimator

Implements index-based walk-forward cross-validation for time series, supporting purging and flexible train/test windowing.

Fields

  • train_size: Number of observations in each training set.

  • test_size: Number of observations in each test set.

  • purged_size: Number of observations to exclude from the end of each train set adjacent to a test set.

  • expand_train: If true, training set always starts from the beginning; otherwise, uses a rolling window.

  • reduce_test: If true, reduces the last test set to fit remaining data; otherwise, drops incomplete test sets.

Constructors

julia
IndexWalkForward(
    train_size::Integer,
    test_size::Integer;
    purged_size::Integer = 0,
    expand_train::Bool = false,
    reduce_test::Bool = false,
) -> IndexWalkForward

Positional and keyword arguments correspond to the struct's fields.

Validation

  • train_size, test_size, and purged_size must be non-empty, non-negative, and finite.

  • Ensures train_size + purged_size < T where T is the total number of observations.

Examples

julia
julia> IndexWalkForward(100, 20; purged_size = 5, expand_train = true, reduce_test = false)
IndexWalkForward
    train_size ┼ Int64: 100
     test_size ┼ Int64: 20
   purged_size ┼ Int64: 5
  expand_train ┼ Bool: true
   reduce_test ┴ Bool: false

Related

source
Base.split Method
julia
Base.split(iwf::IndexWalkForward, rd::ReturnsResult) -> WalkForwardResult

Split the returns data rd into sequential walk-forward folds using integer observation indices. Each fold advances the test window by test_size observations.

Arguments

  • iwf::IndexWalkForward: Index-based walk-forward cross-validation estimator.

  • rd::ReturnsResult: Returns data to split.

Returns

  • WalkForwardResult: Result containing train and test index ranges for each fold.

Related

source
PortfolioOptimisers.DateWalkForward Type
julia
struct DateWalkForward{__T_train_size, __T_test_size, __T_period, __T_period_offset, __T_purged_size, __T_adjuster, __T_previous, __T_expand_train, __T_reduce_test} <: WalkForwardEstimator

Implements date-based walk-forward cross-validation for time series, supporting flexible windowing, purging, and custom date adjustment.

Fields

  • train_size: Number of periods or length of training window (integer or date period).

  • test_size: Number of observations in each test set.

  • period: Step size for each split.

  • period_offset: Optional offset to shift the split dates.

  • purged_size: Number of observations to exclude from the end of each train set adjacent to a test set.

  • adjuster: Function or estimator to adjust the date range (e.g., for business days).

  • previous: If true, allows test indices to use previous available date if exact match not found, else use the next available date.

  • expand_train: If true, training set always starts from the beginning; otherwise, uses a rolling window.

  • reduce_test: If true, reduces the last test set to fit remaining data; otherwise, drops incomplete test sets.

Constructors

julia
DateWalkForward(
    train_size::IntPeriodDateRange,
    test_size::Integer;
    period::DatesUnionPeriod = Dates.Day(1),
    period_offset::Option{<:DatesUnionPeriod} = nothing,
    purged_size::Integer = 0,
    adjuster::DateAdjType = identity,
    previous::Bool = false,
    expand_train::Bool = false,
    reduce_test::Bool = false,
) -> DateWalkForward

Positional and keyword arguments correspond to the struct's fields.

Validation

  • test_size and purged_size must be non-empty, non-negative, and finite.

  • If train_size is an integer, it must be non-empty, non-negative, and finite.

Examples

julia
julia> DateWalkForward(252, 21; period = Dates.Day(1), purged_size = 5, expand_train = true)
DateWalkForward
     train_size ┼ Int64: 252
      test_size ┼ Int64: 21
         period ┼ Dates.Day: Dates.Day(1)
  period_offset ┼ nothing
    purged_size ┼ Int64: 5
       adjuster ┼ typeof(identity): identity
       previous ┼ Bool: false
   expand_train ┼ Bool: true
    reduce_test ┴ Bool: false

Related

source
Base.split Method
julia
Base.split(dwf::DateWalkForward{<:Integer}, rd::ReturnsResult) -> WalkForwardResult

Split the returns data rd into sequential walk-forward folds using date-aligned indices, where train_size is specified as an integer number of date-range steps.

The timestamp vector rd.ts must not be nothing. Training and test windows are aligned to the period date range and advanced by test_size steps at a time.

Arguments

  • dwf::DateWalkForward{<:Integer}: Date-based walk-forward estimator with an integer train_size.

  • rd::ReturnsResult: Returns data containing the matrix X and timestamp vector ts.

Returns

  • WalkForwardResult: Result containing train and test index ranges for each fold.

Related

source
Base.split Method
julia
Base.split(dwf::DateWalkForward{<:Any}, rd::ReturnsResult) -> WalkForwardResult

Split the returns data rd into sequential walk-forward folds using date-aligned indices, where train_size is specified as a date Period (e.g., Dates.Month(6)).

The timestamp vector rd.ts must not be nothing. Training windows are defined by subtracting train_size from the split date, allowing calendar-based window lengths.

Arguments

  • dwf::DateWalkForward{<:Any}: Date-based walk-forward estimator with a Period train_size.

  • rd::ReturnsResult: Returns data containing the matrix X and timestamp vector ts.

Returns

  • WalkForwardResult: Result containing train and test index ranges for each fold.

Related

source
PortfolioOptimisers.n_splits Function
julia
n_splits(cv, rd::ReturnsResult)
n_splits(cv)

Return the number of cross-validation splits (folds) that would be produced by cv for the given returns data rd.

Arguments

Returns

  • Integer: The number of folds.

Related

source
julia
n_splits(dwf::DateWalkForward{<:Integer}, rd::ReturnsResult) -> Integer

Return the number of walk-forward folds that would be produced by dwf for the given returns data rd when the training window size is specified as an integer number of date-range steps.

Arguments

  • dwf::DateWalkForward{<:Integer}: Date-based walk-forward estimator with an integer train_size.

  • rd::ReturnsResult: Returns data containing a timestamp vector ts.

Returns

  • Integer: The number of folds.

Related

source
julia
n_splits(dwf::DateWalkForward{<:Any}, rd::ReturnsResult) -> Integer

Return the number of walk-forward folds that would be produced by dwf for the given returns data rd when the training window size is specified as a date Period.

Arguments

  • dwf::DateWalkForward{<:Any}: Date-based walk-forward estimator with a Period train_size.

  • rd::ReturnsResult: Returns data containing a timestamp vector ts.

Returns

  • Integer: The number of folds.

Related

source
PortfolioOptimisers.n_splits Method
julia
n_splits(dwf::DateWalkForward{<:Integer}, rd::ReturnsResult) -> Integer

Return the number of walk-forward folds that would be produced by dwf for the given returns data rd when the training window size is specified as an integer number of date-range steps.

Arguments

  • dwf::DateWalkForward{<:Integer}: Date-based walk-forward estimator with an integer train_size.

  • rd::ReturnsResult: Returns data containing a timestamp vector ts.

Returns

  • Integer: The number of folds.

Related

source
PortfolioOptimisers.n_splits Method
julia
n_splits(dwf::DateWalkForward{<:Any}, rd::ReturnsResult) -> Integer

Return the number of walk-forward folds that would be produced by dwf for the given returns data rd when the training window size is specified as a date Period.

Arguments

  • dwf::DateWalkForward{<:Any}: Date-based walk-forward estimator with a Period train_size.

  • rd::ReturnsResult: Returns data containing a timestamp vector ts.

Returns

  • Integer: The number of folds.

Related

source