// Rolling / expanding window statistics (CPU FP64 reference). // Implementation detail: running sums use Neumaier compensation AND a periodic exact re-summation of the window // (every `resum_every` steps) so that drift cannot accumulate over 390 000-bar series. #pragma once #include #include #include #include "raid/core/types.hpp" namespace raid::stats { constexpr f64 kNaN = std::numeric_limits::quiet_NaN(); struct RollingOptions { usize window{22}; usize min_periods{0}; // 0 = window (full windows only); otherwise partial windows allowed at the start usize resum_every{1024}; // exact re-summation cadence }; std::vector rolling_sum(std::span x, const RollingOptions& o); std::vector rolling_mean(std::span x, const RollingOptions& o); std::vector rolling_variance(std::span x, const RollingOptions& o, int ddof = 1); std::vector rolling_covariance(std::span x, std::span y, const RollingOptions& o, int ddof = 1); std::vector rolling_correlation(std::span x, std::span y, const RollingOptions& o); std::vector expanding_mean(std::span x, usize min_periods = 1); std::vector expanding_variance(std::span x, usize min_periods = 2, int ddof = 1); /// Causal lag (positive k) or lead (negative k); NaN where undefined. std::vector shift(std::span x, long k); } // namespace raid::stats