Special Relativity in Financial Modeling 1.0.0
Lorentz transforms, spacetime classification, and geodesic price paths for quantitative finance
Loading...
Searching...
No Matches
geodesic_signal.cpp
Go to the documentation of this file.
1/// @file src/tensor/geodesic_signal.cpp
2/// @brief Implementation of GeodesicDeviationCalculator — AGT-07.
3///
4/// Integrates the geodesic equation from the first bar and computes the
5/// Euclidean spatial deviation between the actual and predicted market position
6/// at each subsequent bar.
7///
8/// The core loop:
9/// 1. Convert events → SpacetimePoints
10/// 2. Estimate u₀ from the first displacement
11/// 3. Integrate geodesic for (n−1) steps (one step per bar gap)
12/// 4. For bar i: deviation = (actual[1:3] − geodesic[1:3]).norm()
13
15#include "srfm/constants.hpp"
16
17#include <algorithm>
18#include <cmath>
19#include <limits>
20
21namespace srfm::tensor {
22
23// ─── Construction ─────────────────────────────────────────────────────────────
24
26 const MetricTensor& metric,
27 double step_size) noexcept
28 : metric_(metric)
29 , solver_(metric, step_size)
30{}
31
32// ─── Private Helpers ──────────────────────────────────────────────────────────
33
34SpacetimePoint GeodesicDeviationCalculator::to_point(
35 const manifold::SpacetimeEvent& ev) noexcept
36{
38 p[0] = ev.time;
39 p[1] = ev.price;
40 p[2] = ev.volume;
41 p[3] = ev.momentum;
42 return p;
43}
44
45FourVelocity GeodesicDeviationCalculator::estimate_velocity(
46 const SpacetimePoint& p0,
47 const SpacetimePoint& p1) noexcept
48{
49 FourVelocity u = p1 - p0;
50
51 // Guard against zero or non-finite displacement
52 double norm = u.norm();
53 if (!std::isfinite(norm) || norm < std::numeric_limits<double>::epsilon() * 100.0) {
54 // Fall back to canonical timelike direction (along τ axis)
55 u = FourVelocity::Zero();
56 u[0] = 1.0;
57 return u;
58 }
59
60 return u / norm;
61}
62
63double GeodesicDeviationCalculator::spatial_deviation(
64 const SpacetimePoint& actual,
65 const SpacetimePoint& geodesic) noexcept
66{
67 // Only spatial components [1, 2, 3]
68 double d1 = actual[1] - geodesic[1];
69 double d2 = actual[2] - geodesic[2];
70 double d3 = actual[3] - geodesic[3];
71
72 double sq = d1 * d1 + d2 * d2 + d3 * d3;
73 if (!std::isfinite(sq)) {
74 return 0.0;
75 }
76 return std::sqrt(sq);
77}
78
79// ─── compute ─────────────────────────────────────────────────────────────────
80
81std::vector<GeodesicSignal>
83 const std::vector<manifold::SpacetimeEvent>& events) const noexcept
84{
85 const std::size_t n = events.size();
86
87 if (n == 0) {
88 return {};
89 }
90
91 if (n == 1) {
92 return {GeodesicSignal{0.0, 0.0, true}};
93 }
94
95 // ── Convert events to SpacetimePoints ─────────────────────────────────────
96 std::vector<SpacetimePoint> actual_points;
97 actual_points.reserve(n);
98 for (const auto& ev : events) {
99 actual_points.push_back(to_point(ev));
100 }
101
102 // ── Guard: check first two points are finite ───────────────────────────────
103 auto all_finite = [](const SpacetimePoint& p) -> bool {
104 for (int i = 0; i < SPACETIME_DIM; ++i) {
105 if (!std::isfinite(p[i])) return false;
106 }
107 return true;
108 };
109
110 if (!all_finite(actual_points[0]) || !all_finite(actual_points[1])) {
111 // Return all-invalid signals
112 std::vector<GeodesicSignal> result(n, GeodesicSignal{0.0, 0.0, false});
113 return result;
114 }
115
116 // ── Estimate initial four-velocity ────────────────────────────────────────
117 FourVelocity u0 = estimate_velocity(actual_points[0], actual_points[1]);
118
119 // ── Integrate geodesic for (n-1) steps ────────────────────────────────────
120 //
121 // solver_.integrate returns n states: [x₀, x₁, ..., x_{n-1}]
122 // where x_i is the geodesic prediction at proper time i * step_size.
123 std::vector<GeodesicState> trajectory =
124 solver_.integrate(actual_points[0], u0, static_cast<int>(n) - 1);
125
126 // trajectory.size() == n (initial state + n-1 steps)
127
128 // ── Build output signals ──────────────────────────────────────────────────
129 std::vector<GeodesicSignal> result;
130 result.reserve(n);
131
132 for (std::size_t i = 0; i < n; ++i) {
133 const SpacetimePoint& actual = actual_points[i];
134 const SpacetimePoint& geodesic = trajectory[i].position;
135
136 bool valid = all_finite(actual) && all_finite(geodesic);
137
138 double deviation = 0.0;
139 if (valid) {
140 deviation = spatial_deviation(actual, geodesic);
141 if (!std::isfinite(deviation)) {
142 deviation = 0.0;
143 valid = false;
144 }
145 }
146
147 double proper_time = static_cast<double>(i) * constants::DEFAULT_GEODESIC_STEP;
148
149 result.push_back(GeodesicSignal{deviation, proper_time, valid});
150 }
151
152 return result;
153}
154
155} // namespace srfm::tensor
std::vector< GeodesicSignal > compute(const std::vector< manifold::SpacetimeEvent > &events) const noexcept
GeodesicDeviationCalculator(const MetricTensor &metric, double step_size=constants::DEFAULT_GEODESIC_STEP) noexcept
Physical and financial constants for the SRFM system.
Geodesic Deviation Signal — AGT-07 public API.
static constexpr double DEFAULT_GEODESIC_STEP
Default proper-time step for geodesic integration.
Definition constants.hpp:40
Eigen::Vector< double, SPACETIME_DIM > FourVelocity
A tangent vector at a spacetime point (four-velocity: dx^μ/dτ).
Definition types.hpp:44
Eigen::Vector< double, SPACETIME_DIM > SpacetimePoint
Definition types.hpp:41
static constexpr int SPACETIME_DIM
Dimensionality of the financial spacetime manifold (1 time + 3 assets).
Definition types.hpp:21
A point in 4D spacetime (t, x, y, z).