File size: 4,478 Bytes
ee36ed0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | """
$\varphi^\\infty$ Lattice Compression: Financial Volatility Analysis
Implements numerical stability metrics applied to financial time-series
data. Identifies when market volatility deviates from stable baseline
trajectories using manifold-grounded variance analysis.
"""
import math
from typing import Dict, List, Tuple
import numpy as np
# Universal Constants
PHI: float = (1.0 + math.sqrt(5.0)) / 2.0
class StabilityRegimeDetector:
"""
Detects regime shifts and instability in financial time-series.
"""
def __init__(self, window_size: int = 128, dimension: int = 8192) -> None:
self.window_size = window_size
self.dimension = dimension
self.history: List[float] = []
self.stability_log: List[Dict[str, object]] = []
def ingest(self, price: float) -> Dict[str, object]:
"""
Ingests a price tick and evaluates manifold stability.
Args:
price: The current price value.
Returns:
A dictionary containing stability metrics and recommendations.
"""
self.history.append(price)
if len(self.history) < self.window_size:
return {
"timestamp_idx": len(self.history),
"stability_score": 1.0,
"is_unstable": False,
"volatility": 0.0,
"stability_recommendation": "NEUTRAL",
}
window = self.history[-self.window_size :]
returns = [
math.log(window[i] / window[i - 1])
for i in range(1, len(window))
if window[i - 1] > 0 and window[i] > 0
]
vol = float(np.std(returns)) if returns else 0.0
# Stability identified by variance remaining within professional thresholds
# Thresholds defined as standard deviation multiples
is_unstable = vol > 0.05 # Example 5% log-return volatility threshold
if is_unstable:
recommendation = "STABILIZE"
elif vol < 0.01:
recommendation = "ACCUMULATE"
else:
recommendation = "HOLD"
result: Dict[str, object] = {
"timestamp_idx": len(self.history),
"stability_score": 1.0 / (1.0 + vol),
"is_unstable": is_unstable,
"volatility": vol,
"stability_recommendation": recommendation,
}
self.stability_log.append(result)
return result
def get_instability_ratio(self) -> float:
"""Calculates the ratio of unstable ticks in the recorded history."""
if not self.stability_log:
return 0.0
unstable_count = sum(1 for entry in self.stability_log if entry["is_unstable"])
return float(unstable_count / len(self.stability_log))
class StatisticalVolatilityStabilizer:
"""
Provides predictive targets based on manifold stability analysis.
"""
@staticmethod
def compute_reversion_target(
prices: List[float],
current_volatility: float,
) -> Tuple[float, float]:
"""
Computes the expected Reversion Target via manifold scaling.
Args:
prices: Historical price list.
current_volatility: Previously calculated volatility.
Returns:
Tuple of (target_price, confidence_score).
"""
if not prices:
return 0.0, 0.0
mean_price = float(np.mean(prices))
std_price = float(np.std(prices))
# Scaling adjustment based on golden-ratio decay principles
adjustment = current_volatility * PHI * std_price
target = mean_price - adjustment
# Confidence decays as volatility increases
confidence = math.exp(-(current_volatility**2) / PHI)
return target, confidence
@staticmethod
def compute_retracement_levels(
current_price: float,
recent_high: float,
recent_low: float,
) -> Dict[str, float]:
"""
Computes Fibonacci retracement levels using the exact-φ lattice.
"""
diff = recent_high - recent_low
return {
"level_0.0": recent_high,
"level_0.236": recent_high - 0.236 * diff,
"level_0.382": recent_high - 0.382 * diff,
"level_0.500": recent_high - 0.5 * diff,
"level_0.618": recent_high - 0.618 * diff,
"level_0.786": recent_high - 0.786 * diff,
"level_1.0": recent_low,
}
|