Special Relativity in Financial Modeling 1.0.0
Lorentz transforms, spacetime classification, and geodesic price paths for quantitative finance
Loading...
Searching...
No Matches
n_asset_interval.cpp
Go to the documentation of this file.
1/**
2 * @file n_asset_interval.cpp
3 * @brief Implementation of NAssetEvent and NAssetInterval.
4 *
5 * See include/srfm/manifold/n_asset_interval.hpp for the public API contract.
6 */
7
8#include "../../include/srfm/manifold/n_asset_interval.hpp"
9
10#include <cmath>
11
12namespace srfm::manifold {
13
14// ── NAssetEvent ───────────────────────────────────────────────────────────────
15
16Eigen::VectorXd NAssetEvent::to_coords() const noexcept {
17 const int N = static_cast<int>(prices.size());
18 Eigen::VectorXd coords(N + 1);
19 coords(0) = t;
20 for (int i = 0; i < N; ++i) {
21 coords(i + 1) = prices(i);
22 }
23 return coords;
24}
25
26std::optional<NAssetEvent>
27NAssetEvent::make(double t, Eigen::VectorXd prices) noexcept {
28 if (prices.size() == 0) {
29 return std::nullopt;
30 }
31 NAssetEvent ev;
32 ev.t = t;
33 ev.prices = std::move(prices);
34 return ev;
35}
36
37// ── NAssetInterval ────────────────────────────────────────────────────────────
38
39std::optional<IntervalResult>
41 const NAssetEvent& b,
42 const NAssetManifold& manifold) const noexcept {
43 // Validate dimensions.
44 if (static_cast<int>(a.prices.size()) != manifold.n_assets()) {
45 return std::nullopt;
46 }
47 if (static_cast<int>(b.prices.size()) != manifold.n_assets()) {
48 return std::nullopt;
49 }
50
51 // Compute displacement Δx = b - a in (N+1)-dimensional coordinates.
52 Eigen::VectorXd xa = a.to_coords();
53 Eigen::VectorXd xb = b.to_coords();
54 Eigen::VectorXd dx = xb - xa;
55
56 // Evaluate metric at the midpoint (constant metric → same everywhere).
57 Eigen::VectorXd x_mid = 0.5 * (xa + xb);
58 auto g_opt = manifold.metric_at(x_mid);
59 if (!g_opt) { return std::nullopt; }
60
61 // ds² = dx^T · g · dx.
62 double ds_sq = dx.dot((*g_opt) * dx);
63
64 IntervalResult result;
65 result.ds_sq = ds_sq;
66 result.type = classify(ds_sq);
67 result.magnitude = std::sqrt(std::abs(ds_sq));
68
69 return result;
70}
71
72std::optional<std::vector<IntervalResult>>
74 const NAssetEvent& ref,
75 std::span<const NAssetEvent> events,
76 const NAssetManifold& manifold) const noexcept {
77 std::vector<IntervalResult> results;
78 results.reserve(events.size());
79
80 for (const auto& ev : events) {
81 auto r = compute(ref, ev, manifold);
82 if (!r) { return std::nullopt; }
83 results.push_back(std::move(*r));
84 }
85
86 return results;
87}
88
89} // namespace srfm::manifold
std::optional< std::vector< IntervalResult > > batch_from_reference(const NAssetEvent &ref, std::span< const NAssetEvent > events, const NAssetManifold &manifold) const noexcept
Compute intervals from a reference event to many events.
std::optional< IntervalResult > compute(const NAssetEvent &a, const NAssetEvent &b, const NAssetManifold &manifold) const noexcept
Compute ds² = g_μν Δx^μ Δx^ν between two events.
(N+1)-dimensional Lorentzian manifold for N financial assets.
Result of a single interval computation.
double ds_sq
Squared interval ds².
IntervalType type
TIMELIKE, SPACELIKE, or LIGHTLIKE.
A spacetime event: a moment in time with N asset prices.
static std::optional< NAssetEvent > make(double t, Eigen::VectorXd prices) noexcept
Factory: validate and construct an NAssetEvent.
Eigen::VectorXd prices
N asset price coordinates.
Eigen::VectorXd to_coords() const noexcept
Concatenate (t, prices) into a single (N+1)-vector.