`gamma` enters only as `gamma * gamma`, so the sign was squared away: measured against the old public-field form, `ConstantDrift(-0.0833)` produced results bit identical to `ConstantDrift(0.0833)`. The sign was neither rejected nor honoured — it vanished. It could not be checked while the field was a public tuple position, because there was nothing to intercept. Validating inside `variance_for_elapsed` would have been worse: it runs in the sweep, so a construction-time mistake would panic mid-inference, and `Gaussian::from_ms` is a worked example of why that is the wrong place — rejecting NaN there turned the NonFiniteResult reporting path into a crash. So `ConstantDrift::new` is the only way in and it checks, with `gamma()` to read the value back. 129 call sites rewritten across src, tests, benches, examples and the README. The dated plan and spec documents under docs/superpowers are left alone: they record what was built at the time, and rewriting them would falsify that. tests/constructor_validation.rs is the more valuable half. This defect class was closed three times in one session and reopened twice, because each fix validated the layer it had just touched and inferred the rest — `HistoryBuilder`, then `Game`'s own entry points, then the constructors beneath both. A per-site fix cannot notice the site nobody thought of, so that file enumerates every public entry point taking a magnitude and asserts each refuses negative and non-finite values. It found an eleventh defect on its first run: `HistoryBuilder::score_sigma` accepted infinity, because `inf > 0.0` is true and the assert only tested positivity. Fixed, and its own `should_panic` message updated to match. `Gaussian::from_ms` is deliberately exempt from the non-finite half, for the reason above: a broken fit produces a NaN sigma legitimately and `converge` must be allowed to report it. The convergence-level drift-variance check stays and is now tested through a custom `Drift` implementation, since `ConstantDrift` can no longer reach it. That check is the only thing standing between a third-party `Drift` and a NaN fit. BREAKING CHANGE: `ConstantDrift`'s field is private. Replace `ConstantDrift(x)` with `ConstantDrift::new(x)`, and `drift().0` with `drift().gamma()`. `HistoryBuilder::score_sigma` now rejects infinity. Closes #65 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011hcFjNDmHXZF8URGLku5zZ
62 lines
2.1 KiB
Rust
62 lines
2.1 KiB
Rust
//! Equivalence tests: every historical golden from the pre-T2 tests is
|
|
//! reproduced here at the integration level via the new public API.
|
|
//!
|
|
//! The in-crate tests in `src/history.rs::tests` and
|
|
//! `src/time_slice.rs::tests` are the primary regression net for numerical
|
|
//! behavior. This file provides Game-level goldens that stand alone and are
|
|
//! more naturally expressed as integration tests.
|
|
|
|
use approx::assert_ulps_eq;
|
|
use trueskill_tt::{ConstantDrift, Game, GameOptions, Gaussian, Outcome, Rating};
|
|
|
|
type R = Rating<i64, ConstantDrift>;
|
|
|
|
fn ts_rating(mu: f64, sigma: f64, beta: f64, gamma: f64) -> R {
|
|
R::new(
|
|
Gaussian::from_ms(mu, sigma),
|
|
beta,
|
|
ConstantDrift::new(gamma),
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn game_1v1_golden_matches_historical() {
|
|
let a = ts_rating(25.0, 25.0 / 3.0, 25.0 / 6.0, 25.0 / 300.0);
|
|
let b = ts_rating(25.0, 25.0 / 3.0, 25.0 / 6.0, 25.0 / 300.0);
|
|
let (a_post, b_post) =
|
|
Game::<i64, _>::one_v_one(&a, &b, Outcome::winner(0, 2), &GameOptions::default()).unwrap();
|
|
// Historical golden from pre-T2 test_1vs1 (team 0 wins):
|
|
assert_ulps_eq!(
|
|
a_post,
|
|
Gaussian::from_ms(29.205220, 7.194481),
|
|
epsilon = 1e-6
|
|
);
|
|
assert_ulps_eq!(
|
|
b_post,
|
|
Gaussian::from_ms(20.794779, 7.194481),
|
|
epsilon = 1e-6
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn game_1v1_draw_golden() {
|
|
let a = ts_rating(25.0, 25.0 / 3.0, 25.0 / 6.0, 25.0 / 300.0);
|
|
let b = ts_rating(25.0, 25.0 / 3.0, 25.0 / 6.0, 25.0 / 300.0);
|
|
let g = Game::<i64, _>::ranked(
|
|
&[&[a], &[b]],
|
|
Outcome::draw(2),
|
|
&GameOptions {
|
|
p_draw: 0.25,
|
|
score_sigma: 1.0,
|
|
convergence: Default::default(),
|
|
},
|
|
)
|
|
.unwrap();
|
|
let p = g.posteriors();
|
|
// Historical golden from pre-T2 test_1vs1_draw. The mean is 25.0 exactly
|
|
// by symmetry — two identical competitors drawing cannot move apart — and
|
|
// the reference's 24.999999 is that value transcribed to six decimals.
|
|
assert_ulps_eq!(p[0][0], Gaussian::from_ms(25.0, 6.469480), epsilon = 1e-6);
|
|
assert_ulps_eq!(p[1][0], Gaussian::from_ms(25.0, 6.469480), epsilon = 1e-6);
|
|
}
|