adk_eval/experiments/
sequential.rs1use serde::{Deserialize, Serialize};
5
6use crate::error::{EvalError, Result};
7
8#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
42#[serde(rename_all = "camelCase")]
43pub struct SequentialBoundary {
44 alpha: f64,
45 mixing_rate: f64,
46}
47
48impl SequentialBoundary {
49 pub fn new(alpha: f64, planned_effective_units: f64) -> Result<Self> {
56 if !(alpha > 0.0 && alpha < 1.0) {
57 return Err(EvalError::ConfigError(format!(
58 "sequential boundary alpha = {alpha}; use a value in (0, 1)"
59 )));
60 }
61 if !(planned_effective_units.is_finite() && planned_effective_units > 0.0) {
62 return Err(EvalError::ConfigError(format!(
63 "sequential boundary planned size = {planned_effective_units}; use a positive \
64 number of effective units"
65 )));
66 }
67 let c = 2.0 * (1.0 / alpha).ln();
68 Ok(Self { alpha, mixing_rate: (c + (1.0 + c).ln()) / planned_effective_units })
69 }
70
71 pub fn alpha(&self) -> f64 {
73 self.alpha
74 }
75
76 pub fn mixing_rate(&self) -> f64 {
78 self.mixing_rate
79 }
80
81 pub fn critical_z(&self, effective_units: f64) -> f64 {
83 let x = self.mixing_rate * effective_units;
84 if x.is_nan() || x <= 0.0 {
85 return f64::INFINITY;
86 }
87 ((1.0 + x) / x * (2.0 * (1.0 / self.alpha).ln() + x.ln_1p())).sqrt()
88 }
89
90 pub fn p_value(&self, z: f64, effective_units: f64) -> f64 {
95 let x = self.mixing_rate * effective_units;
96 if x.is_nan() || x <= 0.0 || !z.is_finite() {
97 return 1.0;
98 }
99 let log_inverse_lambda = 0.5 * x.ln_1p() - x * z * z / (2.0 * (1.0 + x));
100 log_inverse_lambda.exp().min(1.0)
101 }
102}
103
104#[cfg(test)]
105mod tests {
106 use super::*;
107
108 fn waudby_smith(alpha: f64, rho2: f64, t: f64) -> f64 {
112 (2.0 * (t * rho2 + 1.0) / (t * t * rho2) * ((t * rho2 + 1.0).sqrt() / alpha).ln()).sqrt()
113 * t.sqrt()
114 }
115
116 #[test]
117 fn critical_z_matches_the_published_confidence_sequence() {
118 for (alpha, planned, n) in [
119 (0.05, 5_000.0, 5_000.0),
120 (0.05, 5_000.0, 100.0),
121 (0.05, 5_000.0, 50_000.0),
122 (0.1, 500.0, 250.0),
123 (0.01, 20_000.0, 1_000.0),
124 ] {
125 let boundary = SequentialBoundary::new(alpha, planned).unwrap();
126 let expected = waudby_smith(alpha, boundary.mixing_rate(), n);
127 assert!((boundary.critical_z(n) - expected).abs() < 1e-12, "{alpha} {planned} {n}");
128 }
129 }
130
131 #[test]
133 fn critical_z_matches_reference_values() {
134 let boundary = SequentialBoundary::new(0.05, 5_000.0).unwrap();
135 assert!((boundary.mixing_rate() - 0.001587230920347513).abs() < 1e-15);
136 assert!((boundary.critical_z(5_000.0) - 3.0352089903762534).abs() < 1e-12);
137 assert!((boundary.critical_z(100.0) - 6.69438861706604).abs() < 1e-12);
138 assert!((boundary.critical_z(50_000.0) - 3.241723145405438).abs() < 1e-12);
139 }
140
141 #[test]
142 fn the_boundary_is_tightest_near_the_planned_size() {
143 let boundary = SequentialBoundary::new(0.05, 5_000.0).unwrap();
144 let at_plan = boundary.critical_z(5_000.0);
145 for n in [500.0, 2_000.0, 20_000.0, 200_000.0] {
146 assert!(boundary.critical_z(n) > at_plan, "n = {n}");
147 }
148 assert!(at_plan > 1.959_963_984_540_054);
149 }
150
151 #[test]
152 fn no_units_means_no_evidence() {
153 let boundary = SequentialBoundary::new(0.05, 5_000.0).unwrap();
154 assert_eq!(boundary.critical_z(0.0), f64::INFINITY);
155 assert_eq!(boundary.p_value(10.0, 0.0), 1.0);
156 assert_eq!(boundary.p_value(0.0, 5_000.0), 1.0);
157 }
158
159 #[test]
160 fn invalid_settings_are_refused() {
161 assert!(SequentialBoundary::new(0.0, 10.0).is_err());
162 assert!(SequentialBoundary::new(1.0, 10.0).is_err());
163 assert!(SequentialBoundary::new(0.05, 0.0).is_err());
164 assert!(SequentialBoundary::new(0.05, f64::INFINITY).is_err());
165 }
166}