{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0f872066",
   "metadata": {},
   "outputs": [],
   "source": [
    "# data for the OLS\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "theta0 = 2.6486\n",
    "theta1 = 4.98\n",
    "m = 50\n",
    "disp = 4\n",
    "x = np.linspace(0,5,m)\n",
    "y = theta0+theta1*x+disp*np.random.rand(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c4906c75",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 11220 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(x,y, '.k')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "896d7633",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.5\n",
      "17.115760540070784\n"
     ]
    }
   ],
   "source": [
    "xSum = 0\n",
    "ySum = 0\n",
    "\n",
    "for i in range(m):\n",
    "    #print(x[i])\n",
    "    xSum = xSum + x[i]\n",
    "    ySum = ySum + y[i]\n",
    "    pass\n",
    "xMean = xSum/m\n",
    "yMean = ySum/m\n",
    "print(xMean)\n",
    "print(yMean)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "3670b14b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "theta 0:  4.012173816371986\n",
      "theta 1:  5.24143468947952\n"
     ]
    }
   ],
   "source": [
    "num = 0\n",
    "den = 0\n",
    "for i in range(m):\n",
    "    num = num + x[i]*(yMean-y[i])\n",
    "    den = den + x[i]*(xMean-x[i])\n",
    "    pass\n",
    "theta1H = num/den\n",
    "theta0H = yMean-theta1H*xMean\n",
    "print(\"theta 0: \", theta0H)\n",
    "print(\"theta 1: \", theta1H)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "729e6b3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 20548 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "yH = theta0H+theta1H*x\n",
    "plt.plot(x, yH, ':r', label=\"Model\")\n",
    "plt.plot(x, y, '.k', label=\"Data\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  }
 ],
 "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
}
