feat: factorise the joint once with History::joint
`posterior_of`, `posterior_of_at` and `expected_variance_reduction` each
built the joint precision matrix, factorised it, asked one question and
threw it away. The factorisation is O(n^3) in the history's appearances
and depends only on the fit, so a caller asking about every pair in a
standings table, every cell in a grid, or every candidate in an
active-learning sweep paid for the same factorisation once per question.
`History::joint()` returns a `Joint` handle that pays it once. Measured
on 1976 appearances, 90 queries: 68.4s one-shot against 745ms factorise
plus 93ms of queries — 81.6x, with bit-identical answers. Per query,
Criterion at 480 appearances: 9.0ms one-shot against 48us cached, 187x.
The handle borrows the history, which is what makes it correct with no
invalidation logic: the borrow checker forbids adding events or refitting
while it is alive, so there is no window in which the factorisation could
describe a fit that no longer exists. It also makes the lifetime of the
n^2 factor explicit rather than parking it in the history forever — at
4000 appearances that is 128MB, which is not something to cache silently.
Every question the joint answers turns out to be a bilinear form,
c^T A^-1 a = (L^-1 c) . (L^-1 a)
so no caller ever needs L^-1 c itself. Replacing the general solve with a
forward substitution drops the back substitution as wasted work, halving
a query, and removes a failure mode: a variance as `c . (A^-1 c)` is a
difference of products that can round negative, where `|L^-1 c|^2` is a
sum of squares and cannot.
The one-shot calls are unchanged in cost and now delegate to the handle,
so the two paths cannot drift apart. tests/joint_handle.rs asserts they
agree bit for bit, including at pinned times, under UnknownKeys::Prior,
and across candidate matchups.
Refs #51
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011hcFjNDmHXZF8URGLku5zZ
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
//! 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_with_key()
|
||||
.mu(0.0)
|
||||
.sigma(6.0)
|
||||
.beta(1.0)
|
||||
.score_sigma(2.0)
|
||||
.drift(ConstantDrift(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);
|
||||
Reference in New Issue
Block a user