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.hpp
Go to the documentation of this file.
1#pragma once
2/**
3 * @file beta_calculator.hpp
4 * @brief Online BetaVelocity calculator from streaming price data (AGT-13 / SRFM)
5 *
6 * Module: src/beta_calculator/
7 * Owner: AGT-13 (Adversarial hardening) — 2026-03-01
8 *
9 * Responsibility
10 * --------------
11 * Compute the relativistic β (normalised market velocity) from a stream of
12 * price observations:
13 *
14 * v_market = Δprice / Δtime (raw price velocity)
15 * β = v_market / c_market (normalised, |β| < BETA_MAX_SAFE)
16 * φ = atanh(β) (rapidity — additive under Lorentz boosts)
17 * D(β) = √((1+β)/(1−β)) (relativistic Doppler factor)
18 *
19 * Design Constraints
20 * ------------------
21 * • All fallible operations return std::optional (no exceptions).
22 * • All public methods are noexcept.
23 * • No raw pointers in the public API.
24 * • Thread-safe: stateless free functions; BetaCalculator is const-callable.
25 *
26 * NOT Responsible For
27 * -------------------
28 * • Sourcing price data (caller provides std::vector<double>)
29 * • Persistence or cross-session state
30 * • Non-normalised velocity units (caller provides c_market)
31 */
32
33#include <cmath>
34#include <optional>
35#include <vector>
36
37#include "../momentum/momentum.hpp"
38
39namespace srfm::beta_calculator {
40
41using momentum::BetaVelocity;
43
44// ── BetaVelocityResult ────────────────────────────────────────────────────────
45
46/**
47 * @brief Computed relativistic quantities for a given β.
48 *
49 * All values are derived from a single validated BetaVelocity.
50 */
52 double beta{0.0}; ///< Normalised market velocity β ∈ (−BETA_MAX_SAFE, BETA_MAX_SAFE)
53 double gamma{1.0}; ///< Lorentz factor γ = 1/√(1−β²) ≥ 1
54 double rapidity{0.0}; ///< φ = atanh(β) (additive under composition)
55 double doppler{1.0}; ///< D(β) = √((1+β)/(1−β)) (Doppler factor > 0)
56};
57
58// ── Free-function physics kernels ─────────────────────────────────────────────
59
60/**
61 * @brief Compute rapidity φ = atanh(β).
62 *
63 * Rapidity is additive under relativistic velocity composition:
64 * φ(β₁ ⊕ β₂) = φ(β₁) + φ(β₂)
65 *
66 * @return std::nullopt if β is non-finite or |β| ≥ BETA_MAX_SAFE.
67 */
68[[nodiscard]] std::optional<double>
69rapidity(BetaVelocity beta) noexcept;
70
71/**
72 * @brief Compute relativistic Doppler factor D(β) = √((1+β)/(1−β)).
73 *
74 * Invariant: D(β) · D(−β) = 1.0 for all valid β.
75 *
76 * @return std::nullopt if result is non-finite.
77 */
78[[nodiscard]] std::optional<double>
79doppler_factor(BetaVelocity beta) noexcept;
80
81/**
82 * @brief Compute full BetaVelocityResult for a given β value.
83 *
84 * Convenience wrapper: γ + φ + D all computed and validated together.
85 *
86 * @return std::nullopt if any sub-computation fails.
87 */
88[[nodiscard]] std::optional<BetaVelocityResult>
89full_beta_result(double beta_value) noexcept;
90
91// ── BetaCalculator ────────────────────────────────────────────────────────────
92
93/**
94 * @brief Stateless online calculator for market β velocity.
95 *
96 * "Online" means the calculation consumes a sequence of price observations
97 * and computes a single representative β for the whole window. The
98 * representative β is the normalised mean log-return velocity.
99 *
100 * @example
101 * @code
102 * std::vector<double> prices = {100.0, 100.5, 101.0, 100.8};
103 * BetaCalculator calc;
104 * auto result = calc.fromPriceVelocityOnline(prices, 1.0);
105 * // result->beta ≈ 0.003 (tiny, normal market)
106 * @endcode
107 */
109public:
110 BetaCalculator() noexcept = default;
111
112 /**
113 * @brief Compute BetaVelocityResult from a streaming price series.
114 *
115 * Algorithm:
116 * 1. Compute log-return velocities: v_i = ln(p_{i+1}/p_i) per time step.
117 * 2. Compute mean velocity: v̄ = mean(v_i).
118 * 3. Normalise: β = clamp(v̄ / c_market, −BETA_MAX_SAFE + ε, BETA_MAX_SAFE − ε).
119 * 4. Compute derived quantities (γ, φ, D).
120 *
121 * @param prices Sequence of ≥2 positive, finite price observations.
122 * @param c_market Market "speed of light" (normalisation constant > 0).
123 * Defaults to 1.0 (prices already in normalised units).
124 * @return BetaVelocityResult, or std::nullopt if inputs are invalid.
125 */
126 [[nodiscard]] std::optional<BetaVelocityResult>
127 fromPriceVelocityOnline(const std::vector<double>& prices,
128 double c_market = 1.0) const noexcept;
129};
130
131} // 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.
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(β).
constexpr double BETA_MAX_SAFE
Definition momentum.hpp:62
Computed relativistic quantities for a given β.
double beta
Normalised market velocity β ∈ (−BETA_MAX_SAFE, BETA_MAX_SAFE)
double rapidity
φ = atanh(β) (additive under composition)
double gamma
Lorentz factor γ = 1/√(1−β²) ≥ 1.
double doppler
D(β) = √((1+β)/(1−β)) (Doppler factor > 0)