|
| 1 | +//! Animal Farm forward-validation harness — D10 from the original plan. |
| 2 | +//! |
| 3 | +//! Scaffold only. The actual book text + ground-truth labels live in a |
| 4 | +//! follow-up data PR. This file fixes the harness API + asserts the metric |
| 5 | +//! shape we'll measure. |
| 6 | +//! |
| 7 | +//! META-AGENT: integration-test scaffold; no lib.rs wiring required — |
| 8 | +//! `cargo test -p deepnsm --test animal_farm_harness` runs it. |
| 9 | +
|
| 10 | +#![cfg(test)] |
| 11 | + |
| 12 | +#[derive(Debug, Clone)] |
| 13 | +pub struct EpiphanyPrediction { |
| 14 | + pub at_chapter: u32, |
| 15 | + pub direction_phase: f32, |
| 16 | + pub initial_truth_freq: f32, |
| 17 | + pub initial_truth_conf: f32, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Debug, Clone)] |
| 21 | +pub struct GroundTruthBeat { |
| 22 | + pub at_chapter: u32, |
| 23 | + pub confirmed_direction: f32, |
| 24 | + pub matched_predictions: Vec<u32>, |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Debug)] |
| 28 | +pub struct HarnessMetrics { |
| 29 | + pub epiphany_precision: f32, |
| 30 | + pub epiphany_recall: f32, |
| 31 | + pub arc_shift_f1: f32, |
| 32 | + pub direction_accuracy: f32, |
| 33 | +} |
| 34 | + |
| 35 | +pub fn evaluate( |
| 36 | + predictions: &[EpiphanyPrediction], |
| 37 | + ground_truth: &[GroundTruthBeat], |
| 38 | +) -> HarnessMetrics { |
| 39 | + if predictions.is_empty() { |
| 40 | + return HarnessMetrics { |
| 41 | + epiphany_precision: 0.0, |
| 42 | + epiphany_recall: 0.0, |
| 43 | + arc_shift_f1: 0.0, |
| 44 | + direction_accuracy: 0.0, |
| 45 | + }; |
| 46 | + } |
| 47 | + let confirmed: u32 = predictions |
| 48 | + .iter() |
| 49 | + .enumerate() |
| 50 | + .filter(|(i, _)| { |
| 51 | + ground_truth |
| 52 | + .iter() |
| 53 | + .any(|b| b.matched_predictions.contains(&(*i as u32))) |
| 54 | + }) |
| 55 | + .count() as u32; |
| 56 | + let precision = confirmed as f32 / predictions.len() as f32; |
| 57 | + let recall = confirmed as f32 / ground_truth.len().max(1) as f32; |
| 58 | + HarnessMetrics { |
| 59 | + epiphany_precision: precision, |
| 60 | + epiphany_recall: recall, |
| 61 | + arc_shift_f1: 2.0 * precision * recall / (precision + recall + 1e-9), |
| 62 | + direction_accuracy: 0.0, // populated when phase-direction comparison lands |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +#[test] |
| 67 | +fn harness_metrics_zero_for_empty_predictions() { |
| 68 | + let m = evaluate(&[], &[]); |
| 69 | + assert_eq!(m.epiphany_precision, 0.0); |
| 70 | + assert_eq!(m.epiphany_recall, 0.0); |
| 71 | +} |
| 72 | + |
| 73 | +#[test] |
| 74 | +fn harness_metrics_perfect_for_all_confirmed() { |
| 75 | + let preds = vec![EpiphanyPrediction { |
| 76 | + at_chapter: 3, |
| 77 | + direction_phase: 0.0, |
| 78 | + initial_truth_freq: 0.6, |
| 79 | + initial_truth_conf: 0.5, |
| 80 | + }]; |
| 81 | + let gt = vec![GroundTruthBeat { |
| 82 | + at_chapter: 5, |
| 83 | + confirmed_direction: 0.0, |
| 84 | + matched_predictions: vec![0], |
| 85 | + }]; |
| 86 | + let m = evaluate(&preds, >); |
| 87 | + assert_eq!(m.epiphany_precision, 1.0); |
| 88 | + assert_eq!(m.epiphany_recall, 1.0); |
| 89 | +} |
| 90 | + |
| 91 | +#[test] |
| 92 | +fn harness_metrics_zero_when_no_predictions_match() { |
| 93 | + let preds = vec![EpiphanyPrediction { |
| 94 | + at_chapter: 1, |
| 95 | + direction_phase: 0.0, |
| 96 | + initial_truth_freq: 0.5, |
| 97 | + initial_truth_conf: 0.5, |
| 98 | + }]; |
| 99 | + let gt = vec![GroundTruthBeat { |
| 100 | + at_chapter: 2, |
| 101 | + confirmed_direction: 0.0, |
| 102 | + matched_predictions: vec![], // no matches |
| 103 | + }]; |
| 104 | + let m = evaluate(&preds, >); |
| 105 | + assert_eq!(m.epiphany_precision, 0.0); |
| 106 | + assert_eq!(m.epiphany_recall, 0.0); |
| 107 | +} |
| 108 | + |
| 109 | +#[test] |
| 110 | +fn harness_f1_is_harmonic_mean() { |
| 111 | + // 2 predictions, 1 confirmed -> precision 0.5 |
| 112 | + // 1 ground-truth beat -> recall 1.0 |
| 113 | + // F1 = 2 * 0.5 * 1.0 / (0.5 + 1.0) = 0.6666... |
| 114 | + let preds = vec![ |
| 115 | + EpiphanyPrediction { |
| 116 | + at_chapter: 1, |
| 117 | + direction_phase: 0.0, |
| 118 | + initial_truth_freq: 0.5, |
| 119 | + initial_truth_conf: 0.5, |
| 120 | + }, |
| 121 | + EpiphanyPrediction { |
| 122 | + at_chapter: 2, |
| 123 | + direction_phase: 0.0, |
| 124 | + initial_truth_freq: 0.5, |
| 125 | + initial_truth_conf: 0.5, |
| 126 | + }, |
| 127 | + ]; |
| 128 | + let gt = vec![GroundTruthBeat { |
| 129 | + at_chapter: 4, |
| 130 | + confirmed_direction: 0.0, |
| 131 | + matched_predictions: vec![0], |
| 132 | + }]; |
| 133 | + let m = evaluate(&preds, >); |
| 134 | + assert!((m.arc_shift_f1 - 2.0 / 3.0).abs() < 1e-3); |
| 135 | +} |
0 commit comments