{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "82599211",
   "metadata": {},
   "source": [
    "# What-If Scenario Tweaks\n",
    "\n",
    "Demonstrate simple parameter tweaks (e.g., downtime probability) and compare KPI impacts."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1c3ff8d9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-14T10:34:39.605739Z",
     "iopub.status.busy": "2025-11-14T10:34:39.604775Z",
     "iopub.status.idle": "2025-11-14T10:34:40.499271Z",
     "shell.execute_reply": "2025-11-14T10:34:40.498054Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Base downtime probability: 0.1\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "from pathlib import Path\n",
    "\n",
    "PROJECT_ROOT = Path.cwd().resolve()\n",
    "while PROJECT_ROOT != PROJECT_ROOT.parent and not (PROJECT_ROOT / \"pyproject.toml\").exists():\n",
    "    PROJECT_ROOT = PROJECT_ROOT.parent\n",
    "if not (PROJECT_ROOT / \"pyproject.toml\").exists():\n",
    "    raise RuntimeError(\n",
    "        \"Notebook must be executed within a FHOPS checkout (pyproject.toml not found).\"\n",
    "    )\n",
    "if str(PROJECT_ROOT) not in sys.path:\n",
    "    sys.path.insert(0, str(PROJECT_ROOT))\n",
    "\n",
    "from docs.examples.analytics import utils\n",
    "\n",
    "SCENARIO = PROJECT_ROOT / \"examples/synthetic/medium/scenario.yaml\"\n",
    "ASSIGNMENTS = PROJECT_ROOT / \"docs/examples/analytics/data/scaling_medium_sa_assignments.csv\"\n",
    "\n",
    "base_tables, base_sampling = utils.run_stochastic_summary(SCENARIO, ASSIGNMENTS, tier=\"medium\")\n",
    "print(\"Base downtime probability:\", base_sampling.downtime.probability)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9be49fbf",
   "metadata": {},
   "source": [
    "### Adjusted Sampling\n",
    "\n",
    "Increase downtime probability and observe KPI deltas."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "81ca832b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-14T10:34:40.502536Z",
     "iopub.status.busy": "2025-11-14T10:34:40.501737Z",
     "iopub.status.idle": "2025-11-14T10:34:40.579111Z",
     "shell.execute_reply": "2025-11-14T10:34:40.578511Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Base mean production: 23.71\n",
      "Adjusted mean production: 23.71\n"
     ]
    }
   ],
   "source": [
    "custom_sampling = base_sampling.model_copy()\n",
    "custom_sampling.downtime.probability = 0.3\n",
    "custom_tables, _ = utils.run_stochastic_summary(\n",
    "    SCENARIO, ASSIGNMENTS, sampling_config=custom_sampling\n",
    ")\n",
    "\n",
    "base_prod = base_tables.shift.groupby(\"sample_id\")[\"production_units\"].sum().mean()\n",
    "custom_prod = custom_tables.shift.groupby(\"sample_id\")[\"production_units\"].sum().mean()\n",
    "print(f\"Base mean production: {base_prod:.2f}\")\n",
    "print(f\"Adjusted mean production: {custom_prod:.2f}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
