//! `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, scale: Option, ) -> Event { 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(); let _ = with.converge().unwrap(); let mut without = history(); without .add_events(vec![bout("a", "b", 0, None, None)]) .unwrap(); let _ = 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(); let _ = late.converge().unwrap(); let mut never = history(); never .add_events(vec![ bout("a", "b", 0, None, None), bout("a", "b", 1, None, None), ]) .unwrap(); let _ = 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(); let _ = 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(); let _ = 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(); let _ = 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(); let _ = 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(); let _ = 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(); let _ = 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:?}" ); }