#!/usr/bin/env python3
"""
worked_board.py
Purpose : the small worked examples in WHY-THE-MLP-LEARNS.md that are not in
          verify_mlp_claims.py: the eight-weight board with one token, the
          flattening of a smooth unit's slope, adding evidence, and weights
          against a lookup table.
Usage   : python3 worked_board.py            (writes worked_board_results.json beside itself)
Needs   : Python 3 and numpy. No network, no GPU. A few seconds.
Author  : written 2026-11-14 for the MLP explainer.
"""
import itertools
import json
import pathlib

import numpy as np

R = {}


def relu(z):
    return np.maximum(0.0, z)


def softmax(s):
    e = np.exp(s - s.max())
    return e / e.sum()


# ---------------------------------------------------------------- P
# The board: 2 token levels -> 2 middle slots (gated) -> 2 bins (probabilities).
# No offsets, so there are exactly eight weights.
x = np.array([2.0, 1.0])                     # the token's levels at the top
W1 = np.array([[1.0, -1.0],                  # slot A: "first level beats second"
               [-1.0, 1.0]])                 # slot B: "second level beats first"
W2 = np.array([[0.0, 1.0],                   # bin 1 listens to (A, B)
               [1.0, -1.0]])                 # bin 2 listens to (A, B)
right = 0                                    # the token belongs in bin 1
rate = 0.1


def run(xv, w1, w2, hard=False):
    z = w1 @ xv
    gate = (z > 0).astype(float)
    h = gate if hard else relu(z)            # hard=True: all-or-none slots (the ball)
    s = w2 @ h
    p = softmax(s)
    return z, gate, h, s, p, -float(np.log(p[right]))


z, gate, h, s, p, loss = run(x, W1, W2)
target = np.zeros(2); target[right] = 1.0
blame_bins = p - target                      # probability given minus probability wanted
slope_W2 = np.outer(blame_bins, h)           # blame below x level above
blame_mid = (W2.T @ blame_bins) * gate       # back up the same wires, stopped by shut gates
would_have_been_B = float((W2.T @ blame_bins)[1])
slope_W1 = np.outer(blame_mid, x)
blame_token = W1.T @ blame_mid               # what reaches the token's own levels

# Check every slope against a direct nudge.
eps = 1e-6
worst = 0.0
for idx in np.ndindex(2, 2):
    for which, (Wa, Sa) in enumerate(((W1, slope_W1), (W2, slope_W2))):
        Wp = Wa.copy(); Wp[idx] += eps
        Wm = Wa.copy(); Wm[idx] -= eps
        lp = run(x, Wp, W2)[5] if which == 0 else run(x, W1, Wp)[5]
        lm = run(x, Wm, W2)[5] if which == 0 else run(x, W1, Wm)[5]
        worst = max(worst, abs((lp - lm) / (2 * eps) - Sa[idx]))
for i in range(2):
    xp = x.copy(); xp[i] += eps
    xm = x.copy(); xm[i] -= eps
    worst = max(worst, abs((run(xp, W1, W2)[5] - run(xm, W1, W2)[5]) / (2 * eps) - blame_token[i]))

W1n = W1 - rate * slope_W1
W2n = W2 - rate * slope_W2
z2, gate2, h2, s2, p2, loss2 = run(x, W1n, W2n)
xn = x - rate * blame_token                  # if the token's levels are learned too
z3, gate3, h3, s3, p3, loss3 = run(xn, W1n, W2n)

# The same board with all-or-none middle slots: does a small nudge to a first-row
# weight move the right bin's probability at all?
_, _, _, _, p_hard, _ = run(x, W1, W2, hard=True)
moved = 0
for idx in np.ndindex(2, 2):
    Wp = W1.copy(); Wp[idx] += 1e-3
    moved += int(abs(run(x, Wp, W2, hard=True)[4][right] - p_hard[right]) > 1e-15)

R["P_eight_weight_board"] = {
    "token_levels": x.tolist(),
    "first_row_weights_A_then_B": W1.tolist(),
    "second_row_weights_bin1_then_bin2": W2.tolist(),
    "middle_sums": z.tolist(), "gates": gate.tolist(), "middle_levels": h.tolist(),
    "scores": s.tolist(), "probabilities": [round(float(v), 4) for v in p],
    "error_before": round(loss, 4),
    "blame_at_bins": [round(float(v), 4) for v in blame_bins],
    "slopes_second_row": np.round(slope_W2, 4).tolist(),
    "blame_at_middle_slots": [round(float(v), 4) for v in blame_mid],
    "blame_slot_B_would_carry_if_its_gate_were_open": round(would_have_been_B, 4),
    "slopes_first_row": np.round(slope_W1, 4).tolist(),
    "blame_on_token_levels": [round(float(v), 4) for v in blame_token],
    "max_abs_difference_slopes_vs_direct_nudge": float(worst),
    "rate": rate,
    "first_row_weights_after": np.round(W1n, 4).tolist(),
    "second_row_weights_after": np.round(W2n, 4).tolist(),
    "weights_changed": int((np.abs(W1n - W1) > 0).sum() + (np.abs(W2n - W2) > 0).sum()),
    "middle_levels_after": [round(float(v), 4) for v in h2],
    "scores_after": [round(float(v), 4) for v in s2],
    "probabilities_after": [round(float(v), 4) for v in p2],
    "error_after": round(loss2, 4),
    "token_levels_after_if_learned": [round(float(v), 4) for v in xn],
    "probabilities_after_with_token_also_adjusted": [round(float(v), 4) for v in p3],
    "error_after_with_token_also_adjusted": round(loss3, 4),
    "all_or_none_version_right_bin_probability": round(float(p_hard[right]), 4),
    "all_or_none_version_first_row_nudges_that_moved_it": f"{moved} of 4",
}

# ---------------------------------------------------------------- O
# The smooth 1986 unit: its slope is at most 0.25, and it flattens when the unit is
# strongly on or off. ReLU's slope is 1 whenever it is on.
zs = np.linspace(-12, 12, 240001)
sig = 1 / (1 + np.exp(-zs))
dsig = sig * (1 - sig)
R["O_slope_of_the_smooth_unit"] = {
    "largest_slope": round(float(dsig.max()), 6),
    "slope_at_sum_3": round(float(dsig[np.argmin(np.abs(zs - 3))]), 5),
    "slope_at_sum_6": round(float(dsig[np.argmin(np.abs(zs - 6))]), 6),
    "ten_rows_at_the_largest_slope": float(0.25 ** 10),
    "relu_slope_when_on": 1.0,
    "ten_rows_of_open_relu_gates": 1.0,
}

# ---------------------------------------------------------------- Q
# Adding evidence. Five yes/no clues, each independently more or less likely under
# hypothesis H than under not-H. Bayes' rule and "weighted sum then squash" give the
# same probability for all 32 combinations.
r = np.random.default_rng(4)
pH = 0.3
a = r.uniform(0.1, 0.9, size=5)              # P(clue i present | H)
b = r.uniform(0.1, 0.9, size=5)              # P(clue i present | not H)
w = np.log(a / b) - np.log((1 - a) / (1 - b))
bias = np.log(pH / (1 - pH)) + np.log((1 - a) / (1 - b)).sum()
worst = 0.0
for combo in itertools.product([0, 1], repeat=5):
    c = np.array(combo, float)
    like_H = np.prod(np.where(c == 1, a, 1 - a))
    like_N = np.prod(np.where(c == 1, b, 1 - b))
    bayes = pH * like_H / (pH * like_H + (1 - pH) * like_N)
    summed = 1 / (1 + np.exp(-(w @ c + bias)))
    worst = max(worst, abs(bayes - summed))
R["Q_adding_evidence"] = {"combinations": 32,
                          "max_abs_difference_bayes_vs_weighted_sum": float(worst)}

# ---------------------------------------------------------------- T
# Weights against a lookup table. Five yes/no inputs give 32 possible inputs. The
# true rule is a weighted vote. Show a learner 20 of the 32 and test it on the 12 it
# never saw. A table has no entry for those 12. A weighted sum adjusted by error x
# input has five weights that every example helped set.
r = np.random.default_rng(12)
allx = np.array(list(itertools.product([-1.0, 1.0], repeat=5)))
acc = []
for _ in range(2000):
    wtrue = r.normal(size=5)
    y = (allx @ wtrue > 0).astype(float)
    order = r.permutation(32)
    tr, te = order[:20], order[20:]
    wl = np.zeros(5); bl = 0.0
    for _ in range(300):
        pr = 1 / (1 + np.exp(-(allx[tr] @ wl + bl)))
        g = pr - y[tr]
        wl -= 0.5 * (allx[tr].T @ g) / 20
        bl -= 0.5 * g.mean()
    pred = (allx[te] @ wl + bl > 0).astype(float)
    acc.append((pred == y[te]).mean())
R["T_weights_against_a_table"] = {
    "trials": 2000, "inputs_shown": 20, "inputs_never_shown": 12,
    "weighted_sum_percent_right_on_never_shown": round(100 * float(np.mean(acc)), 1),
    "lookup_table_percent_right_on_never_shown": "50 (a guess; it has no entry)",
}

out = pathlib.Path(__file__).with_name("worked_board_results.json")
out.write_text(json.dumps(R, indent=2))
print(json.dumps(R, indent=2))
