1// Realized measures from intraday returns (per session). Definitions (registry keys realized.*), with M intraday2// returns r_1..r_M of one session:3// RV = Σ r_j² (Andersen–Bollerslev–Diebold–Labys 2003)4// BPV = μ₁⁻² · (M/(M−1)) · Σ_{j=2}^{M} |r_j||r_{j−1}|, μ₁ = √(2/π) (Barndorff-Nielsen & Shephard 2004; the finite-sample5// factor M/(M−1) is optional: `bpv_finite_sample` — highfrequency::rBPCov applies it by default (makeReturns scaling))6// RSV− = Σ r_j² 1{r_j<0}, RSV+ = Σ r_j² 1{r_j>0} (Barndorff-Nielsen–Kinnebrock–Shephard 2010)7// RQ = (M/3) · Σ r_j⁴ (Barndorff-Nielsen & Shephard 2002; BPQ 2016 HARQ)8// J = max(RV − BPV, 0), relative jump = J / RV (Andersen–Bollerslev–Diebold 2007 proxy, no test)9// TriPV = μ_{4/3}⁻³ · (M/(M−2)) · Σ |r_j|^{4/3}|r_{j−1}|^{4/3}|r_{j−2}|^{4/3}, μ_{4/3} = 2^{2/3} Γ(7/6)/Γ(1/2)10// BNS ratio test statistic (Tier C, Huang–Tauchen 2005 log version) is NOT implemented in Tier A.11// Realized covariance: RCov_{ab} = Σ_j r_{a,j} r_{b,j} on synchronized grids (same session, same timestamps).12#pragma once13#include <span>14#include <vector>15#include <string>16#include <Eigen/Dense>17#include "raid/core/types.hpp"18#include "raid/core/plan.hpp"19#include "raid/data/returns.hpp"20#include "raid/data/panel.hpp"21#include "raid/backend/backend.hpp"2223namespace raid::est {2425struct RealizedOptions {26 bool bpv_finite_sample{true};27 bool fuse_kernels{true}; // Metal: one fused pass for all accumulators (ablation: false → 4 kernels)28 bool tripower{true}; // tripower quarticity (CPU FP64, parallel over segments; |x|^{4/3} computed once per element)29 bool annualize{false}; // multiply variances by `periods_per_year`30 f64 periods_per_year{252.0};31 ExecutionPlan plan{};32};3334struct RealizedMeasures {35 std::vector<Date> sessions;36 std::vector<usize> n_returns; // M per session37 std::vector<f64> rv, bpv, rsv_neg, rsv_pos, rq, jump, tripv;38 std::vector<f64> daily_return; // Σ r_j (open-to-close log return when log returns are used)39 backend::KernelStats stats; // backend / precision / timing of the batched part40 std::vector<std::string> warnings;41};4243/// Per-session realized measures for one instrument from intraday returns (data::IntradayReturns).44RealizedMeasures realized_measures(const data::IntradayReturns& ir, const RealizedOptions& opt = {});45/// Batched over a CSR batch of return segments (one segment = one session or one series), returns the same vectors.46RealizedMeasures realized_measures_batch(const backend::Batch& b, const RealizedOptions& opt = {});4748/// Realized covariance / correlation of N aligned return series (T × N dense).49struct RealizedCov { Eigen::MatrixXd cov; Eigen::MatrixXd corr; usize n_obs; };50RealizedCov realized_covariance(const Eigen::Ref<const Eigen::MatrixXd>& R);5152/// Finite-sample constants exposed for tests and the paper appendix.53constexpr f64 kMu1 = 0.7978845608028654; // √(2/π)54f64 mu_p(f64 p); // E|Z|^p for Z ~ N(0,1)5556} // namespace raid::est57