Special Relativity in Financial Modeling 1.0.0
Lorentz transforms, spacetime classification, and geodesic price paths for quantitative finance
Loading...
Searching...
No Matches
beta_calculator.cpp
Go to the documentation of this file.
1/**
2 * @file beta_calculator.cpp
3 * @brief Implementation of online BetaVelocity calculator (AGT-13 / SRFM).
4 *
5 * See beta_calculator.hpp for the full module contract.
6 */
7
8#include "beta_calculator.hpp"
9
10#include <cmath>
11#include <numeric>
12
14
15// ── rapidity ─────────────────────────────────────────────────────────────────
16
17std::optional<double>
18rapidity(BetaVelocity beta) noexcept {
19 const double b = beta.value();
20 // atanh domain: |b| < 1. BetaVelocity guarantees |b| < BETA_MAX_SAFE < 1.
21 const double phi = std::atanh(b);
22 if (!std::isfinite(phi)) return std::nullopt;
23 return phi;
24}
25
26// ── doppler_factor ────────────────────────────────────────────────────────────
27
28std::optional<double>
30 const double b = beta.value();
31 const double numerator = 1.0 + b;
32 const double denominator = 1.0 - b;
33 // denominator > 0 because |b| < BETA_MAX_SAFE < 1
34 if (denominator <= 0.0 || !std::isfinite(denominator)) return std::nullopt;
35 const double ratio = numerator / denominator;
36 if (ratio < 0.0 || !std::isfinite(ratio)) return std::nullopt;
37 const double d = std::sqrt(ratio);
38 if (!std::isfinite(d) || d <= 0.0) return std::nullopt;
39 return d;
40}
41
42// ── full_beta_result ──────────────────────────────────────────────────────────
43
44std::optional<BetaVelocityResult>
45full_beta_result(double beta_value) noexcept {
46 auto bv_opt = BetaVelocity::make(beta_value);
47 if (!bv_opt) return std::nullopt;
48 const BetaVelocity bv = *bv_opt;
49
50 // Lorentz factor
51 auto gamma_opt = momentum::lorentz_gamma(bv);
52 if (!gamma_opt) return std::nullopt;
53
54 // Rapidity
55 auto phi_opt = rapidity(bv);
56 if (!phi_opt) return std::nullopt;
57
58 // Doppler
59 auto d_opt = doppler_factor(bv);
60 if (!d_opt) return std::nullopt;
61
62 return BetaVelocityResult{
63 bv.value(),
64 gamma_opt->value(),
65 *phi_opt,
66 *d_opt
67 };
68}
69
70// ── BetaCalculator ────────────────────────────────────────────────────────────
71
72std::optional<BetaVelocityResult>
74 const std::vector<double>& prices,
75 double c_market) const noexcept {
76
77 // Validate c_market
78 if (!std::isfinite(c_market) || c_market <= 0.0) return std::nullopt;
79
80 // Need at least 2 prices for 1 return
81 if (prices.size() < 2) return std::nullopt;
82
83 // Validate all prices
84 for (const double p : prices) {
85 if (!std::isfinite(p) || p <= 0.0) return std::nullopt;
86 }
87
88 // Compute log-return velocities: v_i = ln(p_{i+1} / p_i)
89 const std::size_t n = prices.size() - 1u;
90 double sum = 0.0;
91 for (std::size_t i = 0; i < n; ++i) {
92 const double ratio = prices[i + 1u] / prices[i];
93 if (ratio <= 0.0 || !std::isfinite(ratio)) return std::nullopt;
94 const double log_ret = std::log(ratio);
95 if (!std::isfinite(log_ret)) return std::nullopt;
96 sum += log_ret;
97 }
98
99 // Mean log-return velocity
100 const double mean_velocity = sum / static_cast<double>(n);
101 if (!std::isfinite(mean_velocity)) return std::nullopt;
102
103 // Normalise: β = v̄ / c_market
104 double beta_raw = mean_velocity / c_market;
105 if (!std::isfinite(beta_raw)) return std::nullopt;
106
107 // Clamp to safe sub-luminal range (never saturate, just cap)
108 constexpr double CLAMP = momentum::BETA_MAX_SAFE - 1e-7;
109 if (beta_raw > CLAMP) beta_raw = CLAMP;
110 if (beta_raw < -CLAMP) beta_raw = -CLAMP;
111
112 return full_beta_result(beta_raw);
113}
114
115} // namespace srfm::beta_calculator
std::optional< BetaVelocityResult > fromPriceVelocityOnline(const std::vector< double > &prices, double c_market=1.0) const noexcept
Compute BetaVelocityResult from a streaming price series.
Normalised market velocity β = price_velocity / c_market.
Definition momentum.hpp:72
double value() const noexcept
Returns the raw β value.
Definition momentum.hpp:83
static std::optional< BetaVelocity > make(double value) noexcept
Validate and construct a BetaVelocity.
Definition momentum.cpp:18
std::optional< BetaVelocityResult > full_beta_result(double beta_value) noexcept
Compute full BetaVelocityResult for a given β value.
std::optional< double > doppler_factor(BetaVelocity beta) noexcept
Compute relativistic Doppler factor D(β) = √((1+β)/(1−β)).
std::optional< double > rapidity(BetaVelocity beta) noexcept
Compute rapidity φ = atanh(β).
std::optional< LorentzFactor > lorentz_gamma(BetaVelocity beta) noexcept
Compute Lorentz factor γ = 1/√(1−β²).
Definition momentum.cpp:42
constexpr double BETA_MAX_SAFE
Definition momentum.hpp:62
BetaCalculator — financial-to-physics velocity mapping (AGT-01).
Computed relativistic quantities for a given β.