{"record":{"id":"290553a0298f442d","repo":"rohitg00/ai-engineering-from-scratch","slug":"horizon-undefined-slope-w-w-is-0-b-b-p-p","errorCode":null,"errorMessage":"horizon undefined: slope w={w} is ~0 (b={b}, p={p}, logit={logit})","messagePattern":"horizon undefined: slope w=(.+?) is ~0 \\(b=(.+?), p=(.+?), logit=(.+?)\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/15-autonomous-systems/21-metr-external-evaluation/code/main.py","lineNumber":76,"sourceCode":"            p = sigmoid(w * math.log(t) + b)\n            err = p - y\n            dw += err * math.log(t)\n            db += err\n        w -= lr * dw / n\n        b -= lr * db / n\n    return w, b\n\n\ndef horizon_at(w: float, b: float, p: float) -> float:\n    \"\"\"Expert time where P(success) = p.  sigmoid(w*log(t)+b) = p ->\n    log(t) = (logit(p) - b) / w.\"\"\"\n    logit = math.log(p / (1 - p))\n    # A zero (or near-zero) slope means success probability does not\n    # depend on task length, so the horizon is undefined. Raise rather\n    # than silently returning inf/nan so callers see a loud failure.\n    eps = 1e-12\n    if abs(w) < eps:\n        raise ValueError(\n            f\"horizon undefined: slope w={w} is ~0 \"\n            f\"(b={b}, p={p}, logit={logit})\"\n        )\n    return math.exp((logit - b) / w)\n\n\n# ---------- Eval-context gaming simulator ----------\n\ndef inject_gaming(tasks: list[tuple[float, bool]],\n                  gaming_rate: float) -> list[tuple[float, bool]]:\n    \"\"\"Flip `gaming_rate` fraction of failures to successes (model behaves\n    better in eval context). Returns a new list.\"\"\"\n    gamed = []\n    for t, s in tasks:\n        if not s and random.random() < gaming_rate:\n            gamed.append((t, True))\n        else:\n            gamed.append((t, s))","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/15-autonomous-systems/21-metr-external-evaluation/code/main.py#L58-L94","documentation":"Domain guard in horizon_at: the time-to-success logistic has a fitted slope w of approximately zero, so success probability is independent of task length and the t where P(success)=p has no finite solution. The function raises loudly instead of returning inf or nan so callers never consume a meaningless horizon.","triggerScenarios":"Thrown at phases/15-autonomous-systems/21-metr-external-evaluation/code/main.py:76 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Refit the logistic on a wider range of task lengths so the slope is identifiable","Check the training data for a degenerate label distribution (all success or all fail)","Catch the error in report generation and report the horizon as undefined with the fitted parameters","Increase iterations or learning rate if gradient descent converged before the slope moved"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}