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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user