feat!: name the unknown key, expose tail probabilities, flag short fits

Three issues from two downstream consumers, all small, all sharing a
theme: the crate had the information and would not hand it over.

#44 — `UnknownKey { team: 0, member: 0 }` did not say which key. A
consumer upgrading 0.1.2 -> 0.4.1 had every one of 5591 predictions
return this error, fell back to a neutral 0.5, and lost its entire
metadata model for a day. Nothing crashed and nothing logged; it was
found by sweeping an unrelated parameter and noticing the output did not
move. The 0.4.0 change that made unknown keys an error was right — the
error was just too anonymous to act on. It now carries the key's `Debug`
rendering, and its `Display` says what to do about it. The precondition
is documented on every prediction entry point, which the reporter said
would alone have saved the day.

#43 — `cdf` was `pub(crate)`, so a consumer asking "is this competitor
below the cutoff" approximated it with a `mu + z * sigma` band and had no
way to say what confidence any `z` bought. Adds
`Gaussian::probability_below` / `probability_above`. The second is
separate on purpose: `1 - cdf` collapses to exactly zero past ~8.3
sigma, and a stopping rule is evaluated precisely there. Both route
through the survival function added in 0.4.1, so this is visibility
rather than new numerics.

#50 — `ConvergenceReport` was not `#[must_use]`, so the one signal that
a fit stopped short was trivially discarded. It now is, and that
immediately found 78 sites doing exactly that — including this crate's
own ATP example, which was capped at 10 sweeps when the history needs
30. The example now reads the report and says so.

`ITERATIONS = 30` is documented as the floor it is, with the three
measurements to hand: 400 events over 100 competitors already stops
there at ~7e-3 against a 1e-6 tolerance, the ATP example needs 30 at a
much looser one, and a consumer's 2000-node model needs 76 to 161.

BREAKING CHANGE: `InferenceError::UnknownKey` gains a `key` field, and
the prediction methods now require `K: Debug` in order to fill it.

Closes #43, #50. Refs #44 — its third ask, an opt-in `UnknownKeys::Skip`
mode, is a live API question and deliberately not answered here.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011hcFjNDmHXZF8URGLku5zZ
This commit is contained in:
2026-09-07 23:41:03 +02:00
co-authored by Claude Opus 5
parent 901f60972e
commit c12bc830a5
21 changed files with 396 additions and 97 deletions
+25 -1
View File
@@ -196,7 +196,31 @@ stay available at any size:
- `predict_ranking(teams, ranks)` — one specific finishing order. - `predict_ranking(teams, ranks)` — one specific finishing order.
Unknown keys are an error, not a silent omission: a team the history has never Unknown keys are an error, not a silent omission: a team the history has never
seen cannot produce a confident-looking probability. seen cannot produce a confident-looking probability. The error names the key, and
every key must already be known — pre-filter with `lookup` or `current_skill` if
your caller cannot guarantee that.
### Asking about one competitor
`Gaussian` answers tail questions directly, which is what a stopping rule needs:
```rust
use trueskill_tt::History;
let mut h = History::builder().build();
h.record_winner(&"alice", &"bob", 1).unwrap();
let _ = h.converge().unwrap();
let skill = h.current_skill(&"alice").unwrap();
// "How sure am I that this is below the cutoff?" — a probability, not a
// `mu + z * sigma` band whose confidence drifts as sigma changes.
let _ = skill.probability_below(20.0);
// Use this rather than `1.0 - probability_below(x)`: the complement cancels
// away every digit in the upper tail, which is where a stopping rule lives.
let _ = skill.probability_above(30.0);
```
## Which match to play next ## Which match to play next
+3 -3
View File
@@ -82,7 +82,7 @@ fn bench_converge(c: &mut Criterion) {
b.iter_batched( b.iter_batched(
|| build_history_1v1(500, 100, 10, 42), || build_history_1v1(500, 100, 10, 42),
|mut h| { |mut h| {
h.converge().unwrap(); let _ = h.converge().unwrap();
}, },
BatchSize::SmallInput, BatchSize::SmallInput,
); );
@@ -92,7 +92,7 @@ fn bench_converge(c: &mut Criterion) {
b.iter_batched( b.iter_batched(
|| build_history_1v1(2000, 200, 20, 42), || build_history_1v1(2000, 200, 20, 42),
|mut h| { |mut h| {
h.converge().unwrap(); let _ = h.converge().unwrap();
}, },
BatchSize::SmallInput, BatchSize::SmallInput,
); );
@@ -106,7 +106,7 @@ fn bench_converge(c: &mut Criterion) {
b.iter_batched( b.iter_batched(
|| build_history_1v1(5000, 50000, 5000, 42), || build_history_1v1(5000, 50000, 5000, 42),
|mut h| { |mut h| {
h.converge().unwrap(); let _ = h.converge().unwrap();
}, },
BatchSize::SmallInput, BatchSize::SmallInput,
); );
+1 -1
View File
@@ -29,7 +29,7 @@ fn bench_scored_history(c: &mut Criterion) {
}); });
} }
h.add_events(events).unwrap(); h.add_events(events).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
}); });
}); });
} }
+21 -2
View File
@@ -46,14 +46,33 @@ fn main() {
.sigma(1.6) .sigma(1.6)
.drift(ConstantDrift(0.036)) .drift(ConstantDrift(0.036))
.convergence(trueskill_tt::ConvergenceOptions { .convergence(trueskill_tt::ConvergenceOptions {
max_iter: 10, // This history needs 30 sweeps to reach the epsilon below. It was
// capped at 10 until the `#[must_use]` on `ConvergenceReport`
// surfaced that the example had been shipping a short fit.
max_iter: 100,
epsilon: 0.01, epsilon: 0.01,
alpha: 1.0, alpha: 1.0,
}) })
.build(); .build();
hist.add_events(events).unwrap(); hist.add_events(events).unwrap();
hist.converge().unwrap();
// Read the report rather than discarding it. A fit that hits `max_iter`
// without reaching `epsilon` is not an error and does not look wrong — every
// rating comes back finite and sensibly ordered — so this flag is the only
// thing that says the numbers were still moving when the sweep stopped.
let report = hist.converge().unwrap();
eprintln!(
"converged={} after {} sweeps, final step {:?}",
report.converged, report.iterations, report.final_step
);
if !report.converged {
eprintln!(
"warning: stopped after {} sweeps with a final step of {:?}, \
short of epsilon — raise ConvergenceOptions::max_iter",
report.iterations, report.final_step
);
}
let players = [ let players = [
("aggasi", "a092", 38800i64), ("aggasi", "a092", 38800i64),
+3
View File
@@ -63,6 +63,9 @@ impl Default for ConvergenceOptions {
/// Post-hoc summary of a `History::converge` call. /// Post-hoc summary of a `History::converge` call.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
#[must_use = "a ConvergenceReport carries `converged`, and a fit that stopped \
at `max_iter` is wrong by a little rather than loudly broken — \
check it, or bind it to `_` to say you have decided not to"]
pub struct ConvergenceReport { pub struct ConvergenceReport {
pub iterations: usize, pub iterations: usize,
pub final_step: (f64, f64), pub final_step: (f64, f64),
+15 -3
View File
@@ -50,7 +50,17 @@ pub enum InferenceError {
/// ///
/// Reported rather than skipped: dropping unknown keys turns a team of /// Reported rather than skipped: dropping unknown keys turns a team of
/// strangers into a confident-looking probability about nobody. /// strangers into a confident-looking probability about nobody.
UnknownKey { team: usize, member: usize }, ///
/// `key` is the offending key's `Debug` rendering. It is carried because
/// the indices alone are not actionable: a caller that logs
/// `UnknownKey { team: 0, member: 0 }` learns nothing about *which* of its
/// keys the history has not seen, and the natural handling — fall back to a
/// neutral value — turns the whole thing into a plausible constant.
UnknownKey {
team: usize,
member: usize,
key: String,
},
/// A prediction was given a team with no members. /// A prediction was given a team with no members.
EmptyTeam { team: usize }, EmptyTeam { team: usize },
/// Fewer than two teams were supplied to a prediction. /// Fewer than two teams were supplied to a prediction.
@@ -108,10 +118,12 @@ impl fmt::Display for InferenceError {
"competitor {competitor}: this batch sets {field} to two different values" "competitor {competitor}: this batch sets {field} to two different values"
) )
} }
Self::UnknownKey { team, member } => { Self::UnknownKey { team, member, key } => {
write!( write!(
f, f,
"team {team}, member {member}: no skill recorded for this key" "team {team}, member {member}: no skill recorded for key {key} \
(every key must already be known to the history; pre-filter \
with `lookup` or `current_skill` if that is not guaranteed)"
) )
} }
Self::EmptyTeam { team } => { Self::EmptyTeam { team } => {
+104
View File
@@ -145,6 +145,45 @@ impl Gaussian {
Self::from_mv(self.mu(), self.variance() + variance_delta) Self::from_mv(self.mu(), self.variance() + variance_delta)
} }
/// `P(X < x)` under this Gaussian.
///
/// The question a stopping rule asks: *how sure am I that this competitor's
/// true skill is below the cutoff?* Expressing that as a probability keeps
/// its meaning as sigma changes, where a `mu + z * sigma` band silently
/// means different confidence at different uncertainties — which is exactly
/// the regime a stopping rule operates in.
///
/// Accurate in the *lower* tail. For the upper tail use
/// [`Gaussian::probability_above`] rather than `1.0 - probability_below(x)`,
/// which cancels away every significant digit once the result is small.
///
/// An improper Gaussian (non-positive precision) has no defined mean, so
/// this returns `0.5` — the same convention `mu()` and `sigma()` follow.
#[must_use]
pub fn probability_below(&self, x: f64) -> f64 {
if self.pi <= 0.0 {
return 0.5;
}
crate::cdf(x, self.mu(), self.sigma())
}
/// `P(X > x)` under this Gaussian.
///
/// Computed as a survival function rather than `1 - cdf`, so it keeps full
/// relative precision in the upper tail: `1 - cdf` returns exactly zero
/// past about 8.3 sigma, where the true value is still 1e-19 and perfectly
/// representable. A stopping rule is evaluated precisely there — the
/// interesting cases are the ones near certainty.
///
/// An improper Gaussian returns `0.5`, as [`Gaussian::probability_below`].
#[must_use]
pub fn probability_above(&self, x: f64) -> f64 {
if self.pi <= 0.0 {
return 0.5;
}
crate::sf(x, self.mu(), self.sigma())
}
/// EP damping in natural-parameter space: `α·new + (1−α)·self`. /// EP damping in natural-parameter space: `α·new + (1−α)·self`.
/// ///
/// Used by within-game inference to stabilise oscillating fixed-point /// Used by within-game inference to stabilise oscillating fixed-point
@@ -340,3 +379,68 @@ mod tests {
assert!((damped.tau() - expected_tau).abs() < 1e-12); assert!((damped.tau() - expected_tau).abs() < 1e-12);
} }
} }
#[cfg(test)]
mod tail_probability_tests {
use super::*;
#[test]
fn probability_below_matches_published_quantiles() {
let g = Gaussian::from_ms(0.0, 1.0);
for (x, expected) in [
(-1.959_963_984_540_054, 0.025),
(0.0, 0.5),
(1.281_551_565_544_6, 0.9),
(1.959_963_984_540_054, 0.975),
] {
let got = g.probability_below(x);
assert!(
(got - expected).abs() < 1e-12,
"P(X < {x}) = {got}, expected {expected}"
);
}
}
#[test]
fn the_two_tails_partition_the_mass() {
let g = Gaussian::from_ms(3.0, 2.0);
for x in [-4.0f64, 0.0, 3.0, 7.5] {
let total = g.probability_below(x) + g.probability_above(x);
assert!((total - 1.0).abs() < 1e-15, "at {x}: {total}");
}
}
/// The reason `probability_above` exists rather than `1 - probability_below`.
#[test]
fn probability_above_keeps_precision_where_the_complement_collapses() {
let g = Gaussian::from_ms(0.0, 1.0);
for (x, expected) in [(9.0f64, 1.128_588e-19), (20.0, 2.753_624e-89)] {
let got = g.probability_above(x);
assert!(
(got - expected).abs() / expected < 1e-6,
"P(X > {x}) = {got}, expected ~{expected}"
);
assert_eq!(
1.0 - g.probability_below(x),
0.0,
"the complement should still collapse at {x}"
);
}
}
#[test]
fn a_scaled_gaussian_shifts_and_stretches() {
let g = Gaussian::from_ms(25.0, 6.0);
assert!((g.probability_below(25.0) - 0.5).abs() < 1e-15);
// One sigma either side of the mean.
assert!((g.probability_below(31.0) - 0.841_344_746_068_543).abs() < 1e-12);
assert!((g.probability_above(19.0) - 0.841_344_746_068_543).abs() < 1e-12);
}
#[test]
fn an_improper_gaussian_is_uninformative_rather_than_nan() {
let improper = Gaussian::from_ms(0.0, f64::INFINITY);
assert_eq!(improper.probability_below(5.0), 0.5);
assert_eq!(improper.probability_above(5.0), 0.5);
}
}
+105 -33
View File
@@ -583,7 +583,10 @@ impl<T: Time, D: Drift<T>, O: Observer<T>, K: Eq + Hash + Clone> History<T, D, O
/// reported rather than dropped — silently skipping them would turn a team /// reported rather than dropped — silently skipping them would turn a team
/// of strangers into a confident-looking prediction about nobody, which is /// of strangers into a confident-looking prediction about nobody, which is
/// the failure this replaced. /// the failure this replaced.
fn member_skills(&self, teams: &[&[&K]]) -> Result<Vec<Vec<Gaussian>>, InferenceError> { fn member_skills(&self, teams: &[&[&K]]) -> Result<Vec<Vec<Gaussian>>, InferenceError>
where
K: std::fmt::Debug,
{
if teams.len() < 2 { if teams.len() < 2 {
return Err(InferenceError::NotEnoughTeams { got: teams.len() }); return Err(InferenceError::NotEnoughTeams { got: teams.len() });
} }
@@ -600,6 +603,7 @@ impl<T: Time, D: Drift<T>, O: Observer<T>, K: Eq + Hash + Clone> History<T, D, O
let unknown = InferenceError::UnknownKey { let unknown = InferenceError::UnknownKey {
team: team_idx, team: team_idx,
member: member_idx, member: member_idx,
key: format!("{key:?}"),
}; };
let index = self.keys.get(*key).ok_or(unknown.clone())?; let index = self.keys.get(*key).ok_or(unknown.clone())?;
members.push( members.push(
@@ -625,7 +629,10 @@ impl<T: Time, D: Drift<T>, O: Observer<T>, K: Eq + Hash + Clone> History<T, D, O
/// # Errors /// # Errors
/// ///
/// As [`History::member_skills`]. /// As [`History::member_skills`].
fn performances(&self, teams: &[&[&K]]) -> Result<(Vec<Gaussian>, Vec<usize>), InferenceError> { fn performances(&self, teams: &[&[&K]]) -> Result<(Vec<Gaussian>, Vec<usize>), InferenceError>
where
K: std::fmt::Debug,
{
let skills = self.member_skills(teams)?; let skills = self.member_skills(teams)?;
let performances = skills let performances = skills
@@ -669,10 +676,23 @@ impl<T: Time, D: Drift<T>, O: Observer<T>, K: Eq + Hash + Clone> History<T, D, O
/// "is this matchup *informative*" — the two coincide for two evenly /// "is this matchup *informative*" — the two coincide for two evenly
/// matched teams and diverge elsewhere. /// matched teams and diverge elsewhere.
/// ///
/// # Preconditions
///
/// Every key must already be known to the history — that is, must have
/// appeared in an ingested event. An unknown key is `UnknownKey`, not a
/// silently dropped member. If your caller cannot guarantee that, pre-filter
/// with [`History::lookup`] or [`History::current_skill`]; treating the
/// error as "no information" and substituting a neutral value turns a
/// whole-team miss into a plausible constant, which is invisible to any
/// test that does not assert on variation.
///
/// # Errors /// # Errors
/// ///
/// `NotEnoughTeams`, `EmptyTeam`, or `UnknownKey`. /// `NotEnoughTeams`, `EmptyTeam`, or `UnknownKey`.
pub fn predict_quality(&self, teams: &[&[&K]]) -> Result<f64, InferenceError> { pub fn predict_quality(&self, teams: &[&[&K]]) -> Result<f64, InferenceError>
where
K: std::fmt::Debug,
{
let groups = self.member_skills(teams)?; let groups = self.member_skills(teams)?;
let group_refs: Vec<&[Gaussian]> = groups.iter().map(Vec::as_slice).collect(); let group_refs: Vec<&[Gaussian]> = groups.iter().map(Vec::as_slice).collect();
Ok(crate::quality(&group_refs, self.beta)) Ok(crate::quality(&group_refs, self.beta))
@@ -697,9 +717,22 @@ impl<T: Time, D: Drift<T>, O: Observer<T>, K: Eq + Hash + Clone> History<T, D, O
/// ///
/// # Errors /// # Errors
/// ///
/// # Preconditions
///
/// Every key must already be known to the history — that is, must have
/// appeared in an ingested event. An unknown key is `UnknownKey`, not a
/// silently dropped member. If your caller cannot guarantee that, pre-filter
/// with [`History::lookup`] or [`History::current_skill`]; treating the
/// error as "no information" and substituting a neutral value turns a
/// whole-team miss into a plausible constant, which is invisible to any
/// test that does not assert on variation.
///
/// As [`History::member_skills`], plus `TooManyTeams` and anything /// As [`History::member_skills`], plus `TooManyTeams` and anything
/// inference returns for a hypothetical outcome. /// inference returns for a hypothetical outcome.
pub fn expected_information_gain(&self, teams: &[&[&K]]) -> Result<f64, InferenceError> { pub fn expected_information_gain(&self, teams: &[&[&K]]) -> Result<f64, InferenceError>
where
K: std::fmt::Debug,
{
let skills = self.member_skills(teams)?; let skills = self.member_skills(teams)?;
let ratings: Vec<Vec<Rating<T, D>>> = skills let ratings: Vec<Vec<Rating<T, D>>> = skills
@@ -737,10 +770,23 @@ impl<T: Time, D: Drift<T>, O: Observer<T>, K: Eq + Hash + Clone> History<T, D, O
/// not factorially. Prefer this to [`History::predict_outcome`] when you /// not factorially. Prefer this to [`History::predict_outcome`] when you
/// only need to know who wins. /// only need to know who wins.
/// ///
/// # Preconditions
///
/// Every key must already be known to the history — that is, must have
/// appeared in an ingested event. An unknown key is `UnknownKey`, not a
/// silently dropped member. If your caller cannot guarantee that, pre-filter
/// with [`History::lookup`] or [`History::current_skill`]; treating the
/// error as "no information" and substituting a neutral value turns a
/// whole-team miss into a plausible constant, which is invisible to any
/// test that does not assert on variation.
///
/// # Errors /// # Errors
/// ///
/// `NotEnoughTeams`, `EmptyTeam`, or `UnknownKey`. /// `NotEnoughTeams`, `EmptyTeam`, or `UnknownKey`.
pub fn predict_win_probabilities(&self, teams: &[&[&K]]) -> Result<Vec<f64>, InferenceError> { pub fn predict_win_probabilities(&self, teams: &[&[&K]]) -> Result<Vec<f64>, InferenceError>
where
K: std::fmt::Debug,
{
let (performances, sizes) = self.performances(teams)?; let (performances, sizes) = self.performances(teams)?;
Ok(crate::predict::win_probabilities( Ok(crate::predict::win_probabilities(
&performances, &performances,
@@ -770,10 +816,23 @@ impl<T: Time, D: Drift<T>, O: Observer<T>, K: Eq + Hash + Clone> History<T, D, O
/// use [`History::predict_ranking`], and when you only need the winner use /// use [`History::predict_ranking`], and when you only need the winner use
/// [`History::predict_win_probabilities`]; both stay cheap at any size. /// [`History::predict_win_probabilities`]; both stay cheap at any size.
/// ///
/// # Preconditions
///
/// Every key must already be known to the history — that is, must have
/// appeared in an ingested event. An unknown key is `UnknownKey`, not a
/// silently dropped member. If your caller cannot guarantee that, pre-filter
/// with [`History::lookup`] or [`History::current_skill`]; treating the
/// error as "no information" and substituting a neutral value turns a
/// whole-team miss into a plausible constant, which is invisible to any
/// test that does not assert on variation.
///
/// # Errors /// # Errors
/// ///
/// `NotEnoughTeams`, `EmptyTeam`, `UnknownKey`, or `TooManyTeams`. /// `NotEnoughTeams`, `EmptyTeam`, `UnknownKey`, or `TooManyTeams`.
pub fn predict_outcome(&self, teams: &[&[&K]]) -> Result<Prediction, InferenceError> { pub fn predict_outcome(&self, teams: &[&[&K]]) -> Result<Prediction, InferenceError>
where
K: std::fmt::Debug,
{
if teams.len() > crate::MAX_PREDICTED_TEAMS { if teams.len() > crate::MAX_PREDICTED_TEAMS {
return Err(InferenceError::TooManyTeams { return Err(InferenceError::TooManyTeams {
got: teams.len(), got: teams.len(),
@@ -800,9 +859,22 @@ impl<T: Time, D: Drift<T>, O: Observer<T>, K: Eq + Hash + Clone> History<T, D, O
/// ///
/// # Errors /// # Errors
/// ///
/// # Preconditions
///
/// Every key must already be known to the history — that is, must have
/// appeared in an ingested event. An unknown key is `UnknownKey`, not a
/// silently dropped member. If your caller cannot guarantee that, pre-filter
/// with [`History::lookup`] or [`History::current_skill`]; treating the
/// error as "no information" and substituting a neutral value turns a
/// whole-team miss into a plausible constant, which is invisible to any
/// test that does not assert on variation.
///
/// `NotEnoughTeams`, `EmptyTeam`, `UnknownKey`, or `MismatchedShape` if /// `NotEnoughTeams`, `EmptyTeam`, `UnknownKey`, or `MismatchedShape` if
/// `ranks` does not have one entry per team. /// `ranks` does not have one entry per team.
pub fn predict_ranking(&self, teams: &[&[&K]], ranks: &[u32]) -> Result<f64, InferenceError> { pub fn predict_ranking(&self, teams: &[&[&K]], ranks: &[u32]) -> Result<f64, InferenceError>
where
K: std::fmt::Debug,
{
if ranks.len() != teams.len() { if ranks.len() != teams.len() {
return Err(InferenceError::MismatchedShape { return Err(InferenceError::MismatchedShape {
kind: "ranks vs teams", kind: "ranks vs teams",
@@ -1513,7 +1585,7 @@ mod tests {
epsilon = 1e-6 epsilon = 1e-6
); );
h1.converge().unwrap(); let _ = h1.converge().unwrap();
assert_ulps_eq!( assert_ulps_eq!(
h1.time_slices[0].skills.get(a).unwrap().posterior(), h1.time_slices[0].skills.get(a).unwrap().posterior(),
@@ -1558,7 +1630,7 @@ mod tests {
epsilon = 1e-6 epsilon = 1e-6
); );
h2.converge().unwrap(); let _ = h2.converge().unwrap();
assert_ulps_eq!( assert_ulps_eq!(
h2.time_slices[2].skills.get(a).unwrap().posterior(), h2.time_slices[2].skills.get(a).unwrap().posterior(),
@@ -1591,7 +1663,7 @@ mod tests {
&[5, 6, 7], &[5, 6, 7],
); );
h.add_events(events).unwrap(); h.add_events(events).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
let lc_a = h.learning_curve("a"); let lc_a = h.learning_curve("a");
let lc_c = h.learning_curve("c"); let lc_c = h.learning_curve("c");
@@ -1633,7 +1705,7 @@ mod tests {
&[1, 2, 3], &[1, 2, 3],
); );
h.add_events(events).unwrap(); h.add_events(events).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
let a = h.keys.get("a").unwrap(); let a = h.keys.get("a").unwrap();
let b = h.keys.get("b").unwrap(); let b = h.keys.get("b").unwrap();
@@ -1724,7 +1796,7 @@ mod tests {
let evidence_third_event = h.log_evidence_internal(false, &[a]).exp() * 2.0; let evidence_third_event = h.log_evidence_internal(false, &[a]).exp() * 2.0;
assert_ulps_eq!(0.669885, evidence_third_event, epsilon = 1e-6); assert_ulps_eq!(0.669885, evidence_third_event, epsilon = 1e-6);
h.converge().unwrap(); let _ = h.converge().unwrap();
let loocv_hat = h.log_evidence_internal(false, &[]).exp(); let loocv_hat = h.log_evidence_internal(false, &[]).exp();
let p_d_m_hat = h.log_evidence_internal(true, &[]).exp(); let p_d_m_hat = h.log_evidence_internal(true, &[]).exp();
@@ -1789,7 +1861,7 @@ mod tests {
let b = h.keys.get("b").unwrap(); let b = h.keys.get("b").unwrap();
let c = h.keys.get("c").unwrap(); let c = h.keys.get("c").unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
assert_eq!(h.time_slices[2].skills.get(b).unwrap().elapsed, 2); assert_eq!(h.time_slices[2].skills.get(b).unwrap().elapsed, 2);
assert_eq!(h.time_slices[2].skills.get(c).unwrap().elapsed, 1); assert_eq!(h.time_slices[2].skills.get(c).unwrap().elapsed, 1);
@@ -1838,7 +1910,7 @@ mod tests {
] ]
); );
h.converge().unwrap(); let _ = h.converge().unwrap();
assert_ulps_eq!( assert_ulps_eq!(
h.time_slices[0].skills.get(a).unwrap().posterior(), h.time_slices[0].skills.get(a).unwrap().posterior(),
@@ -1886,7 +1958,7 @@ mod tests {
let b = h.keys.get("b").unwrap(); let b = h.keys.get("b").unwrap();
let c = h.keys.get("c").unwrap(); let c = h.keys.get("c").unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
assert_eq!(h.time_slices[2].skills.get(b).unwrap().elapsed, 2); assert_eq!(h.time_slices[2].skills.get(b).unwrap().elapsed, 2);
assert_eq!(h.time_slices[2].skills.get(c).unwrap().elapsed, 1); assert_eq!(h.time_slices[2].skills.get(c).unwrap().elapsed, 1);
@@ -1935,7 +2007,7 @@ mod tests {
] ]
); );
h.converge().unwrap(); let _ = h.converge().unwrap();
assert_ulps_eq!( assert_ulps_eq!(
h.time_slices[0].skills.get(a).unwrap().posterior(), h.time_slices[0].skills.get(a).unwrap().posterior(),
@@ -2001,7 +2073,7 @@ mod tests {
epsilon: EPSILON, epsilon: EPSILON,
alpha: 1.0, alpha: 1.0,
}; };
h.converge().unwrap(); let _ = h.converge().unwrap();
let loocv_approx_2 = h.log_evidence_internal(false, &[]).exp().sqrt(); let loocv_approx_2 = h.log_evidence_internal(false, &[]).exp().sqrt();
@@ -2052,7 +2124,7 @@ mod tests {
&[0, 10, 20], &[0, 10, 20],
); );
h.add_events(events).unwrap(); h.add_events(events).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
let a = h.keys.get("a").unwrap(); let a = h.keys.get("a").unwrap();
let b = h.keys.get("b").unwrap(); let b = h.keys.get("b").unwrap();
@@ -2116,7 +2188,7 @@ mod tests {
assert_eq!(h.time_slices[0].skills.get(b).unwrap().elapsed, 0); assert_eq!(h.time_slices[0].skills.get(b).unwrap().elapsed, 0);
assert_eq!(h.time_slices[end].skills.get(b).unwrap().elapsed, 5); assert_eq!(h.time_slices[end].skills.get(b).unwrap().elapsed, 5);
h.converge().unwrap(); let _ = h.converge().unwrap();
assert_ulps_eq!( assert_ulps_eq!(
h.time_slices[0].skills.get(b).unwrap().posterior(), h.time_slices[0].skills.get(b).unwrap().posterior(),
@@ -2155,7 +2227,7 @@ mod tests {
&[0, 10, 20], &[0, 10, 20],
); );
h2.add_events(events).unwrap(); h2.add_events(events).unwrap();
h2.converge().unwrap(); let _ = h2.converge().unwrap();
let a = h2.keys.get("a").unwrap(); let a = h2.keys.get("a").unwrap();
let b = h2.keys.get("b").unwrap(); let b = h2.keys.get("b").unwrap();
@@ -2219,7 +2291,7 @@ mod tests {
assert_eq!(h2.time_slices[0].skills.get(b).unwrap().elapsed, 0); assert_eq!(h2.time_slices[0].skills.get(b).unwrap().elapsed, 0);
assert_eq!(h2.time_slices[end].skills.get(b).unwrap().elapsed, 5); assert_eq!(h2.time_slices[end].skills.get(b).unwrap().elapsed, 5);
h2.converge().unwrap(); let _ = h2.converge().unwrap();
assert_ulps_eq!( assert_ulps_eq!(
h2.time_slices[0].skills.get(b).unwrap().posterior(), h2.time_slices[0].skills.get(b).unwrap().posterior(),
@@ -2294,7 +2366,7 @@ mod tests {
epsilon = 1e-6 epsilon = 1e-6
); );
h.converge().unwrap(); let _ = h.converge().unwrap();
let lc_a = h.learning_curve("a"); let lc_a = h.learning_curve("a");
let lc_b = h.learning_curve("b"); let lc_b = h.learning_curve("b");
@@ -2368,11 +2440,11 @@ mod tests {
}) })
.build(); .build();
events_for(&mut h_capped); events_for(&mut h_capped);
h_capped.converge().unwrap(); let _ = h_capped.converge().unwrap();
let mut h_full: History<i64, _, _, &'static str> = History::builder().build(); let mut h_full: History<i64, _, _, &'static str> = History::builder().build();
events_for(&mut h_full); events_for(&mut h_full);
h_full.converge().unwrap(); let _ = h_full.converge().unwrap();
let curves_capped = h_capped.learning_curves(); let curves_capped = h_capped.learning_curves();
let curves_full = h_full.learning_curves(); let curves_full = h_full.learning_curves();
@@ -2409,7 +2481,7 @@ mod tests {
let mut h_undamped: History<i64, _, _, &'static str> = History::builder().build(); let mut h_undamped: History<i64, _, _, &'static str> = History::builder().build();
events_for(&mut h_undamped); events_for(&mut h_undamped);
h_undamped.converge().unwrap(); let _ = h_undamped.converge().unwrap();
let mut h_damped: History<i64, _, _, &'static str> = History::builder() let mut h_damped: History<i64, _, _, &'static str> = History::builder()
.convergence(ConvergenceOptions { .convergence(ConvergenceOptions {
@@ -2419,7 +2491,7 @@ mod tests {
}) })
.build(); .build();
events_for(&mut h_damped); events_for(&mut h_damped);
h_damped.converge().unwrap(); let _ = h_damped.converge().unwrap();
let curves_u = h_undamped.learning_curves(); let curves_u = h_undamped.learning_curves();
let curves_d = h_damped.learning_curves(); let curves_d = h_damped.learning_curves();
@@ -2453,7 +2525,7 @@ mod tests {
outcome: Outcome::scores_with_sigma([3.0, 1.0], 0.5), outcome: Outcome::scores_with_sigma([3.0, 1.0], 0.5),
}]) }])
.unwrap(); .unwrap();
h_a.converge().unwrap(); let _ = h_a.converge().unwrap();
// Path B: history-wide default 0.5, no per-event override. // Path B: history-wide default 0.5, no per-event override.
let mut h_b = crate::History::builder().score_sigma(0.5).build(); let mut h_b = crate::History::builder().score_sigma(0.5).build();
@@ -2466,7 +2538,7 @@ mod tests {
outcome: Outcome::scores([3.0, 1.0]), outcome: Outcome::scores([3.0, 1.0]),
}]) }])
.unwrap(); .unwrap();
h_b.converge().unwrap(); let _ = h_b.converge().unwrap();
// Inheritance: posteriors must be bit-equal. // Inheritance: posteriors must be bit-equal.
let curves_a = h_a.learning_curves(); let curves_a = h_a.learning_curves();
@@ -2495,7 +2567,7 @@ mod tests {
outcome: Outcome::scores_with_sigma([3.0, 1.0], 2.0), outcome: Outcome::scores_with_sigma([3.0, 1.0], 2.0),
}]) }])
.unwrap(); .unwrap();
h_a.converge().unwrap(); let _ = h_a.converge().unwrap();
// Path B: history-wide default 2.0, no per-event override. // Path B: history-wide default 2.0, no per-event override.
let mut h_b = crate::History::builder().score_sigma(2.0).build(); let mut h_b = crate::History::builder().score_sigma(2.0).build();
@@ -2508,7 +2580,7 @@ mod tests {
outcome: Outcome::scores([3.0, 1.0]), outcome: Outcome::scores([3.0, 1.0]),
}]) }])
.unwrap(); .unwrap();
h_b.converge().unwrap(); let _ = h_b.converge().unwrap();
// Override == default-set-to-the-override-value: bit-equal. // Override == default-set-to-the-override-value: bit-equal.
let curves_a = h_a.learning_curves(); let curves_a = h_a.learning_curves();
@@ -2532,7 +2604,7 @@ mod tests {
outcome: Outcome::scores([3.0, 1.0]), outcome: Outcome::scores([3.0, 1.0]),
}]) }])
.unwrap(); .unwrap();
h_c.converge().unwrap(); let _ = h_c.converge().unwrap();
let curves_c = h_c.learning_curves(); let curves_c = h_c.learning_curves();
let mut max_diff: f64 = 0.0; let mut max_diff: f64 = 0.0;
@@ -2561,7 +2633,7 @@ mod tests {
.scores_with_sigma([3.0, 1.0], 2.0) .scores_with_sigma([3.0, 1.0], 2.0)
.commit() .commit()
.unwrap(); .unwrap();
h_a.converge().unwrap(); let _ = h_a.converge().unwrap();
// Path B: same outcome via the explicit Outcome constructor. // Path B: same outcome via the explicit Outcome constructor.
let mut h_b = crate::History::builder().score_sigma(0.5).build(); let mut h_b = crate::History::builder().score_sigma(0.5).build();
@@ -2574,7 +2646,7 @@ mod tests {
outcome: Outcome::scores_with_sigma([3.0, 1.0], 2.0), outcome: Outcome::scores_with_sigma([3.0, 1.0], 2.0),
}]) }])
.unwrap(); .unwrap();
h_b.converge().unwrap(); let _ = h_b.converge().unwrap();
let curves_a = h_a.learning_curves(); let curves_a = h_a.learning_curves();
let curves_b = h_b.learning_curves(); let curves_b = h_b.learning_curves();
+17
View File
@@ -158,6 +158,23 @@ pub const SIGMA: f64 = BETA * 6.0;
pub const GAMMA: f64 = BETA * 0.03; pub const GAMMA: f64 = BETA * 0.03;
pub const P_DRAW: f64 = 0.0; pub const P_DRAW: f64 = 0.0;
pub const EPSILON: f64 = 1e-6; pub const EPSILON: f64 = 1e-6;
/// Default cap on convergence sweeps.
///
/// **This is a floor, not a recommendation.** It is adequate for small
/// histories and is quickly outgrown: a history of 400 events over 100
/// competitors already stops here with a final step of ~7e-3 against the 1e-6
/// default tolerance — four orders of magnitude short — and a dense joint model
/// of ~2,000 nodes over ~3,300 events has been measured needing 76 to 161.
///
/// Overrunning it is not an error, and deliberately so: `converge` returns a
/// [`ConvergenceReport`] whose `converged` flag says what happened. But a fit
/// that stopped short is *wrong by a little*, which is the worst available
/// failure — every rating is finite and ordered sensibly, and nothing in the
/// numbers themselves says they were still moving. Read the report; the type is
/// `#[must_use]` for that reason.
///
/// Raise it via [`ConvergenceOptions`]. Convergence cost is roughly linear in
/// the cap, and for anything but a toy the extra sweeps are milliseconds.
pub const ITERATIONS: usize = 30; pub const ITERATIONS: usize = 30;
/// Largest team count `History::predict_outcome` will enumerate. /// Largest team count `History::predict_outcome` will enumerate.
+7 -7
View File
@@ -65,7 +65,7 @@ fn add_events_draw() {
outcome: Outcome::draw(2), outcome: Outcome::draw(2),
}]; }];
h.add_events(events).unwrap(); h.add_events(events).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
} }
#[test] #[test]
@@ -123,7 +123,7 @@ fn fluent_event_builder_winner_convenience() {
.winner(0) .winner(0)
.commit() .commit()
.unwrap(); .unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
} }
#[test] #[test]
@@ -141,7 +141,7 @@ fn fluent_event_builder_draw() {
.draw() .draw()
.commit() .commit()
.unwrap(); .unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
} }
#[test] #[test]
@@ -155,7 +155,7 @@ fn current_skill_and_learning_curve() {
.build(); .build();
h.record_winner(&"a", &"b", 1).unwrap(); h.record_winner(&"a", &"b", 1).unwrap();
h.record_winner(&"a", &"b", 2).unwrap(); h.record_winner(&"a", &"b", 2).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
let a = h.current_skill(&"a").unwrap(); let a = h.current_skill(&"a").unwrap();
assert!(a.mu() > 25.0); assert!(a.mu() > 25.0);
@@ -201,7 +201,7 @@ fn predict_quality_two_teams() {
.p_draw(0.0) .p_draw(0.0)
.build(); .build();
h.record_winner(&"a", &"b", 1).unwrap(); h.record_winner(&"a", &"b", 1).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
let q = h.predict_quality(&[&[&"a"], &[&"b"]]).unwrap(); let q = h.predict_quality(&[&[&"a"], &[&"b"]]).unwrap();
assert!(q > 0.0 && q <= 1.0); assert!(q > 0.0 && q <= 1.0);
@@ -217,7 +217,7 @@ fn predict_outcome_two_teams_sums_to_one() {
.p_draw(0.0) .p_draw(0.0)
.build(); .build();
h.record_winner(&"a", &"b", 1).unwrap(); h.record_winner(&"a", &"b", 1).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
let p = h.predict_outcome(&[&[&"a"], &[&"b"]]).unwrap(); let p = h.predict_outcome(&[&[&"a"], &[&"b"]]).unwrap();
let wins = p.win_probabilities(); let wins = p.win_probabilities();
@@ -245,7 +245,7 @@ fn fluent_event_builder_scores() {
.scores([12.0, 4.0]) .scores([12.0, 4.0])
.commit() .commit()
.unwrap(); .unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
let a = h.current_skill(&"alice").unwrap(); let a = h.current_skill(&"alice").unwrap();
let b = h.current_skill(&"bob").unwrap(); let b = h.current_skill(&"bob").unwrap();
+10 -10
View File
@@ -64,13 +64,13 @@ fn a_prior_applies_to_a_new_competitor() {
let mut with = history(); let mut with = history();
with.add_events(vec![bout("a", "b", 0, Some(seeded), None)]) with.add_events(vec![bout("a", "b", 0, Some(seeded), None)])
.unwrap(); .unwrap();
with.converge().unwrap(); let _ = with.converge().unwrap();
let mut without = history(); let mut without = history();
without without
.add_events(vec![bout("a", "b", 0, None, None)]) .add_events(vec![bout("a", "b", 0, None, None)])
.unwrap(); .unwrap();
without.converge().unwrap(); let _ = without.converge().unwrap();
assert!( assert!(
(skill_of(&with, "a").mu() - skill_of(&without, "a").mu()).abs() > 1.0, (skill_of(&with, "a").mu() - skill_of(&without, "a").mu()).abs() > 1.0,
@@ -91,7 +91,7 @@ fn a_prior_applies_to_a_competitor_the_history_already_knows() {
// "a" now exists. Configuring it here used to do nothing whatsoever. // "a" now exists. Configuring it here used to do nothing whatsoever.
late.add_events(vec![bout("a", "b", 1, Some(seeded), None)]) late.add_events(vec![bout("a", "b", 1, Some(seeded), None)])
.unwrap(); .unwrap();
late.converge().unwrap(); let _ = late.converge().unwrap();
let mut never = history(); let mut never = history();
never never
@@ -100,7 +100,7 @@ fn a_prior_applies_to_a_competitor_the_history_already_knows() {
bout("a", "b", 1, None, None), bout("a", "b", 1, None, None),
]) ])
.unwrap(); .unwrap();
never.converge().unwrap(); let _ = never.converge().unwrap();
assert!( assert!(
(skill_of(&late, "a").mu() - skill_of(&never, "a").mu()).abs() > 1.0, (skill_of(&late, "a").mu() - skill_of(&never, "a").mu()).abs() > 1.0,
@@ -122,7 +122,7 @@ fn a_prior_is_whole_history_scoped_not_per_event() {
.unwrap(); .unwrap();
late.add_events(vec![bout("a", "b", 1, Some(seeded), None)]) late.add_events(vec![bout("a", "b", 1, Some(seeded), None)])
.unwrap(); .unwrap();
late.converge().unwrap(); let _ = late.converge().unwrap();
let mut early = history(); let mut early = history();
early early
@@ -131,7 +131,7 @@ fn a_prior_is_whole_history_scoped_not_per_event() {
bout("a", "b", 1, Some(seeded), None), bout("a", "b", 1, Some(seeded), None),
]) ])
.unwrap(); .unwrap();
early.converge().unwrap(); let _ = early.converge().unwrap();
let (l, e) = (skill_of(&late, "a"), skill_of(&early, "a")); let (l, e) = (skill_of(&late, "a"), skill_of(&early, "a"));
assert!( assert!(
@@ -150,7 +150,7 @@ fn repeating_the_same_prior_is_inert() {
bout("a", "b", 1, None, None), bout("a", "b", 1, None, None),
]) ])
.unwrap(); .unwrap();
once.converge().unwrap(); let _ = once.converge().unwrap();
let mut every_time = history(); let mut every_time = history();
every_time every_time
@@ -159,7 +159,7 @@ fn repeating_the_same_prior_is_inert() {
bout("a", "b", 1, Some(seeded), None), bout("a", "b", 1, Some(seeded), None),
]) ])
.unwrap(); .unwrap();
every_time.converge().unwrap(); let _ = every_time.converge().unwrap();
let (o, e) = (skill_of(&once, "a"), skill_of(&every_time, "a")); let (o, e) = (skill_of(&once, "a"), skill_of(&every_time, "a"));
assert!( assert!(
@@ -203,7 +203,7 @@ fn setting_one_field_late_leaves_the_other_alone() {
// Only the scale this time — the prior above must survive. // Only the scale this time — the prior above must survive.
h.add_events(vec![bout("a", "b", 1, None, Some(0.5))]) h.add_events(vec![bout("a", "b", 1, None, Some(0.5))])
.unwrap(); .unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
let mut both_upfront = history(); let mut both_upfront = history();
both_upfront both_upfront
@@ -212,7 +212,7 @@ fn setting_one_field_late_leaves_the_other_alone() {
bout("a", "b", 1, None, None), bout("a", "b", 1, None, None),
]) ])
.unwrap(); .unwrap();
both_upfront.converge().unwrap(); let _ = both_upfront.converge().unwrap();
let (a, b) = (skill_of(&h, "a"), skill_of(&both_upfront, "a")); let (a, b) = (skill_of(&h, "a"), skill_of(&both_upfront, "a"));
assert!( assert!(
+4 -4
View File
@@ -351,7 +351,7 @@ fn zero_weight_does_not_produce_a_non_finite_posterior() {
.commit() .commit()
.expect("a zero weight is accepted today; update this test if that changes"); .expect("a zero weight is accepted today; update this test if that changes");
h.converge().unwrap(); let _ = h.converge().unwrap();
assert_curve_finite(&h, &["a", "b"], "zero weight"); assert_curve_finite(&h, &["a", "b"], "zero weight");
} }
@@ -368,7 +368,7 @@ fn negative_weight_does_not_produce_a_non_finite_posterior() {
.commit() .commit()
.expect("a negative weight is accepted today; update this test if that changes"); .expect("a negative weight is accepted today; update this test if that changes");
h.converge().unwrap(); let _ = h.converge().unwrap();
assert_curve_finite(&h, &["a", "b"], "negative weight"); assert_curve_finite(&h, &["a", "b"], "negative weight");
} }
@@ -389,7 +389,7 @@ fn out_of_order_timestamps_converge_to_the_same_answer() {
h.record_winner(&"a", &"b", time).unwrap(); h.record_winner(&"a", &"b", time).unwrap();
} }
h.converge().unwrap(); let _ = h.converge().unwrap();
h h
} }
@@ -416,7 +416,7 @@ fn extreme_beta_and_sigma_stay_finite() {
h.record_winner(&"a", &"b", 1).unwrap(); h.record_winner(&"a", &"b", 1).unwrap();
h.record_winner(&"a", &"b", 2).unwrap(); h.record_winner(&"a", &"b", 2).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
assert_curve_finite(&h, &["a", "b"], &format!("beta={beta} sigma={sigma}")); assert_curve_finite(&h, &["a", "b"], &format!("beta={beta} sigma={sigma}"));
} }
+1 -1
View File
@@ -47,7 +47,7 @@ fn build_and_converge(seed: u64) -> Vec<(i64, trueskill_tt::Gaussian)> {
}); });
} }
h.add_events(events).unwrap(); h.add_events(events).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
// Sample one competitor's curve for the comparison. // Sample one competitor's curve for the comparison.
h.learning_curve("p0") h.learning_curve("p0")
} }
+2 -2
View File
@@ -58,7 +58,7 @@ fn fit(events: Vec<Event<i64, &'static str>>, gamma: f64) -> Fit {
.build(); .build();
h.add_events(events).unwrap(); h.add_events(events).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
h h
} }
@@ -385,7 +385,7 @@ fn drift_scale_applies_when_set_after_first_appearance() {
outcome: Outcome::winner(1, 2), outcome: Outcome::winner(1, 2),
}]) }])
.unwrap(); .unwrap();
late.converge().unwrap(); let _ = late.converge().unwrap();
let applied = curve(&late, "anchor"); let applied = curve(&late, "anchor");
let pinned_from_the_start = curve(&fit(distant_pair(Some(0.0)), 25.0 / 300.0), "anchor"); let pinned_from_the_start = curve(&fit(distant_pair(Some(0.0)), 25.0 / 300.0), "anchor");
+6 -6
View File
@@ -47,7 +47,7 @@ fn tight() -> ConvergenceOptions {
fn filtered_evidence_sits_between_coin_flip_and_batch() { fn filtered_evidence_sits_between_coin_flip_and_batch() {
let mut history = repeated_winner(5); let mut history = repeated_winner(5);
history.converge().unwrap(); let _ = history.converge().unwrap();
let coin_flip = 5.0 * 0.5f64.ln(); let coin_flip = 5.0 * 0.5f64.ln();
let batch = history.log_evidence(); let batch = history.log_evidence();
@@ -71,7 +71,7 @@ fn filtered_evidence_sits_between_coin_flip_and_batch() {
fn filtered_first_point_is_less_certain_than_smoothed() { fn filtered_first_point_is_less_certain_than_smoothed() {
let mut history = repeated_winner(12); let mut history = repeated_winner(12);
history.converge().unwrap(); let _ = history.converge().unwrap();
let smoothed = history.learning_curve("a"); let smoothed = history.learning_curve("a");
let filtered = history.filtered_learning_curve("a"); let filtered = history.filtered_learning_curve("a");
@@ -121,7 +121,7 @@ fn filtered_first_point_is_less_certain_than_smoothed() {
fn filtered_curves_plural_agrees_with_singular() { fn filtered_curves_plural_agrees_with_singular() {
let mut history = repeated_winner(4); let mut history = repeated_winner(4);
history.converge().unwrap(); let _ = history.converge().unwrap();
let curves = history.filtered_learning_curves(); let curves = history.filtered_learning_curves();
@@ -180,7 +180,7 @@ fn single_slice_filtered_matches_smoothed() {
]) ])
.unwrap(); .unwrap();
history.converge().unwrap(); let _ = history.converge().unwrap();
let smoothed = history.learning_curve("a"); let smoothed = history.learning_curve("a");
let filtered = history.filtered_learning_curve("a"); let filtered = history.filtered_learning_curve("a");
@@ -223,13 +223,13 @@ fn filtered_curves_do_not_depend_on_ingestion_order() {
let mut batched = History::builder().convergence(tight()).build(); let mut batched = History::builder().convergence(tight()).build();
batched.add_events(all.clone()).unwrap(); batched.add_events(all.clone()).unwrap();
batched.converge().unwrap(); let _ = batched.converge().unwrap();
let mut incremental = History::builder().convergence(tight()).build(); let mut incremental = History::builder().convergence(tight()).build();
for event in all { for event in all {
incremental.add_events([event]).unwrap(); incremental.add_events([event]).unwrap();
} }
incremental.converge().unwrap(); let _ = incremental.converge().unwrap();
let from_batched = batched.filtered_learning_curve("a"); let from_batched = batched.filtered_learning_curve("a");
let from_incremental = incremental.filtered_learning_curve("a"); let from_incremental = incremental.filtered_learning_curve("a");
+1 -1
View File
@@ -46,7 +46,7 @@ fn nan_after_fit(players: usize) -> usize {
let (w, l) = if rng.coin() { (a, b) } else { (b, a) }; let (w, l) = if rng.coin() { (a, b) } else { (b, a) };
h.record_winner(&ids[w], &ids[l], 0).unwrap(); h.record_winner(&ids[w], &ids[l], 0).unwrap();
} }
h.converge().unwrap(); let _ = h.converge().unwrap();
ids.iter() ids.iter()
.filter(|id| { .filter(|id| {
+8 -8
View File
@@ -42,7 +42,7 @@ fn every_observer_callback_fires() {
h.record_winner(&"a", &"b", 1).unwrap(); h.record_winner(&"a", &"b", 1).unwrap();
h.record_winner(&"b", &"c", 2).unwrap(); h.record_winner(&"b", &"c", 2).unwrap();
h.record_winner(&"c", &"a", 3).unwrap(); h.record_winner(&"c", &"a", 3).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
assert!( assert!(
!recorder.iterations.lock().unwrap().is_empty(), !recorder.iterations.lock().unwrap().is_empty(),
@@ -65,7 +65,7 @@ fn slice_callbacks_report_the_slice_they_swept() {
h.record_winner(&"a", &"b", 10).unwrap(); h.record_winner(&"a", &"b", 10).unwrap();
h.record_winner(&"a", &"b", 20).unwrap(); h.record_winner(&"a", &"b", 20).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
let slices = recorder.slices.lock().unwrap(); let slices = recorder.slices.lock().unwrap();
@@ -93,7 +93,7 @@ fn a_single_slice_history_still_reports_its_sweep() {
let mut h = History::builder().observer(Arc::clone(&recorder)).build(); let mut h = History::builder().observer(Arc::clone(&recorder)).build();
h.record_winner(&"a", &"b", 1).unwrap(); h.record_winner(&"a", &"b", 1).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
let slices = recorder.slices.lock().unwrap(); let slices = recorder.slices.lock().unwrap();
assert!( assert!(
@@ -112,7 +112,7 @@ fn a_shared_observer_reaches_the_callers_handle() {
let mut h = History::builder().observer(Arc::clone(&recorder)).build(); let mut h = History::builder().observer(Arc::clone(&recorder)).build();
h.record_winner(&"a", &"b", 1).unwrap(); h.record_winner(&"a", &"b", 1).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
assert!(!recorder.iterations.lock().unwrap().is_empty()); assert!(!recorder.iterations.lock().unwrap().is_empty());
assert!(!recorder.slices.lock().unwrap().is_empty()); assert!(!recorder.slices.lock().unwrap().is_empty());
@@ -125,12 +125,12 @@ fn a_trait_object_observer_works() {
let boxed: Box<dyn Observer<i64>> = Box::new(Recorder::default()); let boxed: Box<dyn Observer<i64>> = Box::new(Recorder::default());
let mut h = History::builder().observer(boxed).build(); let mut h = History::builder().observer(boxed).build();
h.record_winner(&"a", &"b", 1).unwrap(); h.record_winner(&"a", &"b", 1).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
let shared: Arc<dyn Observer<i64>> = Arc::new(Recorder::default()); let shared: Arc<dyn Observer<i64>> = Arc::new(Recorder::default());
let mut h = History::builder().observer(Arc::clone(&shared)).build(); let mut h = History::builder().observer(Arc::clone(&shared)).build();
h.record_winner(&"a", &"b", 1).unwrap(); h.record_winner(&"a", &"b", 1).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
} }
/// A non-shared observer can be reclaimed after convergence instead. /// A non-shared observer can be reclaimed after convergence instead.
@@ -138,7 +138,7 @@ fn a_trait_object_observer_works() {
fn into_observer_returns_the_accumulated_state() { fn into_observer_returns_the_accumulated_state() {
let mut h = History::builder().observer(Recorder::default()).build(); let mut h = History::builder().observer(Recorder::default()).build();
h.record_winner(&"a", &"b", 1).unwrap(); h.record_winner(&"a", &"b", 1).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
// Readable in place... // Readable in place...
assert!(!h.observer().iterations.lock().unwrap().is_empty()); assert!(!h.observer().iterations.lock().unwrap().is_empty());
@@ -155,7 +155,7 @@ fn a_borrowed_observer_works() {
{ {
let mut h = History::builder().observer(&recorder).build(); let mut h = History::builder().observer(&recorder).build();
h.record_winner(&"a", &"b", 1).unwrap(); h.record_winner(&"a", &"b", 1).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
} }
assert!(!recorder.iterations.lock().unwrap().is_empty()); assert!(!recorder.iterations.lock().unwrap().is_empty());
} }
+55 -7
View File
@@ -9,7 +9,7 @@ fn history_with(names: &[&'static str], p_draw: f64) -> History {
for pair in names.windows(2) { for pair in names.windows(2) {
h.record_winner(&pair[0], &pair[1], 1).unwrap(); h.record_winner(&pair[0], &pair[1], 1).unwrap();
} }
h.converge().unwrap(); let _ = h.converge().unwrap();
h h
} }
@@ -20,7 +20,14 @@ fn unknown_keys_are_reported_not_silently_dropped() {
let err = h let err = h
.predict_outcome(&[&[&"a"], &[&"ghost"]]) .predict_outcome(&[&[&"a"], &[&"ghost"]])
.expect_err("an unknown key must not yield a confident prediction"); .expect_err("an unknown key must not yield a confident prediction");
assert_eq!(err, InferenceError::UnknownKey { team: 1, member: 0 }); assert_eq!(
err,
InferenceError::UnknownKey {
team: 1,
member: 0,
key: "\"ghost\"".to_owned(),
}
);
// Every prediction entry point, not just one. // Every prediction entry point, not just one.
assert!( assert!(
@@ -35,7 +42,14 @@ fn unknown_keys_are_reported_not_silently_dropped() {
fn an_entirely_unknown_team_is_an_error() { fn an_entirely_unknown_team_is_an_error() {
let h = history_with(&["a", "b"], 0.0); let h = history_with(&["a", "b"], 0.0);
let err = h.predict_outcome(&[&[&"a"], &[&"x", &"y"]]).unwrap_err(); let err = h.predict_outcome(&[&[&"a"], &[&"x", &"y"]]).unwrap_err();
assert_eq!(err, InferenceError::UnknownKey { team: 1, member: 0 }); assert_eq!(
err,
InferenceError::UnknownKey {
team: 1,
member: 0,
key: "\"x\"".to_owned(),
}
);
} }
#[test] #[test]
@@ -184,7 +198,7 @@ fn the_stronger_competitor_is_favoured() {
for t in 1..=10 { for t in 1..=10 {
h.record_winner(&"strong", &"weak", t).unwrap(); h.record_winner(&"strong", &"weak", t).unwrap();
} }
h.converge().unwrap(); let _ = h.converge().unwrap();
let p = h.predict_outcome(&[&[&"strong"], &[&"weak"]]).unwrap(); let p = h.predict_outcome(&[&[&"strong"], &[&"weak"]]).unwrap();
let (best, _) = p.most_likely().expect("a most likely outcome"); let (best, _) = p.most_likely().expect("a most likely outcome");
@@ -206,7 +220,7 @@ fn team_size_affects_the_prediction() {
.winner(0) .winner(0)
.commit() .commit()
.unwrap(); .unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
let p = h.predict_outcome(&[&[&"a", &"b"], &[&"c"]]).unwrap(); let p = h.predict_outcome(&[&[&"a", &"b"], &[&"c"]]).unwrap();
assert!((p.total() - 1.0).abs() < 1e-6, "total = {}", p.total()); assert!((p.total() - 1.0).abs() < 1e-6, "total = {}", p.total());
@@ -229,7 +243,7 @@ fn information_gain_prefers_the_uncertain_pairing() {
h.record_winner(&"rival", &"known", t + 100).unwrap(); h.record_winner(&"rival", &"known", t + 100).unwrap();
} }
h.record_winner(&"known", &"newcomer", 500).unwrap(); h.record_winner(&"known", &"newcomer", 500).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
let settled = h let settled = h
.expected_information_gain(&[&[&"known"], &[&"rival"]]) .expected_information_gain(&[&[&"known"], &[&"rival"]])
@@ -271,7 +285,11 @@ fn information_gain_reports_unknown_keys() {
assert_eq!( assert_eq!(
h.expected_information_gain(&[&[&"a"], &[&"ghost"]]) h.expected_information_gain(&[&[&"a"], &[&"ghost"]])
.unwrap_err(), .unwrap_err(),
InferenceError::UnknownKey { team: 1, member: 0 } InferenceError::UnknownKey {
team: 1,
member: 0,
key: "\"ghost\"".to_owned(),
}
); );
} }
@@ -289,3 +307,33 @@ fn information_gain_accounts_for_draws() {
let dist = with_draws.predict_outcome(&[&[&"a"], &[&"b"]]).unwrap(); let dist = with_draws.predict_outcome(&[&[&"a"], &[&"b"]]).unwrap();
assert!(dist.probability_of(&[0, 0]) > 0.0); assert!(dist.probability_of(&[0, 0]) > 0.0);
} }
/// The defect that cost a consumer a day: `UnknownKey { team: 0, member: 0 }`
/// says nothing about *which* key is unknown, so the natural handling — log it,
/// fall back to a neutral value — converts a total miss into a plausible
/// constant. The key has to be in the error, and in its `Display`.
#[test]
fn unknown_key_names_the_key_it_could_not_find() {
let h = history_with(&["a", "b"], 0.0);
let err = h.predict_outcome(&[&[&"a"], &[&"never_seen"]]).unwrap_err();
match &err {
InferenceError::UnknownKey { key, .. } => {
assert!(
key.contains("never_seen"),
"the error should name the key, got {key}"
);
}
other => panic!("expected UnknownKey, got {other:?}"),
}
let rendered = err.to_string();
assert!(
rendered.contains("never_seen"),
"Display should name the key: {rendered}"
);
assert!(
rendered.contains("pre-filter"),
"Display should say what to do about it: {rendered}"
);
}
+5 -5
View File
@@ -61,7 +61,7 @@ proptest! {
fn converged_posteriors_are_always_finite(games in pairs()) { fn converged_posteriors_are_always_finite(games in pairs()) {
let mut h = history_from(&games); let mut h = history_from(&games);
h.converge().unwrap(); let _ = h.converge().unwrap();
for key in KEYS { for key in KEYS {
for (time, g) in h.learning_curve(key) { for (time, g) in h.learning_curve(key) {
@@ -79,7 +79,7 @@ proptest! {
fn log_evidence_is_a_finite_log_probability(games in pairs()) { fn log_evidence_is_a_finite_log_probability(games in pairs()) {
let mut h = history_from(&games); let mut h = history_from(&games);
h.converge().unwrap(); let _ = h.converge().unwrap();
let batch = h.log_evidence(); let batch = h.log_evidence();
let filtered = h.filtered_log_evidence(); let filtered = h.filtered_log_evidence();
@@ -98,7 +98,7 @@ proptest! {
let before = h.filtered_log_evidence(); let before = h.filtered_log_evidence();
h.converge().unwrap(); let _ = h.converge().unwrap();
let after = h.filtered_log_evidence(); let after = h.filtered_log_evidence();
@@ -114,7 +114,7 @@ proptest! {
fn ingestion_order_does_not_change_the_answer(games in pairs()) { fn ingestion_order_does_not_change_the_answer(games in pairs()) {
let batched = { let batched = {
let mut h = history_from(&games); let mut h = history_from(&games);
h.converge().unwrap(); let _ = h.converge().unwrap();
h h
}; };
@@ -139,7 +139,7 @@ proptest! {
.unwrap(); .unwrap();
} }
h.converge().unwrap(); let _ = h.converge().unwrap();
h h
}; };
+1 -1
View File
@@ -108,7 +108,7 @@ fn history_predict_quality_supports_three_teams() {
let mut h = History::default(); let mut h = History::default();
h.record_winner(&"a", &"b", 1).unwrap(); h.record_winner(&"a", &"b", 1).unwrap();
h.record_winner(&"b", &"c", 2).unwrap(); h.record_winner(&"b", &"c", 2).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
let q = h.predict_quality(&[&[&"a"], &[&"b"], &[&"c"]]).unwrap(); let q = h.predict_quality(&[&[&"a"], &[&"b"], &[&"c"]]).unwrap();
assert!( assert!(
+2 -2
View File
@@ -15,7 +15,7 @@ fn record_winner_builds_history() {
.build(); .build();
h.record_winner(&"alice", &"bob", 1).unwrap(); h.record_winner(&"alice", &"bob", 1).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
let a_idx = h.lookup(&"alice").unwrap(); let a_idx = h.lookup(&"alice").unwrap();
let b_idx = h.lookup(&"bob").unwrap(); let b_idx = h.lookup(&"bob").unwrap();
@@ -48,7 +48,7 @@ fn record_draw_with_p_draw_set() {
.build(); .build();
h.record_draw(&"alice", &"bob", 1).unwrap(); h.record_draw(&"alice", &"bob", 1).unwrap();
h.converge().unwrap(); let _ = h.converge().unwrap();
assert!(h.lookup(&"alice").is_some()); assert!(h.lookup(&"alice").is_some());
assert!(h.lookup(&"bob").is_some()); assert!(h.lookup(&"bob").is_some());