Bootstrap-t-student/bootstrap-t.ipynb

266 lines
12 KiB
Plaintext
Raw Normal View History

2022-05-11 15:02:15 +02:00
{
"cells": [
2022-05-13 22:06:56 +02:00
{
"cell_type": "markdown",
"source": [
"Bootstrapowa wersja testu t.\n",
"Implementacja powinna obejmować test dla jednej próby, dla dwóch prób niezależnych oraz dla dwóch prób zależnych.\n",
"W każdej sytuacji oczekiwanym wejście jest zbiór danych w odpowiednim formacie, a wyjściem p-wartość oraz ostateczna decyzja.\n",
"Dodatkowo powinien być rysowany odpowiedni rozkład statystyki testowej."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"Zbiór danych - ???\n",
"Hipoteza zerowa - ???\n",
2022-05-13 23:43:00 +02:00
"Hipoteza alternatywna - ???\n",
"\n",
"Dla każdego z 3 testów inne\n",
"https://www.jmp.com/en_ch/statistics-knowledge-portal/t-test.html"
2022-05-13 22:06:56 +02:00
],
"metadata": {
"collapsed": false
}
},
2022-05-11 15:02:15 +02:00
{
"cell_type": "code",
2022-05-14 15:31:47 +02:00
"execution_count": 15,
2022-05-11 15:02:15 +02:00
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"from math import sqrt\n",
"from scipy.stats import sem\n",
2022-05-13 23:43:00 +02:00
"from scipy.stats import t\n",
2022-05-14 15:31:47 +02:00
"import matplotlib.pyplot as plt\n",
"from statistics import mean, stdev"
2022-05-11 15:02:15 +02:00
]
},
{
"cell_type": "code",
2022-05-14 15:31:47 +02:00
"execution_count": 16,
2022-05-11 15:02:15 +02:00
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"def generate_bootstraps(data, n_bootstraps=100):\n",
" data_size = data.shape[0]\n",
2022-05-13 22:06:56 +02:00
" for _ in range(n_bootstraps):\n",
" indices = np.random.choice(len(data), size=data_size)\n",
" yield data.iloc[indices, :]"
2022-05-11 15:02:15 +02:00
]
},
{
"cell_type": "code",
2022-05-14 15:31:47 +02:00
"execution_count": 17,
2022-05-11 15:02:15 +02:00
"outputs": [],
"source": [
"def get_t_stat(data1, data2):\n",
" mean1 = np.mean(data1)\n",
" mean2 = np.mean(data2)\n",
" sem1 = sem(data1)\n",
" sem2 = sem(data2)\n",
"\n",
" sed = sqrt(sem1**2.0 + sem2**2.0)\n",
2022-05-13 23:43:00 +02:00
" # To jest wzór chyba tylko dla jednego przypadku\n",
2022-05-11 15:02:15 +02:00
" return (mean1 - mean2) / sed"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
2022-05-14 15:31:47 +02:00
"execution_count": null,
"outputs": [],
"source": [
"def t_stat_single(sample, population_mean):\n",
" \"\"\"Funkcja oblicza wartość statystyki testowej dla jednej próbki\"\"\"\n",
" sample_mean = mean(sample)\n",
" sample_std = stdev(sample)\n",
" sample_size = len(sample)\n",
" return (sample_mean - population_mean) / (sample_std / sqrt(sample_size))"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"def t_stat_indept():\n",
" pass"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"def t_stat_dep():\n",
" pass"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": 18,
2022-05-11 15:02:15 +02:00
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"def independent_t_test(data, columns, alpha=0.05):\n",
" t_stat_sum = 0\n",
" for sample in generate_bootstraps(data):\n",
" t_stat_sum += get_t_stat(sample[columns[0]], sample[columns[1]])\n",
"\n",
" data_size = data.shape[0]\n",
" t_stat = t_stat_sum / data_size\n",
" df = 2 * data_size - 2\n",
" cv = t.ppf(1.0 - alpha, df)\n",
" p = (1.0 - t.cdf(abs(t_stat), df)) * 2.0\n",
" return t_stat, df, cv, p"
]
},
{
"cell_type": "code",
2022-05-14 15:31:47 +02:00
"execution_count": 19,
2022-05-11 15:02:15 +02:00
"outputs": [],
"source": [
"def make_decision(data, columns, alpha=0.05):\n",
" t_stat, df, cv, p = independent_t_test(data, columns, alpha)\n",
" print(f't: {t_stat}, df: {df}, cv: {cv}, p: {p}\\n')\n",
" if abs(t_stat) <= cv:\n",
"\t print('Accept null hypothesis that the means are equal.')\n",
" else:\n",
" print('Reject the null hypothesis that the means are equal.')\n",
" if p > alpha:\n",
" print('Accept null hypothesis that the means are equal.')\n",
" else:\n",
"\t print('Reject the null hypothesis that the means are equal.')"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
2022-05-14 15:31:47 +02:00
"execution_count": 20,
2022-05-13 23:43:00 +02:00
"outputs": [
{
"data": {
"text/plain": "<Figure size 432x288 with 1 Axes>",
2022-05-14 15:31:47 +02:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAQxElEQVR4nO3dfYxldX3H8fdHECxaBdyB0t1NZ6NoQk2rOKU01oeCUR6MS1tLoE3dKslGRYsPLS7YiEljsqiVatLSrEJZW4JQpIUWbUWKJU1kdUCeH2RFkF0XdgyKtiYq+u0f96zcDrO7M3Pm4e7P9yu5uef8fufc883Jnc+c+7vnnJuqQpLUlqctdwGSpIVnuEtSgwx3SWqQ4S5JDTLcJalB+y93AQArVqyo8fHx5S5DkvYpN99887eramymvpEI9/HxcSYnJ5e7DEnapyR5aHd9DstIUoMMd0lqkOEuSQ0y3CWpQYa7JDXIcJekBhnuktQgw12SGmS4S1KDRuIKVWlvxjdcu2zbfnDjycu2bWm+PHKX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FGSNEcLMuaeZBx4CbAFOLyqdnRdjwCH72ad9Ukmk0xOTU0tRBmSpE7vcE/yLOAzwDur6nvDfVVVQM20XlVtqqqJqpoYGxvrW4YkaUivcE/ydAbBfmlVXdU1P5rkiK7/CGBnvxIlSXPV52yZABcB91TVR4e6rgHWddPrgKvnX54kaT763PL3ZcAfA3ckubVrOxfYCFyR5AzgIeDUXhVKkuZs3uFeVf8NZDfdx8/3dSVJ/XmFqiQ1yHCXpAYZ7pLUIMNdkhpkuEtSgwx3SWqQ4S5JDTLcJalBhrskNchwl6QGGe6S1KA+Nw77uTe+4drlLkGSZuSRuyQ1yCN3SSN
2022-05-13 23:43:00 +02:00
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
2022-05-13 22:06:56 +02:00
"source": [
"def draw_distribution():\n",
" \"\"\"Funkcja rysuje rozkład statystyki testowej\"\"\"\n",
2022-05-13 23:43:00 +02:00
" dummy = np.random.normal(170, 10, 500)\n",
" plt.hist(dummy)\n",
" plt.show()\n",
" pass\n",
"draw_distribution()"
2022-05-13 22:06:56 +02:00
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
2022-05-14 15:31:47 +02:00
"execution_count": 21,
2022-05-11 15:02:15 +02:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2022-05-14 15:31:47 +02:00
"t: 6.940510630195086, df: 998, cv: 1.6463818766348755, p: 7.02371494298859e-12\n",
2022-05-11 15:02:15 +02:00
"\n",
"Reject the null hypothesis that the means are equal.\n",
"Reject the null hypothesis that the means are equal.\n"
]
}
],
"source": [
"dataset = pd.read_csv('experiment_data.csv')\n",
"make_decision(dataset, ['Weight', 'Age'])"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
}
],
"metadata": {
"interpreter": {
"hash": "11938c6bc6919ae2720b4d5011047913343b08a43b18698fd82dedb0d4417594"
},
"kernelspec": {
"display_name": "Python 3.9.1 64-bit",
"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.9.1"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}