Special Relativity in Financial Modeling 1.0.0
Lorentz transforms, spacetime classification, and geodesic price paths for quantitative finance
Loading...
Searching...
No Matches
geodesic_strategy.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file include/srfm/geodesic_strategy.hpp
4/// @brief Extended Backtester with Geodesic Deviation Strategy — AGT-07.
5///
6/// # Module: Geodesic Strategy
7///
8/// ## Responsibility
9/// Adds a third strategy mode — GEODESIC_DEVIATION — to the existing
10/// Backtester infrastructure. The geodesic deviation strategy goes long when
11/// the market deviates above the rolling 75th percentile of recent deviations
12/// (mean-reversion hypothesis: pulled-away markets snap back).
13///
14/// ## Strategy Modes
15/// - RAW — Unit position, unmodified signal (baseline)
16/// - RELATIVISTIC — γ-scaled position (original Backtester strategy)
17/// - GEODESIC_DEVIATION — Long when deviation > rolling p75, flat otherwise
18///
19/// ## Guarantees
20/// - All fallible paths return std::optional
21/// - No raw pointers
22/// - Thread-safe reads (const methods)
23
24#include "srfm/backtest.hpp"
25#include "srfm/types.hpp"
26
27#include <optional>
28#include <string>
29#include <vector>
30
31namespace srfm::backtest {
32
33// ─── Types ────────────────────────────────────────────────────────────────────
34
35/// Extended bar data that carries the geodesic deviation signal alongside the
36/// standard BarData used by the base Backtester.
38 BarData base; ///< Standard bar: raw_signal, beta, benchmark
39 double geodesic_deviation; ///< ||x_actual − x_geodesic||₂ from GeodesicDeviationCalculator
40};
41
42/// Strategy selection for ExtendedBacktester.
43enum class StrategyMode {
44 RAW, ///< Baseline: sign(signal) × return
45 RELATIVISTIC, ///< γ-scaled: sign(γ × signal) × return
46 GEODESIC_DEVIATION, ///< Mean-reversion: long when deviation > rolling p75
47};
48
49// ─── ExtendedBacktester ───────────────────────────────────────────────────────
50
51/// Runs all three strategy modes side by side and reports metrics.
52///
53/// Usage
54/// -----
55/// ```cpp
56/// ExtendedBacktester ext;
57/// auto result = ext.run_triple(bars, asset_returns, "AAPL");
58/// if (result) {
59/// fmt::print("RAW Sharpe: {:.3f}\n", result->raw.sharpe_ratio);
60/// fmt::print("Relativistic Sharpe: {:.3f}\n", result->relativistic.sharpe_ratio);
61/// fmt::print("Geodesic Sharpe: {:.3f}\n", result->geodesic.sharpe_ratio);
62/// }
63/// ```
65public:
66 /// Three-way performance comparison across all strategy modes.
68 PerformanceMetrics raw; ///< RAW strategy metrics
69 PerformanceMetrics relativistic; ///< RELATIVISTIC strategy metrics
70 PerformanceMetrics geodesic; ///< GEODESIC_DEVIATION strategy metrics
71 std::string ticker; ///< Ticker symbol (informational)
72
73 /// Human-readable comparison table.
74 [[nodiscard]] std::string to_string() const;
75 };
76
77 /// Construct with backtesting configuration and rolling window size.
78 ///
79 /// # Arguments
80 /// * `config` — Risk-free rate, annualisation, effective mass, etc.
81 /// * `rolling_window` — Window for rolling p75 deviation (in bars).
82 explicit ExtendedBacktester(
84 std::size_t rolling_window = 100) noexcept;
85
86 /// Run all three strategies and return a three-way comparison.
87 ///
88 /// # Arguments
89 /// * `bars` — One GeodesicBarData per time step.
90 /// * `asset_returns` — Realised asset returns aligned to bars (same length).
91 /// * `ticker` — Ticker label for the output (informational only).
92 ///
93 /// # Returns
94 /// TripleComparison, or nullopt if:
95 /// - bars and asset_returns have different sizes
96 /// - Fewer than MIN_RETURN_SERIES_LENGTH bars
97 /// - Any metric computation is numerically degenerate
98 [[nodiscard]] std::optional<TripleComparison>
99 run_triple(const std::vector<GeodesicBarData>& bars,
100 const std::vector<double>& asset_returns,
101 const std::string& ticker = "") const noexcept;
102
103 /// Compute only the geodesic strategy return series (for inspection).
104 ///
105 /// # Returns
106 /// Return series, or nullopt on invalid input.
107 [[nodiscard]] std::optional<std::vector<double>>
108 geodesic_returns(const std::vector<GeodesicBarData>& bars,
109 const std::vector<double>& asset_returns) const noexcept;
110
111private:
112 /// Compute rolling 75th percentile of geodesic_deviation up to (and
113 /// including) bar i, looking back at most rolling_window_ bars.
114 [[nodiscard]] double rolling_p75(
115 const std::vector<GeodesicBarData>& bars,
116 std::size_t i) const noexcept;
117
118 /// Build the position series for the GEODESIC_DEVIATION strategy:
119 /// position_i = 1.0 if deviation_i > rolling_p75_i, else 0.0
120 [[nodiscard]] std::vector<double>
121 geodesic_positions(const std::vector<GeodesicBarData>& bars) const noexcept;
122
123 BacktestConfig config_;
124 Backtester base_backtester_;
125 std::size_t rolling_window_;
126};
127
128} // namespace srfm::backtest
Relativistic Backtester — AGT-05 public API.
std::optional< TripleComparison > run_triple(const std::vector< GeodesicBarData > &bars, const std::vector< double > &asset_returns, const std::string &ticker="") const noexcept
std::optional< std::vector< double > > geodesic_returns(const std::vector< GeodesicBarData > &bars, const std::vector< double > &asset_returns) const noexcept
StrategyMode
Strategy selection for ExtendedBacktester.
@ RAW
Baseline: sign(signal) × return.
@ RELATIVISTIC
γ-scaled: sign(γ × signal) × return
@ GEODESIC_DEVIATION
Mean-reversion: long when deviation > rolling p75.
Configuration for a backtest run.
Definition backtest.hpp:113
A single time-bar of backtester input.
Definition backtest.hpp:57
Three-way performance comparison across all strategy modes.
std::string to_string() const
Human-readable comparison table.
PerformanceMetrics geodesic
GEODESIC_DEVIATION strategy metrics.
PerformanceMetrics raw
RAW strategy metrics.
std::string ticker
Ticker symbol (informational)
PerformanceMetrics relativistic
RELATIVISTIC strategy metrics.
BarData base
Standard bar: raw_signal, beta, benchmark.
double geodesic_deviation
||x_actual − x_geodesic||₂ from GeodesicDeviationCalculator
Performance metrics for a single strategy evaluation.
Definition backtest.hpp:76
Shared primitive types for the Special Relativity in Financial Modeling (SRFM) system.