`History<T: Time, ..>` has always been generic over the time axis,
`Untimed` has always been exported, and `Drift<T>` is generic specifically
so that "seasonal or calendar-aware drift is expressible without going
through i64". None of it was reachable from a downstream crate.
Every construction route pinned `T = i64`: `History::builder()`,
`History::builder_with_key()`, and the only `Default` impl on
`HistoryBuilder`. Its fields are private and it had no `new`. So all three
escape routes failed to compile, and a consumer with domain timestamps
had to convert to i64 — which is the exact thing the parameter exists to
avoid. One of `History`'s four type parameters was paid for at every
signature and could never be varied.
`Default` is now generic over `T` and `K`, `HistoryBuilder::new()` exists,
and `time_type::<T2>()` / `key_type::<K2>()` join `drift` and `observer`
as type-changing setters:
History::builder().time_type::<Untimed>().build()
History::builder().key_type::<String>().build()
HistoryBuilder::<Season, _, _, String>::new().build()
`key_type` replaces `builder_with_key`, which could not be turbofished —
`K` sat on the impl rather than the function, so callers had to spell
`History::<i64, _, _, String>::builder_with_key()`. 18 call sites across
15 files migrated.
tests/time_axis.rs is the part that matters. NOTHING in the repository
constructed a non-i64 history, which is precisely why this survived, so
the fix is only half done without a test that exercises the generic. It
defines a `Season(u16)` time type and a `SeasonalDrift` that accumulates
between seasons but not within one — the calendar-aware case the trait's
docs cite — and checks the whole path: fit, converge, and read a learning
curve whose times come back as `Season`, not as integers.
Two of the six tests are controls rather than assertions about output.
`Untimed` must ignore drift entirely, since elapsed is always zero, so
gamma 0.0 and gamma 5.0 must agree bit for bit. And a custom `Drift` must
actually widen a gap across seasons, or the test above would pass whether
or not the drift was consulted at all.
The README's ticked "Generalise a time axis" box is now true.
BREAKING CHANGE: `History::builder_with_key()` is removed. Use
`History::builder().key_type::<K>()`.
Closes #68
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011hcFjNDmHXZF8URGLku5zZ
73 lines
2.4 KiB
Rust
73 lines
2.4 KiB
Rust
//! Regression: a single time slice with many distinct competitors must converge to finite
|
|
//! skills. Before the `pi <= 0` guard in `Gaussian::mu()/sigma()`, EP message cancellation
|
|
//! produced a tiny-negative precision whose `sigma() = 1/sqrt(pi)` was NaN, which the
|
|
//! moment-space `Sub` in the game chain propagated into every skill once the slice grew past
|
|
//! ~75 competitors (e.g. a real ranking dataset with hundreds of players).
|
|
use trueskill_tt::{ConstantDrift, ConvergenceOptions, EPSILON, History, ITERATIONS, NullObserver};
|
|
|
|
/// Tiny deterministic LCG — avoids a dev-dependency on `rand`.
|
|
struct Lcg(u64);
|
|
impl Lcg {
|
|
fn next(&mut self) -> u64 {
|
|
self.0 = self
|
|
.0
|
|
.wrapping_mul(6364136223846793005)
|
|
.wrapping_add(1442695040888963407);
|
|
self.0
|
|
}
|
|
fn below(&mut self, n: usize) -> usize {
|
|
(self.next() >> 33) as usize % n
|
|
}
|
|
fn coin(&mut self) -> bool {
|
|
self.next() & 1 == 0
|
|
}
|
|
}
|
|
|
|
fn nan_after_fit(players: usize) -> usize {
|
|
let mut h: History<i64, ConstantDrift, NullObserver, String> = History::builder()
|
|
.key_type::<String>()
|
|
.beta(1.0)
|
|
.sigma(6.0)
|
|
.drift(ConstantDrift::new(0.1))
|
|
.convergence(ConvergenceOptions {
|
|
max_iter: ITERATIONS,
|
|
epsilon: EPSILON,
|
|
..Default::default()
|
|
})
|
|
.build();
|
|
|
|
let ids: Vec<String> = (0..players).map(|i| format!("p{i:04}")).collect();
|
|
let mut rng = Lcg(1);
|
|
for _ in 0..(players * 4) {
|
|
let a = rng.below(players);
|
|
let mut b = rng.below(players - 1);
|
|
if b >= a {
|
|
b += 1;
|
|
}
|
|
let (w, l) = if rng.coin() { (a, b) } else { (b, a) };
|
|
h.record_winner(&ids[w], &ids[l], 0).unwrap();
|
|
}
|
|
let _ = h.converge().unwrap();
|
|
|
|
ids.iter()
|
|
.filter(|id| {
|
|
h.current_skill(id.as_str())
|
|
.map(|g| !g.mu().is_finite() || !g.sigma().is_finite())
|
|
.unwrap_or(true)
|
|
})
|
|
.count()
|
|
}
|
|
|
|
#[test]
|
|
fn many_competitors_converge_to_finite_skills() {
|
|
// The NaN regression onset was between 70 and 80 competitors; 250 is comfortably past it
|
|
// and in the range of a real ranking dataset.
|
|
for players in [12usize, 75, 150, 250] {
|
|
assert_eq!(
|
|
nan_after_fit(players),
|
|
0,
|
|
"{players}-competitor history produced NaN skills"
|
|
);
|
|
}
|
|
}
|