{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Single Sample t-test"
      ],
      "id": "23884a78-1e52-4fc4-b6a7-a59d62f899e7"
    },
    {
      "cell_type": "code",
      "execution_count": 22,
      "metadata": {},
      "outputs": [],
      "source": [
        "import polars as pl\n",
        "import scipy as sp\n",
        "import numpy as np"
      ],
      "id": "dcc6886d-323e-4c44-a323-24221954cd6b"
    },
    {
      "cell_type": "code",
      "execution_count": 23,
      "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\"]) # this is the trick"
      ],
      "id": "4de9f4e1-0ad2-430c-bb57-7da30f2a6af5"
    },
    {
      "cell_type": "code",
      "execution_count": 24,
      "metadata": {},
      "outputs": [],
      "source": [
        "bill_length = df.select(\"bill_length_mm\").drop_nulls().to_numpy()"
      ],
      "id": "e269a93b-95c9-4d8c-af96-c758f2c03699"
    },
    {
      "cell_type": "code",
      "execution_count": 25,
      "metadata": {},
      "outputs": [],
      "source": [
        "m = np.mean(bill_length)\n",
        "s = np.std(bill_length)\n",
        "N = np.size(bill_length)\n",
        "t = (m - 43) / (s / np.sqrt(N)) # t^*, test-statistic"
      ],
      "id": "0ee2c294-3a4f-4933-813e-9ab99e3bd397"
    },
    {
      "cell_type": "code",
      "execution_count": 26,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "np.float64(0.0019152230811219084)"
            ]
          }
        }
      ],
      "source": [
        "2 * (1 - sp.stats.t(df = N - 1).cdf(np.abs(t))) # p-value"
      ],
      "id": "f10d237d-11ee-4b6d-9495-afd08c697bad"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Because the p-value \\< alpha = 0.05, reject H_0. There is evidence that\n",
        "the mean bill length of penguins is not 43mm.\n",
        "\n",
        "# Paired t-test"
      ],
      "id": "58ded61a-dc86-4e5a-91d9-0ec05df13a0c"
    },
    {
      "cell_type": "code",
      "execution_count": 27,
      "metadata": {},
      "outputs": [],
      "source": [
        "ndf = (df\n",
        " .select(\"bill_length_mm\", \"bill_depth_mm\")\n",
        " .drop_nulls()\n",
        " .with_columns((pl.col(\"bill_length_mm\") - pl.col(\"bill_depth_mm\")).alias(\"d\")) # create new cols based on old cols\n",
        ")"
      ],
      "id": "f5d96c88-cf7d-4f3f-bdf4-86ebb250e5ee"
    },
    {
      "cell_type": "code",
      "execution_count": 28,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "np.float64(0.0)"
            ]
          }
        }
      ],
      "source": [
        "d = ndf[\"d\"].to_numpy()\n",
        "m = np.mean(d)\n",
        "s = np.std(d)\n",
        "N = np.size(d)\n",
        "t = (m - 0) / (s / np.sqrt(N))\n",
        "2 * (1 - sp.stats.t(df = N - 1).cdf(np.abs(t))) # p-value"
      ],
      "id": "7cb9e2a7-c276-4c58-a16f-0ab7d3899de0"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Based on the data, because the p-value \\< level of significance, the\n",
        "mean difference (bill_length - bill_depth) is not equal to 0."
      ],
      "id": "c92bf419-1fcf-4599-b345-312901938fe3"
    },
    {
      "cell_type": "code",
      "execution_count": 29,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "array([26.10942588, 27.43209458])"
            ]
          }
        }
      ],
      "source": [
        "t = sp.stats.t(df = N - 1).ppf([0.025, 0.975])\n",
        "m + t * s / np.sqrt(N)"
      ],
      "id": "a2ecda7a-6b81-4db5-b627-118136abd68c"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Two Sample t-test"
      ],
      "id": "22389f95-380e-4d3f-b4c6-0b5d2a2edf6f"
    },
    {
      "cell_type": "code",
      "execution_count": 35,
      "metadata": {},
      "outputs": [],
      "source": [
        "sdf = (df\n",
        "    .select(\"species\", \"bill_length_mm\")\n",
        "    .drop_nulls()\n",
        ")\n",
        "da = sdf.filter(pl.col(\"species\") == \"Adelie\")[\"bill_length_mm\"].to_numpy()\n",
        "dc = sdf.filter(pl.col(\"species\") == \"Chinstrap\")[\"bill_length_mm\"].to_numpy()\n"
      ],
      "id": "14d4d2a8-99e5-47e1-8509-56072fab5a21"
    },
    {
      "cell_type": "code",
      "execution_count": 36,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "TtestResult(statistic=np.float64(-23.801939237440887), pvalue=np.float64(2.011759018655462e-62), df=np.float64(217.0))"
            ]
          }
        }
      ],
      "source": [
        "sp.stats.ttest_ind(da, dc)"
      ],
      "id": "fff8c5fa-4138-4135-a005-74eb263476f2"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Because p-value \\< 0.0001 \\< alpha = 0.05, we reject H0. There is\n",
        "sufficient evidence to claim that the difference of mean bill length\n",
        "between Adelie and Chinstrap penguins is not equal to zero. On average,\n",
        "Chinstrap and Adelie penguins have differen bill lengths."
      ],
      "id": "2beaa98f-3ab1-4e1d-a1e2-efd2eb71b3b4"
    }
  ],
  "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"
    }
  }
}