Observer-Corrected Robust Optimizer / make_report.py
Mechanism confirmed, baseline not beaten
1import json
2from pathlib import Path
3
4x = json.loads(Path('observer_results.json').read_text())
5stab_ok = all(r['relative_error'] <= 0.01 for r in x['stability'])
6rows = x['disturbance_rows']
7slow = [r for r in rows if r['case'] == 'slow_sine']
8high = [r for r in rows if r['case'] == 'high_sine']
9response_ok = all(r['ratio_relative_error'] is not None and r['ratio_relative_error'] < 0.02 for r in slow + high)
10slow_gain = next(r for r in slow if r['alpha'] == 0.5)
11high_gain = next(r for r in high if r['alpha'] == 1.0)
12summary = {
13 'prediction_checks': {
14 'P1_stability_boundary': {'pass': stab_ok, 'max_relative_error': max(r['relative_error'] for r in x['stability'])},
15 'P2_ema_frequency_response': {'pass': response_ok, 'max_relative_error': max(r['ratio_relative_error'] for r in slow + high)},
16 'P3_slow_vs_high_frequency': {
17 'pass': slow_gain['residual_ratio'] < 0.1 and high_gain['residual_ratio'] > 1.0,
18 'slow_alpha_0.5_ratio': slow_gain['residual_ratio'],
19 'high_alpha_1.0_ratio': high_gain['residual_ratio']}}
20}
21Path('observer_report.json').write_text(json.dumps(summary, indent=2))
22with open('observer_report.txt', 'w') as f:
23 f.write('Observer-Corrected Robust Optimizer MVP report\n\n')
24 f.write('Model: scalar quadratic f(theta)=lambda*theta, gradient disturbance d[k].\n')
25 f.write('The observer is an EMA of the one-step transition residual; d_hat is fed forward with a subtractive correction.\n\n')
26 f.write('Prediction checks (observed versus predicted)\n')
27 f.write('P1 stability: h_critical predicted=2/lambda; observed grid brackets are in observer_results.json.\n')
28 for r in x['stability']:
29 f.write(f" lambda={r['lambda']}: predicted {r['predicted_h_critical']:.6f}, observed {r['observed_h_critical']:.6f}, relative error {r['relative_error']:.4%}\n")
30 f.write('P2 frequency response: predicted and observed residual RMS ratios:\n')
31 for r in rows:
32 if r['case'] != 'constant':
33 f.write(f" {r['case']}, alpha={r['alpha']}: observed {r['residual_ratio']:.6f}, predicted {r['predicted_ratio']:.6f}, error {r['ratio_relative_error']:.3%}\n")
34 f.write(f"\nP3 mechanism: slow sine alpha=0.5 residual ratio={slow_gain['residual_ratio']:.6f}; high sine alpha=1 residual ratio={high_gain['residual_ratio']:.6f}.\n")
35 f.write(f"Baseline versus idea on the quadratic: slow-sine theta RMS {slow_gain['baseline_theta_rms']:.6f} -> {slow_gain['observer_theta_rms']:.6f}; high-sine theta RMS {high_gain['baseline_theta_rms']:.6f} -> {high_gain['observer_theta_rms']:.6f}.\n")
36 f.write('Conclusion: mechanism manifested in this toy setting; slow disturbance cancellation is real, but high observer gain amplifies high-frequency disturbance and is not a universal optimizer win.\n')
37 f.write('Limitations: no neural-network/MNIST experiment, no momentum state, no curvature-bin gain scheduling, no measurement noise, and exact transition identification is more favorable than a real minibatch optimizer.\n')
38print(json.dumps(summary, indent=2))