veridict 0.19.7

Statistical regression gate for A/B and hypothesis testing: compare candidate vs baseline results with win-rate, Elo, SPRT, and bootstrap confidence intervals.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! Reward schedules for time-sensitive testing: how much a rejection at time `t` is worth.
//!
//! Every schedule here is non-negative and non-increasing in `t`, with reward at infinite time
//! interpreted as 0 (`R(infinity) = 0`) - a decision that never arrives isn't worth waiting for,
//! no matter how strong the evidence eventually gets. `grid.rs`'s backward induction needs a
//! finite horizon to start from; each variant below documents what its own truncation actually
//! discards, rather than asserting the discarded tail is always negligible.

use crate::error::VeridictError;

/// A tabulated schedule is deliberately dense (`rewards[t]`), so cap the
/// sparse-file expansion before allocation. Larger horizons should use a
/// formula-backed schedule rather than materializing millions of entries.
const MAX_TABULATED_HORIZON: u64 = 1_000_000;

#[derive(Debug, Clone)]
pub enum RewardSchedule {
    HardDeadline {
        deadline: u64,
    },
    ExponentialDecay {
        time_scale: f64,
        horizon: u64,
    },
    /// Already-expanded per-`t` values, `rewards[t] = R(t)` for `t` in `0..=horizon()`. Built by
    /// hand for a library caller, or via [`expand_schedule_file`] from the CLI's sparse
    /// breakpoint JSON format.
    Tabulated {
        rewards: Vec<f64>,
    },
}

impl RewardSchedule {
    /// Non-negative, non-increasing, at least one entry - the three properties every reward
    /// schedule must have for "expected reward" to be a well-posed objective at all. Called from
    /// `TimeSensitiveConfig`'s own validation, not automatically on construction (a library
    /// caller building a `Tabulated` schedule by hand may want to construct first, validate
    /// later).
    pub(crate) fn validate(&self) -> Result<(), VeridictError> {
        match self {
            RewardSchedule::HardDeadline { deadline } => {
                if *deadline == 0 {
                    return Err(VeridictError::InvalidThreshold(
                        "--deadline must be >= 1".to_string(),
                    ));
                }
            }
            RewardSchedule::ExponentialDecay {
                time_scale,
                horizon,
            } => {
                if !time_scale.is_finite() || *time_scale <= 0.0 {
                    return Err(VeridictError::InvalidThreshold(format!(
                        "--time-scale must be finite and > 0, got {time_scale}"
                    )));
                }
                if *horizon == 0 {
                    return Err(VeridictError::InvalidThreshold(
                        "--horizon must be >= 1".to_string(),
                    ));
                }
            }
            RewardSchedule::Tabulated { rewards } => {
                // len - 1 (not len) is the schedule's horizon (see `horizon()`) - requiring at
                // least 2 entries keeps that horizon >= 1, the same floor `HardDeadline`/
                // `ExponentialDecay` enforce on their own horizon-shaped parameter.
                if rewards.len() < 2 {
                    return Err(VeridictError::InvalidThreshold(
                        "tabulated reward schedule must have at least 2 entries (t=0 and t=1), \
                         so its horizon is >= 1"
                            .to_string(),
                    ));
                }
                let mut prev = f64::INFINITY;
                for (t, &r) in rewards.iter().enumerate() {
                    if !r.is_finite() || r < 0.0 {
                        return Err(VeridictError::InvalidThreshold(format!(
                            "tabulated reward at t={t} must be finite and >= 0, got {r}"
                        )));
                    }
                    if r > prev {
                        return Err(VeridictError::InvalidThreshold(format!(
                            "tabulated reward schedule must be non-increasing: reward at t={t} \
                             ({r}) exceeds reward at t={} ({prev})",
                            t - 1
                        )));
                    }
                    prev = r;
                }
            }
        }
        Ok(())
    }

    /// Finite truncation horizon for `grid.rs`'s backward induction: the value iteration starts
    /// here and works back to `t=0`, treating "still running at `t=horizon`" as worth exactly
    /// `R(horizon)` (see `grid.rs`'s terminal boundary). `R(t)` for `t` beyond this is never
    /// evaluated - see `truncation_note` for what that actually discards.
    pub fn horizon(&self) -> u64 {
        match self {
            RewardSchedule::HardDeadline { deadline } => *deadline,
            RewardSchedule::ExponentialDecay { horizon, .. } => *horizon,
            RewardSchedule::Tabulated { rewards } => rewards.len() as u64 - 1,
        }
    }

    /// `R(t)`: reward for a rejection landing exactly at time `t`. `t > horizon()` returns 0,
    /// matching the `R(infinity) = 0` convention - this schedule was never going to reward
    /// waiting that long.
    pub fn at(&self, t: u64) -> f64 {
        match self {
            RewardSchedule::HardDeadline { deadline } => {
                if t <= *deadline {
                    1.0
                } else {
                    0.0
                }
            }
            RewardSchedule::ExponentialDecay {
                time_scale,
                horizon,
            } => {
                if t > *horizon {
                    0.0
                } else {
                    (-(t as f64) / time_scale).exp()
                }
            }
            RewardSchedule::Tabulated { rewards } => {
                rewards.get(t as usize).copied().unwrap_or(0.0)
            }
        }
    }

    pub(crate) fn kind_label(&self) -> &'static str {
        match self {
            RewardSchedule::HardDeadline { .. } => "hard_deadline",
            RewardSchedule::ExponentialDecay { .. } => "exponential_decay",
            RewardSchedule::Tabulated { .. } => "tabulated",
        }
    }

    /// Caller-facing parameters, for `TimeSensitiveReport::reward_parameters`.
    pub(crate) fn parameters(&self) -> serde_json::Value {
        match self {
            RewardSchedule::HardDeadline { deadline } => {
                serde_json::json!({ "deadline": deadline })
            }
            RewardSchedule::ExponentialDecay {
                time_scale,
                horizon,
            } => {
                serde_json::json!({ "time_scale": time_scale, "horizon": horizon })
            }
            RewardSchedule::Tabulated { rewards } => {
                serde_json::json!({ "horizon": self.horizon(), "entries": rewards.len() })
            }
        }
    }

    /// One sentence on what this schedule's finite-horizon truncation actually discards -
    /// `HardDeadline` truncates exactly (reward really is 0 past the deadline, not an
    /// approximation of anything); `ExponentialDecay`/`Tabulated` report the residual reward
    /// value at the horizon so a caller can judge negligibility for their own alpha/horizon
    /// choice, rather than veridict silently asserting it always is.
    pub(crate) fn truncation_note(&self) -> String {
        match self {
            RewardSchedule::HardDeadline { deadline } => format!(
                "hard-deadline reward is exact: R(t) = 0 for t > {deadline} by construction, not \
                 a truncation approximation"
            ),
            RewardSchedule::ExponentialDecay { horizon, .. } => {
                let residual = self.at(*horizon);
                format!(
                    "exponential-decay reward truncated at horizon={horizon}: R(horizon) = \
                     {residual:.3e} against R(0) = 1.0 is discarded for t > horizon; judge \
                     negligibility against your own horizon-to-time_scale ratio before trusting \
                     planned_expected_reward_under_p1 at a small ratio"
                )
            }
            RewardSchedule::Tabulated { rewards } => {
                let horizon = self.horizon();
                let last = *rewards.last().unwrap_or(&0.0);
                format!(
                    "tabulated reward truncated at horizon={horizon}: last declared value is \
                     {last}; R(t) = 0 for t > horizon"
                )
            }
        }
    }
}

/// One entry of a user-supplied reward-schedule JSON file (`--reward-schedule`). Exactly one of
/// `until`/`after` is set per entry: `until` declares "value holds for t <= until" (there may be
/// several, in any order), `after` declares the tail beyond the largest `until`. Kept as a
/// separate on-disk shape from `RewardSchedule::Tabulated`'s already-expanded `Vec<f64>` - the
/// sparse breakpoint form is what a human writes, the dense per-`t` form is what `grid.rs` needs.
#[derive(Debug, serde::Deserialize)]
pub struct RewardScheduleEntry {
    pub until: Option<u64>,
    pub after: Option<u64>,
    pub value: f64,
}

#[derive(Debug, serde::Deserialize)]
pub struct RewardScheduleFile {
    pub rewards: Vec<RewardScheduleEntry>,
}

/// Expands a sparse breakpoint list into `RewardSchedule::Tabulated`'s dense per-`t` form.
///
/// The tail (`after`) value is required to be exactly `0.0`: the module's `R(infinity) = 0`
/// invariant has no finite representation of "stays worth `V` forever" for `V != 0`, so a
/// nonzero tail isn't a value this schedule shape can express, not merely an unusual one.
pub fn expand_schedule_file(file: &RewardScheduleFile) -> Result<RewardSchedule, VeridictError> {
    let mut untils: Vec<(u64, f64)> = Vec::new();
    let mut after: Option<(u64, f64)> = None;
    for entry in &file.rewards {
        match (entry.until, entry.after) {
            (Some(u), None) => untils.push((u, entry.value)),
            (None, Some(_)) if after.is_some() => {
                return Err(VeridictError::InvalidThreshold(
                    "reward schedule file must declare at most one 'after' entry".to_string(),
                ));
            }
            (None, Some(boundary)) => after = Some((boundary, entry.value)),
            _ => {
                return Err(VeridictError::InvalidThreshold(
                    "each reward schedule entry must set exactly one of 'until'/'after'"
                        .to_string(),
                ));
            }
        }
    }
    let Some((after_boundary, after)) = after else {
        return Err(VeridictError::InvalidThreshold(
            "reward schedule file must declare exactly one 'after' entry for the tail value"
                .to_string(),
        ));
    };
    if after != 0.0 {
        return Err(VeridictError::InvalidThreshold(format!(
            "reward schedule file's 'after' entry must be 0.0 (got {after}): R(t) as t -> \
             infinity must be 0 by definition, so a schedule that stays rewarding forever has no \
             well-defined finite-horizon Bellman problem"
        )));
    }
    if untils.is_empty() {
        return Err(VeridictError::InvalidThreshold(
            "reward schedule file must declare at least one 'until' entry".to_string(),
        ));
    }
    untils.sort_by_key(|(u, _)| *u);
    for pair in untils.windows(2) {
        if pair[0].0 == pair[1].0 {
            return Err(VeridictError::InvalidThreshold(format!(
                "reward schedule file declares 'until: {}' more than once",
                pair[0].0
            )));
        }
    }
    let horizon = untils.last().unwrap().0;
    if after_boundary != horizon {
        return Err(VeridictError::InvalidThreshold(format!(
            "reward schedule file's 'after' boundary ({after_boundary}) must equal the largest \
             'until' boundary ({horizon})"
        )));
    }
    if horizon > MAX_TABULATED_HORIZON {
        return Err(VeridictError::InvalidThreshold(format!(
            "tabulated reward schedule horizon {horizon} exceeds the supported maximum of \
             {MAX_TABULATED_HORIZON}; use a formula-backed hard-deadline or exponential schedule \
             for larger horizons"
        )));
    }
    let len = usize::try_from(horizon)
        .ok()
        .and_then(|h| h.checked_add(1))
        .ok_or_else(|| {
            VeridictError::InvalidThreshold(format!(
                "tabulated reward schedule horizon {horizon} cannot be represented on this platform"
            ))
        })?;
    let mut rewards = vec![after; len];
    // Fill widest-range (largest `until`) first, then let each narrower, smaller-`until` entry
    // overwrite its own prefix - matches the format's "first matching until wins" reading of a
    // breakpoint list (see the spec's own 400/1600/3200 example).
    for &(until, value) in untils.iter().rev() {
        for slot in rewards.iter_mut().take(until as usize + 1) {
            *slot = value;
        }
    }
    let schedule = RewardSchedule::Tabulated { rewards };
    schedule.validate()?;
    Ok(schedule)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn hard_deadline_is_one_then_zero() {
        let s = RewardSchedule::HardDeadline { deadline: 400 };
        assert_eq!(s.at(0), 1.0);
        assert_eq!(s.at(400), 1.0);
        assert_eq!(s.at(401), 0.0);
        assert_eq!(s.horizon(), 400);
        s.validate().unwrap();
    }

    #[test]
    fn hard_deadline_zero_is_rejected() {
        assert!(
            RewardSchedule::HardDeadline { deadline: 0 }
                .validate()
                .is_err()
        );
    }

    #[test]
    fn exponential_decay_is_monotonic_and_positive() {
        let s = RewardSchedule::ExponentialDecay {
            time_scale: 800.0,
            horizon: 3200,
        };
        s.validate().unwrap();
        assert_eq!(s.at(0), 1.0);
        assert!(s.at(800) < s.at(0));
        assert!(s.at(3200) < s.at(800));
        assert_eq!(s.at(3201), 0.0);
    }

    #[test]
    fn exponential_decay_rejects_non_positive_time_scale() {
        assert!(
            RewardSchedule::ExponentialDecay {
                time_scale: 0.0,
                horizon: 100,
            }
            .validate()
            .is_err()
        );
        assert!(
            RewardSchedule::ExponentialDecay {
                time_scale: -1.0,
                horizon: 100,
            }
            .validate()
            .is_err()
        );
    }

    #[test]
    fn tabulated_rejects_negative_reward() {
        let s = RewardSchedule::Tabulated {
            rewards: vec![1.0, -0.1],
        };
        assert!(s.validate().is_err());
    }

    #[test]
    fn tabulated_rejects_increasing_reward() {
        let s = RewardSchedule::Tabulated {
            rewards: vec![0.1, 0.4],
        };
        assert!(s.validate().is_err());
    }

    #[test]
    fn expand_schedule_file_matches_spec_example() {
        let file = RewardScheduleFile {
            rewards: vec![
                RewardScheduleEntry {
                    until: Some(400),
                    after: None,
                    value: 1.0,
                },
                RewardScheduleEntry {
                    until: Some(1600),
                    after: None,
                    value: 0.4,
                },
                RewardScheduleEntry {
                    until: Some(3200),
                    after: None,
                    value: 0.1,
                },
                RewardScheduleEntry {
                    until: None,
                    after: Some(3200),
                    value: 0.0,
                },
            ],
        };
        let schedule = expand_schedule_file(&file).unwrap();
        assert_eq!(schedule.horizon(), 3200);
        assert_eq!(schedule.at(0), 1.0);
        assert_eq!(schedule.at(400), 1.0);
        assert_eq!(schedule.at(401), 0.4);
        assert_eq!(schedule.at(1600), 0.4);
        assert_eq!(schedule.at(1601), 0.1);
        assert_eq!(schedule.at(3200), 0.1);
        assert_eq!(schedule.at(3201), 0.0);
    }

    #[test]
    fn expand_schedule_file_rejects_nonzero_tail() {
        let file = RewardScheduleFile {
            rewards: vec![
                RewardScheduleEntry {
                    until: Some(100),
                    after: None,
                    value: 1.0,
                },
                RewardScheduleEntry {
                    until: None,
                    after: Some(100),
                    value: 0.2,
                },
            ],
        };
        assert!(expand_schedule_file(&file).is_err());
    }

    #[test]
    fn expand_schedule_file_requires_an_after_entry() {
        let file = RewardScheduleFile {
            rewards: vec![RewardScheduleEntry {
                until: Some(100),
                after: None,
                value: 1.0,
            }],
        };
        assert!(expand_schedule_file(&file).is_err());
    }

    #[test]
    fn expand_schedule_file_rejects_mismatched_after_boundary() {
        let file = RewardScheduleFile {
            rewards: vec![
                RewardScheduleEntry {
                    until: Some(100),
                    after: None,
                    value: 1.0,
                },
                RewardScheduleEntry {
                    until: None,
                    after: Some(99),
                    value: 0.0,
                },
            ],
        };
        assert!(expand_schedule_file(&file).is_err());
    }

    #[test]
    fn expand_schedule_file_rejects_huge_horizon_before_allocation() {
        let file = RewardScheduleFile {
            rewards: vec![
                RewardScheduleEntry {
                    until: Some(u64::MAX),
                    after: None,
                    value: 1.0,
                },
                RewardScheduleEntry {
                    until: None,
                    after: Some(u64::MAX),
                    value: 0.0,
                },
            ],
        };
        let error = expand_schedule_file(&file).unwrap_err();
        assert!(error.to_string().contains("supported maximum"));
    }
}