From 7e0cf14302191a0c1333e94c0319c4bd21013795 Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Wed, 11 May 2022 15:02:15 +0200 Subject: [PATCH] Initial implementation --- bootstrap-t.ipynb | 157 ++++++++++++++ experiment_data.csv | 501 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 658 insertions(+) create mode 100644 bootstrap-t.ipynb create mode 100644 experiment_data.csv diff --git a/bootstrap-t.ipynb b/bootstrap-t.ipynb new file mode 100644 index 0000000..cd93786 --- /dev/null +++ b/bootstrap-t.ipynb @@ -0,0 +1,157 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 46, + "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", + "from scipy.stats import t" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [], + "source": [ + "def generate_bootstraps(data, n_bootstraps=100):\n", + " data_size = data.shape[0]\n", + " for b in range(n_bootstraps):\n", + " indicies = np.random.choice(len(data), size=data_size)\n", + " yield data.iloc[indicies, :]" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "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", + " return (mean1 - mean2) / sed" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "code", + "execution_count": 49, + "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", + "execution_count": 50, + "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", + "execution_count": 51, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t: 6.903407918031469, df: 998, cv: 1.6463818766348755, p: 9.018563673635072e-12\n", + "\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 +} \ No newline at end of file diff --git a/experiment_data.csv b/experiment_data.csv new file mode 100644 index 0000000..9e807ab --- /dev/null +++ b/experiment_data.csv @@ -0,0 +1,501 @@ +Gender,Height,Weight,Index,Age +Male,174,96,4,42 +Male,189,87,2,50 +Female,185,110,4,43 +Female,195,104,3,48 +Male,149,61,3,46 +Male,189,104,3,18 +Male,147,92,5,57 +Male,154,111,5,22 +Male,174,90,3,31 +Female,169,103,4,21 +Male,195,81,2,72 +Female,159,80,4,45 +Female,192,101,3,51 +Male,155,51,2,46 +Male,191,79,2,40 +Female,153,107,5,22 +Female,157,110,5,69 +Male,140,129,5,76 +Male,144,145,5,21 +Male,172,139,5,60 +Male,157,110,5,41 +Female,153,149,5,73 +Female,169,97,4,21 +Male,185,139,5,50 +Female,172,67,2,49 +Female,151,64,3,31 +Male,190,95,3,67 +Male,187,62,1,27 +Female,163,159,5,61 +Male,179,152,5,49 +Male,153,121,5,22 +Male,178,52,1,46 +Female,195,65,1,73 +Female,160,131,5,22 +Female,157,153,5,42 +Female,189,132,4,69 +Female,197,114,3,71 +Male,144,80,4,32 +Female,171,152,5,29 +Female,185,81,2,36 +Female,175,120,4,54 +Female,149,108,5,39 +Male,157,56,2,23 +Male,161,118,5,62 +Female,182,126,4,30 +Male,185,76,2,31 +Female,188,122,4,30 +Male,181,111,4,77 +Male,161,72,3,39 +Male,140,152,5,35 +Female,168,135,5,80 +Female,176,54,1,47 +Male,163,110,5,38 +Male,172,105,4,21 +Male,196,116,4,39 +Female,187,89,3,58 +Male,172,92,4,48 +Male,178,127,5,23 +Female,164,70,3,76 +Male,143,88,5,20 +Female,191,54,0,80 +Female,141,143,5,55 +Male,193,54,0,54 +Male,190,83,2,80 +Male,175,135,5,46 +Female,179,158,5,67 +Female,172,96,4,53 +Female,168,59,2,57 +Female,164,82,4,48 +Female,194,136,4,32 +Female,153,51,2,32 +Male,178,117,4,56 +Male,141,80,5,43 +Male,180,75,2,37 +Female,185,100,3,18 +Female,197,154,4,72 +Male,165,104,4,61 +Female,168,90,4,64 +Female,176,122,4,69 +Male,181,51,0,21 +Male,164,75,3,68 +Female,166,140,5,39 +Female,190,105,3,47 +Male,186,118,4,74 +Male,168,123,5,62 +Male,198,50,0,25 +Female,175,141,5,58 +Male,145,117,5,49 +Female,159,104,5,72 +Female,185,140,5,38 +Female,178,154,5,66 +Female,183,96,3,79 +Female,194,111,3,63 +Male,177,61,2,37 +Male,197,119,4,40 +Female,170,156,5,42 +Male,142,69,4,54 +Male,160,139,5,72 +Male,195,69,1,35 +Female,190,50,0,46 +Male,199,156,4,78 +Male,154,105,5,56 +Male,161,155,5,79 +Female,198,145,4,70 +Female,192,140,4,30 +Male,195,126,4,42 +Male,166,160,5,42 +Male,159,154,5,78 +Female,181,106,4,28 +Male,149,66,3,18 +Female,150,70,4,19 +Female,146,157,5,22 +Male,190,135,4,76 +Female,192,90,2,38 +Female,177,96,4,72 +Male,148,60,3,51 +Female,165,57,2,60 +Female,146,104,5,57 +Male,144,108,5,54 +Female,176,156,5,35 +Female,168,87,4,35 +Male,187,122,4,71 +Male,187,138,4,57 +Female,184,160,5,56 +Female,158,149,5,32 +Male,158,96,4,39 +Male,194,115,4,56 +Female,145,79,4,74 +Male,182,151,5,67 +Male,154,54,2,68 +Female,168,139,5,32 +Female,187,70,2,40 +Female,158,153,5,68 +Female,167,110,4,33 +Female,171,155,5,60 +Female,183,150,5,40 +Female,190,156,5,28 +Male,194,108,3,38 +Male,171,147,5,58 +Male,159,124,5,34 +Female,169,54,2,51 +Female,167,85,4,69 +Male,180,149,5,60 +Male,163,123,5,22 +Male,140,79,5,74 +Male,197,125,4,66 +Male,194,106,3,66 +Female,140,146,5,18 +Male,195,98,3,62 +Female,168,115,3,19 +Female,196,50,0,20 +Male,140,52,3,49 +Female,150,60,3,36 +Female,168,140,5,57 +Female,155,111,5,62 +Female,179,103,4,52 +Female,182,84,3,73 +Male,168,160,5,62 +Female,187,102,3,48 +Male,181,105,4,72 +Male,199,99,2,61 +Female,184,76,2,29 +Male,192,101,3,57 +Female,182,143,5,33 +Female,172,111,4,55 +Male,181,78,2,72 +Male,176,109,4,47 +Female,156,106,5,69 +Female,151,67,3,24 +Female,188,80,2,29 +Male,187,136,4,18 +Male,174,138,5,45 +Male,167,151,5,74 +Female,196,131,4,60 +Male,197,149,4,27 +Female,185,119,4,31 +Female,170,102,4,54 +Female,181,94,3,59 +Female,166,126,5,72 +Male,188,100,3,20 +Female,162,74,3,25 +Male,177,117,4,65 +Male,162,97,4,67 +Male,180,73,2,34 +Female,192,108,3,58 +Male,165,80,3,23 +Female,167,135,5,19 +Female,182,84,3,57 +Female,161,134,5,50 +Male,158,95,4,35 +Male,141,85,5,49 +Male,154,100,5,80 +Male,165,105,4,31 +Female,142,137,5,23 +Male,141,94,5,35 +Male,145,108,5,42 +Male,157,74,4,55 +Female,177,117,4,24 +Female,166,144,5,73 +Male,193,151,5,72 +Male,184,57,1,65 +Male,179,93,3,31 +Female,156,89,4,25 +Male,182,104,4,30 +Male,145,160,5,60 +Female,150,87,4,63 +Male,145,99,5,55 +Female,196,122,4,46 +Male,191,96,3,60 +Female,148,67,4,65 +Female,150,84,4,45 +Male,148,155,5,36 +Female,153,146,5,49 +Female,196,159,5,20 +Female,185,52,0,74 +Female,171,131,5,50 +Female,143,118,5,41 +Female,142,86,5,77 +Female,141,126,5,37 +Male,159,109,5,74 +Female,173,82,2,18 +Male,183,138,5,65 +Female,152,90,4,24 +Male,178,140,5,56 +Male,188,54,0,20 +Female,155,144,5,58 +Male,166,70,3,68 +Male,188,123,4,31 +Female,171,120,5,51 +Male,179,130,5,41 +Female,186,137,4,77 +Female,153,78,2,51 +Female,184,86,3,56 +Female,177,81,3,31 +Male,145,78,4,39 +Male,170,81,3,37 +Male,181,141,5,56 +Male,165,155,5,18 +Female,174,65,2,57 +Female,146,110,5,65 +Male,178,85,3,38 +Male,166,61,2,35 +Male,191,62,1,38 +Female,177,155,5,62 +Female,183,50,0,27 +Male,151,114,5,55 +Male,182,98,3,30 +Female,142,159,5,61 +Female,188,90,3,19 +Male,161,89,4,35 +Male,153,70,3,70 +Male,140,143,5,78 +Male,169,141,5,48 +Female,162,159,5,41 +Male,183,147,5,37 +Female,162,58,2,77 +Female,172,109,4,28 +Female,150,119,5,49 +Female,169,145,5,74 +Female,184,132,4,23 +Male,159,104,5,44 +Male,163,131,5,22 +Male,156,137,5,45 +Female,157,52,2,79 +Male,147,84,4,77 +Male,141,86,5,32 +Male,173,139,5,59 +Male,154,145,5,75 +Male,168,148,5,21 +Male,168,50,1,71 +Male,145,130,5,24 +Male,152,103,5,36 +Female,187,121,4,25 +Female,163,57,0,30 +Male,178,83,3,24 +Female,187,94,3,35 +Female,179,114,4,25 +Male,190,80,2,45 +Male,172,75,3,49 +Male,188,57,1,25 +Male,193,65,1,66 +Female,147,126,5,40 +Female,147,94,5,72 +Male,166,107,4,27 +Female,192,139,4,63 +Male,181,139,4,41 +Male,150,74,4,50 +Male,178,160,5,48 +Female,156,52,2,80 +Male,149,100,5,22 +Male,156,74,4,64 +Male,183,105,3,30 +Female,162,68,3,43 +Female,165,83,4,27 +Female,168,143,5,53 +Male,160,156,5,36 +Female,169,88,2,75 +Female,140,76,4,23 +Female,187,92,3,28 +Male,151,82,4,34 +Female,186,140,5,79 +Male,182,108,4,48 +Male,188,81,2,25 +Male,179,110,4,45 +Female,156,126,5,59 +Male,188,114,4,19 +Male,183,153,5,72 +Male,144,88,5,53 +Male,196,69,1,40 +Male,171,141,5,26 +Male,171,147,5,51 +Female,180,156,5,68 +Male,191,146,5,65 +Female,179,67,2,77 +Female,180,60,2,71 +Female,154,132,5,19 +Male,188,99,3,54 +Male,142,135,5,74 +Male,170,95,4,55 +Male,152,141,5,66 +Female,190,118,4,37 +Female,181,111,4,73 +Male,153,104,5,26 +Male,187,140,5,61 +Female,144,66,4,20 +Female,148,54,2,48 +Female,199,92,2,49 +Female,167,85,4,73 +Female,164,71,3,19 +Female,185,102,3,65 +Female,164,160,5,77 +Male,142,71,4,50 +Male,165,68,2,59 +Female,172,62,2,42 +Female,157,56,2,18 +Male,155,57,2,37 +Female,167,153,5,79 +Female,164,126,5,80 +Female,189,125,4,39 +Female,161,145,5,32 +Female,155,71,3,56 +Female,171,118,4,74 +Female,154,92,4,34 +Male,179,83,3,53 +Male,170,115,4,73 +Female,184,106,4,54 +Female,191,68,2,60 +Male,162,58,2,22 +Male,178,138,5,61 +Female,157,60,2,35 +Male,184,83,2,59 +Male,197,88,2,68 +Female,160,51,2,34 +Male,184,153,5,65 +Male,190,50,0,18 +Male,174,90,3,49 +Female,189,124,4,80 +Female,186,143,5,55 +Female,180,58,1,41 +Female,186,148,4,42 +Female,193,61,1,21 +Male,161,103,4,33 +Female,151,158,5,50 +Female,195,147,4,42 +Female,184,152,5,80 +Male,141,80,5,69 +Female,185,94,3,47 +Female,186,127,4,37 +Male,142,131,5,68 +Female,147,67,4,36 +Male,151,62,3,72 +Female,160,124,5,67 +Male,185,60,1,79 +Female,163,63,2,42 +Male,174,95,4,29 +Female,150,144,5,46 +Male,142,91,5,61 +Male,178,142,5,20 +Female,154,96,5,69 +Male,176,87,3,75 +Male,159,120,5,22 +Male,191,62,1,32 +Male,177,117,4,66 +Male,151,154,5,62 +Female,182,149,5,56 +Female,197,72,2,57 +Male,146,138,5,25 +Female,160,83,4,65 +Female,157,66,3,44 +Female,150,50,2,60 +Female,167,58,2,51 +Female,180,70,2,29 +Female,183,76,2,79 +Female,183,87,3,58 +Female,152,154,5,21 +Female,164,71,3,74 +Male,187,96,3,69 +Male,169,136,5,44 +Female,149,61,3,75 +Male,163,137,5,32 +Female,195,104,3,76 +Male,174,107,4,65 +Male,182,70,2,60 +Male,169,110,4,53 +Male,193,130,4,63 +Male,148,141,5,59 +Male,186,68,2,48 +Male,165,143,5,59 +Female,146,123,5,53 +Female,166,133,5,26 +Male,179,56,1,45 +Female,177,101,4,36 +Male,181,154,5,66 +Female,161,154,5,65 +Female,157,103,5,25 +Female,169,98,4,80 +Female,152,114,5,30 +Female,162,64,2,67 +Male,162,130,5,57 +Female,177,61,2,48 +Female,195,61,1,56 +Male,140,146,5,69 +Female,186,146,5,79 +Female,178,107,4,51 +Male,174,54,1,18 +Female,180,59,1,26 +Male,188,141,4,51 +Female,187,130,4,18 +Female,153,77,4,75 +Female,165,95,4,51 +Female,178,79,2,79 +Female,163,154,5,28 +Female,150,97,5,77 +Male,179,127,4,20 +Male,165,62,2,58 +Male,168,158,5,67 +Female,153,133,5,28 +Male,184,157,5,24 +Male,188,65,1,64 +Female,166,153,5,33 +Female,172,116,4,22 +Male,182,73,2,74 +Male,143,149,5,54 +Male,152,146,5,36 +Female,186,128,4,26 +Male,159,140,5,70 +Male,146,70,4,54 +Female,176,121,4,35 +Female,146,101,5,49 +Male,159,145,5,31 +Male,162,157,5,30 +Female,172,90,4,78 +Female,169,121,5,54 +Male,182,50,0,36 +Female,183,79,2,41 +Male,176,77,2,60 +Female,188,128,4,53 +Female,175,83,2,41 +Male,154,81,4,28 +Female,184,147,5,43 +Male,179,123,4,21 +Male,152,132,5,49 +Male,179,56,1,49 +Female,145,141,5,77 +Female,181,80,2,20 +Male,158,127,5,30 +Female,188,99,3,76 +Male,145,142,5,61 +Male,161,115,5,39 +Male,198,109,3,69 +Male,147,142,5,47 +Male,154,112,5,21 +Female,178,65,2,70 +Male,195,153,5,43 +Female,167,79,3,54 +Male,183,131,4,73 +Female,164,142,5,46 +Male,167,64,2,33 +Female,151,55,2,36 +Female,147,107,5,68 +Female,155,115,5,47 +Female,172,108,4,68 +Female,142,86,5,41 +Male,146,85,4,38 +Female,188,115,4,25 +Male,173,111,4,21 +Female,160,109,5,30 +Male,187,80,2,75 +Male,198,136,4,78 +Female,179,150,5,36 +Female,164,59,2,62 +Female,146,147,5,37 +Female,198,50,0,56 +Female,170,53,1,59 +Male,152,98,5,53 +Female,150,153,5,59 +Female,184,121,4,37 +Female,141,136,5,52 +Male,150,95,5,28 +Male,173,131,5,66