Skip to content

NB-1 — BKT from scratch

This notebook is ~30 lines of numpy rebuilding BKT end-to-end. Goal: prove the TS implementation in web/lib/bkt.ts matches a Python reference exactly.

Coming soon: interactive JupyterLite on /lab/ (Phase 3c). For now — statically rendered.

import numpy as np
import matplotlib.pyplot as plt
from dataclasses import dataclass
DEFAULT = {"pInit": 0.2, "pTransit": 0.1, "pSlip": 0.1, "pGuess": 0.2}
def p_solve(pL, params=DEFAULT):
return pL * (1 - params["pSlip"]) + (1 - pL) * params["pGuess"]
def bkt_update(pL, observed_correct, params=DEFAULT):
pS, pG, pT = params["pSlip"], params["pGuess"], params["pTransit"]
if observed_correct:
post = (pL * (1 - pS)) / (pL * (1 - pS) + (1 - pL) * pG)
else:
post = (pL * pS) / (pL * pS + (1 - pL) * (1 - pG))
return post + (1 - post) * pT
def closeness(p, target=0.7, sigma2=0.03):
return np.exp(-((p - target) ** 2) / sigma2)
def joint_p_solve(pLs, params=DEFAULT):
"""Geometric mean of per-skill P(solve)."""
p = np.array([p_solve(x, params) for x in pLs])
return np.exp(np.mean(np.log(np.clip(p, 1e-6, 1.0))))

That’s the entire BKT math — under 20 lines.

Replay the walk-through from chapter 7: Ivan starts at P(L0)=0.2P(L_0) = 0.2, answers six times in order ✗✓✓✗✗✗, final P(L)0.166P(L) \approx 0.166.

pL = 0.2
trace = [pL]
for ans in [False, True, True, False, False, False]:
pL = bkt_update(pL, ans)
trace.append(round(pL, 4))
print(trace)
# Ожидаем (с округлением):
# [0.2, 0.057, 0.247, 0.595, 0.196, 0.116, 0.166]

Digits exactly match TS and the study guide.

fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(trace, marker='o', color='#9333ea', linewidth=2)
ax.axhline(0.7, color='orange', linestyle='--', label='ZPD target')
ax.set_xlabel('шаг')
ax.set_ylabel('P(L)')
ax.set_ylim(0, 1)
ax.set_title("P(L) Ивана — навык 'раскрытие скобок'")
ax.legend()
ax.grid(alpha=0.3)
plt.show()
xs = np.linspace(0, 1, 100)
ys = [p_solve(x) for x in xs]
plt.figure(figsize=(8, 4))
plt.plot(xs, ys, color='#0ea5e9', linewidth=2)
plt.axhline(0.7, color='orange', linestyle='--', label='ZPD')
plt.axvline(0.5, color='gray', linestyle=':', alpha=0.5)
plt.fill_between(xs, 0.6, 0.8, alpha=0.15, color='orange', label='ZPD band')
plt.xlabel('P(L)')
plt.ylabel('P(solve)')
plt.title('P(solve) = P(L)·(1−P(S)) + (1−P(L))·P(G)')
plt.legend()
plt.grid(alpha=0.3)
plt.show()

At defaults the segment runs from (0, 0.2) to (1, 0.9). P(L)=0.71P(L) = 0.71P(solve)0.7P(\text{solve}) \approx 0.7 — lands on ZPD.

FunctionTSPythonMatch?
pSolve(pL)web/lib/bkt.ts:29p_solve
bktUpdate(pL, c)web/lib/bkt.ts:33bkt_update
closeness(p)web/lib/bkt.ts:120closeness
joint pSolve (GM)web/lib/bkt.ts:111joint_p_solve

So the TS core fits in 30 lines of Python — and vice versa. That simplicity is BKT’s headline advantage.

  • NB-2 — Parameter sensitivity — fragile parameter regimes.
  • NB-3 — EM fitting (soon).
  • NB-4 — IRT vs BKT (soon).
  • NB-5 — Class simulation (soon).