| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| export interface BrierEntry { |
| gaugeId: string; |
| predicted: number; |
| outcome: 0 | 1; |
| score: number; |
| timestamp: string; |
| } |
|
|
| |
| |
| |
|
|
| export class BrierLedger { |
| private readonly buffer: BrierEntry[]; |
| private head = 0; |
| private count = 0; |
| readonly capacity: number; |
|
|
| constructor(capacity = 1000) { |
| this.capacity = capacity; |
| this.buffer = new Array(capacity); |
| } |
|
|
| |
| record(gaugeId: string, predicted: number, outcome: 0 | 1): BrierEntry { |
| if (predicted < 0 || predicted > 1) { |
| throw new RangeError(`predicted probability must be in [0,1], got ${predicted}`); |
| } |
| const score = Math.pow(predicted - outcome, 2); |
| const entry: BrierEntry = { |
| gaugeId, |
| predicted, |
| outcome, |
| score, |
| timestamp: new Date().toISOString(), |
| }; |
| this.buffer[this.head] = entry; |
| this.head = (this.head + 1) % this.capacity; |
| if (this.count < this.capacity) this.count++; |
| return entry; |
| } |
|
|
| |
| entries(): BrierEntry[] { |
| if (this.count < this.capacity) return this.buffer.slice(0, this.count) as BrierEntry[]; |
| |
| return [ |
| ...this.buffer.slice(this.head), |
| ...this.buffer.slice(0, this.head), |
| ] as BrierEntry[]; |
| } |
|
|
| |
| mean(): number { |
| const all = this.entries(); |
| if (all.length === 0) return 0; |
| return all.reduce((s, e) => s + e.score, 0) / all.length; |
| } |
|
|
| |
| meanForGauge(gaugeId: string): number | undefined { |
| const filtered = this.entries().filter((e) => e.gaugeId === gaugeId); |
| if (filtered.length === 0) return undefined; |
| return filtered.reduce((s, e) => s + e.score, 0) / filtered.length; |
| } |
|
|
| |
| summary(): Record<string, { count: number; meanScore: number }> { |
| const out: Record<string, { count: number; meanScore: number }> = {}; |
| for (const e of this.entries()) { |
| if (!out[e.gaugeId]) out[e.gaugeId] = { count: 0, meanScore: 0 }; |
| out[e.gaugeId]!.count++; |
| out[e.gaugeId]!.meanScore += e.score; |
| } |
| for (const [k, v] of Object.entries(out)) { |
| out[k]!.meanScore = v.meanScore / v.count; |
| } |
| return out; |
| } |
|
|
| get size(): number { return this.count; } |
| } |
|
|
| |
| export const defaultLedger = new BrierLedger(1000); |
|
|