1// Block bootstrap for dependent data (registry keys bootstrap.*):2// stationary (Politis & Romano 1994): blocks start at uniform random indices, lengths ~ Geometric(p = 1/mean_block),3// wrapping circularly; expected block length = mean_block.4// circular block (Politis & Romano 1992): fixed block length b, start indices uniform on [0, n), wrap around.5// iid (for comparison only; not valid for dependent data — flagged in the registry).6// Index generation is deterministic given (seed, replica id): replica r uses Rng(seed, r), so replicas can be generated7// in parallel (CPU threads) or on the GPU with the same streams.8#pragma once9#include <vector>10#include <span>11#include <functional>12#include "raid/core/types.hpp"13#include "raid/core/rng.hpp"14#include "raid/core/plan.hpp"15#include "raid/core/estimate.hpp"1617namespace raid::stats {1819enum class BootstrapScheme { stationary, circular_block, iid };2021struct BootstrapOptions {22 BootstrapScheme scheme{BootstrapScheme::stationary};23 f64 mean_block{0}; // 0 → n^{1/3} (rule of thumb; the paper reports the value used)24 usize replicas{999};25 u64 seed{20261102};26 unsigned threads{0};27 f64 ci_level{0.95};28};2930/// Index vector of one replica (length n).31std::vector<u32> bootstrap_indices(usize n, const BootstrapOptions& o, usize replica);3233struct BootstrapResult {34 std::vector<f64> statistics; // one per replica (NaN if the statistic failed)35 f64 point{0}; // statistic on the original sample36 f64 se{0}; // bootstrap standard error37 f64 ci_lo{0}, ci_hi{0}; // percentile CI38 f64 bias{0}; // mean(stat*) − point39 usize failures{0};40 f64 block_length{0};41 f64 seconds{0};42};4344/// Generic scalar statistic bootstrap: stat(x_resampled) evaluated for each replica.45BootstrapResult bootstrap(std::span<const f64> x, const std::function<f64(std::span<const f64>)>& stat, const BootstrapOptions& o);4647/// Multivariate: statistic returns a vector (e.g. regression coefficients); rows of (y, X) are resampled jointly.48struct BootstrapVecResult { std::vector<std::vector<f64>> statistics; std::vector<f64> point, se, ci_lo, ci_hi; usize failures{0}; f64 block_length{0}; f64 seconds{0}; };49BootstrapVecResult bootstrap_rows(usize n, const std::function<std::vector<f64>(std::span<const u32>)>& stat_of_indices, const BootstrapOptions& o);5051/// Realized-variance bootstrap: for each replica, resample intraday returns within a session by blocks and recompute52/// RV (Gonçalves & Meddahi 2009 use the iid wild bootstrap for RV; we use block schemes to respect dependence).53BootstrapResult bootstrap_rv(std::span<const f64> intraday_returns, const BootstrapOptions& o);5455} // namespace raid::stats56