algotradelib
0.0.0.9000
C++ header-only library for algorithmic trading
Toggle main menu visibility
Loading...
Searching...
No Matches
resample.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include <cstddef>
4
#include <cstdint>
5
#include <random>
6
#include <stdexcept>
7
#include <vector>
8
9
namespace
algotradelib
{
10
namespace
analytics
{
11
16
18
enum class
BlockScheme
{
20
MOVING
,
22
CIRCULAR
,
24
STATIONARY
,
25
};
26
46
inline
std::vector<std::size_t>
bootstrap_indices
(
47
std::size_t count,
BlockScheme
scheme, std::size_t block, std::uint64_t seed, std::uint64_t path
48
) {
49
if
(count == 0 || block == 0 || (scheme !=
BlockScheme::STATIONARY
&& block > count)) {
50
throw
std::invalid_argument(
"bootstrap_indices needs a positive count and block, and a block within count"
);
51
}
52
std::seed_seq seq{
53
static_cast<
std::uint32_t
>
(seed),
static_cast<
std::uint32_t
>
(seed >> 32),
static_cast<
std::uint32_t
>
(path),
54
static_cast<
std::uint32_t
>
(path >> 32)
55
};
56
std::mt19937_64 engine(seq);
57
// Unbiased: values below `threshold` are the incomplete last run of `n` and are drawn again.
58
const
auto
below = [&engine](std::uint64_t n) {
59
const
std::uint64_t threshold = (std::uint64_t{0} - n) % n;
60
for
(;;) {
61
const
std::uint64_t x = engine();
62
if
(x >= threshold) {
63
return
static_cast<
std::size_t
>
(x % n);
64
}
65
}
66
};
67
std::vector<std::size_t> out;
68
out.reserve(count);
69
if
(scheme ==
BlockScheme::STATIONARY
) {
70
std::size_t at = below(count);
71
out.push_back(at);
72
while
(out.size() < count) {
73
at = below(block) == 0 ? below(count) : (at + 1) % count;
74
out.push_back(at);
75
}
76
return
out;
77
}
78
while
(out.size() < count) {
79
const
std::size_t start = scheme ==
BlockScheme::MOVING
? below(count - block + 1) : below(count);
80
for
(std::size_t step = 0; step < block && out.size() < count; ++step) {
81
out.push_back((start + step) % count);
82
}
83
}
84
return
out;
85
}
86
87
}
// namespace analytics
88
}
// namespace algotradelib
algotradelib::analytics
Series buffers and frames, the conversions to and from exact decimals, and performance metrics.
Definition
conversion.hpp:14
algotradelib::analytics::bootstrap_indices
std::vector< std::size_t > bootstrap_indices(std::size_t count, BlockScheme scheme, std::size_t block, std::uint64_t seed, std::uint64_t path)
Draws the steps of one bootstrap path: entry k names the step from close j to close j + 1,...
Definition
resample.hpp:46
algotradelib::analytics::BlockScheme
BlockScheme
How a block bootstrap draws its blocks of consecutive returns.
Definition
resample.hpp:18
algotradelib::analytics::BlockScheme::STATIONARY
@ STATIONARY
Wrapping blocks whose lengths are geometric with mean block (Politis and Romano).
Definition
resample.hpp:24
algotradelib::analytics::BlockScheme::CIRCULAR
@ CIRCULAR
Blocks of block returns that wrap from the last to the first.
Definition
resample.hpp:22
algotradelib::analytics::BlockScheme::MOVING
@ MOVING
Blocks of block returns that never wrap past the last.
Definition
resample.hpp:20
algotradelib
The library: broker-neutral types (core), and the modules below.
Definition
conversion.hpp:13
algotradelib
src
analytics
resample.hpp
Generated by
1.18.0