## Tier A unit tests for the CHIMERA Hedge posterior (chimera/hedge.nim). ## Run: from ChimeraBot_garage/src, `nimble test`. import std/math import chimera/hedge var failed = 0 proc check(name: string, cond: bool, detail: string = "") = if cond: echo " ok ", name else: echo " FAIL ", name, " [", detail, "]"; inc failed proc sum(h: Hedge): float64 = for w in h.weights: result += w block: echo "test 1: uniform prior and normalization" var h = initHedge(@["a", "b", "c", "d"]) check("sums to 1", abs(sum(h) - 1.0) < 1e-12, $sum(h)) check("entropy = ln N", abs(h.entropy() - ln(4.0)) < 1e-12, $h.entropy()) h.update(@[0.0, 1.0, 0.5, 2.0]) check("stays normalized after update", abs(sum(h) - 1.0) < 1e-12, $sum(h)) block: echo "test 2: eta effect sign (sharper with larger eta)" var slow = initHedge(@["good", "bad"]) var fast = initHedge(@["good", "bad"]) slow.cfg.eta = 0.5 fast.cfg.eta = 4.0 for i in 0 ..< 10: slow.update(@[0.0, 1.0]) fast.update(@[0.0, 1.0]) check("both favour the good kernel", slow.posterior("good") > 0.5 and fast.posterior("good") > 0.5) check("larger eta is sharper", fast.posterior("good") > slow.posterior("good"), $fast.posterior("good") & " vs " & $slow.posterior("good")) block: echo "test 3: forgetting revives a switched kernel" var h = initHedge(@["A", "B"]) for i in 0 ..< 200: h.update(@[0.0, 1.0]) # A is right for a long time let aHigh = h.posterior("A") check("A dominates before the switch", aHigh > 0.9, $aHigh) for i in 0 ..< 200: h.update(@[1.0, 0.0]) # policy switches to B check("B recovers after the switch", h.posterior("B") > 0.9, $h.posterior("B")) block: echo "test 4: Fixed-Share mass floor w_i >= alpha*m_i" var h = initHedge(@["a", "b", "c", "d"]) h.cfg.alpha = 0.05 for i in 0 ..< 500: h.update(@[0.0, 100.0, 100.0, 100.0]) # crush b, c, d let m = 1.0 / 4.0 var floored = true for i in 0 ..< h.weights.len: if h.weights[i] < h.cfg.alpha * m - 1e-12: floored = false check("every weight respects the floor", floored, $h.weights) # The floor a*m is exact pre-normalization; after renormalizing (Z <= 1) a # crushed loser sits at or just above it. check("loser mass sits at the floor", h.posterior("b") >= 0.05 * m - 1e-12 and h.posterior("b") < 0.05 * m * 1.1, $h.posterior("b")) block: echo "test 5: entropy and top-k sanity" var h = initHedge(@["a", "b", "c"]) let top = h.topK(2) check("top-k returns k entries", top.len == 2) check("top-k sorted descending", top[0].weight >= top[1].weight) for i in 0 ..< 200: h.update(@[0.0, 3.0, 3.0]) check("entropy collapses toward 0", h.entropy() < 0.5, $h.entropy()) check("argmax names the winner", h.argmaxName() == "a", h.argmaxName()) block: echo "test 6: revision-safe clone / setWeights" var h = initHedge(@["a", "b"]) h.update(@[0.0, 1.0]) let c = h.clone() h.update(@[1.0, 0.0]) check("clone is independent", c.posterior("a") != h.posterior("a")) var d = c.clone() d.setWeights(@[3.0, 1.0]) check("setWeights normalizes", abs(sum(d) - 1.0) < 1e-12) check("setWeights ratios preserved", abs(d.posterior("a") - 0.75) < 1e-12, $d.posterior("a")) echo "" if failed == 0: echo "all hedge tests passed" else: echo failed, " hedge test(s) FAILED"; quit(1)