47 lines
2.1 KiB
Python
47 lines
2.1 KiB
Python
"""Array-only residual interface; no I/O, model, labels, or clock fitting policy.
|
|
|
|
The caller supplies a frozen, training-only clock. It also fixes the F precision
|
|
in the reviewed contract. Passing F explicitly avoids a hidden extra feature.
|
|
"""
|
|
import numpy as np
|
|
|
|
|
|
def prior(cdf, clock):
|
|
f = np.asarray(cdf)
|
|
if f.ndim != 1 or f.dtype.kind != 'f' or not len(f):
|
|
raise ValueError('Expected nonempty floating-point CDF vector')
|
|
if not np.isfinite(f).all() or np.any(f < 0) or np.any(f > 1):
|
|
raise ValueError('CDF outside [0,1] or nonfinite')
|
|
if not np.isscalar(clock) or not np.isfinite(clock) or clock <= 0:
|
|
raise ValueError('Clock must be a finite positive frozen scalar')
|
|
return np.asarray(f, dtype=np.float64)*float(clock)
|
|
|
|
|
|
def residual_target(target_star, cdf, clock, *, fit_dtype):
|
|
y = np.asarray(target_star, dtype=np.float64)
|
|
base = prior(cdf, clock)
|
|
if y.shape != base.shape or not np.isfinite(y).all() or np.any(y < 0):
|
|
raise ValueError('Invalid original normalized arrival target')
|
|
dtype = np.dtype(fit_dtype)
|
|
if dtype not in (np.dtype('float32'), np.dtype('float64')):
|
|
raise ValueError('Fit precision must be explicitly float32 or float64')
|
|
out = np.asarray(y-base, dtype=dtype)
|
|
if not np.isfinite(out).all():
|
|
raise ValueError('Residual overflow at fit precision')
|
|
# Negative residuals are valid. Never clip or impose a positivity loss here.
|
|
return out
|
|
|
|
|
|
def decode_seconds(predicted_residual, cdf, clock, t_set):
|
|
residual = np.asarray(predicted_residual, dtype=np.float64)
|
|
base = prior(cdf, clock)
|
|
if residual.shape != base.shape or not np.isfinite(residual).all():
|
|
raise ValueError('Invalid predicted residual')
|
|
if not np.isscalar(t_set) or not np.isfinite(t_set) or t_set <= 0:
|
|
raise ValueError('Invalid pre-solve seconds scale')
|
|
out = (base+residual)*float(t_set)
|
|
if not np.isfinite(out).all():
|
|
raise ValueError('Decoded prediction overflow')
|
|
# Keep negative predictions visible to scoring/diagnostics; no silent clamp.
|
|
return out
|