{
  "nbformat": 4,
  "nbformat_minor": 5,
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.10"
    }
  },
  "cells": [
    {
      "id": "scope",
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# cold-start reproduction check\n",
        "Run in the extracted bundle folder. Read README.md for provenance, prerequisites, and limitations. This is not full-paper replication.\n"
      ]
    },
    {
      "id": "check",
      "cell_type": "code",
      "metadata": {},
      "execution_count": null,
      "outputs": [],
      "source": [
        "\"\"\"Recompute published paired deltas, exact sign flips, and four-endpoint Holm.\"\"\"\n",
        "import itertools\n",
        "import json\n",
        "import math\n",
        "from pathlib import Path\n",
        "\n",
        "ROOT = Path(__file__).resolve().parent if \"__file__\" in globals() else Path.cwd()\n",
        "\n",
        "\n",
        "def exact_sign_flip(deltas):\n",
        "    \"\"\"Two-sided test: enumerate all 2**n signs; include ties in the tail.\"\"\"\n",
        "    observed = abs(sum(deltas))\n",
        "    extreme = sum(abs(sum(sign * value for sign, value in zip(signs, deltas))) >= observed - 1e-12\n",
        "                  for signs in itertools.product((-1, 1), repeat=len(deltas)))\n",
        "    return extreme / (2 ** len(deltas))\n",
        "\n",
        "\n",
        "def reproduce():\n",
        "    records = json.loads((ROOT / \"source.json\").read_text(encoding=\"utf-8\"))\n",
        "    results = {}\n",
        "    for protocol in (\"S2\", \"S4\"):\n",
        "        assert len(records[protocol]) == 10\n",
        "        assert sorted(row[\"fold\"] for row in records[protocol]) == list(range(10))\n",
        "        for metric, column in ((\"CI\", \"deltaCi\"), (\"RMSE\", \"deltaRmse\")):\n",
        "            # Use the published delta, not subtraction of independently rounded model scores.\n",
        "            deltas = [row[column] for row in records[protocol]]\n",
        "            results[f\"{protocol}_{metric}\"] = {\n",
        "                \"n\": len(deltas), \"mean_delta\": sum(deltas) / len(deltas),\n",
        "                \"exact_p\": exact_sign_flip(deltas)}\n",
        "    ordered = sorted(results, key=lambda key: results[key][\"exact_p\"])\n",
        "    previous = 0\n",
        "    for rank, key in enumerate(ordered):\n",
        "        adjusted = min(1, max(previous, (len(ordered) - rank) * results[key][\"exact_p\"]))\n",
        "        results[key][\"holm_p\"] = adjusted\n",
        "        previous = adjusted\n",
        "    return results\n",
        "\n",
        "\n",
        "if __name__ == \"__main__\":\n",
        "    result = reproduce()\n",
        "    expected = json.loads((ROOT / \"expected.json\").read_text(encoding=\"utf-8\"))\n",
        "    for contrast, values in expected.items():\n",
        "        for key, value in values.items():\n",
        "            assert math.isclose(result[contrast][key], value, rel_tol=0, abs_tol=1e-10), (contrast, key)\n",
        "    assert [key for key, value in result.items() if value[\"holm_p\"] < 0.05] == [\"S2_RMSE\"]\n",
        "    print(json.dumps(result, indent=2))\n",
        "    print(\"PASS: 20 reported folds and four contrasts checked; only S2 RMSE passes Holm < 0.05.\")\n",
        "    print(\"No training, bootstrap intervals, missing folds, or quantum hardware reproduced.\")\n"
      ]
    }
  ]
}
