"""二分类、阈值、指标与 ROC 练习。

依赖：torch、matplotlib。直接运行此文件即可。
600 条模拟样本：480 条训练、120 条验证；没有独立测试集。
图保存在 ../images/roc-curve.png 和 ../images/sigmoid.png。
数据和运行结果仅用于教学，不代表真实学生的考试表现。
"""

from pathlib import Path

import matplotlib.pyplot as plt
import torch
from torch import nn
from torch.utils.data import DataLoader, TensorDataset


def run_experiment(seed=42):
    torch.manual_seed(seed)

    # 两个 0～1 特征，模拟学习投入和练习投入。
    X_all = torch.rand(600, 2)
    true_probability = torch.sigmoid(6 * X_all[:, 0] + 4 * X_all[:, 1] - 5)

    # 标签在模型创建前按预设概率生成，不来自待训练模型。
    y_all = (torch.rand(600) < true_probability).float().reshape(-1, 1)
    X_train, y_train = X_all[:480], y_all[:480]
    X_val, y_val = X_all[480:], y_all[480:]

    loader = DataLoader(
        TensorDataset(X_train, y_train), batch_size=32, shuffle=True
    )
    model = nn.Linear(2, 1)
    loss_fn = nn.BCEWithLogitsLoss()
    optimizer = torch.optim.SGD(model.parameters(), lr=0.1)

    model.train()
    for epoch in range(300):
        for batch_X, batch_y in loader:
            optimizer.zero_grad()
            logits = model(batch_X)
            loss = loss_fn(logits, batch_y)
            loss.backward()
            optimizer.step()

    model.eval()
    with torch.no_grad():
        val_logits = model(X_val)
        probabilities = torch.sigmoid(val_logits)
        val_loss = loss_fn(val_logits, y_val).item()

    return probabilities, y_val, val_loss


def confusion_counts(probabilities, labels, threshold):
    predicted_positive = probabilities >= threshold
    actual_positive = labels == 1
    return {
        "tp": (predicted_positive & actual_positive).sum().item(),
        "fp": (predicted_positive & ~actual_positive).sum().item(),
        "fn": (~predicted_positive & actual_positive).sum().item(),
        "tn": (~predicted_positive & ~actual_positive).sum().item(),
    }


def format_ratio(numerator, denominator):
    if denominator == 0:
        return "未定义"
    return f"{numerator / denominator:.1%}"


def compute_roc(probabilities, labels):
    # ROC 需要验证集中同时存在正类和负类。
    positive_count = (labels == 1).sum().item()
    negative_count = (labels == 0).sum().item()
    if positive_count == 0 or negative_count == 0:
        raise ValueError("计算 ROC 需要同时有正类和负类样本。")

    # 阈值从 inf 往下降：从全部预测为负，直到全部预测为正。
    thresholds = [float("inf")] + torch.unique(probabilities).sort(
        descending=True
    ).values.tolist()
    fpr_values = []
    tpr_values = []

    for threshold in thresholds:
        counts = confusion_counts(probabilities, labels, threshold)
        fpr_values.append(counts["fp"] / negative_count)
        tpr_values.append(counts["tp"] / positive_count)

    # 对完整经验 ROC 计算梯形面积，不只使用四个展示阈值。
    auc = torch.trapezoid(
        torch.tensor(tpr_values, dtype=torch.float64),
        torch.tensor(fpr_values, dtype=torch.float64),
    ).item()
    return fpr_values, tpr_values, auc


def plot_roc(probabilities, labels, thresholds, image_dir):
    fpr_values, tpr_values, auc = compute_roc(probabilities, labels)
    fig, ax = plt.subplots(figsize=(6.5, 6))
    ax.plot(fpr_values, tpr_values, label=f"ROC (AUC = {auc:.3f})", linewidth=2)
    ax.plot([0, 1], [0, 1], color="gray", linestyle="--", label="Random baseline")

    selected_points = []
    for threshold in thresholds:
        counts = confusion_counts(probabilities, labels, threshold)
        fpr = counts["fp"] / (counts["fp"] + counts["tn"])
        tpr = counts["tp"] / (counts["tp"] + counts["fn"])
        selected_points.append((threshold, fpr, tpr))

    ax.scatter(
        [point[1] for point in selected_points],
        [point[2] for point in selected_points],
        color="tab:orange", s=45, zorder=3, label="Selected thresholds",
    )
    for threshold, fpr, tpr in selected_points:
        ax.annotate(
            f"t = {threshold:.1f}", (fpr, tpr),
            xytext=(10, -16), textcoords="offset points",
        )

    ax.set_xlabel("False positive rate (FPR)")
    ax.set_ylabel("True positive rate (TPR / Recall)")
    ax.set_title("Validation ROC")
    ax.set_xlim(-0.02, 1.02)
    ax.set_ylim(-0.02, 1.02)
    ax.set_aspect("equal", adjustable="box")
    ax.grid(True, alpha=0.25)
    ax.legend(loc="lower right", fontsize=10)
    fig.tight_layout()
    path = image_dir / "roc-curve.png"
    fig.savefig(path, dpi=180, bbox_inches="tight")
    print("ROC 图片已保存：", path)
    return auc


def plot_sigmoid(image_dir):
    z = torch.linspace(-8, 8, 401)
    p = torch.sigmoid(z)
    fig, ax = plt.subplots(figsize=(7, 4.5))
    ax.plot(z.numpy(), p.numpy(), linewidth=2)
    ax.axhline(0.5, color="gray", linestyle="--", linewidth=1)
    ax.axvline(0, color="gray", linestyle=":", linewidth=1)

    for value, offset in [(-2.0, (-54, 14)), (0.0, (12, -22)), (2.0, (12, -22))]:
        probability = torch.sigmoid(torch.tensor(value)).item()
        ax.scatter([value], [probability], color="tab:orange", s=35, zorder=3)
        ax.annotate(
            f"z = {value:g}, p = {probability:.3f}",
            (value, probability), xytext=offset, textcoords="offset points",
        )

    ax.set_xlabel("Linear output z (logit)")
    ax.set_ylabel("Predicted probability")
    ax.set_title("Sigmoid")
    ax.set_xlim(-8, 8)
    ax.set_ylim(-0.03, 1.03)
    ax.grid(True, alpha=0.25)
    fig.tight_layout()
    path = image_dir / "sigmoid.png"
    fig.savefig(path, dpi=180, bbox_inches="tight")
    print("Sigmoid 图片已保存：", path)


def main():
    image_dir = Path(__file__).resolve().parent.parent / "images"
    image_dir.mkdir(parents=True, exist_ok=True)
    plt.rcParams.update({"font.size": 11, "axes.titlesize": 13})

    probabilities, labels, val_loss = run_experiment()
    positive_count = (labels == 1).sum().item()
    print("训练样本数：480")
    print("验证样本数：", len(labels))
    print("验证集真实正类/负类数：", positive_count, len(labels) - positive_count)
    print(f"验证损失：{val_loss:.4f}")

    thresholds = [0.2, 0.4, 0.6, 0.8]
    for threshold in thresholds:
        counts = confusion_counts(probabilities, labels, threshold)
        tp, fp, fn, tn = (counts[name] for name in ("tp", "fp", "fn", "tn"))
        print(f"\n阈值：{threshold:.2f}")
        print(f"TP={tp}, FP={fp}, FN={fn}, TN={tn}")
        print("准确率 Accuracy：", format_ratio(tp + tn, tp + fp + fn + tn))
        print("精确率 Precision：", format_ratio(tp, tp + fp))
        print("召回率 Recall：", format_ratio(tp, tp + fn))
        print(f"ROC 坐标：(FPR={fp / (fp + tn):.3f}, TPR={tp / (tp + fn):.3f})")

    auc = plot_roc(probabilities, labels, thresholds, image_dir)
    print(f"\n验证集 ROC AUC：{auc:.4f}")
    plot_sigmoid(image_dir)
    plt.show()


if __name__ == "__main__":
    main()
