`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.5 KiB
Rust
73 lines
2.5 KiB
Rust
//! Cost of the joint posterior: factorising versus querying.
|
|
//!
|
|
//! The split is the whole point of `History::joint`. Factorising is `O(n^3)` in
|
|
//! the history's appearances and depends only on the fit; a query is `O(n^2)`
|
|
//! and depends only on the question. `posterior_of_one_shot` pays both every
|
|
//! time, `joint_query` pays only the second.
|
|
|
|
use criterion::{Criterion, criterion_group, criterion_main};
|
|
use smallvec::smallvec;
|
|
use trueskill_tt::{ConstantDrift, ConvergenceOptions, Event, History, Member, Outcome, Team};
|
|
|
|
/// 30 slices of 8 duels: 480 appearances over 100 competitors.
|
|
fn fitted() -> History<i64, ConstantDrift, trueskill_tt::NullObserver, String> {
|
|
let mut h: History<i64, ConstantDrift, _, String> = History::builder()
|
|
.key_type::<String>()
|
|
.mu(0.0)
|
|
.sigma(6.0)
|
|
.beta(1.0)
|
|
.score_sigma(2.0)
|
|
.drift(ConstantDrift::new(0.05))
|
|
.convergence(ConvergenceOptions {
|
|
max_iter: 30,
|
|
epsilon: 1e-10,
|
|
alpha: 1.0,
|
|
})
|
|
.build();
|
|
|
|
let mut events: Vec<Event<i64, String>> = Vec::new();
|
|
let mut k = 0usize;
|
|
for t in 0..30i64 {
|
|
for _ in 0..8 {
|
|
k += 1;
|
|
events.push(Event {
|
|
time: t,
|
|
teams: smallvec![
|
|
Team::with_members([Member::new(format!("p{}", k % 100))]),
|
|
Team::with_members([Member::new(format!("p{}", (k + 37) % 100))]),
|
|
],
|
|
outcome: Outcome::scores([
|
|
(k as f64 * 0.3).sin().abs() * 20.0,
|
|
(k as f64 * 0.3).cos().abs() * 20.0,
|
|
]),
|
|
});
|
|
}
|
|
}
|
|
h.add_events(events).unwrap();
|
|
let _ = h.converge().unwrap();
|
|
h
|
|
}
|
|
|
|
fn bench_joint(c: &mut Criterion) {
|
|
let h = fitted();
|
|
let a = "p0".to_string();
|
|
let b = "p1".to_string();
|
|
let terms = [(&a, 1.0), (&b, -1.0)];
|
|
|
|
c.bench_function("joint_factorise_480_appearances", |bencher| {
|
|
bencher.iter(|| std::hint::black_box(h.joint().unwrap().variables()));
|
|
});
|
|
|
|
c.bench_function("posterior_of_one_shot_480_appearances", |bencher| {
|
|
bencher.iter(|| std::hint::black_box(h.posterior_of(&terms).unwrap()));
|
|
});
|
|
|
|
let joint = h.joint().unwrap();
|
|
c.bench_function("joint_query_480_appearances", |bencher| {
|
|
bencher.iter(|| std::hint::black_box(joint.posterior_of(&terms).unwrap()));
|
|
});
|
|
}
|
|
|
|
criterion_group!(benches, bench_joint);
|
|
criterion_main!(benches);
|