Drift was a property of the History, so every competitor drifted at the same rate and a fixed reference point could not share a graph with moving competitors. A bot at a known strength, a rating floor, a course difficulty — all of them drifted along with the players. Member::with_drift_scale(s) multiplies the drift *variance* a competitor accumulates, so s is in the same units as gamma: ConstantDrift(g) at scale s behaves exactly as ConstantDrift(g * s) would for that competitor. A scalar rather than a per-competitor Drift keeps History's single D type parameter untouched and stays Copy. 0.0 pins a competitor still. The scale lives on Rating, beside the drift it scales, and is applied only through Rating::drift_variance_delta / drift_variance_for_elapsed. Making those the sole entry points means a caller cannot reach the raw drift and silently skip a competitor's scale — the filtered pass was exactly that bug during development, caught because its test was written before the wiring. Like with_prior, the scale is competitor configuration captured at first appearance rather than a per-event override; a competitor that is static is static, and a scale that changed between events would make the skill trajectory hard to interpret. Member's docs claimed prior was a per-event override, which the code has never done — corrected here. A negative scale is rejected rather than squared into its absolute value, and a non-finite one rejected outright, both as InvalidParameter. None means 1.0, so no existing call site changes and no existing fit moves. Adding a public field to Member does break struct-literal construction downstream. Closes #34 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014b6wy2q8rnFK8U8GPJVQNU
166 lines
4.6 KiB
Rust
166 lines
4.6 KiB
Rust
//! Typed event description for bulk ingestion.
|
|
//!
|
|
//! `Event<T, K>` is the new public event shape (spec Section 4). Replaces
|
|
//! the nested `Vec<Vec<Vec<Index>>>`, `Vec<Vec<f64>>`, `Vec<Vec<Vec<f64>>>`
|
|
//! that the old `add_events_with_prior` took.
|
|
|
|
use smallvec::SmallVec;
|
|
|
|
use crate::{gaussian::Gaussian, outcome::Outcome, time::Time};
|
|
|
|
/// A single match at time `time` involving some number of teams.
|
|
#[derive(Clone, Debug)]
|
|
pub struct Event<T: Time, K> {
|
|
pub time: T,
|
|
pub teams: SmallVec<[Team<K>; 4]>,
|
|
pub outcome: Outcome,
|
|
}
|
|
|
|
/// A team: list of members competing together.
|
|
#[derive(Clone, Debug)]
|
|
pub struct Team<K> {
|
|
pub members: SmallVec<[Member<K>; 4]>,
|
|
}
|
|
|
|
impl<K> Team<K> {
|
|
#[must_use]
|
|
pub fn new() -> Self {
|
|
Self {
|
|
members: SmallVec::new(),
|
|
}
|
|
}
|
|
|
|
pub fn with_members<I: IntoIterator<Item = Member<K>>>(members: I) -> Self {
|
|
Self {
|
|
members: members.into_iter().collect(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<K> Default for Team<K> {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
/// One member of a team, identified by user key `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.
|
|
#[derive(Clone, Debug)]
|
|
pub struct Member<K> {
|
|
pub key: K,
|
|
pub weight: f64,
|
|
pub prior: Option<Gaussian>,
|
|
/// Multiplier on the drift *variance* this competitor accumulates.
|
|
/// `None` means 1.0.
|
|
pub drift_scale: Option<f64>,
|
|
}
|
|
|
|
impl<K> Member<K> {
|
|
pub fn new(key: K) -> Self {
|
|
Self {
|
|
key,
|
|
weight: 1.0,
|
|
prior: None,
|
|
drift_scale: None,
|
|
}
|
|
}
|
|
|
|
pub fn with_weight(mut self, weight: f64) -> Self {
|
|
self.weight = weight;
|
|
self
|
|
}
|
|
|
|
/// Set this competitor's starting skill estimate.
|
|
///
|
|
/// Captured at the competitor's first appearance; see the type docs.
|
|
pub fn with_prior(mut self, prior: Gaussian) -> Self {
|
|
self.prior = Some(prior);
|
|
self
|
|
}
|
|
|
|
/// Scale how fast this competitor drifts, relative to the history's drift.
|
|
///
|
|
/// The scale multiplies the drift *variance*, so it is in the same units as
|
|
/// `gamma`: `ConstantDrift(g)` at `scale = s` behaves exactly as
|
|
/// `ConstantDrift(g * s)` would for this competitor alone.
|
|
///
|
|
/// `0.0` pins the competitor still — useful for a reference point that
|
|
/// shares a scale with moving competitors but should not itself move: a bot
|
|
/// at a known strength, a rating floor, a course difficulty.
|
|
///
|
|
/// Captured at the competitor's first appearance; see the type docs.
|
|
/// Must be finite and non-negative, or ingestion fails with
|
|
/// [`InferenceError::InvalidParameter`](crate::InferenceError::InvalidParameter).
|
|
pub fn with_drift_scale(mut self, scale: f64) -> Self {
|
|
self.drift_scale = Some(scale);
|
|
self
|
|
}
|
|
}
|
|
|
|
/// Convenience: a member is a user key with default weight 1.0 and no prior.
|
|
impl<K> From<K> for Member<K> {
|
|
fn from(key: K) -> Self {
|
|
Self::new(key)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::Outcome;
|
|
|
|
#[test]
|
|
fn member_new_has_unit_weight_no_prior() {
|
|
let m = Member::new("alice");
|
|
assert_eq!(m.key, "alice");
|
|
assert_eq!(m.weight, 1.0);
|
|
assert!(m.prior.is_none());
|
|
assert!(m.drift_scale.is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn member_builder_methods_chain() {
|
|
let m = Member::new("alice")
|
|
.with_weight(0.5)
|
|
.with_prior(Gaussian::from_ms(20.0, 5.0))
|
|
.with_drift_scale(0.0);
|
|
assert_eq!(m.weight, 0.5);
|
|
assert!(m.prior.is_some());
|
|
assert_eq!(m.drift_scale, Some(0.0));
|
|
}
|
|
|
|
#[test]
|
|
fn member_from_key() {
|
|
let m: Member<&str> = "bob".into();
|
|
assert_eq!(m.key, "bob");
|
|
assert_eq!(m.weight, 1.0);
|
|
}
|
|
|
|
#[test]
|
|
fn team_with_members_collects() {
|
|
let t: Team<&str> = Team::with_members([Member::new("a"), Member::new("b")]);
|
|
assert_eq!(t.members.len(), 2);
|
|
}
|
|
|
|
#[test]
|
|
fn event_construction() {
|
|
use smallvec::smallvec;
|
|
let e: Event<i64, &str> = Event {
|
|
time: 1,
|
|
teams: smallvec![
|
|
Team::with_members([Member::new("a")]),
|
|
Team::with_members([Member::new("b")]),
|
|
],
|
|
outcome: Outcome::winner(0, 2),
|
|
};
|
|
assert_eq!(e.teams.len(), 2);
|
|
assert_eq!(e.time, 1);
|
|
}
|
|
}
|