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:
@@ -63,6 +63,9 @@ impl Default for ConvergenceOptions {
|
||||
|
||||
/// Post-hoc summary of a `History::converge` call.
|
||||
#[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 iterations: usize,
|
||||
pub final_step: (f64, f64),
|
||||
|
||||
+15
-3
@@ -50,7 +50,17 @@ pub enum InferenceError {
|
||||
///
|
||||
/// Reported rather than skipped: dropping unknown keys turns a team of
|
||||
/// 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.
|
||||
EmptyTeam { team: usize },
|
||||
/// 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"
|
||||
)
|
||||
}
|
||||
Self::UnknownKey { team, member } => {
|
||||
Self::UnknownKey { team, member, key } => {
|
||||
write!(
|
||||
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 } => {
|
||||
|
||||
+104
@@ -145,6 +145,45 @@ impl Gaussian {
|
||||
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`.
|
||||
///
|
||||
/// Used by within-game inference to stabilise oscillating fixed-point
|
||||
@@ -340,3 +379,68 @@ mod tests {
|
||||
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
@@ -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
|
||||
/// of strangers into a confident-looking prediction about nobody, which is
|
||||
/// 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 {
|
||||
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 {
|
||||
team: team_idx,
|
||||
member: member_idx,
|
||||
key: format!("{key:?}"),
|
||||
};
|
||||
let index = self.keys.get(*key).ok_or(unknown.clone())?;
|
||||
members.push(
|
||||
@@ -625,7 +629,10 @@ impl<T: Time, D: Drift<T>, O: Observer<T>, K: Eq + Hash + Clone> History<T, D, O
|
||||
/// # Errors
|
||||
///
|
||||
/// 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 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
|
||||
/// 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
|
||||
///
|
||||
/// `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 group_refs: Vec<&[Gaussian]> = groups.iter().map(Vec::as_slice).collect();
|
||||
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
|
||||
///
|
||||
/// # 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
|
||||
/// 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 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
|
||||
/// 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
|
||||
///
|
||||
/// `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)?;
|
||||
Ok(crate::predict::win_probabilities(
|
||||
&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
|
||||
/// [`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
|
||||
///
|
||||
/// `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 {
|
||||
return Err(InferenceError::TooManyTeams {
|
||||
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
|
||||
///
|
||||
/// # 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
|
||||
/// `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() {
|
||||
return Err(InferenceError::MismatchedShape {
|
||||
kind: "ranks vs teams",
|
||||
@@ -1513,7 +1585,7 @@ mod tests {
|
||||
epsilon = 1e-6
|
||||
);
|
||||
|
||||
h1.converge().unwrap();
|
||||
let _ = h1.converge().unwrap();
|
||||
|
||||
assert_ulps_eq!(
|
||||
h1.time_slices[0].skills.get(a).unwrap().posterior(),
|
||||
@@ -1558,7 +1630,7 @@ mod tests {
|
||||
epsilon = 1e-6
|
||||
);
|
||||
|
||||
h2.converge().unwrap();
|
||||
let _ = h2.converge().unwrap();
|
||||
|
||||
assert_ulps_eq!(
|
||||
h2.time_slices[2].skills.get(a).unwrap().posterior(),
|
||||
@@ -1591,7 +1663,7 @@ mod tests {
|
||||
&[5, 6, 7],
|
||||
);
|
||||
h.add_events(events).unwrap();
|
||||
h.converge().unwrap();
|
||||
let _ = h.converge().unwrap();
|
||||
|
||||
let lc_a = h.learning_curve("a");
|
||||
let lc_c = h.learning_curve("c");
|
||||
@@ -1633,7 +1705,7 @@ mod tests {
|
||||
&[1, 2, 3],
|
||||
);
|
||||
h.add_events(events).unwrap();
|
||||
h.converge().unwrap();
|
||||
let _ = h.converge().unwrap();
|
||||
|
||||
let a = h.keys.get("a").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;
|
||||
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 p_d_m_hat = h.log_evidence_internal(true, &[]).exp();
|
||||
@@ -1789,7 +1861,7 @@ mod tests {
|
||||
let b = h.keys.get("b").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(c).unwrap().elapsed, 1);
|
||||
@@ -1838,7 +1910,7 @@ mod tests {
|
||||
]
|
||||
);
|
||||
|
||||
h.converge().unwrap();
|
||||
let _ = h.converge().unwrap();
|
||||
|
||||
assert_ulps_eq!(
|
||||
h.time_slices[0].skills.get(a).unwrap().posterior(),
|
||||
@@ -1886,7 +1958,7 @@ mod tests {
|
||||
let b = h.keys.get("b").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(c).unwrap().elapsed, 1);
|
||||
@@ -1935,7 +2007,7 @@ mod tests {
|
||||
]
|
||||
);
|
||||
|
||||
h.converge().unwrap();
|
||||
let _ = h.converge().unwrap();
|
||||
|
||||
assert_ulps_eq!(
|
||||
h.time_slices[0].skills.get(a).unwrap().posterior(),
|
||||
@@ -2001,7 +2073,7 @@ mod tests {
|
||||
epsilon: EPSILON,
|
||||
alpha: 1.0,
|
||||
};
|
||||
h.converge().unwrap();
|
||||
let _ = h.converge().unwrap();
|
||||
|
||||
let loocv_approx_2 = h.log_evidence_internal(false, &[]).exp().sqrt();
|
||||
|
||||
@@ -2052,7 +2124,7 @@ mod tests {
|
||||
&[0, 10, 20],
|
||||
);
|
||||
h.add_events(events).unwrap();
|
||||
h.converge().unwrap();
|
||||
let _ = h.converge().unwrap();
|
||||
|
||||
let a = h.keys.get("a").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[end].skills.get(b).unwrap().elapsed, 5);
|
||||
|
||||
h.converge().unwrap();
|
||||
let _ = h.converge().unwrap();
|
||||
|
||||
assert_ulps_eq!(
|
||||
h.time_slices[0].skills.get(b).unwrap().posterior(),
|
||||
@@ -2155,7 +2227,7 @@ mod tests {
|
||||
&[0, 10, 20],
|
||||
);
|
||||
h2.add_events(events).unwrap();
|
||||
h2.converge().unwrap();
|
||||
let _ = h2.converge().unwrap();
|
||||
|
||||
let a = h2.keys.get("a").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[end].skills.get(b).unwrap().elapsed, 5);
|
||||
|
||||
h2.converge().unwrap();
|
||||
let _ = h2.converge().unwrap();
|
||||
|
||||
assert_ulps_eq!(
|
||||
h2.time_slices[0].skills.get(b).unwrap().posterior(),
|
||||
@@ -2294,7 +2366,7 @@ mod tests {
|
||||
epsilon = 1e-6
|
||||
);
|
||||
|
||||
h.converge().unwrap();
|
||||
let _ = h.converge().unwrap();
|
||||
|
||||
let lc_a = h.learning_curve("a");
|
||||
let lc_b = h.learning_curve("b");
|
||||
@@ -2368,11 +2440,11 @@ mod tests {
|
||||
})
|
||||
.build();
|
||||
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();
|
||||
events_for(&mut h_full);
|
||||
h_full.converge().unwrap();
|
||||
let _ = h_full.converge().unwrap();
|
||||
|
||||
let curves_capped = h_capped.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();
|
||||
events_for(&mut h_undamped);
|
||||
h_undamped.converge().unwrap();
|
||||
let _ = h_undamped.converge().unwrap();
|
||||
|
||||
let mut h_damped: History<i64, _, _, &'static str> = History::builder()
|
||||
.convergence(ConvergenceOptions {
|
||||
@@ -2419,7 +2491,7 @@ mod tests {
|
||||
})
|
||||
.build();
|
||||
events_for(&mut h_damped);
|
||||
h_damped.converge().unwrap();
|
||||
let _ = h_damped.converge().unwrap();
|
||||
|
||||
let curves_u = h_undamped.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),
|
||||
}])
|
||||
.unwrap();
|
||||
h_a.converge().unwrap();
|
||||
let _ = h_a.converge().unwrap();
|
||||
|
||||
// Path B: history-wide default 0.5, no per-event override.
|
||||
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]),
|
||||
}])
|
||||
.unwrap();
|
||||
h_b.converge().unwrap();
|
||||
let _ = h_b.converge().unwrap();
|
||||
|
||||
// Inheritance: posteriors must be bit-equal.
|
||||
let curves_a = h_a.learning_curves();
|
||||
@@ -2495,7 +2567,7 @@ mod tests {
|
||||
outcome: Outcome::scores_with_sigma([3.0, 1.0], 2.0),
|
||||
}])
|
||||
.unwrap();
|
||||
h_a.converge().unwrap();
|
||||
let _ = h_a.converge().unwrap();
|
||||
|
||||
// Path B: history-wide default 2.0, no per-event override.
|
||||
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]),
|
||||
}])
|
||||
.unwrap();
|
||||
h_b.converge().unwrap();
|
||||
let _ = h_b.converge().unwrap();
|
||||
|
||||
// Override == default-set-to-the-override-value: bit-equal.
|
||||
let curves_a = h_a.learning_curves();
|
||||
@@ -2532,7 +2604,7 @@ mod tests {
|
||||
outcome: Outcome::scores([3.0, 1.0]),
|
||||
}])
|
||||
.unwrap();
|
||||
h_c.converge().unwrap();
|
||||
let _ = h_c.converge().unwrap();
|
||||
|
||||
let curves_c = h_c.learning_curves();
|
||||
let mut max_diff: f64 = 0.0;
|
||||
@@ -2561,7 +2633,7 @@ mod tests {
|
||||
.scores_with_sigma([3.0, 1.0], 2.0)
|
||||
.commit()
|
||||
.unwrap();
|
||||
h_a.converge().unwrap();
|
||||
let _ = h_a.converge().unwrap();
|
||||
|
||||
// Path B: same outcome via the explicit Outcome constructor.
|
||||
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),
|
||||
}])
|
||||
.unwrap();
|
||||
h_b.converge().unwrap();
|
||||
let _ = h_b.converge().unwrap();
|
||||
|
||||
let curves_a = h_a.learning_curves();
|
||||
let curves_b = h_b.learning_curves();
|
||||
|
||||
+17
@@ -158,6 +158,23 @@ pub const SIGMA: f64 = BETA * 6.0;
|
||||
pub const GAMMA: f64 = BETA * 0.03;
|
||||
pub const P_DRAW: f64 = 0.0;
|
||||
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;
|
||||
|
||||
/// Largest team count `History::predict_outcome` will enumerate.
|
||||
|
||||
Reference in New Issue
Block a user