{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "#"
      ],
      "id": "ffbf04bf-8b91-4577-b534-9d486ab98e66"
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {},
      "outputs": [],
      "source": [
        "import polars as pl\n",
        "import scipy as sp\n",
        "import numpy as np"
      ],
      "id": "f9ebe109-a4d8-4ee0-b9f9-94f2455d752c"
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {},
      "outputs": [],
      "source": [
        "url = \"https://roualdes.sfo3.digitaloceanspaces.com/data/penguins.csv\"\n",
        "df = pl.read_csv(url, null_values = [\"NA\"]) # this is the trick"
      ],
      "id": "d721c6f2-3d28-497b-8706-ec8709f0b720"
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/html": [
              "</div>"
            ]
          }
        }
      ],
      "source": [
        "df"
      ],
      "id": "f305f49f-1edc-4568-9891-cf3bcb917948"
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {},
      "outputs": [],
      "source": [
        "bill_length = df[\"bill_length_mm\"].to_numpy() # .cast(pl.Float64, strict=False).to_numpy()"
      ],
      "id": "a109ac37-01f9-4ce4-8de4-f87031b7c9c7"
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "np.float64(nan)"
            ]
          }
        }
      ],
      "source": [
        "np.mean(bill_length)"
      ],
      "id": "4ad68e4b-2969-45a7-b9dc-c389b0106a60"
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {},
      "outputs": [],
      "source": [
        "data = bill_length[~np.isnan(bill_length)]\n",
        "mhat = np.mean(data)\n",
        "shat = np.std(data)\n",
        "N = np.size(data)\n",
        "p = .99\n",
        "t = sp.stats.t(df = N - 1).ppf(p + (1 - p)/2)"
      ],
      "id": "926150ba-d8dc-42ce-a66c-848883217c96"
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "np.float64(44.685527571236165)"
            ]
          }
        }
      ],
      "source": [
        "mhat + t * shat / np.sqrt(N)"
      ],
      "id": "a3908b56-ac73-4bc1-8e43-041edef917f4"
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "np.float64(43.15833207788664)"
            ]
          }
        }
      ],
      "source": [
        "mhat - t * shat / np.sqrt(N)"
      ],
      "id": "a2032b9a-808e-488d-9fad-4ff299e7f7ee"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We are 99% confident that the true mean bill length of penguins is\n",
        "between 43.2 and 44.7 milimeters."
      ],
      "id": "dcd0cfaf-9c67-4a87-9b2b-3a5da2d318a5"
    }
  ],
  "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.12"
    }
  }
}