84template <
typename Scheme,
typename =
void>
88template <
typename Scheme>
91 std::enable_if_t<std::is_convertible_v<
92 decltype(std::declval<const Scheme&>().train(std::size_t{}, std::declval<const FoldLengths&>())), IndexRange
113template <
typename Scheme = Rolling>
114std::vector<Fold>
folds(std::size_t count,
const FoldLengths& lengths,
const Scheme& scheme = Scheme{}) {
116 is_window_scheme<Scheme>::value,
117 "a fold scheme needs the member IndexRange train(std::size_t test_begin, const FoldLengths& lengths) const"
120 throw std::invalid_argument(
"folds need a positive train_ and step_");
122 const auto& [train, test, step, warmup] = lengths;
123 if (warmup > count || train > count - warmup || test > count - warmup - train) {
124 throw std::invalid_argument(
125 "folds: warmup_ + train_ + test_ (" + std::to_string(warmup) +
" + " + std::to_string(train) +
" + " +
126 std::to_string(test) +
") exceeds the " + std::to_string(count) +
" positions"
129 std::vector<Fold> out;
130 for (std::size_t begin = warmup + train;; begin += step) {
131 const IndexRange range = scheme.train(begin, lengths);
132 if (range.begin_ < warmup || range.begin_ >= range.end_ || range.end_ > begin) {
133 throw std::invalid_argument(
134 "folds: the scheme's training range [" + std::to_string(range.begin_) +
", " + std::to_string(range.end_) +
135 ") is empty, starts before warmup_ " + std::to_string(warmup) +
", or ends after the test start " +
136 std::to_string(begin)
139 out.push_back(
Fold{out.size(), range,
IndexRange{begin, begin + test}, range.
begin_ - warmup, begin - warmup});
140 if (step > count - begin || test > count - begin - step) {
Replays a strategy over stored bars: fill and cost models, the replay ledger, and the report.
std::vector< Fold > folds(std::size_t count, const FoldLengths &lengths, const Scheme &scheme=Scheme{})
Lists the folds over a time index of count positions.
The library: broker-neutral types (core), and the modules below.
Training on every position from the end of the first history to each test range.
IndexRange train(std::size_t test_begin, const FoldLengths &lengths) const
Returns the training range of the test range starting at test_begin.
The lengths of a fold, in positions.
std::size_t test_
The test range's length; zero makes each fold a plain window, train_.
std::size_t train_
The training range's length (for Expanding, the first fold's); positive.
std::size_t warmup_
The positions of history every range has before it.
std::size_t step_
How far each fold's test range starts after the previous one's; positive.
One fold: its training and test ranges, and where each one's history starts.
IndexRange train_
The training range.
std::size_t index_
The fold's place, from 0, in order.
IndexRange test_
The test range, test_ long; empty at its scheduled start when test_ is zero.
std::size_t train_warmup_
The first position of the training range's history: train_.begin_ - warmup_.
std::size_t test_warmup_
The first position of the test range's history: test_.begin_ - warmup_.
A half-open range of positions, [begin_, end_).
std::size_t begin_
The first position.
std::size_t end_
One past the last position.
Training on the train_ positions just before each test range.
IndexRange train(std::size_t test_begin, const FoldLengths &lengths) const
Returns the training range of the test range starting at test_begin.
Whether Scheme can choose a fold's training range: true when a const Scheme has a member train(std::s...