WalkForward: private API
PortfolioOptimisers.WalkForwardEstimator — Type
abstract type WalkForwardEstimator <: SequentialCrossValidationEstimatorAbstract supertype for all walk-forward cross-validation estimators.
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
PortfolioOptimisers.WalkForward_Onl — Type
const WalkForward_Onl = Union{WalkForwardEstimator, Online{<:WalkForwardEstimator}}Alias for a walk-forward scheme, plain or an Online Scheme.
An Online Scheme is an Online around a walk-forward, built by the scheme's function constructor — OnlineIndexWalkForward, OnlineDateWalkForward, OnlineHindsightSplit. It is not a WalkForwardEstimator by supertype, because a struct has one, so every bound that admits a walk-forward is written on this alias and the wrapped form reaches the same methods as the plain one. split, n_splits and fold_evaluation forward to the wrapped scheme; folds_are_stepped is where the two differ.
Related
PortfolioOptimisers.WFCVER — Type
const WFCVER = Union{WalkForward_Onl, WalkForwardResult}Alias for a walk-forward cross-validation scheme, plain or online, or its result.
Related
PortfolioOptimisers.OptCVE_Onl — Type
const OptCVE_Onl = Union{OptimisationCrossValidationEstimator, Online{<:WalkForwardEstimator}}Alias for the schemes that the cv field of a meta-optimiser takes: an optimisation cross-validation scheme, plain or an Online Scheme.
NestedClustered and Stacking bind their cv field to it, and predict_outer_returns dispatches on it. It takes no split result, because an estimator holds no result. An Online Scheme is an Online around a walk-forward, so it is not an OptimisationCrossValidationEstimator by supertype, and the alias names it.
Related
PortfolioOptimisers.DatesUnionPeriod — Type
const DatesUnionPeriod = Union{Dates.Period, Dates.CompoundPeriod}Alias for a Dates period or compound period.
Used internally to accept either simple date periods (e.g., Dates.Month(1)) or compound periods (e.g., Dates.Month(1) + Dates.Day(1)) as date offsets in walk-forward cross-validation.
Related
PortfolioOptimisers.DateAdjType — Type
const DateAdjType = Union{Function, DateAdjusterEstimator}Alias for a date adjustment function or estimator.
Matches either a plain Function or a DateAdjusterEstimator for adjusting dates in walk-forward cross-validation.
Related
PortfolioOptimisers.IntPeriodDateRange — Type
const IntPeriodDateRange = Union{Integer, DatesUnionPeriod}Alias for an integer or date period used to specify window sizes.
Matches either a plain integer (number of observations) or a date period (DatesUnionPeriod) for walk-forward cross-validation splits.
Related
PortfolioOptimisers.assert_positive_period — Function
assert_positive_period(
p::Union{Dates.CompoundPeriod, Period},
sym::Union{AbstractString, Symbol}
)
Assert that the period p moves a date forward.
A Dates.Period must be greater than zero. A Dates.CompoundPeriod must hold at least one positive part, so a form such as Month(1) - Day(1) passes. A compound period whose parts are all zero or negative moves no date forward.
Arguments
p: The period to check (DatesUnionPeriod).sym: Symbolic name used in the error message.
Validation
- A positive period, as above, which raises a
DomainErrorthat namessymandp.
Returns
nothing.
Related
PortfolioOptimisers.walk_forward_date_range — Function
walk_forward_date_range(ts::AbstractVector, period::DatesUnionPeriod,
period_offset::Option{<:DatesUnionPeriod}, adjuster::DateAdjType)Build the date range that a DateWalkForward estimator walks over the timestamps ts.
The range runs from the first to the last timestamp with a step of period. If period_offset is not nothing, the start moves to min(ts[1], ts[1] - period_offset) and the end moves to max(ts[end], ts[end] - period_offset). Then the adjuster is applied, and the offset is added to every date. This shifts each date by the offset and loses neither end of ts. A negative offset moves a date after ts[end] back into the data, so that date stays in the range.
Arguments
ts: Sorted timestamp vector (cv_timestamps).period: Step of the date range.period_offset: Offset applied to every date of the range, ornothing.adjuster: Function applied to the unshifted range, e.g. a business-day adjuster.
Returns
- The date range walked by
Base.splitandn_splits.
Related
PortfolioOptimisers.date_index_positions — Function
date_index_positions(ts::AbstractVector, date_range, previous::Bool,
::Type{T} = Int) where {T <: Integer}Map every date of date_range to a position in the sorted timestamp vector ts.
A date that falls between two timestamps maps to the previous timestamp if previous is true, and to the next timestamp if previous is false. A date before ts[1] maps to the first timestamp. The mapping stops at the first date that falls after ts[end], so the result is not longer than date_range.
Arguments
ts: Sorted timestamp vector (cv_timestamps).date_range: Date range fromwalk_forward_date_range.previous: Iftrue, a date between two timestamps takes the previous timestamp.T: Element type of the returned vector.
Returns
Vector{T}: Positions ints, in the order ofdate_range.
Related
PortfolioOptimisers.date_walk_forward_train_starts — Function
date_walk_forward_train_starts(train_size, ts::AbstractVector, date_range,
idx::AbstractVector{<:Integer})Return the row where the training window of each test start begins, one entry per position of idx, and zero where no training window fits inside the data.
An Integer train_size counts dates of the range, so the training window of the test that starts at the date in position p starts at the row of the date in position p - train_size. A Period train_size is subtracted from the date in position p, and the training window starts at the last timestamp on or before the result.
Arguments
train_size: Training size of theDateWalkForwardestimator.ts: Sorted timestamp vector (cv_timestamps).date_range: Date range fromwalk_forward_date_range.idx: Rows of the dates of the range, fromdate_index_positions.
Returns
Vector{<:Integer}: The first training row for each position ofidx, or zero.
Related
PortfolioOptimisers.date_walk_forward_bounds — Function
date_walk_forward_bounds(dwf::DateWalkForward, rd::Prices_RR)Compute the bounds of every fold that the date walk-forward dwf makes over the returns data rd.
Fold k trains on train_lo[k]:(test_lo[k] - purged_size - 1) and tests on test_lo[k]:test_hi[k]. train_lo already holds the start that expand_train sets. The first test starts at the first position whose training window starts inside the data (date_walk_forward_train_starts).
Two dates of the range can map to one timestamp, for example the two days of a weekend under a daily period over business-day data. A fold whose test window holds no observation for this reason is dropped. A fold that is kept must have at least one training observation after the purge.
Arguments
dwf: Date-based walk-forward estimator.rd: Returns-level or price-level data with timestamps (Prices_RR).
Validation
- The timestamp vector (
cv_timestamps) must not benothing. - Every fold that is kept must have a training window that is not empty after
expand_trainandpurged_size, else anIsEmptyErroris thrown before a fold is returned.
Returns
(train_lo, test_lo, test_hi): Three vectors with one entry per fold.
Related
PortfolioOptimisers.resolve_previous_weights_source — Function
resolve_previous_weights_source(pws::Nothing, wd::Nothing)
resolve_previous_weights_source(pws::Nothing, wd::AbstractWeightDrift)
resolve_previous_weights_source(pws::TargetWeights, wd::Option{<:AbstractWeightDrift})
resolve_previous_weights_source(pws::AbstractPreviousWeightsSource, wd::Option{<:AbstractWeightDrift})Resolve the Previous-Weights Source a fold loop runs, from the two switches of a walk-forward scheme.
An unset pws follows wd. If the series drifts, the fund holds the drifted weights at the end of the fold, so the trade it places next is the distance from them. A turnover charged against the targets while the series drifts, or the reverse, prices a trade that no one placed. So the two consistent pairs are no drift with the target weights, and a drift with the drifted weights. An explicit pws overrides the rule, for the two mixed pairs.
The fold loop reads nothing as the target weights. So the resolution maps TargetWeights to nothing, and every reader of the source keeps its two arms.
Algorithm
pwsandwdare both unset: givenothing, the target weights.pwsis unset andwdis set: giveDriftedWeights, andheld_weights_driftrunswd.pwsis aTargetWeights: givenothing, the target weights, whateverwdis.- Any other
pws: give it unchanged.
Arguments
pws: Previous-weights source of the scheme, ornothing.wd: Weight drift of the scheme, ornothing.
Returns
Option{<:AbstractPreviousWeightsSource}: The source the fold loop reads, wherenothingis the target weights.
Related