{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "87f1925e",
   "metadata": {},
   "source": [
    "# Homework\n",
    "\n",
    "- Use this assignment to practice the chapter's concepts and coding skills in runnable Python code.\n",
    "- Sign in from the account menu before you start working on the assignment.\n",
    "- Write and run your code directly in the coding cells.\n",
    "- Multiple submissions are allowed. The highest score is kept.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed4a5366",
   "metadata": {},
   "source": [
    "::::{div} assignment-question-card\n",
    "\n",
    "**1. Choosing k Honestly**\n",
    "\n",
    "An analyst tries k = 1 to 51, picks the k with the highest accuracy on the test set, and reports that same test accuracy to the manager as the expected accuracy on new customers.\n",
    "\n",
    "<div class=\"tf-options\" role=\"radiogroup\" aria-label=\"Question 1 answer\">\n",
    "  <label><input type=\"radio\" name=\"ch15-hw-q1\" value=\"true\"> True</label>\n",
    "  <label><input type=\"radio\" name=\"ch15-hw-q1\" value=\"false\"> False</label>\n",
    "</div>\n",
    "\n",
    "::::\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7fe88727",
   "metadata": {
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [],
   "source": [
    "### Solution\n",
    "# False\n",
    "# Choosing k by test accuracy uses the test set to build the model, so the reported number is optimistic. Choose k on a validation set and keep the test set for the final report."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2c039b0",
   "metadata": {},
   "source": [
    "::::{div} assignment-question-card\n",
    "\n",
    "**2. Accuracy and Cost**\n",
    "\n",
    "Classifier A is more accurate than classifier B on the same test set, so A must also have the lower total error cost.\n",
    "\n",
    "<div class=\"tf-options\" role=\"radiogroup\" aria-label=\"Question 2 answer\">\n",
    "  <label><input type=\"radio\" name=\"ch15-hw-q2\" value=\"true\"> True</label>\n",
    "  <label><input type=\"radio\" name=\"ch15-hw-q2\" value=\"false\"> False</label>\n",
    "</div>\n",
    "\n",
    "::::\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3ccbe17",
   "metadata": {
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [],
   "source": [
    "### Solution\n",
    "# False\n",
    "# Accuracy counts every error the same. When a missed case costs much more than a false alarm, a less accurate classifier that misses fewer costly cases can be cheaper."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d661804",
   "metadata": {},
   "source": [
    "::::{div} assignment-question-card\n",
    "\n",
    "**3. Lowering the Threshold**\n",
    "\n",
    "A missed fraud case costs a bank far more than reviewing a legitimate payment. Lowering the logistic regression threshold from 0.5 to 0.2 can reduce the bank's total error cost.\n",
    "\n",
    "<div class=\"tf-options\" role=\"radiogroup\" aria-label=\"Question 3 answer\">\n",
    "  <label><input type=\"radio\" name=\"ch15-hw-q3\" value=\"true\"> True</label>\n",
    "  <label><input type=\"radio\" name=\"ch15-hw-q3\" value=\"false\"> False</label>\n",
    "</div>\n",
    "\n",
    "::::\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc0d6fe7",
   "metadata": {
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [],
   "source": [
    "### Solution\n",
    "# True\n",
    "# A lower threshold flags more payments, catching more fraud (higher recall) at the price of more false alarms. When misses cost much more, that trade can lower the total cost."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fdd17602",
   "metadata": {},
   "source": [
    "::::{div} assignment-question-card\n",
    "\n",
    "**4. Precision or Recall**\n",
    "\n",
    "A retailer asks: of the orders the model flagged as fraud, what share really were fraud? That question asks for the model's recall.\n",
    "\n",
    "<div class=\"tf-options\" role=\"radiogroup\" aria-label=\"Question 4 answer\">\n",
    "  <label><input type=\"radio\" name=\"ch15-hw-q4\" value=\"true\"> True</label>\n",
    "  <label><input type=\"radio\" name=\"ch15-hw-q4\" value=\"false\"> False</label>\n",
    "</div>\n",
    "\n",
    "::::\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17e04e0d",
   "metadata": {
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [],
   "source": [
    "### Solution\n",
    "# False\n",
    "# That is precision: TP / (TP + FP), the share of flagged cases that are real. Recall asks what share of the real fraud cases were flagged."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0271ab10",
   "metadata": {},
   "source": [
    "::::{div} assignment-question-card\n",
    "\n",
    "**5. Reading a Coefficient**\n",
    "\n",
    "In a logistic regression that predicts purchase, page value has a positive coefficient. Holding the other features fixed, sessions with higher page value get higher predicted purchase probabilities.\n",
    "\n",
    "<div class=\"tf-options\" role=\"radiogroup\" aria-label=\"Question 5 answer\">\n",
    "  <label><input type=\"radio\" name=\"ch15-hw-q5\" value=\"true\"> True</label>\n",
    "  <label><input type=\"radio\" name=\"ch15-hw-q5\" value=\"false\"> False</label>\n",
    "</div>\n",
    "\n",
    "::::\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9df4b415",
   "metadata": {
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [],
   "source": [
    "### Solution\n",
    "# True\n",
    "# A positive coefficient raises the weighted sum, and the logistic curve turns a larger sum into a larger probability."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eba2cd33",
   "metadata": {
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### 6. From Score to Probability\n",
    "#   A model's score for a session is z = -4 + 0.25 * page_value.\n",
    "#   1. Turn the score into a probability with the logistic function 1 / (1 + e^(-z))\n",
    "#      for page values 8, 16, and 24, each rounded to 3 decimals.\n",
    "# Print exactly:\n",
    "# Page value 8 probability: 0.119\n",
    "# Page value 16 probability: 0.5\n",
    "# Page value 24 probability: 0.881\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "page_values = [8, 16, 24]\n",
    "\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56158cec",
   "metadata": {
    "tags": [
     "hide-input",
     "homework-answer"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Page value 8 probability: 0.119\n",
      "Page value 16 probability: 0.5\n",
      "Page value 24 probability: 0.881\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "page_values = [8, 16, 24]\n",
    "\n",
    "for page_value in page_values:\n",
    "    z = -4 + 0.25 * page_value\n",
    "    probability = 1 / (1 + np.exp(-z))\n",
    "    print(\"Page value\", page_value, \"probability:\", round(probability, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6d27232",
   "metadata": {
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### 7. Fitting LogisticRegression\n",
    "#   1. Fit a LogisticRegression on all 16 sessions (X and y).\n",
    "#   2. Print the predicted purchase probability (rounded to 2 decimals) and the\n",
    "#      predicted label for each new session.\n",
    "# Print exactly:\n",
    "# Probability: 0.26 Prediction: 0\n",
    "# Probability: 0.74 Prediction: 1\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "# Shopping sessions (made-up numbers for practice): page value in dollars,\n",
    "# seconds spent on product pages, and whether the session ended in a purchase.\n",
    "sessions = pd.DataFrame({\n",
    "    \"page_value\": [0, 12, 0, 35, 0, 4, 28, 0, 0, 19, 0, 41, 0, 7, 0, 22],\n",
    "    \"duration\": [120, 1500, 300, 2400, 60, 900, 1300, 200, 2600, 1700, 150, 3100, 700, 450, 30, 1400],\n",
    "    \"purchase\": [0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1],\n",
    "})\n",
    "X = sessions[[\"page_value\", \"duration\"]]\n",
    "y = sessions[\"purchase\"]\n",
    "new_sessions = pd.DataFrame({\"page_value\": [9, 12], \"duration\": [900, 900]})\n",
    "\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10fb210e",
   "metadata": {
    "tags": [
     "hide-input",
     "homework-answer"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Probability: 0.26 Prediction: 0\n",
      "Probability: 0.74 Prediction: 1\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "# Shopping sessions (made-up numbers for practice): page value in dollars,\n",
    "# seconds spent on product pages, and whether the session ended in a purchase.\n",
    "sessions = pd.DataFrame({\n",
    "    \"page_value\": [0, 12, 0, 35, 0, 4, 28, 0, 0, 19, 0, 41, 0, 7, 0, 22],\n",
    "    \"duration\": [120, 1500, 300, 2400, 60, 900, 1300, 200, 2600, 1700, 150, 3100, 700, 450, 30, 1400],\n",
    "    \"purchase\": [0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1],\n",
    "})\n",
    "X = sessions[[\"page_value\", \"duration\"]]\n",
    "y = sessions[\"purchase\"]\n",
    "new_sessions = pd.DataFrame({\"page_value\": [9, 12], \"duration\": [900, 900]})\n",
    "\n",
    "model = LogisticRegression().fit(X, y)\n",
    "probabilities = model.predict_proba(new_sessions)[:, 1]\n",
    "labels = model.predict(new_sessions)\n",
    "for probability, label in zip(probabilities, labels):\n",
    "    print(\"Probability:\", round(probability, 2), \"Prediction:\", label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abd0d589",
   "metadata": {
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### 8. Moving the Threshold\n",
    "#   1. For thresholds 0.5 and 0.3, predict purchase when prob >= threshold.\n",
    "#   2. Print how many sessions are flagged and the recall, rounded to 2 decimals.\n",
    "# Print exactly:\n",
    "# Threshold 0.5 flagged: 4 recall: 0.6\n",
    "# Threshold 0.3 flagged: 7 recall: 1.0\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# Predicted purchase probabilities for 10 test sessions and what actually happened\n",
    "# (made-up data for practice; 1 = purchase).\n",
    "prob = [0.92, 0.35, 0.61, 0.08, 0.44, 0.27, 0.75, 0.12, 0.55, 0.31]\n",
    "actual = [1, 1, 1, 0, 0, 0, 1, 0, 0, 1]\n",
    "\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f42df54f",
   "metadata": {
    "tags": [
     "hide-input",
     "homework-answer"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Threshold 0.5 flagged: 4 recall: 0.6\n",
      "Threshold 0.3 flagged: 7 recall: 1.0\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# Predicted purchase probabilities for 10 test sessions and what actually happened\n",
    "# (made-up data for practice; 1 = purchase).\n",
    "prob = [0.92, 0.35, 0.61, 0.08, 0.44, 0.27, 0.75, 0.12, 0.55, 0.31]\n",
    "actual = [1, 1, 1, 0, 0, 0, 1, 0, 0, 1]\n",
    "\n",
    "for threshold in [0.5, 0.3]:\n",
    "    flagged = [int(p >= threshold) for p in prob]\n",
    "    caught = sum([a == 1 and f == 1 for a, f in zip(actual, flagged)])\n",
    "    print(\"Threshold\", threshold, \"flagged:\", sum(flagged), \"recall:\", round(caught / sum(actual), 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65a27a0e",
   "metadata": {
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### 9. The Cheapest Threshold\n",
    "#   A missed buyer costs $30 and a wasted offer to a non-buyer costs $3.\n",
    "#   1. For thresholds 0.2, 0.4, and 0.6, compute the total error cost.\n",
    "#   2. Print each cost and the cheapest threshold.\n",
    "# Print exactly:\n",
    "# Threshold 0.2 cost: $9\n",
    "# Threshold 0.4 cost: $66\n",
    "# Threshold 0.6 cost: $60\n",
    "# Cheapest threshold: 0.2\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# Predicted purchase probabilities for 10 test sessions and what actually happened\n",
    "# (made-up data for practice; 1 = purchase).\n",
    "prob = [0.92, 0.35, 0.61, 0.08, 0.44, 0.27, 0.75, 0.12, 0.55, 0.31]\n",
    "actual = [1, 1, 1, 0, 0, 0, 1, 0, 0, 1]\n",
    "COST_FN = 30\n",
    "COST_FP = 3\n",
    "\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f75913d1",
   "metadata": {
    "tags": [
     "hide-input",
     "homework-answer"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Threshold 0.2 cost: $9\n",
      "Threshold 0.4 cost: $66\n",
      "Threshold 0.6 cost: $60\n",
      "Cheapest threshold: 0.2\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# Predicted purchase probabilities for 10 test sessions and what actually happened\n",
    "# (made-up data for practice; 1 = purchase).\n",
    "prob = [0.92, 0.35, 0.61, 0.08, 0.44, 0.27, 0.75, 0.12, 0.55, 0.31]\n",
    "actual = [1, 1, 1, 0, 0, 0, 1, 0, 0, 1]\n",
    "COST_FN = 30\n",
    "COST_FP = 3\n",
    "\n",
    "costs = {}\n",
    "for threshold in [0.2, 0.4, 0.6]:\n",
    "    flagged = [int(p >= threshold) for p in prob]\n",
    "    fn = sum([a == 1 and f == 0 for a, f in zip(actual, flagged)])\n",
    "    fp = sum([a == 0 and f == 1 for a, f in zip(actual, flagged)])\n",
    "    costs[threshold] = COST_FN * fn + COST_FP * fp\n",
    "    print(\"Threshold\", threshold, \"cost: $\" + str(costs[threshold]))\n",
    "print(\"Cheapest threshold:\", min(costs, key=costs.get))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a45c6945",
   "metadata": {
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### 10. The Cost Rule\n",
    "#   The cost-minimizing threshold is C_FP / (C_FP + C_FN).\n",
    "#   1. Compute it for each pair of costs below, rounded to 3 decimals.\n",
    "# Print exactly:\n",
    "# FN $30, FP $3: threshold 0.091\n",
    "# FN $100, FP $25: threshold 0.2\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "cost_pairs = [(30, 3), (100, 25)]   # (cost of a false negative, cost of a false positive)\n",
    "\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "950e00ec",
   "metadata": {
    "tags": [
     "hide-input",
     "homework-answer"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FN $30, FP $3: threshold 0.091\n",
      "FN $100, FP $25: threshold 0.2\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "cost_pairs = [(30, 3), (100, 25)]   # (cost of a false negative, cost of a false positive)\n",
    "\n",
    "for cost_fn, cost_fp in cost_pairs:\n",
    "    threshold = cost_fp / (cost_fp + cost_fn)\n",
    "    print(\"FN $\" + str(cost_fn) + \", FP $\" + str(cost_fp) + \": threshold\", round(threshold, 3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e2d9a07",
   "metadata": {},
   "source": [
    "<style>\n",
    ".ch15-hw-grader { max-width: 900px; margin-top: 1rem; }\n",
    ".ch15-hw-feedback-list { display: grid; gap: 0.45rem; margin: 0 0 1rem; }\n",
    ".ch15-hw-feedback-list[hidden] { display: none; }\n",
    ".ch15-hw-feedback-row { border: 1px solid #d8dee4; border-radius: 6px; padding: 0.55rem 0.65rem; }\n",
    ".ch15-hw-feedback-row strong { margin-right: 0.35rem; }\n",
    ".ch15-hw-feedback-row.correct { border-color: #aceebb; background: #dafbe1; color: #116329; }\n",
    ".ch15-hw-feedback-row.incorrect { border-color: #ffcecb; background: #ffebe9; color: #cf222e; }\n",
    ".ch15-hw-actions { display: flex; gap: 0.75rem; align-items: center; margin: 1rem 0 0; flex-wrap: wrap; }\n",
    ".ch15-hw-actions button { border: 1px solid #176de8; border-radius: 6px; background: #176de8; color: white; padding: 0.55rem 0.9rem; cursor: pointer; font-weight: 600; }\n",
    ".ch15-hw-actions button.secondary { background: white; color: #176de8; }\n",
    ".ch15-hw-actions.is-locked { opacity: 0.55; }\n",
    ".ch15-hw-actions button:disabled { cursor: not-allowed; opacity: 0.65; }\n",
    ".ch15-hw-result { font-weight: 700; }\n",
    "</style>\n",
    "\n",
    "<div class='ch15-hw-grader' id='ch15-hw-grader'>\n",
    "  <div class='ch15-hw-feedback-list' aria-live='polite' hidden>\n",
    "    <div class='ch15-hw-feedback-row' data-question='q1'><strong>Question 1</strong><span></span></div>\n",
    "    <div class='ch15-hw-feedback-row' data-question='q2'><strong>Question 2</strong><span></span></div>\n",
    "    <div class='ch15-hw-feedback-row' data-question='q3'><strong>Question 3</strong><span></span></div>\n",
    "    <div class='ch15-hw-feedback-row' data-question='q4'><strong>Question 4</strong><span></span></div>\n",
    "    <div class='ch15-hw-feedback-row' data-question='q5'><strong>Question 5</strong><span></span></div>\n",
    "    <div class='ch15-hw-feedback-row' data-question='q6'><strong>Question 6</strong><span></span></div>\n",
    "    <div class='ch15-hw-feedback-row' data-question='q7'><strong>Question 7</strong><span></span></div>\n",
    "    <div class='ch15-hw-feedback-row' data-question='q8'><strong>Question 8</strong><span></span></div>\n",
    "    <div class='ch15-hw-feedback-row' data-question='q9'><strong>Question 9</strong><span></span></div>\n",
    "    <div class='ch15-hw-feedback-row' data-question='q10'><strong>Question 10</strong><span></span></div>\n",
    "  </div>\n",
    "  <div class='ch15-hw-actions'>\n",
    "    <button type='button' id='ch15-hw-submit' disabled>Submit</button>\n",
    "    <button type='button' class='secondary' id='ch15-hw-reset'>Clear Results</button>\n",
    "    <span class='ch15-hw-result' id='ch15-hw-result' aria-live='polite'></span>\n",
    "  </div>\n",
    "</div>\n",
    "\n",
    "<script>\n",
    "(function () {\n",
    "  var homeworkId = 'ch15-homework';\n",
    "\n",
    "  function getParams() {\n",
    "    var params = new URLSearchParams(window.location.search);\n",
    "    return {\n",
    "      canvas_course_id: params.get('canvas_course_id') || params.get('course_id') || '',\n",
    "      canvas_assignment_id: params.get('canvas_assignment_id') || params.get('assignment_id') || '',\n",
    "      canvas_user_id: params.get('canvas_user_id') || params.get('user_id') || ''\n",
    "    };\n",
    "  }\n",
    "\n",
    "  function loadSessionIdentity(identity, actions, submit, result) {\n",
    "    submit.disabled = true;\n",
    "    actions.classList.add('is-locked');\n",
    "    result.textContent = 'Sign in from the account menu to submit this assignment.';\n",
    "\n",
    "    return fetch('/api/v1/session.php', { credentials: 'same-origin' })\n",
    "      .then(function (response) { return response.ok ? response.json() : null; })\n",
    "      .then(function (payload) {\n",
    "        if (!payload || !payload.authenticated || !payload.identity) return identity;\n",
    "        identity.canvas_user_id = payload.identity.canvas_user_id || identity.canvas_user_id || '';\n",
    "        identity.student_identifier = payload.identity.student_identifier || '';\n",
    "        submit.disabled = false;\n",
    "        actions.classList.remove('is-locked');\n",
    "        result.textContent = payload.identity.display_name ? 'Signed in as ' + payload.identity.display_name + '.' : 'Signed in.';\n",
    "        return identity;\n",
    "      })\n",
    "      .catch(function () { return identity; });\n",
    "  }\n",
    "\n",
    "  function codeFromCell(cell) {\n",
    "    var cm = cell.querySelector('.CodeMirror');\n",
    "    if (cm && cm.CodeMirror && typeof cm.CodeMirror.getValue === 'function') return cm.CodeMirror.getValue();\n",
    "    var cmContent = cell.querySelector('.cm-content');\n",
    "    if (cmContent && cmContent.cmView && cmContent.cmView.view) return cmContent.cmView.view.state.doc.toString();\n",
    "    var textarea = cell.querySelector('textarea');\n",
    "    if (textarea) return textarea.value;\n",
    "    var pre = cell.querySelector('.cell_input pre');\n",
    "    return pre ? pre.textContent : '';\n",
    "  }\n",
    "\n",
    "  function collectCodeCells() {\n",
    "    var cells = Array.prototype.slice.call(document.querySelectorAll('div.cell.tag_thebe-interactive')).slice(0, 5);\n",
    "    var code = {};\n",
    "    cells.forEach(function (cell, index) {\n",
    "      code['q' + (index + 6)] = codeFromCell(cell);\n",
    "    });\n",
    "    return code;\n",
    "  }\n",
    "\n",
    "  function collectAnswers() {\n",
    "    var answers = {};\n",
    "    ['q1', 'q2', 'q3', 'q4', 'q5'].forEach(function (question) {\n",
    "      var checked = document.querySelector('input[name=\"ch15-hw-' + question + '\"]:checked');\n",
    "      answers[question] = checked ? checked.value : '';\n",
    "    });\n",
    "    return answers;\n",
    "  }\n",
    "\n",
    "  function applyFeedback(grader, feedback) {\n",
    "    var list = grader.querySelector('.ch15-hw-feedback-list');\n",
    "    list.hidden = false;\n",
    "    ['q1', 'q2', 'q3', 'q4', 'q5', 'q6', 'q7', 'q8', 'q9', 'q10'].forEach(function (question) {\n",
    "      var row = grader.querySelector('[data-question=' + question + ']');\n",
    "      var message = row.querySelector('span');\n",
    "      var item = feedback[question];\n",
    "      row.className = 'ch15-hw-feedback-row';\n",
    "      if (!item) return;\n",
    "      message.textContent = item.message + ' (' + item.score + ' / ' + item.max_score + ')';\n",
    "      row.classList.add(item.correct ? 'correct' : 'incorrect');\n",
    "    });\n",
    "  }\n",
    "\n",
    "  function clearFeedback(grader) {\n",
    "    var list = grader.querySelector('.ch15-hw-feedback-list');\n",
    "    list.hidden = true;\n",
    "    grader.querySelectorAll('.ch15-hw-feedback-row').forEach(function (row) {\n",
    "      row.className = 'ch15-hw-feedback-row';\n",
    "      var message = row.querySelector('span');\n",
    "      if (message) message.textContent = '';\n",
    "    });\n",
    "  }\n",
    "\n",
    "  function saveAttempt(payload) {\n",
    "    return fetch('/api/v1/homework-attempts.php', {\n",
    "      method: 'POST',\n",
    "      headers: { 'Content-Type': 'application/json' },\n",
    "      credentials: 'same-origin',\n",
    "      body: JSON.stringify(payload)\n",
    "    }).then(function (response) {\n",
    "      return response.text().then(function (text) {\n",
    "        var payload = text ? JSON.parse(text) : null;\n",
    "        if (!payload || !response.ok || !payload.ok) {\n",
    "          throw new Error((payload && (payload.message || payload.error)) || 'Submission API is unavailable.');\n",
    "        }\n",
    "        return payload;\n",
    "      });\n",
    "    });\n",
    "  }\n",
    "\n",
    "  function initHomeworkGrader() {\n",
    "    var grader = document.getElementById('ch15-hw-grader');\n",
    "    if (!grader || grader.dataset.ready === 'true') return;\n",
    "    grader.dataset.ready = 'true';\n",
    "\n",
    "    var identity = getParams();\n",
    "    var actions = grader.querySelector('.ch15-hw-actions');\n",
    "    var result = document.getElementById('ch15-hw-result');\n",
    "    var submit = document.getElementById('ch15-hw-submit');\n",
    "    var reset = document.getElementById('ch15-hw-reset');\n",
    "\n",
    "    loadSessionIdentity(identity, actions, submit, result);\n",
    "\n",
    "    submit.addEventListener('click', function () {\n",
    "      clearFeedback(grader);\n",
    "      submit.disabled = true;\n",
    "      result.textContent = 'Submitting homework.';\n",
    "\n",
    "      saveAttempt({\n",
    "        homework_id: homeworkId,\n",
    "        answers: collectAnswers(),\n",
    "        code: collectCodeCells(),\n",
    "        canvas_course_id: identity.canvas_course_id,\n",
    "        canvas_assignment_id: identity.canvas_assignment_id,\n",
    "        canvas_user_id: identity.canvas_user_id\n",
    "      })\n",
    "        .then(function (payload) {\n",
    "          var best = (payload.best_score === null || typeof payload.best_score === 'undefined') ? payload.score : payload.best_score;\n",
    "          var attempt = (payload.attempt_count === null || typeof payload.attempt_count === 'undefined') ? '' : ' Attempt ' + payload.attempt_count + '.';\n",
    "          applyFeedback(grader, payload.feedback || {});\n",
    "          result.textContent = 'Submission saved.' + attempt + ' Score: ' + payload.score + ' / ' + payload.max_score + '. Best score: ' + best + ' / ' + payload.max_score + '. Canvas sync: ' + payload.canvas_sync_status + '.';\n",
    "        })\n",
    "        .catch(function (error) {\n",
    "          result.textContent = 'Could not save this attempt: ' + error.message;\n",
    "        })\n",
    "        .finally(function () {\n",
    "          submit.disabled = false;\n",
    "        });\n",
    "    });\n",
    "\n",
    "    reset.addEventListener('click', function () {\n",
    "      clearFeedback(grader);\n",
    "      result.textContent = '';\n",
    "    });\n",
    "  }\n",
    "\n",
    "  if (document.readyState === 'loading') {\n",
    "    document.addEventListener('DOMContentLoaded', initHomeworkGrader);\n",
    "  } else {\n",
    "    initHomeworkGrader();\n",
    "  }\n",
    "})();\n",
    "</script>\n"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
