import json from pathlib import Path x = json.loads(Path('observer_results.json').read_text()) stab_ok = all(r['relative_error'] <= 0.01 for r in x['stability']) rows = x['disturbance_rows'] slow = [r for r in rows if r['case'] == 'slow_sine'] high = [r for r in rows if r['case'] == 'high_sine'] response_ok = all(r['ratio_relative_error'] is not None and r['ratio_relative_error'] < 0.02 for r in slow + high) slow_gain = next(r for r in slow if r['alpha'] == 0.5) high_gain = next(r for r in high if r['alpha'] == 1.0) summary = { 'prediction_checks': { 'P1_stability_boundary': {'pass': stab_ok, 'max_relative_error': max(r['relative_error'] for r in x['stability'])}, 'P2_ema_frequency_response': {'pass': response_ok, 'max_relative_error': max(r['ratio_relative_error'] for r in slow + high)}, 'P3_slow_vs_high_frequency': { 'pass': slow_gain['residual_ratio'] < 0.1 and high_gain['residual_ratio'] > 1.0, 'slow_alpha_0.5_ratio': slow_gain['residual_ratio'], 'high_alpha_1.0_ratio': high_gain['residual_ratio']}} } Path('observer_report.json').write_text(json.dumps(summary, indent=2)) with open('observer_report.txt', 'w') as f: f.write('Observer-Corrected Robust Optimizer MVP report\n\n') f.write('Model: scalar quadratic f(theta)=lambda*theta, gradient disturbance d[k].\n') f.write('The observer is an EMA of the one-step transition residual; d_hat is fed forward with a subtractive correction.\n\n') f.write('Prediction checks (observed versus predicted)\n') f.write('P1 stability: h_critical predicted=2/lambda; observed grid brackets are in observer_results.json.\n') for r in x['stability']: 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") f.write('P2 frequency response: predicted and observed residual RMS ratios:\n') for r in rows: if r['case'] != 'constant': 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") 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") 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") 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') 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') print(json.dumps(summary, indent=2))