89 lines
3.1 KiB
Python
89 lines
3.1 KiB
Python
|
|
"""
|
||
|
|
flint_vs_float_analysis.py
|
||
|
|
Differential analysis of 550-bit FLINT vs 64-bit float64 precision
|
||
|
|
specifically for Proxy C (kurtosis = 3798).
|
||
|
|
"""
|
||
|
|
|
||
|
|
import numpy as np
|
||
|
|
import sys
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
PROJECT_ROOT = r"C:\Users\Lenovo\Documents\- DOLPHIN NG HD HCM TSF Predict"
|
||
|
|
if PROJECT_ROOT not in sys.path:
|
||
|
|
sys.path.insert(0, PROJECT_ROOT)
|
||
|
|
|
||
|
|
from SILOQY_NN_Kernel_COMPLETE6 import arb, with_precision, safe_float, FLINT_AVAILABLE
|
||
|
|
from nautilus_dolphin.dvae.corpus_builder import DolphinCorpus, OFF, T1 as T1_DIM
|
||
|
|
|
||
|
|
def compare_precision():
|
||
|
|
corpus_path = str(Path(PROJECT_ROOT) / 'nautilus_dolphin' / 'dvae' / 'corpus_cache.npz')
|
||
|
|
corpus = DolphinCorpus.load(corpus_path)
|
||
|
|
X_e = corpus.X[corpus.mask[:, 1]]
|
||
|
|
t1 = X_e[:, OFF[1]:OFF[1]+T1_DIM]
|
||
|
|
|
||
|
|
# Proxy C: vel_w50 - vel_w750
|
||
|
|
proxy_c = t1[:, 1] - t1[:, 16]
|
||
|
|
|
||
|
|
# Select extreme tails
|
||
|
|
tails_idx = np.where(np.abs(proxy_c) > np.percentile(np.abs(proxy_c), 99))[0]
|
||
|
|
sample_tails = proxy_c[tails_idx]
|
||
|
|
|
||
|
|
kurt = float(((proxy_c - proxy_c.mean())**4).mean() / (proxy_c.std()**4 + 1e-8))
|
||
|
|
print(f"Analyzing {len(sample_tails)} extreme samples from Proxy C (kurt={kurt:.2f})")
|
||
|
|
|
||
|
|
# MCDAIN logic: y = (x - mean) * scale * gate
|
||
|
|
# scale = 1.0 / (log(mag) + eps)
|
||
|
|
|
||
|
|
mag_f64 = np.sqrt(np.mean(proxy_c**2))
|
||
|
|
log_mag_f64 = np.log(mag_f64 + 1e-8)
|
||
|
|
scale_f64 = 1.0 / (log_mag_f64 + 1e-8)
|
||
|
|
|
||
|
|
results = []
|
||
|
|
|
||
|
|
with with_precision(550):
|
||
|
|
# Calc 550-bit magnitude
|
||
|
|
sum_sq_arb = arb(0)
|
||
|
|
for val in proxy_c:
|
||
|
|
sum_sq_arb += arb(str(val))**2
|
||
|
|
mag_arb = (sum_sq_arb / arb(len(proxy_c))).sqrt()
|
||
|
|
log_mag_arb = mag_arb.log()
|
||
|
|
scale_arb = arb(1) / (log_mag_arb + arb("1e-8"))
|
||
|
|
|
||
|
|
for x in sample_tails[:10]:
|
||
|
|
# 64-bit path
|
||
|
|
y_f64 = x * scale_f64
|
||
|
|
|
||
|
|
# 550-bit path
|
||
|
|
x_arb = arb(str(x))
|
||
|
|
y_arb = x_arb * scale_arb
|
||
|
|
y_arb_to_f = safe_float(y_arb)
|
||
|
|
|
||
|
|
diff = abs(y_f64 - y_arb_to_f)
|
||
|
|
results.append((x, y_f64, y_arb_to_f, diff))
|
||
|
|
|
||
|
|
print("\n| Input (Extreme) | Float64 Norm | 550-bit Norm | Delta |")
|
||
|
|
print("|-----------------|--------------|--------------|-------|")
|
||
|
|
for x, f, a, d in results:
|
||
|
|
print(f"| {x:15.10f} | {f:12.8f} | {a:12.8f} | {d:.2e} |")
|
||
|
|
|
||
|
|
# Gradient Stability Mock
|
||
|
|
# (x + eps) - (x) / eps
|
||
|
|
eps_range = [1e-8, 1e-15, 1e-30]
|
||
|
|
print("\nNumerical Gradient Stability (Finite Difference):")
|
||
|
|
x_test = sample_tails[0]
|
||
|
|
for e in eps_range:
|
||
|
|
# Float64
|
||
|
|
g_f64 = ((x_test + e) * scale_f64 - (x_test) * scale_f64) / e
|
||
|
|
|
||
|
|
# 550 bit
|
||
|
|
with with_precision(550):
|
||
|
|
e_arb = arb(str(e))
|
||
|
|
x_arb = arb(str(x_test))
|
||
|
|
g_arb = ((x_arb + e_arb) * scale_arb - (x_arb) * scale_arb) / e_arb
|
||
|
|
g_arb_f = safe_float(g_arb)
|
||
|
|
|
||
|
|
print(f" eps={e:.1e}: Float64 Grad={g_f64:.8f}, 550-bit Grad={g_arb_f:.8f}")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
compare_precision()
|