fix!: apply competitor configuration whenever it is supplied
`Member::with_prior` and `with_drift_scale` were consumed only on the branch that *creates* a competitor — `priors.remove` sat inside `if !self.agents.contains(..)`. Supplying either for a key the history already knew did nothing at all: no error, no warning, and output computed from the default prior. A prior applied on a competitor's very first event and was silently discarded ever after. Configuration now applies whenever supplied. Two details this forced: Configuration is tracked per *field* rather than as a merged `Rating`. A member setting only `drift_scale` must not also assert the default prior, or it would silently undo a prior seeded on an earlier event. Slice state has to be refreshed. `drift_scale` is re-derived on every forward pass, but a prior is written into the competitor's earliest slice once, at ingestion, and `iteration` refreshes only slices after the first. Without the refresh a late prior would reach the drift terms and nothing else — a subtler version of the drop being fixed. This was caught by a test, not by reading the code. Conflicting values for one competitor within a single batch are now `ConflictingCompetitorConfig` rather than resolved by iteration order. Events in a batch are unordered, so "last one wins" would make the result depend on traversal — and `tests/ingestion_equivalence.rs` exists to rule exactly that out. Repeating the same value stays inert, which is the shape callers get when configuration is a property of the domain. That invariant turned out to be tested only for *unconfigured* competitors: every helper in that file built members with `Member::new`. Extended to cover configured ones, including a check that configuration changes the fit at all, so the order tests cannot pass vacuously. `with_prior` had no coverage under `tests/` whatsoever, which is how this survived. Adds `tests/competitor_config.rs`. `drift_scale_is_ignored_after_first_appearance` asserted the old behaviour and now asserts the new one. It was written as a deliberate change-detector — "moving the capture would be a visible break, not a silent one" — so it inverted rather than being deleted. Also removes `InferenceError::ConvergenceFailed` and `NegativePrecision`, which no code path ever constructed: public variants advertising failure modes no caller could observe. Partial #20 — its other items were already resolved, except `Outcome::winner` still panicking. BREAKING CHANGE: `prior` and `drift_scale` now take effect for competitors the history already knows, where they were previously ignored; a batch supplying conflicting values for one competitor is now an error. `InferenceError::ConvergenceFailed` and `InferenceError::NegativePrecision` are removed. Closes #10. Refs #20. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011hcFjNDmHXZF8URGLku5zZ
This commit is contained in:
+15
-15
@@ -25,11 +25,6 @@ pub enum InferenceError {
|
||||
/// result has no representable likelihood. Configure a positive `p_draw`
|
||||
/// (via `HistoryBuilder::p_draw` or `GameOptions::p_draw`) to admit ties.
|
||||
TieWithoutDrawProbability { teams: (usize, usize) },
|
||||
/// Convergence exceeded `max_iter` without falling below `epsilon`.
|
||||
ConvergenceFailed {
|
||||
last_step: (f64, f64),
|
||||
iterations: usize,
|
||||
},
|
||||
/// Inference produced a non-finite value (NaN or infinity).
|
||||
///
|
||||
/// Indicates numerical breakdown; the resulting skills are meaningless
|
||||
@@ -38,8 +33,19 @@ pub enum InferenceError {
|
||||
context: &'static str,
|
||||
step: (f64, f64),
|
||||
},
|
||||
/// Negative precision: a Gaussian with `pi < 0` slipped into an API call.
|
||||
NegativePrecision { pi: f64 },
|
||||
/// One batch declared two different values for the same competitor's
|
||||
/// configuration.
|
||||
///
|
||||
/// `prior` and `drift_scale` configure a competitor, not an event, so a
|
||||
/// batch that sets one of them twice with different values has no
|
||||
/// well-defined meaning: events within a batch are not ordered, so
|
||||
/// "last one wins" would make the result depend on iteration order.
|
||||
/// Declaring the same value repeatedly is fine and is the expected shape
|
||||
/// when a competitor's configuration is a property of the domain.
|
||||
ConflictingCompetitorConfig {
|
||||
competitor: usize,
|
||||
field: &'static str,
|
||||
},
|
||||
/// A prediction referenced a key the history has no skill for.
|
||||
///
|
||||
/// Reported rather than skipped: dropping unknown keys turns a team of
|
||||
@@ -96,18 +102,12 @@ impl fmt::Display for InferenceError {
|
||||
Self::InvalidParameter { name, value } => {
|
||||
write!(f, "{name} is invalid: {value}")
|
||||
}
|
||||
Self::ConvergenceFailed {
|
||||
last_step,
|
||||
iterations,
|
||||
} => {
|
||||
Self::ConflictingCompetitorConfig { competitor, field } => {
|
||||
write!(
|
||||
f,
|
||||
"convergence failed after {iterations} iterations; last step = {last_step:?}"
|
||||
"competitor {competitor}: this batch sets {field} to two different values"
|
||||
)
|
||||
}
|
||||
Self::NegativePrecision { pi } => {
|
||||
write!(f, "precision must be non-negative; got {pi}")
|
||||
}
|
||||
Self::UnknownKey { team, member } => {
|
||||
write!(
|
||||
f,
|
||||
|
||||
+11
-3
@@ -50,9 +50,17 @@ impl<K> Default for Team<K> {
|
||||
/// `weight` applies per event and defaults to 1.0.
|
||||
///
|
||||
/// `prior` and `drift_scale` are **competitor configuration**, not per-event
|
||||
/// values: both are captured when the competitor is first created and ignored
|
||||
/// on every later appearance. Setting either on a key the history already knows
|
||||
/// has no effect.
|
||||
/// values. Setting either applies to the competitor for the whole history, not
|
||||
/// just to this event, and applies whenever it is supplied — including on a key
|
||||
/// the history already knows. Because configuration lives on the competitor and
|
||||
/// `converge` refits from competitor state, configuring one late still refits
|
||||
/// the whole history rather than taking effect only from that event onward.
|
||||
///
|
||||
/// Repeating the same value is inert, which is the expected shape when the
|
||||
/// configuration is a property of the domain. Supplying two *different* values
|
||||
/// for one competitor within a single batch is
|
||||
/// `InferenceError::ConflictingCompetitorConfig`: events in a batch have no
|
||||
/// order, so there would be no well-defined winner.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Member<K> {
|
||||
pub key: K,
|
||||
|
||||
+96
-22
@@ -172,6 +172,23 @@ impl Default for HistoryBuilder<i64, ConstantDrift, NullObserver, &'static str>
|
||||
}
|
||||
}
|
||||
|
||||
/// Configuration a caller attached to a competitor via `Member`.
|
||||
///
|
||||
/// Carries *what was explicitly set* rather than a merged `Rating`, so a member
|
||||
/// that sets only `drift_scale` does not also assert the default prior — which
|
||||
/// would spuriously conflict with a prior seeded on an earlier event.
|
||||
#[derive(Clone, Copy, Default)]
|
||||
pub(crate) struct CompetitorConfig {
|
||||
prior: Option<Gaussian>,
|
||||
drift_scale: Option<f64>,
|
||||
}
|
||||
|
||||
impl CompetitorConfig {
|
||||
fn is_empty(self) -> bool {
|
||||
self.prior.is_none() && self.drift_scale.is_none()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct History<
|
||||
T: Time = i64,
|
||||
D: Drift<T> = ConstantDrift,
|
||||
@@ -876,7 +893,7 @@ impl<T: Time, D: Drift<T>, O: Observer<T>, K: Eq + Hash + Clone> History<T, D, O
|
||||
times: Vec<T>,
|
||||
mut weights: Option<Vec<Vec<Vec<f64>>>>,
|
||||
kinds: Vec<EventKind>,
|
||||
mut priors: HashMap<Index, Rating<T, D>>,
|
||||
priors: HashMap<Index, CompetitorConfig>,
|
||||
) -> Result<(), InferenceError> {
|
||||
if results
|
||||
.as_ref()
|
||||
@@ -943,17 +960,60 @@ impl<T: Time, D: Drift<T>, O: Observer<T>, K: Eq + Hash + Clone> History<T, D, O
|
||||
|
||||
this_agent.push(*agent);
|
||||
|
||||
if !self.agents.contains(*agent) {
|
||||
let config = priors.get(agent).copied().unwrap_or_default();
|
||||
|
||||
if self.agents.contains(*agent) {
|
||||
// Seeding a competitor the history already knows. This used to
|
||||
// be dropped on the floor: `remove` was only reached on the
|
||||
// create path, so a prior applied on a competitor's very first
|
||||
// event and was silently ignored ever after.
|
||||
if config.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let rating = &mut self.agents.get_mut(*agent).unwrap().rating;
|
||||
if let Some(prior) = config.prior {
|
||||
rating.prior = prior;
|
||||
}
|
||||
if let Some(scale) = config.drift_scale {
|
||||
rating.drift_scale = scale;
|
||||
}
|
||||
let seeded = rating.prior;
|
||||
|
||||
if config.prior.is_some() {
|
||||
// The prior is not re-derived every pass the way drift is.
|
||||
// A competitor's earliest slice has its forward message set
|
||||
// to the prior once, at ingestion, and `iteration` refreshes
|
||||
// only slices after the first — so without this, a late
|
||||
// prior would reach the drift terms and nothing else, which
|
||||
// is a subtler version of the silent drop this replaced.
|
||||
//
|
||||
// `clean` has just nulled every message, so the earliest
|
||||
// slice's forward is exactly the prior.
|
||||
for slice in &mut self.time_slices {
|
||||
if let Some(skill) = slice.skills.get_mut(*agent) {
|
||||
skill.forward = seeded;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let mut rating = Rating::new(
|
||||
Gaussian::from_ms(self.mu, self.sigma),
|
||||
self.beta,
|
||||
self.drift,
|
||||
);
|
||||
if let Some(prior) = config.prior {
|
||||
rating.prior = prior;
|
||||
}
|
||||
if let Some(scale) = config.drift_scale {
|
||||
rating.drift_scale = scale;
|
||||
}
|
||||
|
||||
self.agents.insert(
|
||||
*agent,
|
||||
Competitor {
|
||||
rating: priors.remove(agent).unwrap_or_else(|| {
|
||||
Rating::new(
|
||||
Gaussian::from_ms(self.mu, self.sigma),
|
||||
self.beta,
|
||||
self.drift,
|
||||
)
|
||||
}),
|
||||
rating,
|
||||
message: None,
|
||||
last_time: None,
|
||||
},
|
||||
@@ -1170,7 +1230,7 @@ impl<T: Time, D: Drift<T>, O: Observer<T>, K: Eq + Hash + Clone> History<T, D, O
|
||||
let mut times: Vec<T> = Vec::with_capacity(events.len());
|
||||
let mut weights: Vec<Vec<Vec<f64>>> = Vec::with_capacity(events.len());
|
||||
let mut kinds: Vec<EventKind> = Vec::with_capacity(events.len());
|
||||
let mut priors: HashMap<Index, Rating<T, D>> = HashMap::new();
|
||||
let mut priors: HashMap<Index, CompetitorConfig> = HashMap::new();
|
||||
|
||||
for ev in events {
|
||||
if ev.outcome.team_count() != ev.teams.len() {
|
||||
@@ -1204,23 +1264,37 @@ impl<T: Time, D: Drift<T>, O: Observer<T>, K: Eq + Hash + Clone> History<T, D, O
|
||||
}
|
||||
}
|
||||
|
||||
// `prior` and `drift_scale` are competitor configuration,
|
||||
// captured here and consumed at competitor creation. Both
|
||||
// land in the same entry so a member may set either alone.
|
||||
// `prior` and `drift_scale` configure the competitor, not
|
||||
// the event. Both land in the same entry so a member may
|
||||
// set either alone.
|
||||
//
|
||||
// Events within a batch are not ordered, so a batch that
|
||||
// sets one field twice with different values has no
|
||||
// well-defined result — "last one wins" would depend on
|
||||
// iteration order, which `tests/ingestion_equivalence.rs`
|
||||
// exists to rule out. Repeating the *same* value is fine,
|
||||
// and is the expected shape when the configuration is a
|
||||
// property of the domain rather than of one event.
|
||||
if member.prior.is_some() || member.drift_scale.is_some() {
|
||||
let rating = priors.entry(idx).or_insert_with(|| {
|
||||
Rating::new(
|
||||
Gaussian::from_ms(self.mu, self.sigma),
|
||||
self.beta,
|
||||
self.drift,
|
||||
)
|
||||
});
|
||||
let entry = priors.entry(idx).or_default();
|
||||
|
||||
if let Some(prior) = member.prior {
|
||||
rating.prior = prior;
|
||||
if entry.prior.is_some_and(|held| held != prior) {
|
||||
return Err(InferenceError::ConflictingCompetitorConfig {
|
||||
competitor: idx.get(),
|
||||
field: "prior",
|
||||
});
|
||||
}
|
||||
entry.prior = Some(prior);
|
||||
}
|
||||
if let Some(scale) = member.drift_scale {
|
||||
rating.drift_scale = scale;
|
||||
if entry.drift_scale.is_some_and(|held| held != scale) {
|
||||
return Err(InferenceError::ConflictingCompetitorConfig {
|
||||
competitor: idx.get(),
|
||||
field: "drift_scale",
|
||||
});
|
||||
}
|
||||
entry.drift_scale = Some(scale);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,222 @@
|
||||
//! `Member::with_prior` / `with_drift_scale` — competitor configuration.
|
||||
//!
|
||||
//! Both were previously consumed only on the branch that *creates* a
|
||||
//! competitor, so configuration supplied for a key the history already knew was
|
||||
//! dropped with no error. `with_prior` had no coverage in this directory at
|
||||
//! all, which is how that survived.
|
||||
|
||||
use smallvec::smallvec;
|
||||
use trueskill_tt::{
|
||||
ConvergenceOptions, Event, Gaussian, History, InferenceError, Member, Outcome, Team,
|
||||
};
|
||||
|
||||
const CONVERGENCE: ConvergenceOptions = ConvergenceOptions {
|
||||
max_iter: 2_000,
|
||||
epsilon: 1e-12,
|
||||
alpha: 1.0,
|
||||
};
|
||||
|
||||
fn history() -> History {
|
||||
History::builder()
|
||||
.mu(25.0)
|
||||
.sigma(25.0 / 3.0)
|
||||
.beta(25.0 / 6.0)
|
||||
.p_draw(0.0)
|
||||
.convergence(CONVERGENCE)
|
||||
.build()
|
||||
}
|
||||
|
||||
/// One event, optionally configuring `a`.
|
||||
fn bout(
|
||||
a: &'static str,
|
||||
b: &'static str,
|
||||
time: i64,
|
||||
prior: Option<Gaussian>,
|
||||
scale: Option<f64>,
|
||||
) -> Event<i64, &'static str> {
|
||||
let mut member = Member::new(a);
|
||||
if let Some(p) = prior {
|
||||
member = member.with_prior(p);
|
||||
}
|
||||
if let Some(s) = scale {
|
||||
member = member.with_drift_scale(s);
|
||||
}
|
||||
|
||||
Event {
|
||||
time,
|
||||
teams: smallvec![
|
||||
Team::with_members([member]),
|
||||
Team::with_members([Member::new(b)]),
|
||||
],
|
||||
outcome: Outcome::winner(0, 2),
|
||||
}
|
||||
}
|
||||
|
||||
fn skill_of(h: &History, key: &str) -> Gaussian {
|
||||
h.current_skill(&key).expect("key in history")
|
||||
}
|
||||
|
||||
/// Baseline: the mechanism works at all on a competitor's first appearance.
|
||||
#[test]
|
||||
fn a_prior_applies_to_a_new_competitor() {
|
||||
let seeded = Gaussian::from_ms(40.0, 1.0);
|
||||
|
||||
let mut with = history();
|
||||
with.add_events(vec![bout("a", "b", 0, Some(seeded), None)])
|
||||
.unwrap();
|
||||
with.converge().unwrap();
|
||||
|
||||
let mut without = history();
|
||||
without
|
||||
.add_events(vec![bout("a", "b", 0, None, None)])
|
||||
.unwrap();
|
||||
without.converge().unwrap();
|
||||
|
||||
assert!(
|
||||
(skill_of(&with, "a").mu() - skill_of(&without, "a").mu()).abs() > 1.0,
|
||||
"a seeded prior should move the fit"
|
||||
);
|
||||
}
|
||||
|
||||
/// The defect in #10: a prior supplied for a competitor the history already
|
||||
/// knows was silently discarded, and the caller got output computed from the
|
||||
/// default prior with no indication anything had been dropped.
|
||||
#[test]
|
||||
fn a_prior_applies_to_a_competitor_the_history_already_knows() {
|
||||
let seeded = Gaussian::from_ms(40.0, 1.0);
|
||||
|
||||
let mut late = history();
|
||||
late.add_events(vec![bout("a", "b", 0, None, None)])
|
||||
.unwrap();
|
||||
// "a" now exists. Configuring it here used to do nothing whatsoever.
|
||||
late.add_events(vec![bout("a", "b", 1, Some(seeded), None)])
|
||||
.unwrap();
|
||||
late.converge().unwrap();
|
||||
|
||||
let mut never = history();
|
||||
never
|
||||
.add_events(vec![
|
||||
bout("a", "b", 0, None, None),
|
||||
bout("a", "b", 1, None, None),
|
||||
])
|
||||
.unwrap();
|
||||
never.converge().unwrap();
|
||||
|
||||
assert!(
|
||||
(skill_of(&late, "a").mu() - skill_of(&never, "a").mu()).abs() > 1.0,
|
||||
"a late prior must not be silently dropped: {} vs {}",
|
||||
skill_of(&late, "a").mu(),
|
||||
skill_of(&never, "a").mu()
|
||||
);
|
||||
}
|
||||
|
||||
/// Configuration is competitor-scoped, not event-scoped, and `converge` refits
|
||||
/// from competitor state — so seeding late reaches the same fit as seeding from
|
||||
/// the start. This is the documented scope, asserted rather than assumed.
|
||||
#[test]
|
||||
fn a_prior_is_whole_history_scoped_not_per_event() {
|
||||
let seeded = Gaussian::from_ms(40.0, 1.0);
|
||||
|
||||
let mut late = history();
|
||||
late.add_events(vec![bout("a", "b", 0, None, None)])
|
||||
.unwrap();
|
||||
late.add_events(vec![bout("a", "b", 1, Some(seeded), None)])
|
||||
.unwrap();
|
||||
late.converge().unwrap();
|
||||
|
||||
let mut early = history();
|
||||
early
|
||||
.add_events(vec![
|
||||
bout("a", "b", 0, Some(seeded), None),
|
||||
bout("a", "b", 1, Some(seeded), None),
|
||||
])
|
||||
.unwrap();
|
||||
early.converge().unwrap();
|
||||
|
||||
let (l, e) = (skill_of(&late, "a"), skill_of(&early, "a"));
|
||||
assert!(
|
||||
(l.mu() - e.mu()).abs() < 1e-9 && (l.sigma() - e.sigma()).abs() < 1e-9,
|
||||
"late seeding should refit the whole history: {l:?} vs {e:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeating_the_same_prior_is_inert() {
|
||||
let seeded = Gaussian::from_ms(40.0, 1.0);
|
||||
|
||||
let mut once = history();
|
||||
once.add_events(vec![
|
||||
bout("a", "b", 0, Some(seeded), None),
|
||||
bout("a", "b", 1, None, None),
|
||||
])
|
||||
.unwrap();
|
||||
once.converge().unwrap();
|
||||
|
||||
let mut every_time = history();
|
||||
every_time
|
||||
.add_events(vec![
|
||||
bout("a", "b", 0, Some(seeded), None),
|
||||
bout("a", "b", 1, Some(seeded), None),
|
||||
])
|
||||
.unwrap();
|
||||
every_time.converge().unwrap();
|
||||
|
||||
let (o, e) = (skill_of(&once, "a"), skill_of(&every_time, "a"));
|
||||
assert!(
|
||||
(o.mu() - e.mu()).abs() < 1e-12 && (o.sigma() - e.sigma()).abs() < 1e-12,
|
||||
"declaring the same prior repeatedly changed the fit: {o:?} vs {e:?}"
|
||||
);
|
||||
}
|
||||
|
||||
/// Events within a batch have no order, so two different values for one
|
||||
/// competitor have no well-defined winner. Rejecting is what keeps the answer
|
||||
/// independent of iteration order.
|
||||
#[test]
|
||||
fn a_batch_declaring_two_different_priors_is_rejected() {
|
||||
let mut h = history();
|
||||
let err = h
|
||||
.add_events(vec![
|
||||
bout("a", "b", 0, Some(Gaussian::from_ms(40.0, 1.0)), None),
|
||||
bout("a", "b", 1, Some(Gaussian::from_ms(10.0, 1.0)), None),
|
||||
])
|
||||
.expect_err("two different priors for one competitor in one batch");
|
||||
|
||||
assert!(
|
||||
matches!(
|
||||
err,
|
||||
InferenceError::ConflictingCompetitorConfig { field: "prior", .. }
|
||||
),
|
||||
"got {err:?}"
|
||||
);
|
||||
}
|
||||
|
||||
/// A member setting only `drift_scale` must not also assert the default prior,
|
||||
/// or it would silently undo a prior seeded earlier. This is why the collected
|
||||
/// configuration tracks each field separately rather than a merged `Rating`.
|
||||
#[test]
|
||||
fn setting_one_field_late_leaves_the_other_alone() {
|
||||
let seeded = Gaussian::from_ms(40.0, 1.0);
|
||||
|
||||
let mut h = history();
|
||||
h.add_events(vec![bout("a", "b", 0, Some(seeded), None)])
|
||||
.unwrap();
|
||||
// Only the scale this time — the prior above must survive.
|
||||
h.add_events(vec![bout("a", "b", 1, None, Some(0.5))])
|
||||
.unwrap();
|
||||
h.converge().unwrap();
|
||||
|
||||
let mut both_upfront = history();
|
||||
both_upfront
|
||||
.add_events(vec![
|
||||
bout("a", "b", 0, Some(seeded), Some(0.5)),
|
||||
bout("a", "b", 1, None, None),
|
||||
])
|
||||
.unwrap();
|
||||
both_upfront.converge().unwrap();
|
||||
|
||||
let (a, b) = (skill_of(&h, "a"), skill_of(&both_upfront, "a"));
|
||||
assert!(
|
||||
(a.mu() - b.mu()).abs() < 1e-9 && (a.sigma() - b.sigma()).abs() < 1e-9,
|
||||
"setting drift_scale late clobbered the earlier prior: {a:?} vs {b:?}"
|
||||
);
|
||||
}
|
||||
+112
-15
@@ -341,13 +341,20 @@ fn zero_scale_pins_a_competitor_in_the_filtered_pass() {
|
||||
);
|
||||
}
|
||||
|
||||
/// `drift_scale` is competitor configuration captured at first appearance, the
|
||||
/// same as `prior` — a later `with_drift_scale` on a key the history already
|
||||
/// knows is ignored. This guards that decision rather than driving it: the
|
||||
/// behaviour falls out of where the capture happens, and the point of the test
|
||||
/// is that moving the capture would be a visible break, not a silent one.
|
||||
/// `drift_scale` is competitor configuration, and configuration supplied for a
|
||||
/// competitor the history already knows is now *applied* rather than dropped.
|
||||
///
|
||||
/// This test previously asserted the opposite. It was written as a deliberate
|
||||
/// change-detector — "moving the capture would be a visible break, not a silent
|
||||
/// one" — and that is exactly what happened: the capture moved, and the
|
||||
/// assertion inverted rather than being deleted.
|
||||
///
|
||||
/// Because configuration lives on the competitor and `converge` refits from
|
||||
/// competitor state, a late pin applies to the *whole* history, not just to
|
||||
/// events after it. So a scale set on the second batch must reach the same fit
|
||||
/// as one set from the very first event.
|
||||
#[test]
|
||||
fn drift_scale_is_ignored_after_first_appearance() {
|
||||
fn drift_scale_applies_when_set_after_first_appearance() {
|
||||
let mut late = History::builder()
|
||||
.mu(25.0)
|
||||
.sigma(25.0 / 3.0)
|
||||
@@ -368,7 +375,7 @@ fn drift_scale_is_ignored_after_first_appearance() {
|
||||
}])
|
||||
.unwrap();
|
||||
|
||||
// Second batch asks for a pin. Too late: the competitor already exists.
|
||||
// Second batch asks for a pin. No longer too late.
|
||||
late.add_events(vec![Event {
|
||||
time: 1000,
|
||||
teams: smallvec![
|
||||
@@ -380,23 +387,113 @@ fn drift_scale_is_ignored_after_first_appearance() {
|
||||
.unwrap();
|
||||
late.converge().unwrap();
|
||||
|
||||
let ignored = curve(&late, "anchor");
|
||||
let drifting = curve(&fit(distant_pair(None), 25.0 / 300.0), "anchor");
|
||||
let applied = curve(&late, "anchor");
|
||||
let pinned_from_the_start = curve(&fit(distant_pair(Some(0.0)), 25.0 / 300.0), "anchor");
|
||||
let never_pinned = curve(&fit(distant_pair(None), 25.0 / 300.0), "anchor");
|
||||
|
||||
for ((t_l, g_l), (t_r, g_r)) in ignored.iter().zip(drifting.iter()) {
|
||||
for ((t_l, g_l), (t_r, g_r)) in applied.iter().zip(pinned_from_the_start.iter()) {
|
||||
assert_eq!(t_l, t_r);
|
||||
assert!(
|
||||
(g_l.sigma() - g_r.sigma()).abs() < 1e-9,
|
||||
"a scale set after first appearance must be ignored, leaving the fit \
|
||||
identical to one that never set it: t={t_l}, {} vs {}",
|
||||
"a late pin should refit the whole history: t={t_l}, {} vs {}",
|
||||
g_l.sigma(),
|
||||
g_r.sigma()
|
||||
);
|
||||
}
|
||||
|
||||
let pinned = curve(&fit(distant_pair(Some(0.0)), 25.0 / 300.0), "anchor");
|
||||
// And it must actually have done something.
|
||||
assert!(
|
||||
(ignored[1].1.sigma() - pinned[1].1.sigma()).abs() > 1e-6,
|
||||
"sanity: the pinned fit must actually differ, or the assertion above is vacuous"
|
||||
applied
|
||||
.iter()
|
||||
.zip(never_pinned.iter())
|
||||
.any(|((_, a), (_, b))| (a.sigma() - b.sigma()).abs() > 1e-9),
|
||||
"the pin had no effect at all — the silent drop is back"
|
||||
);
|
||||
}
|
||||
|
||||
/// Re-declaring the same configuration must be inert. This is the shape a
|
||||
/// caller gets when the configuration is a property of the domain — "layouts
|
||||
/// are static" — so every ingestion path repeats it on every event.
|
||||
///
|
||||
/// Both histories see exactly the same events; only how many times the scale
|
||||
/// is declared differs.
|
||||
#[test]
|
||||
fn repeating_the_same_configuration_changes_nothing() {
|
||||
let events = |declare_every_time: bool| {
|
||||
let anchor = |first: bool| {
|
||||
if first || declare_every_time {
|
||||
Member::new("anchor").with_drift_scale(0.0)
|
||||
} else {
|
||||
Member::new("anchor")
|
||||
}
|
||||
};
|
||||
vec![
|
||||
Event {
|
||||
time: 0,
|
||||
teams: smallvec![
|
||||
Team::with_members([anchor(true)]),
|
||||
Team::with_members([Member::new("player")]),
|
||||
],
|
||||
outcome: Outcome::winner(0, 2),
|
||||
},
|
||||
Event {
|
||||
time: 1000,
|
||||
teams: smallvec![
|
||||
Team::with_members([anchor(false)]),
|
||||
Team::with_members([Member::new("player")]),
|
||||
],
|
||||
outcome: Outcome::winner(1, 2),
|
||||
},
|
||||
]
|
||||
};
|
||||
|
||||
let once = curve(&fit(events(false), 25.0 / 300.0), "anchor");
|
||||
let every_time = curve(&fit(events(true), 25.0 / 300.0), "anchor");
|
||||
|
||||
for ((t_l, a), (t_r, b)) in once.iter().zip(every_time.iter()) {
|
||||
assert_eq!(t_l, t_r);
|
||||
assert!(
|
||||
(a.sigma() - b.sigma()).abs() < 1e-12,
|
||||
"t={t_l}: declaring the same scale repeatedly changed the fit, {} vs {}",
|
||||
a.sigma(),
|
||||
b.sigma()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_batch_that_contradicts_itself_is_rejected() {
|
||||
let mut h = History::builder().convergence(CONVERGENCE).build();
|
||||
|
||||
let err = h
|
||||
.add_events(vec![
|
||||
Event {
|
||||
time: 0,
|
||||
teams: smallvec![
|
||||
Team::with_members([Member::new("anchor").with_drift_scale(0.0)]),
|
||||
Team::with_members([Member::new("player")]),
|
||||
],
|
||||
outcome: Outcome::winner(0, 2),
|
||||
},
|
||||
Event {
|
||||
time: 1,
|
||||
teams: smallvec![
|
||||
Team::with_members([Member::new("anchor").with_drift_scale(1.0)]),
|
||||
Team::with_members([Member::new("player")]),
|
||||
],
|
||||
outcome: Outcome::winner(0, 2),
|
||||
},
|
||||
])
|
||||
.expect_err("two different scales for one competitor in one batch");
|
||||
|
||||
assert!(
|
||||
matches!(
|
||||
err,
|
||||
InferenceError::ConflictingCompetitorConfig {
|
||||
field: "drift_scale",
|
||||
..
|
||||
}
|
||||
),
|
||||
"got {err:?}"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -30,6 +30,22 @@ fn event(a: &str, b: &str, time: i64) -> Event<i64, String> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Like [`event`], but `a` carries competitor configuration.
|
||||
///
|
||||
/// `prior` and `drift_scale` configure the competitor rather than the event, so
|
||||
/// they are the part of ingestion most exposed to order: they are consumed once,
|
||||
/// where the competitor's state is written.
|
||||
fn configured_event(a: &str, b: &str, time: i64, scale: f64) -> Event<i64, String> {
|
||||
Event {
|
||||
time,
|
||||
teams: smallvec![
|
||||
Team::with_members([Member::new(a.to_string()).with_drift_scale(scale)]),
|
||||
Team::with_members([Member::new(b.to_string())]),
|
||||
],
|
||||
outcome: Outcome::winner(0, 2),
|
||||
}
|
||||
}
|
||||
|
||||
fn converged_skills(events: Vec<Event<i64, String>>, batched: bool) -> Vec<(String, Gaussian)> {
|
||||
let mut h: History<i64, _, _, String> =
|
||||
History::builder_with_key().convergence(tight()).build();
|
||||
@@ -145,3 +161,65 @@ fn back_dated_event_matches_batched() {
|
||||
let incremental = converged_skills(events, false);
|
||||
assert_same(&batched, &incremental, "back-dated event");
|
||||
}
|
||||
|
||||
/// The invariant this file protects was only ever checked for *unconfigured*
|
||||
/// competitors — every helper above built members with `Member::new`.
|
||||
///
|
||||
/// Configuration is the part most exposed to ordering, because it is consumed
|
||||
/// once at the point the competitor's state is written rather than replayed per
|
||||
/// event. These cover it.
|
||||
#[test]
|
||||
fn configured_competitors_are_order_independent() {
|
||||
let events = vec![
|
||||
configured_event("a", "b", 0, 0.0),
|
||||
configured_event("a", "c", 1, 0.0),
|
||||
configured_event("a", "b", 2, 0.0),
|
||||
event("b", "c", 3),
|
||||
];
|
||||
|
||||
assert_same(
|
||||
&converged_skills(events.clone(), true),
|
||||
&converged_skills(events, false),
|
||||
"configuration repeated on every appearance",
|
||||
);
|
||||
}
|
||||
|
||||
/// Configuration supplied only on a *later* event is the case that used to be
|
||||
/// silently dropped. It must now reach the same fit either way it is ingested.
|
||||
#[test]
|
||||
fn late_configuration_is_order_independent() {
|
||||
let events = vec![
|
||||
event("a", "b", 0),
|
||||
configured_event("a", "c", 1, 0.0),
|
||||
event("a", "b", 2),
|
||||
];
|
||||
|
||||
assert_same(
|
||||
&converged_skills(events.clone(), true),
|
||||
&converged_skills(events, false),
|
||||
"configuration supplied after first appearance",
|
||||
);
|
||||
}
|
||||
|
||||
/// And it must actually be doing something — an implementation that dropped
|
||||
/// configuration entirely would pass both tests above.
|
||||
#[test]
|
||||
fn configuration_changes_the_fit_however_it_is_ingested() {
|
||||
let configured = vec![
|
||||
event("a", "b", 0),
|
||||
configured_event("a", "c", 1, 0.0),
|
||||
event("a", "b", 2),
|
||||
];
|
||||
let plain = vec![event("a", "b", 0), event("a", "c", 1), event("a", "b", 2)];
|
||||
|
||||
for batched in [true, false] {
|
||||
let with = converged_skills(configured.clone(), batched);
|
||||
let without = converged_skills(plain.clone(), batched);
|
||||
assert!(
|
||||
with.iter()
|
||||
.zip(&without)
|
||||
.any(|((_, x), (_, y))| (x.sigma() - y.sigma()).abs() > 1e-9),
|
||||
"batched={batched}: configuration had no effect, so the order tests are vacuous"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user