"""Python 3.11+, standard library only; deterministic synthetic examples."""
import math

X = [[1.0, 0.0], [1.0, 1.0]]
y = [1.0, 3.0]
w = [0.0, 0.0]


def loss(weights):
    errors = [sum(a * b for a, b in zip(row, weights)) - target
              for row, target in zip(X, y)]
    return sum(e * e for e in errors) / (2 * len(y))


def gradient(weights):
    errors = [sum(a * b for a, b in zip(row, weights)) - target
              for row, target in zip(X, y)]
    return [sum(row[j] * e for row, e in zip(X, errors)) / len(y)
            for j in range(len(weights))]


def softmax(logits):
    if not logits or not all(math.isfinite(v) for v in logits):
        raise ValueError("finite, nonempty logits required")
    maximum = max(logits)
    values = [math.exp(v - maximum) for v in logits]
    return [v / sum(values) for v in values]


def td_target(reward, gamma, next_values, terminated):
    return reward if terminated else reward + gamma * max(next_values)


g = gradient(w)
eps = 1e-6
for j, analytic in enumerate(g):
    plus, minus = w.copy(), w.copy()
    plus[j] += eps
    minus[j] -= eps
    numeric = (loss(plus) - loss(minus)) / (2 * eps)
    assert math.isclose(analytic, numeric, abs_tol=1e-8)
updated = [a - 0.1 * b for a, b in zip(w, g)]
assert loss(updated) < loss(w)
p = softmax([1000.0, 1000.0])
assert p == [0.5, 0.5]
assert math.isclose(sum(softmax([1.0, 2.0, 3.0])), 1.0)
try:
    softmax([])
except ValueError:
    pass
else:
    raise AssertionError("empty inputs must fail")
assert td_target(1, 0.9, [2, 4], True) == 1
assert math.isclose(td_target(1, 0.9, [2, 4], False), 4.6)
print(f"gradient={g}")
print(f"weights={[round(v, 2) for v in updated]}")
print(f"loss={loss(updated):.6f}")
print(f"cross_entropy={-math.log(0.25):.6f}")
print(f"td_target={td_target(1, 0.9, [2, 4], False):.1f}")


# Additional synthetic decision/resource examples in the expanded tutorial.
base_rate, sensitivity, false_positive_rate = 0.01, 0.90, 0.05
true_alerts = 10_000 * base_rate * sensitivity
false_alerts = 10_000 * (1 - base_rate) * false_positive_rate
precision = true_alerts / (true_alerts + false_alerts)
assert true_alerts == 90 and false_alerts == 495
assert math.isclose(precision, 90 / 585)
miss_cost, false_alert_cost = 20, 1
threshold = false_alert_cost / (miss_cost + false_alert_cost)
assert math.isclose(threshold, 1 / 21)
for probability in (0.01, 0.05, 0.9):
    flag = probability > threshold
    assert flag == ((1 - probability) * false_alert_cost < probability * miss_cost)
assert 32 * 64 + 64 + 64 * 10 + 10 == 2762
assert 4 * 128 * 1024 * 4 == 2 * 1024**2
assert math.ceil(100_000 / (4 * 128)) == 196
assert math.isclose(sum(0.9**i * reward for i, reward in enumerate([1, 0, 2])), 2.62)
print("checks=passed")
