Time-dependent: private API
PortfolioOptimisers.TD_Option — Type
const TD_Option{X} = Union{Nothing, TimeDependent, X}Alias for an optimiser field that takes nothing, a static value of type X, or a TimeDependent schedule.
The fields whose constructor signatures use this alias, or TD, are the optimiser inputs that can change from fold to fold. No other list of them exists.
Related
PortfolioOptimisers.TD — Type
const TD{X} = Union{TimeDependent, X}Alias for a required optimiser field that takes a static value of type X or a TimeDependent schedule, but not nothing.
The prior estimator, the returns model, the scalariser, the clustering estimator and the weight finaliser always hold a value. They use this alias and not TD_Option, so they do not take nothing. Such a field has a static default, so a solve with no fold resets a schedule in it to that default. A field that holds an optimiser has no static default, see TD_OptE_Opt.
Related
PortfolioOptimisers.TD_OptE_Opt — Type
const TD_OptE_Opt = Union{TimeDependent{<:AbstractVector{<:OptE_Opt}},
TimeDependent{<:TimeDependentOptimiserCallable},
TimeDependent{<:PreviousWeightsFunction},
TimeDependent{<:Base.Callable}}Alias for the TimeDependent forms that a field which holds an optimiser takes, where the schedule changes the optimiser itself.
The type of two forms states their value before any fold runs. The first is a vector whose entries are all OptE_Opt. An entry is an optimiser or a precomputed result, so a mixed schedule optimises at some folds and predicts at others. The second is a TimeDependentOptimiserCallable.
The other two forms are a bare function ctx -> optimiser and a PreviousWeightsFunction that holds one. Their value is not known before they run, so it is checked when the fold loop puts it into place. In a field of an estimator, the keyword constructor of the estimator checks it. When the schedule is the optimiser itself, assert_time_dependent_optimiser checks it.
A field that holds an optimiser is required, and it has no static default. So a schedule in it must set default, see NoDefault and TimeDependentDefaultError.
Related
PortfolioOptimisers.TDO_Option — Type
const TDO_Option{X} = Union{Nothing, TDO_OptE_Opt, X}Alias for an optional field that holds an optimiser, such as a fallback.
The field takes nothing, a static value of type X, or a schedule of optimisers whose entries can be nothing, see TDO_OptE_Opt. A required field that holds an optimiser does not take nothing, so its signature writes Union{X, TD_OptE_Opt}.
Related
PortfolioOptimisers.OptE_TD — Type
const OptE_TD = Union{NonFiniteAllocationOptimisationEstimator, TD_OptE_Opt}Alias for an optimisation estimator, or a TimeDependent schedule in the place of one.
The cross-validation fold loops that fit take this type. A schedule given directly to cross_val_predict is the optimiser, and fold i runs entry i. A bare precomputed result is not a member, because it takes the path that only predicts, which has no fold loop to resolve a schedule against. A schedule whose entries are results is a member, and each such entry predicts at its fold, see OptE_Opt_TD.
Related
PortfolioOptimisers.OptE_Opt_TD — Type
const OptE_Opt_TD = Union{OptE_Opt, TD_OptE_Opt}Alias for an optimisation estimator, a precomputed result, or a TimeDependent schedule in the place of one.
The fold loops that take a precomputed result as well as an estimator take this type. The entries of a schedule are OptE_Opt, so a mixed schedule is a member. Fold i optimises when entry i is an estimator, and predicts when entry i is a result. The methods of fit_and_predict select the path by dispatch.
Related
PortfolioOptimisers.VecOptE_Opt_TD — Type
const VecOptE_Opt_TD = AbstractVector{<:OptE_Opt_TD}Alias for a vector of optimisation estimators or results in which an entry can be a TimeDependent schedule.
A field needs it when a fold loop reads each entry of the vector as an optimiser, one entry at a time. Stacking.opti is such a field, because its inner cross-validation runs once for each candidate. The alias contains VecOptE_Opt, so each method that takes it also takes a vector with no schedule.
Related
PortfolioOptimisers.TD_VecOptE_Opt — Type
const TD_VecOptE_Opt = Union{TimeDependent{<:AbstractVector{<:VecOptE_Opt_TD}},
TimeDependent{<:TimeDependentOptimiserCallable},
TimeDependent{<:PreviousWeightsFunction},
TimeDependent{<:Base.Callable}}Alias for the TimeDependent forms that a field which holds a vector of optimisers takes, such as Stacking.opti.
A vector schedule holds one vector of optimisers for each fold, and a function returns the vector of its fold. Entry i is the complete vector of candidates at fold i, so the schedule changes the whole set of candidates from fold to fold. An element of an entry can also be a schedule, a VecOptE_Opt_TD, which the inner fold loop of the optimiser resolves. A schedule of the whole field takes only bind = :outermost. The constructor of the optimiser states the reason.
Related
PortfolioOptimisers.TDO_OptE_Opt — Type
const TDO_OptE_Opt = Union{TD_OptE_Opt,
TimeDependent{<:AbstractVector{<:Option{<:OptE_Opt}}}}Alias for the TimeDependent forms that an optional field which holds an optimiser takes, such as a fallback.
It holds every form of TD_OptE_Opt, and a vector whose entries can be nothing. An optional field takes nothing as a static value, and an entry nothing gives the field nothing at that fold. So an optional field takes TimeDependent([mr, nothing]), a fallback that is off at some folds. A required field that holds an optimiser never takes nothing, so it keeps the bound TD_OptE_Opt.
Related
PortfolioOptimisers.VecOptE_Opt — Type
const VecOptE_Opt = AbstractVector{<:OptE_Opt}Alias for a vector of continuous optimisation estimators or results.
A method that reads many optimisers, such as the candidates of a meta-optimiser, dispatches on it.
Related
PortfolioOptimisers.assert_special_nco_requirements — Method
assert_special_nco_requirements(
opt::AbstractVector{<:Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult, TimeDependent{<:AbstractVector{<:Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult}}}, TimeDependent{<:TimeDependentOptimiserCallable}, TimeDependent{<:PreviousWeightsFunction}, TimeDependent{<:Union{Function, Type}}}}
)
Checks the requirements of an inner optimiser of NestedClustered for each element of a vector of optimisers.
Related
PortfolioOptimisers.assert_nearest_optimiser_schedule — Function
assert_nearest_optimiser_schedule(x, field::Symbol, cv, opt_name::Symbol)Checks a TimeDependent with bind = :nearest in a field that holds an optimiser and that the inner cross-validation of the optimiser reads.
Two parts of the optimiser read such a field. The inner cross-validation resolves the schedule at each fold. The solve on the full sample, which is the fit of wi of a meta-optimiser or the optimisation of each cluster, always resolves it to its default. So the constructor needs two conditions, which the validation below states. The first condition applies to this field only. Elsewhere a schedule with no default is valid at construction.
The function checks nothing when x is not a TimeDependent with bind = :nearest.
Validation
x.defaultis notNoDefault, else aTimeDependentDefaultErroris thrown. With nodefault, every solve throws when the solve on the full sample reads the schedule.cv !== nothing, else anArgumentErroris thrown. With no inner cross-validation, the schedule always takes itsdefault, and it has no effect.
Related
PortfolioOptimisers.inner_fold_fields — Function
inner_fold_fields(opt)Returns the names of the fields of opt that the optimiser gives to a fold loop of its own.
For such a field, the fold loop that reaches the optimiser is not the nearest fold loop. The fallback returns an empty tuple, because an ordinary optimiser opens no inner fold loop. Then the loop that reaches it is the outermost and the nearest loop for every field.
A meta-optimiser whose inner cross-validation reads a field directly names that field here. For example, NestedClustered names opti, which it runs once for each cluster as cross_val_predict(opti, …; cols = cl). Then time_dependent_fields leaves a schedule with bind = :nearest in that field for the inner loop, and so do the update, the reset and the check of the fold count. Without this rule, the reset at the start of _optimise replaces such a schedule with its default before the inner cross-validation reads it.
Related
PortfolioOptimisers.time_dependent_value — Function
time_dependent_value(
td::TimeDependent,
ctx::TimeDependentContext
) -> Any
Resolves a schedule to its value at the fold that ctx describes.
Algorithm
- When
td.valis a vector, return its entryctx.i. - When
td.valis aPreviousWeightsFunction, returntd.val.f(ctx). - Else return
td.val(ctx).
Related
PortfolioOptimisers.typed_time_dependent_fields — Function
typed_time_dependent_fields(opt)Returns the names of the fields of opt whose type can hold a TimeDependent.
The type of a field decides whether it can hold a schedule. A constructor that takes a schedule, see TD_Option, records it in a type parameter. So a field that holds no schedule has a type whose intersection with TimeDependent is empty.
A generated function computes the tuple once for each type, from the field types, and no list of fields is written by hand. time_dependent_candidate_fields returns this tuple, and an owner of a ConstraintBlock adds the slots of its block to it.
Related
PortfolioOptimisers.time_dependent_candidate_fields — Function
time_dependent_candidate_fields(opt)Returns the names under which opt can hold a TimeDependent.
time_dependent_fields reads the values of these names only. The fallback returns the fields of typed_time_dependent_fields. An owner of a ConstraintBlock, such as JuMPOptimiser, also returns the slots of its block that can hold a schedule, under their flat names. time_dependent_slot reads such a name, and rebuild_struct passes it to the keyword constructor of the owner, which replaces that slot of the block.
For a JuMPOptimiser that holds no schedule, the tuple is empty at compile time, so split and _optimise read none of its fields.
Related
PortfolioOptimisers.time_dependent_slot — Function
time_dependent_slot(opt, f::Symbol)Returns the value that opt holds under the name f of time_dependent_candidate_fields.
The fallback reads the field f. An owner of a ConstraintBlock reads a slot of its block under the flat name of the slot, as opt.wb reads it. The walks of the schedules read every value through this function, so a schedule in a slot of the block resolves, resets and counts its folds as a schedule in a field does.
Related
PortfolioOptimisers.time_dependent_fields — Function
time_dependent_fields(opt, all_binds::Bool = true)Returns the names of the fields of opt that hold a TimeDependent which the current fold loop resolves.
The function reads only the fields whose type can hold a schedule, see time_dependent_candidate_fields. So an optimiser that holds no schedule returns an empty tuple, and the function reads none of its fields.
The argument all_binds states a fact about the position of the fold loop, and the field bind of a schedule cannot state it. Take an outer fold loop that runs a meta-optimiser, whose inner cross-validation runs an estimator with a schedule that has bind = :nearest. Both loops reach that field. The outer loop must pass through the meta-optimiser, because that is how it resolves the schedules with bind = :outermost of the inner estimator. It must leave the schedule with :nearest, because it is not the nearest loop. The inner loop reaches the same estimator directly, and it must resolve that schedule.
So every ordinary fold loop passes all_binds = true. Such a loop is the outermost and the nearest loop, so it resolves every schedule that remains. A meta-optimiser passes false when it recurses into the estimators of its own inner cross-validation.
inner_fold_fields refines the rule for each field. With all_binds = true, a schedule with :nearest in a field that the optimiser gives to its own inner fold loop stays in place, see entitled.
Arguments
opt: The optimiser.all_binds::Bool = true: Whentrue, return each field that holds a schedule, except the schedules with:nearestin the fields ofinner_fold_fields. Whenfalse, return only the fields whose schedule hasbind === :outermost.
Returns
fns::Tuple: The names of the fields.
Related
PortfolioOptimisers.time_dependent_entries — Function
time_dependent_entries(td::TimeDependent) -> Any
Returns the values of a TimeDependent that are known before any fold runs.
They are the entries of a vector schedule, and default when it is set. A function schedule gives no entry, because its values do not exist before it runs.
Related
PortfolioOptimisers.schedule_reads_previous_weights — Function
schedule_reads_previous_weights(x)Returns true if a schedule in x reads the weights of the previous fold from its context.
needs_previous_weights answers a wider question. It also counts a turnover, a fee, a tracking input, a cost budget or a risk measure that factory gives the previous weights. This function counts only a schedule that reads ctx.w_prev itself, see TimeDependentContext. An estimator whose factory receives its weights from a source other than the fold loop asks this question of the parts that factory reaches. The leader of a FollowTheLeader is such an estimator. Its factory receives the Price-Adjusted Allocation of the step, so only a schedule in it reads the weights of the loop.
The walk reads every schedule, whatever its bind. A schedule that a loop nearer to it resolves still reads its context there, so the answer is true for it too, and the loop runs its folds in sequence.
Algorithm
- A
TimeDependentwhose value is a vector returnstrueif one of its entries does, by this function. An entry is a fixed value, so it reads no context. An optimiser entry can hold a schedule of its own. - Any other
TimeDependentanswers asneeds_previous_weightsdoes:truefor aPreviousWeightsFunction, the answer of aTimeDependentCallable, andfalsefor a bare function. - An optimisation estimator returns
trueif one of the fields ofschedule_tree_fieldsdoes. AnOnlinePortfolioSelectionadds its rule tree onalg, and a rule walks the fields ofrule_tree_fields. - A vector of optimisers or of rules returns
trueif one of its elements does. - Every other value returns
false.
Related
PortfolioOptimisers.schedule_tree_fields — Function
schedule_tree_fields(opt)Returns the names of the fields of opt whose type can hold a TimeDependent, an optimisation estimator, or a vector of optimisers.
schedule_reads_previous_weights walks these fields alone. A generated function computes the tuple once for each type, from the field types, as time_dependent_candidate_fields does. So an optimiser that holds no schedule and no inner optimiser gives an empty tuple at compile time.
Related
PortfolioOptimisers.assert_time_dependent_substitution — Function
assert_time_dependent_substitution(
T::Union{Function, Type},
args::NamedTuple,
defaults::NamedTuple
)
Checks each entry of the schedules in args with the keyword constructor of T, or with the keyword function T.
The constructor of an optimiser calls this function, so a type error or an error between two fields shows at construction and not in the middle of a backtest. The argument args holds the arguments of the constructor, and defaults holds the static defaults that time_dependent_field_defaults gives. A call that the check makes holds no schedule, so the recursion stops. A check that reads only some fields of an estimator passes a keyword function of those fields as T, as the budget check of a FollowTheLeader leader does.
Algorithm
- Collect the names
tdfsof the arguments that hold aTimeDependent. Return when there is none. - Find a static value for each of them with
time_dependent_stand_in. Return with no check when one of them has none. That is a function schedule in a required field, whose value exists only at a fold. - Replace each schedule in
argswith its static value, givingbase. - For each name in
tdfs, callTwithbaseas keywords, once for each entry of the schedule and once for its explicitdefault, seesubstitute_time_dependent_entries.
Related
PortfolioOptimisers.substitute_time_dependent_entries — Function
substitute_time_dependent_entries(T::Base.Callable, base::NamedTuple, f::Symbol, td::TimeDependent)
substitute_time_dependent_entries(::Base.Callable, ::NamedTuple, ::Symbol, ::Any)Calls the keyword constructor of T, or the keyword function T, with each vector entry of the schedule td, and with its explicit default, in the field f of base.
The second method takes a value that is not a schedule, and calls nothing. assert_time_dependent_substitution passes only schedules. The second method states this fact to a static analyser, which cannot read the filter at a call whose arguments hold no schedule.
Related
PortfolioOptimisers.time_dependent_stand_in — Function
time_dependent_stand_in(
td::TimeDependent,
defaults::NamedTuple,
field::Symbol
) -> Union{Nothing, Some}
Returns a valid static value for a field that holds a TimeDependent, in a Some, or nothing when none exists.
assert_time_dependent_substitution gives the other scheduled fields these values while it checks one of them. This function never throws, unlike time_dependent_reset_value. A schedule with no value outside every fold loop is valid at construction, and fails only when it reaches a solve with no fold.
Algorithm
- When
td.defaultis notNoDefault, return it. - Read the static default of
fieldfromdefaults, which isnothingfor a field thatdefaultsdoes not list. Return it when it is notNoDefault. - When
td.valis a vector, return its first entry. - Else return
nothing. A function schedule in a required field has a value only at a fold.
Related
PortfolioOptimisers.time_dependent_reset_value — Function
time_dependent_reset_value(
td::TimeDependent,
defaults::NamedTuple,
field::Symbol,
opt
) -> Any
Returns the value of a field that holds a TimeDependent, outside every fold loop.
Algorithm
- When
td.defaultis notNoDefault, return it. - Read the static default of
fieldfromdefaults, seetime_dependent_field_defaults. It isnothingfor a field thatdefaultsdoes not list. - When the static default is
NoDefault, throw. Else return it.
Validation
- The schedule or the field has a value outside every fold loop, else a
TimeDependentDefaultErrorthat names the field and the type ofoptis thrown.
Related
PortfolioOptimisers.assert_time_dependent_optimiser — Function
assert_time_dependent_optimiser(
_::Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult}
)
Checks that a TimeDependent in the place of an optimiser resolved to a value that can optimise or predict.
The type of a vector schedule and of a TimeDependentOptimiserCallable states the kind of their value before any fold runs, see TD_OptE_Opt. A bare function ctx -> optimiser and a PreviousWeightsFunction that holds one do not. So this function checks their value when the fold loop puts it into place.
Validation
optis anOptE_Opt, else anArgumentErrorthat names the type ofoptis thrown.
Related
PortfolioOptimisers.assert_time_dependent_fold_count — Function
assert_time_dependent_fold_count(opt, n::Integer, all_binds::Bool = true)Checks that each vector schedule in opt has exactly n entries.
The cross-validation fold loops call it directly after split, before any fold runs. The fallback checks nothing. An optimiser with scheduled fields checks the fields that time_dependent_fields returns, and an optimiser that wraps others recurses. When all_binds is false, the check leaves out the schedules with bind === :nearest. The nearest fold loop checks them against its own fold count.
Validation
- Each vector schedule that the check reads has
nentries, else aDimensionMismatchis thrown.
Related
PortfolioOptimisers.assert_time_dependent_fold_count — Function
assert_time_dependent_fold_count(
td::Union{TimeDependent{<:TimeDependentOptimiserCallable}, TimeDependent{<:PreviousWeightsFunction}, TimeDependent{<:Union{Function, Type}}, TimeDependent{<:AbstractVector{<:Union{Nothing, var"#s9831"} where var"#s9831"<:Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult}}}},
n::Integer
)
assert_time_dependent_fold_count(
td::Union{TimeDependent{<:TimeDependentOptimiserCallable}, TimeDependent{<:PreviousWeightsFunction}, TimeDependent{<:Union{Function, Type}}, TimeDependent{<:AbstractVector{<:Union{Nothing, var"#s9831"} where var"#s9831"<:Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult}}}},
n::Integer,
all_binds::Bool
)
Checks that a TimeDependent in the place of an optimiser has one entry for each fold, and that the schedules in each entry have the same fold count.
Entry i runs at fold i of this loop, so its own schedules with :outermost resolve against this loop too, see update_time_dependent_estimator. The check does not read default, which runs only outside every fold loop, where its schedules reset.
When all_binds is false and td.bind is not :outermost, the function checks nothing. The fold loop that the optimiser opens checks the schedule against its own fold count.
Validation
- A vector schedule has
nentries, else aDimensionMismatchis thrown. - Each entry passes
assert_time_dependent_fold_countwith the samen.
Related
PortfolioOptimisers.assert_time_dependent_fold_count — Function
assert_time_dependent_fold_count(
opt::AbstractVector{<:Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult, TimeDependent{<:AbstractVector{<:Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult}}}, TimeDependent{<:TimeDependentOptimiserCallable}, TimeDependent{<:PreviousWeightsFunction}, TimeDependent{<:Union{Function, Type}}}},
n::Integer
)
assert_time_dependent_fold_count(
opt::AbstractVector{<:Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult, TimeDependent{<:AbstractVector{<:Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult}}}, TimeDependent{<:TimeDependentOptimiserCallable}, TimeDependent{<:PreviousWeightsFunction}, TimeDependent{<:Union{Function, Type}}}},
n::Integer,
all_binds::Bool
)
Applies assert_time_dependent_fold_count to each element of a vector of optimisers.
Related
PortfolioOptimisers.assert_time_dependent_fields_fold_count — Function
assert_time_dependent_fields_fold_count(opt, n::Integer)
assert_time_dependent_fields_fold_count(
opt,
n::Integer,
all_binds::Bool
)
Checks the fold count of each field of an optimiser that holds a TimeDependent which the current fold loop resolves.
Validation
- Each vector schedule has
nentries, else aDimensionMismatchthat names the field is thrown.
Related
PortfolioOptimisers.rebuild_struct — Function
rebuild_struct(x, repl::NamedTuple) -> Pipeline
Rebuilds a struct with its keyword constructor, with the fields in repl replaced. The struct can be an estimator, a result, or any other type whose keyword constructor takes every field by its name, except a derived field that forward_derived names.
The other fields keep their values. The keyword constructor checks every rule of construction again, and it computes each derived field again from the new values.
The function reads each field in one tuple expression, and it does not merge the fields with repl. A field with a declared Union type, such as the gap marks of a SampleBufferState, makes the type of a merged NamedTuple abstract. The call to the constructor then dispatches at run time and allocates, and an online step calls this function on every fold.
Validation
assert_not_derivedrefuses areplthat names a derived field.
Algorithm
- Refuse a
replthat names a derived field. - Read each field of
xinto one tuple, in the order of the fields. Take the value inreplwhenreplnames the field. - Remove the derived fields from the tuple of step 2.
- Keep the names of
replthat are not fields ofx. - Call the keyword constructor of the type of
xwith the fields and the kept names.
A type with no derived field gives the empty tuple of names, so step 3 removes nothing, and the compiler folds it.
Related
rebuild_struct(p::Pipeline, repl::NamedTuple)Rebuilds a Pipeline with the fields in repl replaced, through the positional constructor.
The keyword constructor derives the names from the steps. A rebuild that seeds a state must keep the names as they are, and only the positional constructor takes them as given.
Related
PortfolioOptimisers.is_time_dependent — Method
is_time_dependent(opt)Returns true if the optimiser holds a schedule.
The fallback returns false for every value, because a query trait owns false. An optimiser returns true when one of its fields holds a TimeDependent, see time_dependent_fields. An optimiser that wraps others recurses into its inner optimiser and its fallback. A step of a Pipeline that holds no schedule, such as a prior or a callable, answers through the fallback.
Arguments
opt: An optimisation estimator or result, a vector of them, or any other value.
Returns
flag::Bool:trueif the optimiser holds a schedule.
Related
PortfolioOptimisers.is_time_dependent — Method
is_time_dependent(opt)Returns true if the optimiser holds a schedule.
The fallback returns false for every value, because a query trait owns false. An optimiser returns true when one of its fields holds a TimeDependent, see time_dependent_fields. An optimiser that wraps others recurses into its inner optimiser and its fallback. A step of a Pipeline that holds no schedule, such as a prior or a callable, answers through the fallback.
Arguments
opt: An optimisation estimator or result, a vector of them, or any other value.
Returns
flag::Bool:trueif the optimiser holds a schedule.
Related
is_time_dependent(
opt::AbstractVector{<:Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult, TimeDependent{<:AbstractVector{<:Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult}}}, TimeDependent{<:TimeDependentOptimiserCallable}, TimeDependent{<:PreviousWeightsFunction}, TimeDependent{<:Union{Function, Type}}}}
) -> Any
Returns true if one element of a vector of optimisers holds a schedule, or is one.
Related
PortfolioOptimisers.is_time_dependent — Method
is_time_dependent(opt::BaseOptimisationEstimator) -> Bool
Returns true if an optimiser configuration holds a schedule in one of its fields.
Related
PortfolioOptimisers.is_time_dependent — Method
is_time_dependent(_::TimeDependent) -> Bool
Returns true, because a TimeDependent is a schedule.
Related
PortfolioOptimisers.is_time_dependent — Method
is_time_dependent(
opt::AbstractVector{<:Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult, TimeDependent{<:AbstractVector{<:Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult}}}, TimeDependent{<:TimeDependentOptimiserCallable}, TimeDependent{<:PreviousWeightsFunction}, TimeDependent{<:Union{Function, Type}}}}
) -> Any
Returns true if one element of a vector of optimisers holds a schedule, or is one.
Related
PortfolioOptimisers.update_time_dependent_estimator — Function
update_time_dependent_estimator(
opt::BaseOptimisationEstimator,
ctx::TimeDependentContext
) -> Any
update_time_dependent_estimator(
opt::BaseOptimisationEstimator,
ctx::TimeDependentContext,
all_binds::Bool
) -> Any
Resolves the schedules of an optimiser configuration at the fold that ctx describes.
The function rebuilds the configuration with its keyword constructor, which checks the values, and puts the value at the fold into each scheduled field. So the answer is an ordinary static configuration. When all_binds is false, the schedules with bind === :nearest stay in place for the nearest fold loop.
Related
update_time_dependent_estimator(opt, ctx::TimeDependentContext, all_binds::Bool = true)Resolves the schedules of opt at the fold that ctx describes.
The fallback returns its argument unchanged, the identity of a traversal on a leaf. A step of a Pipeline that holds no schedule, such as a prior or a callable, comes back through it. An optimiser with scheduled fields rebuilds itself with its keyword constructor, which checks the values, and puts the value at the fold into each scheduled field. So the answer is an ordinary static estimator. An optimiser that wraps others recurses.
Arguments
opt: An optimisation estimator or result, or any other value.ctx::TimeDependentContext: The context of the fold.all_binds::Bool = true: Whenfalse, the schedules withbind === :neareststay in place for the nearest fold loop. A meta-optimiser passesfalsewhen it recurses into the estimators of its own inner fold loop. A fold loop passestrue.
Returns
opt: The estimator with its schedules resolved.
Related
update_time_dependent_estimator(
td::Union{TimeDependent{<:AbstractVector{<:Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult}}}, TimeDependent{<:TimeDependentOptimiserCallable}, TimeDependent{<:PreviousWeightsFunction}, TimeDependent{<:Union{Function, Type}}},
ctx::TimeDependentContext
) -> Any
update_time_dependent_estimator(
td::Union{TimeDependent{<:AbstractVector{<:Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult}}}, TimeDependent{<:TimeDependentOptimiserCallable}, TimeDependent{<:PreviousWeightsFunction}, TimeDependent{<:Union{Function, Type}}},
ctx::TimeDependentContext,
all_binds::Bool
) -> Any
Resolves a TimeDependent in the place of an optimiser to the optimiser of fold ctx.i.
Entry i can be an estimator or a precomputed result, so a mixed schedule optimises at some folds and predicts at others. The resolved optimiser can hold schedules of its own. The function resolves them with the same context, so its schedules with :outermost resolve against this fold loop.
Algorithm
- When
all_bindsisfalseandtd.bindis not:outermost, returntdunchanged. The fold loop that the optimiser opens resolves such a schedule, not the loop that reached the optimiser. - Resolve
tdat the fold withtime_dependent_value, givingopt. - Check
optwithassert_time_dependent_optimiser. - Return
update_time_dependent_estimator(opt, ctx, all_binds).
Related
update_time_dependent_estimator(
opt::AbstractVector{<:Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult, TimeDependent{<:AbstractVector{<:Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult}}}, TimeDependent{<:TimeDependentOptimiserCallable}, TimeDependent{<:PreviousWeightsFunction}, TimeDependent{<:Union{Function, Type}}}},
ctx::TimeDependentContext
) -> Any
update_time_dependent_estimator(
opt::AbstractVector{<:Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult, TimeDependent{<:AbstractVector{<:Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult}}}, TimeDependent{<:TimeDependentOptimiserCallable}, TimeDependent{<:PreviousWeightsFunction}, TimeDependent{<:Union{Function, Type}}}},
ctx::TimeDependentContext,
all_binds::Bool
) -> Any
Applies update_time_dependent_estimator to each element of a vector of optimisers, and returns a new vector.
Related
update_time_dependent_estimator(
opt::NaiveOptimisationEstimator,
ctx::TimeDependentContext
) -> OnlinePortfolioSelection
update_time_dependent_estimator(
opt::NaiveOptimisationEstimator,
ctx::TimeDependentContext,
all_binds::Bool
) -> OnlinePortfolioSelection
Resolve the time-dependent fields of a naive optimiser for the fold that ctx describes.
The answer is a static optimiser, built through the keyword constructor, so the constructor checks every resolved value.
Algorithm
- Return
optunchanged whenis_time_dependentisfalse. - Replace each
TimeDependentfield ofoptwith its value for the fold, withupdate_time_dependent_fields, givingopt. - Resolve the fallback
opt.fbfor the same fold, and rebuildoptwith it.
Related
update_time_dependent_estimator(
opt::ClusteringOptimisationEstimator,
ctx::TimeDependentContext
) -> NestedClustered
update_time_dependent_estimator(
opt::ClusteringOptimisationEstimator,
ctx::TimeDependentContext,
all_binds::Bool
) -> NestedClustered
Replace every TimeDependent schedule of a clustering optimiser with its value at the fold that ctx describes.
The fallback is resolved after the optimiser's own fields, so a fallback that a schedule selects for this fold is itself resolved. NestedClustered overrides this with its own method.
Algorithm
- Return
optunchanged whenis_time_dependentisfalse. - Replace each scheduled field of
optwith its value at the fold, throughupdate_time_dependent_fields. This gives the newopt. - Resolve the inner optimiser
opt.optand the fallbackopt.fbof the newoptwith the samectxandall_binds. - Rebuild
optwith the two resolved estimators, throughrebuild_struct.
Arguments
opt: Clustering optimiser that holds the fieldsoptandfb.ctx: Fold context of the current fold.all_binds: Whether the fold loop resolves the schedules bound to:nearestas well as those bound to:outermost.
Returns
opt: The optimiser with no schedule left for this fold loop, of the same type as the input.
Related
update_time_dependent_estimator(
opt::JuMPOptimisationEstimator,
ctx::TimeDependentContext
) -> Any
update_time_dependent_estimator(
opt::JuMPOptimisationEstimator,
ctx::TimeDependentContext,
all_binds::Bool
) -> Any
Resolve the time-dependent fields of the optimiser for the fold that ctx describes.
The optimiser's own scheduled fields take their value for the fold first. The fallback can be one of those fields, so the recursion into the fallback reads the value that the first step put in place.
Algorithm
- Return
optunchanged whenis_time_dependentisfalse. - Replace each scheduled field of
optwith its value for the fold, throughupdate_time_dependent_fields, giving the newopt. - Resolve the inner optimiser
opt.optand the fallbackopt.fbof the newoptwith the samectx. - Rebuild
optwith the two resolved values.
update_time_dependent_estimator(
opt::NestedClustered,
ctx::TimeDependentContext
) -> NestedClustered
update_time_dependent_estimator(
opt::NestedClustered,
ctx::TimeDependentContext,
all_binds::Bool
) -> NestedClustered
Resolve the TimeDependent schedules of opt for the fold that ctx describes.
Algorithm
- When
optholds no schedule, returnoptunchanged. - Resolve the schedules in the fields of
optwithupdate_time_dependent_fields, givingopt. A schedule inoptithat binds:neareststays, because the inner fold loop readsopti. - Resolve the schedules inside
optiwithall_binds = false, so that a schedule that binds:neareststays for the inner fold loop. - Resolve the schedules inside
optoandfbwithall_binds. - Rebuild
optwith the three resolved values throughrebuild_struct, which runs the checks of the constructor again.
update_time_dependent_estimator(
opt::Stacking,
ctx::TimeDependentContext
) -> Stacking
update_time_dependent_estimator(
opt::Stacking,
ctx::TimeDependentContext,
all_binds::Bool
) -> Stacking
Resolve the TimeDependent schedules of a Stacking for the fold that ctx describes.
Algorithm
- When
is_time_dependentisfalseforopt, returnoptunchanged. - Resolve the schedules in the fields of
optwithupdate_time_dependent_fields, underall_binds. - Resolve
optiwithall_binds = false, so that an element schedule that binds:neareststays for the inner cross-validation ofopt. - Resolve
optoandfbunderall_binds. - Rebuild
optwith the resolvedopti,optoandfb.
Arguments
opt: The stacking optimiser.ctx: TheTimeDependentContextof the fold.all_binds:trueresolves every schedule.falseresolves the schedules that bind:outermostalone.
Returns
Stacking: The optimiser with the schedules of the fold resolved.
Related
update_time_dependent_estimator(
opt::SubsetResampling,
ctx::TimeDependentContext
) -> SubsetResampling
update_time_dependent_estimator(
opt::SubsetResampling,
ctx::TimeDependentContext,
all_binds::Bool
) -> SubsetResampling
Resolve every TimeDependent schedule of opt for the fold that ctx describes.
Algorithm
- Return
optunchanged whenis_time_dependentisfalsefor it. - Resolve the fields of
optitself withupdate_time_dependent_fields, giving a newopt. - Resolve
opt.optandopt.fbwith this function, and rebuildoptwith them throughrebuild_struct.
Related
update_time_dependent_estimator(
p::Pipeline,
ctx::TimeDependentContext
) -> Pipeline
update_time_dependent_estimator(
p::Pipeline,
ctx::TimeDependentContext,
all_binds::Bool
) -> Pipeline
Resolve the schedules of a Pipeline for the fold that ctx describes.
The fold loop does this swap before fit runs, so fit and run_step never see a fold. When fit runs on the training window of the fold, each schedule step is an optimiser or a precomputed result. So inject_context and maybe_inject_step never receive a schedule.
Algorithm
- Resolve each step with
update_time_dependent_estimator, giving the new steps. A schedule step gives its entry for the fold, an estimator or a precomputed result. An optimisation step resolves its scheduled fields, and a nested pipeline resolves its own steps. Every other step, a callable among them, comes back unchanged through the fallback. - Return a pipeline with the same names, the new steps and the same
cache, throughrebuild_struct.
Validation
- Everything
update_time_dependent_estimatorrefuses for a step. This includes an entry of a callable schedule that is not an optimiser.
Related
update_time_dependent_estimator(
ps::PipelineStep,
ctx::TimeDependentContext
) -> Any
update_time_dependent_estimator(
ps::PipelineStep,
ctx::TimeDependentContext,
all_binds::Bool
) -> Any
Resolve one PipelineStep of a Pipeline for the fold that ctx describes.
Algorithm
- Resolve a wrapper of a schedule to the entry of the schedule for the fold, through the
TD_OptE_Optmethod ofupdate_time_dependent_estimator, with no wrapper. The wrapper only declares the slot that dispatch cannot infer, andrun_stepdispatches on the entry. An entry can be a result, and a wrapper cannot hold one. - Resolve the estimator of any other wrapper by
update_time_dependent_estimator, givingnewest. A callable comes back unchanged. - Rebuild the wrapper around
newestwithrebuild_structwhennewestis a new value. Return the wrapper unchanged otherwise.
Validation
- Everything
update_time_dependent_estimatorrefuses for the estimator of the wrapper.
Related
PortfolioOptimisers.update_time_dependent_fields — Function
update_time_dependent_fields(
opt,
ctx::TimeDependentContext
) -> Any
update_time_dependent_fields(
opt,
ctx::TimeDependentContext,
all_binds::Bool
) -> Any
Rebuilds an optimiser with the value at the fold ctx in each field that holds a TimeDependent.
The methods of update_time_dependent_estimator for the optimisers call this function.
Algorithm
- Collect the names
tdfsof the fields that the current fold loop resolves, withtime_dependent_fields. Returnoptunchanged when there is none. - Resolve each of them with
time_dependent_value, givingrepl. - Rebuild
optwithrebuild_struct.
Related
PortfolioOptimisers.reset_time_dependent_estimator — Method
reset_time_dependent_estimator(opt)Replaces each TimeDependent field of opt with its value outside every fold loop, and recurses into the optimisers that opt wraps.
A schedule is defined only over the folds of a cross-validation scheme. So a solve with no fold runs with each scheduled field at its value outside every fold loop, which time_dependent_reset_value gives. The _optimise methods call this function first. An estimator that update_time_dependent_estimator resolved holds no schedule, so this function returns it unchanged.
This fallback serves every optimisation estimator and every optimisation result, and returns opt unchanged. An optimiser that can hold a schedule adds a method that rebuilds it, and an optimiser that wraps others adds a method that recurses.
Related
PortfolioOptimisers.reset_time_dependent_estimator — Method
reset_time_dependent_estimator(
opt::BaseOptimisationEstimator
) -> Any
Replaces the schedules of an optimiser configuration with their values outside every fold loop, see time_dependent_reset_value.
Related
PortfolioOptimisers.reset_time_dependent_estimator — Method
reset_time_dependent_estimator(
td::Union{TimeDependent{<:AbstractVector{<:Union{NonFiniteAllocationOptimisationEstimator, NonFiniteAllocationOptimisationResult}}}, TimeDependent{<:TimeDependentOptimiserCallable}, TimeDependent{<:PreviousWeightsFunction}, TimeDependent{<:Union{Function, Type}}}
) -> Any
Returns the optimiser that a TimeDependent in the place of an optimiser gives outside every fold loop.
The place of an optimiser is required and has no static default. So the schedule must carry its own default. The function resets that optimiser too, so its own schedules take their values outside every fold loop.
Validation
td.defaultis notNoDefault, else aTimeDependentDefaultErroris thrown.
Related
PortfolioOptimisers.reset_time_dependent_fields — Function
reset_time_dependent_fields(opt) -> Any
Rebuilds an optimiser with the value outside every fold loop in each field that holds a TimeDependent.
The methods of reset_time_dependent_estimator for the optimisers call this function.
Algorithm
- Collect the names
tdfsof the scheduled fields withtime_dependent_fields, which leaves out the schedules with:nearestin the fields ofinner_fold_fields. Returnoptunchanged when there is none. - Read the static defaults of
optwithtime_dependent_field_defaults. - Find the value of each field in
tdfswithtime_dependent_reset_value, givingrepl. - Rebuild
optwithrebuild_struct.
Related
PortfolioOptimisers.assert_no_nearest_bind_optimiser_schedule — Method
assert_no_nearest_bind_optimiser_schedule(x, field::Symbol, opt_name::Symbol)Refuses a TimeDependent with bind = :nearest in a field that holds an optimiser and that no inner fold loop reads.
The field bind selects the fold loop whose index the schedule reads. So :nearest differs from :outermost only where the optimiser opens its own fold loop and gives the field to it. inner_fold_fields names those fields: NestedClustered.opti, which runs once for each cluster, and Stacking.opti[k], which runs once for each candidate. In every other field, the loop that reaches the optimiser is the nearest loop, and the two values of bind select the same loop.
This function guards the fields that have no inner fold loop:
- The fallback
fbof every optimiser. The fallback chain runs inside the solve of one fold, and it has no fold index of its own. So:nearestthere selects the same loop as:outermost, or it is wrong behind the inner cross-validation of a meta-optimiser. There it reads the index of a tuning fold and not the period of the backtest. A schedule with:outermoststates every fallback that changes by fold, and an entrynothingturns the fallback off at a fold, seeTDO_OptE_Opt. - The outer optimiser
opto, which reads the combined inner output once for each solve. SubsetResampling.opt, whose inner loop runs over random subsets of the assets, not over folds of time.
So a schedule with :nearest in one of these fields has no nearest fold loop, and the constructor refuses it. The function checks nothing when x is not a TimeDependent.
Validation
x.bind !== :nearestwhenxis aTimeDependent, else anArgumentErrorthat names the field and the optimiser is thrown.
Related
PortfolioOptimisers.entitled — Function
entitled(opt, f::Symbol, all_binds::Bool)Returns true when the current fold loop can resolve a schedule with bind = :nearest in the field f of opt.
The answer is for each field, not for each optimiser. A loop with all_binds = true resolves such schedules in every field except the fields that the optimiser gives to its own inner fold loop, see inner_fold_fields. For those fields, the inner loop is the nearest loop. A pass resolves a field when entitled(opt, f, all_binds) || bind === :outermost is true.
Related
PortfolioOptimisers.reader_frames — Method
reader_frames(td::TimeDependent, path::AbstractString, refit::Bool) -> ReturnsFrames
refits_regression(td::TimeDependent) -> BoolWalk the values of a TimeDependent that are known before any fold runs, for the frame walk of a head.
A schedule gives one value to each fold, so the group of the head can read the frame of any entry. A function schedule gives no entry before it runs, so it adds nothing. A regression reader at nothing refits when the prior of any entry gives no loadings.
Algorithm
- Walk the entries of a vector schedule at
"path.val[i]"withreader_frames, givingfv. A function schedule gives no frames. - Walk the
defaultat"path.default", givingfd.NoDefaultgives no frames. - Join
fvandfdwithmerge_frames.
Arguments
td: The schedule.path: The path of the value from the head, for example"pe","cle"or"r[2].pe". A warning names a reader by its path.refit:truewhen a regression reader atx_src = nothingin the group fits its regression on the raw returns data.refits_regressionfinds the value.
Returns
fr::ReturnsFrames: The frames of the entries, joined withmerge_frames.refit::Bool: Whetherrefits_regressionanswerstruefor an entry.
Related