Download src/calibration.py from PureOne/VSCE-Vacuum-State-Cognitive-Engineering-v6: direct link, hf CLI and curl.
- Browser
- Download file 3.79 kB
-
https://huggingface.co/datasets/PureOne/VSCE-Vacuum-State-Cognitive-Engineering-v6/resolve/main/src/calibration.py
- Command line
-
hf download hf://datasets/PureOne/VSCE-Vacuum-State-Cognitive-Engineering-v6/src/calibration.py
-
curl -L -o calibration.py https://huggingface.co/datasets/PureOne/VSCE-Vacuum-State-Cognitive-Engineering-v6/resolve/main/src/calibration.py
3.79 kB
| """Exact OU innovation variance calibration and bounded-drift tube budgets. | |
| Calibration assumes known kappa, independent exact-state observations, | |
| Gaussian innovations, and fixed (not data-selected) pairs. It is not valid | |
| for censored, noisy-camera, or locally stopped trajectories without a new | |
| observation likelihood. Units: time seconds, distance arbitrary consistent. | |
| """ | |
| import numpy as np | |
| from scipy.stats import chi2 | |
| def _finite(*values): | |
| if not all(np.isfinite(v) for v in values): | |
| raise ValueError("all scalar parameters must be finite") | |
| def diffusion_interval(innovations, kappa, dt, alpha=0.05): | |
| """Two-sided (1-alpha) CI for d in dZ=-k Z dt+sqrt(2d)dB. | |
| Each flattened value must be an independent scalar innovation with | |
| zero known mean. Degrees of freedom are not adjusted for fitted kappa. | |
| """ | |
| _finite(kappa, dt, alpha) | |
| values = np.asarray(innovations, dtype=float).ravel() | |
| if values.size < 1 or not np.isfinite(values).all(): | |
| raise ValueError("finite nonempty innovations required") | |
| if kappa <= 0 or dt <= 0 or not 0 < alpha < 1: | |
| raise ValueError("kappa, dt > 0 and alpha in (0,1) required") | |
| scale = -np.expm1(-2 * kappa * dt) / kappa | |
| ss = float(values @ values) | |
| df = values.size | |
| return (ss / (scale * chi2.ppf(1-alpha/2, df)), | |
| ss / (scale * chi2.ppf(alpha/2, df))) | |
| def diffusion_upper(innovations, kappa, dt, alpha=0.05): | |
| """Exact one-sided variance bound; Bonferroni handles dependent pairs.""" | |
| _finite(kappa, dt, alpha) | |
| values = np.asarray(innovations, dtype=float).ravel() | |
| if values.size < 1 or not np.isfinite(values).all(): | |
| raise ValueError("finite nonempty innovations required") | |
| if kappa <= 0 or dt <= 0 or not 0 < alpha < 1: | |
| raise ValueError("kappa, dt > 0 and alpha in (0,1) required") | |
| scale = -np.expm1(-2*kappa*dt) / kappa | |
| return float(values @ values) / (scale * chi2.ppf(alpha, values.size)) | |
| def ou_innovations(positions, kappa, dt): | |
| """Residuals from equally spaced exact observations of a global OU.""" | |
| _finite(kappa, dt) | |
| z = np.asarray(positions, dtype=float) | |
| if z.ndim < 1 or z.shape[0] < 2 or kappa <= 0 or dt <= 0 or not np.isfinite(z).all(): | |
| raise ValueError("at least two times and positive kappa,dt required") | |
| return z[1:] - np.exp(-kappa*dt)*z[:-1] | |
| def drift_tube(kappa, horizon, initial_error, drift_bound): | |
| """Maximum scalar convolution bound on [0,T], with nonnegative inputs.""" | |
| _finite(kappa, horizon, initial_error, drift_bound) | |
| if kappa <= 0 or horizon < 0 or initial_error < 0 or drift_bound < 0: | |
| raise ValueError("invalid tube parameters") | |
| end = initial_error*np.exp(-kappa*horizon) | |
| end += drift_bound/kappa * (-np.expm1(-kappa*horizon)) | |
| return max(float(initial_error), float(end)) | |
| def robust_margin(clearance, kappa, horizon, initial_error=0, drift_bound=0): | |
| """Remaining OU margin; nonpositive output rejects the certificate.""" | |
| _finite(clearance, kappa, horizon, initial_error, drift_bound) | |
| if clearance <= 0: | |
| raise ValueError("clearance must be positive") | |
| return clearance-drift_tube(kappa, horizon, initial_error, drift_bound) | |
| def girsanov_event_allowance(kappa, d, horizon, drift_bound): | |
| """Pinsker event allowance under a common full-rank isotropic diffusion. | |
| kappa is unused in the formula but retained for common calling convention. | |
| Bound: KL <= b^2 T/(4d), TV <= b sqrt(T/(8d)). This is a | |
| same-diffusion result, not a bound for diffusion-coefficient mismatch. | |
| """ | |
| _finite(kappa, d, horizon, drift_bound) | |
| if kappa <= 0 or d <= 0 or horizon < 0 or drift_bound < 0: | |
| raise ValueError("invalid parameters") | |
| return min(1.0, drift_bound*np.sqrt(horizon/(8*d))) | |