hoops_ai.ml.context_layer.NumericWeightedRule
- class hoops_ai.ml.context_layer.NumericWeightedRule(log_scale=True, min_evidence=3, interval_sigmas=1.0, relevance_weighter=None, auto_relevance_weight=True, score_temperature=None, nearest_neighbor_threshold=None)
Bases:
AggregationRulePredict continuous numeric context (e.g. cost, weight, time) from neighbors.
Three operating modes selected at construction:
Plain weighted mean —
relevance_weighter=Noneandauto_relevance_weight=False. Pure shape-weighted aggregation.Auto-fitted weighter (default) — the rule lazily fits a per-key
RelevanceWeighterfrom the current call’s hit metadata.Explicit weighter — pass
relevance_weighter=RelevanceWeighter(...)to skip auto-fit and use hand-tuned factors.
Confidence comes from the coefficient of variation (CV) of neighbor values:
confidence = 1 / (1 + CV). Log-space is appropriate for multiplicative quantities (cost, weight, time); setlog_scale=Falsefor additive quantities.- Parameters:
log_scale (bool) – If True (default), operates in log-space. Set False for additive quantities (counts, dimensions).
min_evidence (int) – Minimum hits required before predicting. Default 3.
interval_sigmas (float) – Prediction-interval width in σ. Default 1.0 (≈68%).
relevance_weighter (RelevanceWeighter | None) – Explicit weighter; disables auto-fit for keys this rule sees.
auto_relevance_weight (bool) – When True (default) and
relevance_weighteris None, the rule lazily fits a per-key weighter the first timepredict_with_context()runs for that key, using the call’s hit metadata.score_temperature (float | None) – When set, neighbour weights are computed by a softmax
exp(T·s) / Σ exp(T·s)over the adjusted scores instead of the default linears / Σ s. Sharper weighting: a hit that matches on shape and on every injected metadata key takes almost all of the mass, so the estimate converges to that neighbour’s value. Useful when the cost surface is highly non-linear and you want a near-duplicate, full-metadata-match neighbour to dominate.None(default) preserves the linear behaviour. Typical values:4.0(moderate sharpening) to12.0(near-argmax). Must be a positive finite number.nearest_neighbor_threshold (float | None) – When set, the rule first checks whether any neighbour reaches a normalised global agreement score
adjusted_i / max_possible_boost≥ threshold. The numerator is the weighter-adjusted score (shape × per-key boosts); the denominator is what a perfect-match neighbour would score, so the ratio lives in[0, 1]. When the gate fires the rule short-circuits to that neighbour’s value with confidence equal to the normalised score — the same idea asNearestNeighborRulebut ranked by a global signal that also reflectsMaterial/Process/InternalFeaturesagreement instead of raw shape similarity alone. When no neighbour clears the gate the rule falls through to the softmax / linear path as usual.None(default) disables the gate. Must be in(0, 1]when set. With no weighter and noquery_contextthe normalisation degenerates to1.0and this is exactlyNearestNeighborRule(threshold)on the raw shape score.
- predict(values, scores, key)
Predict a context value from neighbor evidence.
- Parameters:
- Returns:
ContextPrediction or None if insufficient evidence.
- Return type:
ContextPrediction | None
- predict_with_context(values, scores, key, query_context=None, *, hits=None)
Adjust scores via the (auto- or hand-fit) weighter, then aggregate.
Pipeline:
Resolve the weighter for
key: explicitrelevance_weighterwins; otherwise lazy-fit per-key ifauto_relevance_weight.Auto-infer a baseline
query_contextfrom the top-K hits’ metadata using the weighter’s known factor keys, then overlay any caller-suppliedquery_contexton top (caller wins). This lets a partial injection (e.g.{Material, Process}forwarded from earlier categorical predictions) still benefit from neighbor-derived numeric factors likeInternalFeatures.Multiply
scoresby the weighter’s per-hit boosts.Fall through to
predict()on the adjusted scores.
- property relevance_weighter: RelevanceWeighter | None
The explicit weighter passed at construction (
Noneif auto-fitting).