algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
folds.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <stdexcept>
5#include <string>
6#include <type_traits>
7#include <utility>
8#include <vector>
9
10namespace algotradelib {
11namespace backtest {
12
18
20struct IndexRange {
22 std::size_t begin_{0};
24 std::size_t end_{0};
25};
26
30 std::size_t train_{0};
32 std::size_t test_{0};
34 std::size_t step_{0};
36 std::size_t warmup_{0};
37};
38
40struct Fold {
42 std::size_t index_{0};
48 std::size_t train_warmup_{0};
50 std::size_t test_warmup_{0};
51};
52
54struct Rolling {
61 IndexRange train(std::size_t test_begin, const FoldLengths& lengths) const {
62 return IndexRange{test_begin - lengths.train_, test_begin};
63 }
64};
65
67struct Expanding {
74 IndexRange train(std::size_t test_begin, const FoldLengths& lengths) const {
75 return IndexRange{lengths.warmup_, test_begin};
76 }
77};
78
84template <typename Scheme, typename = void>
85struct is_window_scheme : std::false_type {};
86
88template <typename Scheme>
90 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
93 >>
94> : std::true_type {};
95
113template <typename Scheme = Rolling>
114std::vector<Fold> folds(std::size_t count, const FoldLengths& lengths, const Scheme& scheme = Scheme{}) {
115 static_assert(
116 is_window_scheme<Scheme>::value,
117 "a fold scheme needs the member IndexRange train(std::size_t test_begin, const FoldLengths& lengths) const"
118 );
119 if (lengths.train_ == 0 || lengths.step_ == 0) {
120 throw std::invalid_argument("folds need a positive train_ and step_");
121 }
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"
127 );
128 }
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)
137 );
138 }
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) {
141 break;
142 }
143 }
144 return out;
145}
146
147} // namespace backtest
148} // namespace algotradelib
Replays a strategy over stored bars: fill and cost models, the replay ledger, and the report.
Definition bootstrap.hpp:16
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.
Definition folds.hpp:114
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.
Definition folds.hpp:67
IndexRange train(std::size_t test_begin, const FoldLengths &lengths) const
Returns the training range of the test range starting at test_begin.
Definition folds.hpp:74
The lengths of a fold, in positions.
Definition folds.hpp:28
std::size_t test_
The test range's length; zero makes each fold a plain window, train_.
Definition folds.hpp:32
std::size_t train_
The training range's length (for Expanding, the first fold's); positive.
Definition folds.hpp:30
std::size_t warmup_
The positions of history every range has before it.
Definition folds.hpp:36
std::size_t step_
How far each fold's test range starts after the previous one's; positive.
Definition folds.hpp:34
One fold: its training and test ranges, and where each one's history starts.
Definition folds.hpp:40
IndexRange train_
The training range.
Definition folds.hpp:44
std::size_t index_
The fold's place, from 0, in order.
Definition folds.hpp:42
IndexRange test_
The test range, test_ long; empty at its scheduled start when test_ is zero.
Definition folds.hpp:46
std::size_t train_warmup_
The first position of the training range's history: train_.begin_ - warmup_.
Definition folds.hpp:48
std::size_t test_warmup_
The first position of the test range's history: test_.begin_ - warmup_.
Definition folds.hpp:50
A half-open range of positions, [begin_, end_).
Definition folds.hpp:20
std::size_t begin_
The first position.
Definition folds.hpp:22
std::size_t end_
One past the last position.
Definition folds.hpp:24
Training on the train_ positions just before each test range.
Definition folds.hpp:54
IndexRange train(std::size_t test_begin, const FoldLengths &lengths) const
Returns the training range of the test range starting at test_begin.
Definition folds.hpp:61
Whether Scheme can choose a fold's training range: true when a const Scheme has a member train(std::s...
Definition folds.hpp:85