{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5b299da0",
   "metadata": {},
   "source": [
    "Lab Example 1 — Sampling variability and CLT\n",
    "\n",
    "Objective: show that individual measurements vary, but sample means are more stable.\n",
    "\n",
    "Students should:\n",
    "\n",
    "1. Generate a non-normal population.\n",
    "2. Take many random samples.\n",
    "3. Compute the mean of each sample.\n",
    "4. Plot the distribution of the sample means."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "07e7f5cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 18108 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 19592 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "np.random.seed(10)\n",
    "\n",
    "population = np.random.exponential(scale=2.0, size=100000)\n",
    "\n",
    "sample_size = 30\n",
    "n_samples = 1000\n",
    "\n",
    "sample_means = []\n",
    "\n",
    "for i in range(n_samples):\n",
    "    sample = np.random.choice(population, size=sample_size, replace=True)\n",
    "    sample_means.append(sample.mean())\n",
    "\n",
    "plt.hist(population, bins=40)\n",
    "plt.title(\"Original population\")\n",
    "plt.xlabel(\"Value\")\n",
    "plt.ylabel(\"Frequency\")\n",
    "plt.show()\n",
    "\n",
    "plt.hist(sample_means, bins=40)\n",
    "plt.title(\"Distribution of sample means\")\n",
    "plt.xlabel(\"Sample mean\")\n",
    "plt.ylabel(\"Frequency\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e78d8f7",
   "metadata": {},
   "source": [
    "Even when the original data are not normally distributed, the distribution of sample means tends to become approximately normal when the sample size increases."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "411bd317",
   "metadata": {},
   "source": [
    "# Lab Example 2 — Confidence interval for a mean\n",
    "\n",
    "Objective: estimate a parameter with uncertainty.\n",
    "\n",
    "Use the repeated measurement example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "615beff5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mean: 9.999333333333333\n",
      "Standard deviation: 0.04832430129073875\n",
      "95% confidence interval: 9.972572227269502 10.026094439397163\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from scipy import stats\n",
    "\n",
    "measurements = np.array([9.91, 10.03, 9.98, 10.05, 10.01, 9.96, 10.08, 9.94,\n",
    "                         10.02, 9.99, 10.04, 9.97, 10.06, 9.95, 10.00])\n",
    "\n",
    "n = len(measurements)\n",
    "xbar = measurements.mean()\n",
    "s = measurements.std(ddof=1)\n",
    "\n",
    "confidence = 0.95\n",
    "alpha = 1 - confidence\n",
    "\n",
    "t_critical = stats.t.ppf(1 - alpha/2, df=n-1)\n",
    "margin_error = t_critical * s / np.sqrt(n)\n",
    "\n",
    "ci_lower = xbar - margin_error\n",
    "ci_upper = xbar + margin_error\n",
    "\n",
    "print(\"Mean:\", xbar)\n",
    "print(\"Standard deviation:\", s)\n",
    "print(\"95% confidence interval:\", ci_lower, ci_upper)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75ec3573",
   "metadata": {},
   "source": [
    "Interpretation:\n",
    "\n",
    "We do not report only the mean. We report the mean with an interval that reflects uncertainty.\n",
    "\n",
    "Suggested question:\n",
    "\n",
    "Does the confidence interval include 10\\,\\Omega?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2024490",
   "metadata": {},
   "source": [
    "# Lab Example 3 — Hypothesis test for a nominal value\n",
    "\n",
    "Objective: test whether the measured component is statistically consistent with a reference value.\n",
    "\n",
    "For the resistor example:\n",
    "\n",
    "H_0: \\mu = 10\n",
    "\n",
    "H_a: \\mu \\neq 10\n",
    "\n",
    "Python:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2fee9dc6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "t statistic: -0.05343044448672866\n",
      "p-value: 0.9581438956150026\n"
     ]
    }
   ],
   "source": [
    "from scipy import stats\n",
    "\n",
    "mu0 = 10.0\n",
    "\n",
    "t_statistic, p_value = stats.ttest_1samp(measurements, popmean=mu0)\n",
    "\n",
    "print(\"t statistic:\", t_statistic)\n",
    "print(\"p-value:\", p_value)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv (3.14.3)",
   "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.14.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
