{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Problem 4\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.stats import norm\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.stats import norm\n",
    "import pandas as pd\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sample Mean (ybar): 4.66 hours\n",
      "Posterior Mean (µ_n): 4.70\n",
      "Posterior Standard Deviation (σ_n): 0.21\n"
     ]
    }
   ],
   "source": [
    "np.random.seed(42)\n",
    "n = 50 #sample size\n",
    "mu_true = 5 #avg sleep time\n",
    "sigma = 1.5  #standard deviation\n",
    "data = np.random.normal(mu_true, sigma, n)\n",
    "ybar = np.mean(data) \n",
    "print(f\"Sample Mean (ybar): {ybar:.2f} hours\")\n",
    "\n",
    "# Prior parameters (initial belief about sleep time)\n",
    "mu0 = 5.5\n",
    "sigma0 = 1.0\n",
    "\n",
    "# Posterior parameters\n",
    "sigma_n = np.sqrt(1 / (1 / sigma0**2 + n / sigma**2))\n",
    "mu_n = (mu0 / sigma0**2 + n * ybar / sigma**2) / (1 / sigma0**2 + n / sigma**2)\n",
    "\n",
    "print(f\"Posterior Mean (µ_n): {mu_n:.2f}\")\n",
    "print(f\"Posterior Standard Deviation (σ_n): {sigma_n:.2f}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 26172 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Define x-axis range\n",
    "x = np.linspace(3, 8, 1000)\n",
    "\n",
    "# Generate random samples for histograms\n",
    "prior_samples = norm.rvs(loc=mu0, scale=sigma0, size=1000)\n",
    "likelihood_samples = norm.rvs(loc=ybar, scale=sigma / np.sqrt(n), size=1000)\n",
    "posterior_samples = norm.rvs(loc=mu_n, scale=sigma_n, size=1000)\n",
    "\n",
    "# Plot histograms\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.hist(prior_samples, bins=30, density=True, alpha=0.5, label=\"Prior\")\n",
    "plt.hist(likelihood_samples, bins=30, density=True, alpha=0.5, label=\"Likelihood\")\n",
    "plt.hist(posterior_samples, bins=30, density=True, alpha=0.5, label=\"Posterior\")\n",
    "plt.title(\"Hours College Students Sleep\")\n",
    "plt.xlabel(\"Sleep Time (hours)\")\n",
    "\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              Prior  Data (Sample Mean)  Posterior\n",
      "0  Precision    1.0              44.444     45.444\n",
      "1         SD    1.0               0.150      0.148\n",
      "2       Mean    5.5               4.844      4.859\n"
     ]
    }
   ],
   "source": [
    "# Create a DataFrame\n",
    "df = pd.DataFrame({\n",
    "    \"\": [\"Precision\", \"SD\", \"Mean\"],\n",
    "    \"Prior\": [1 / sigma0**2, sigma0, mu0],\n",
    "    \"Data (Sample Mean)\": [n / sigma**2, sigma / np.sqrt(n), ybar],\n",
    "    \"Posterior\": [1 / sigma_n**2, sigma_n, mu_n]\n",
    "})\n",
    "\n",
    "# Display the DataFrame\n",
    "print(df.round(3))"
   ]
  }
 ],
 "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": 2
}
