{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "#"
      ],
      "id": "1305a7fc-9fd7-4d6e-a25a-f476eb902a2b"
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {},
      "outputs": [],
      "source": [
        " # Get set up, but we'll start on the board today\n",
        "import polars as pl\n",
        "import scipy as sp\n",
        "import numpy as np\n",
        "import matplotlib.pyplot as plt"
      ],
      "id": "d61979af-c5ee-4194-a420-a430d93abf49"
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {},
      "outputs": [],
      "source": [
        "# dataset on penguins\n",
        "# copy from lecture notes: ci example\n",
        "url = \"https://roualdes.sfo3.digitaloceanspaces.com/data/penguins.csv\"\n",
        "df = pl.read_csv(url, null_values = [\"NA\"]).select(\"body_mass_g\").drop_nulls()"
      ],
      "id": "6a44f752-5e28-469c-985a-c5a7da91f42f"
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "(np.float64(4201.754385964912), np.float64(800.781229238452))"
            ]
          }
        }
      ],
      "source": [
        "x = df[\"body_mass_g\"].to_numpy()\n",
        "np.mean(x), np.std(x)"
      ],
      "id": "5082f8e9-1ceb-4ad9-8c21-022e7cdf1b26"
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {},
      "outputs": [],
      "source": [
        "def ll_normal(theta, data):\n",
        "    mu = theta[0]\n",
        "    sigma = theta[1]\n",
        "    x = data\n",
        "    N = np.size(x)\n",
        "    d = x - mu\n",
        "    return N * np.log(sigma) + np.sum(d * d) / (2 * sigma * sigma)"
      ],
      "id": "84afd996-0c58-48f6-bf63-fba453f5c227"
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "  message: CONVERGENCE: NORM OF PROJECTED GRADIENT <= PGTOL\n",
              "  success: True\n",
              "   status: 0\n",
              "      fun: 2457.471023626704\n",
              "        x: [ 4.202e+03  8.008e+02]\n",
              "      nit: 49\n",
              "      jac: [ 0.000e+00  0.000e+00]\n",
              "     nfev: 171\n",
              "     njev: 57\n",
              " hess_inv: <2x2 LbfgsInvHessProduct with dtype=float64>"
            ]
          }
        }
      ],
      "source": [
        "init = sp.stats.expon().rvs(size = 2)\n",
        "sp.optimize.minimize(ll_normal, init, args = (x,),\n",
        "                     method = \"L-BFGS-B\", bounds = [(-np.inf, np.inf), (1e-5, np.inf)])"
      ],
      "id": "43194c28-3b41-468b-87b5-d999b49bd793"
    }
  ],
  "nbformat": 4,
  "nbformat_minor": 5,
  "metadata": {
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3 (ipykernel)",
      "language": "python"
    },
    "language_info": {
      "name": "python",
      "codemirror_mode": {
        "name": "ipython",
        "version": "3"
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.16"
    }
  }
}