Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
309 changes: 301 additions & 8 deletions Untitled.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
"cells": [
{
"cell_type": "markdown",
"id": "ee314908",
"metadata": {},
"source": [
"# Lab | Inferential statistics - T-test & P-value"
]
},
{
"cell_type": "markdown",
"id": "515aa25e",
"metadata": {},
"source": [
"## 1. We will have another simple example on two sample t test (pooled- when the variances are equal). But this time this is a one sided t-test\n",
Expand All @@ -21,17 +19,300 @@
"\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"C:\\Users\\Dubun\\IronHack\\Other\\Labs\\lab-t-tests-p-values\\files_for_lab\n"
]
}
],
"source": [
"cd C:\\Users\\Dubun\\IronHack\\Other\\Labs\\lab-t-tests-p-values\\files_for_lab"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import warnings\n",
"warnings.filterwarnings('ignore')"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>New machine</th>\n",
" <th>Old machine</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>42.1</td>\n",
" <td>42.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>41.0</td>\n",
" <td>43.6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>41.3</td>\n",
" <td>43.8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>41.8</td>\n",
" <td>43.3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>42.4</td>\n",
" <td>42.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>42.8</td>\n",
" <td>43.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>43.2</td>\n",
" <td>43.1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>42.3</td>\n",
" <td>41.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>41.8</td>\n",
" <td>44.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>42.7</td>\n",
" <td>44.1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" New machine Old machine\n",
"0 42.1 42.7\n",
"1 41.0 43.6\n",
"2 41.3 43.8\n",
"3 41.8 43.3\n",
"4 42.4 42.5\n",
"5 42.8 43.5\n",
"6 43.2 43.1\n",
"7 42.3 41.7\n",
"8 41.8 44.0\n",
"9 42.7 44.1"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = pd.read_excel('machine.xlsx')\n",
"data"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>New machine</th>\n",
" <th>Old machine</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>count</th>\n",
" <td>10.000000</td>\n",
" <td>10.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>mean</th>\n",
" <td>42.140000</td>\n",
" <td>43.230000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>std</th>\n",
" <td>0.683455</td>\n",
" <td>0.749889</td>\n",
" </tr>\n",
" <tr>\n",
" <th>min</th>\n",
" <td>41.000000</td>\n",
" <td>41.700000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25%</th>\n",
" <td>41.800000</td>\n",
" <td>42.800000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>50%</th>\n",
" <td>42.200000</td>\n",
" <td>43.400000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>75%</th>\n",
" <td>42.625000</td>\n",
" <td>43.750000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>max</th>\n",
" <td>43.200000</td>\n",
" <td>44.100000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" New machine Old machine\n",
"count 10.000000 10.000000\n",
"mean 42.140000 43.230000\n",
"std 0.683455 0.749889\n",
"min 41.000000 41.700000\n",
"25% 41.800000 42.800000\n",
"50% 42.200000 43.400000\n",
"75% 42.625000 43.750000\n",
"max 43.200000 44.100000"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.describe()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"sample_mean_new = 42.14\n",
"sample_std_new = 0.683455\n",
"n1 = 10\n",
"\n",
"sample_mean_old = 43.23\n",
"sample_std_old = 0.749889\n",
"n2 = 10"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"from scipy.stats import ttest_ind, norm\n",
"\n",
"# create the samples\n",
"new = norm.rvs(loc=sample_mean_new, scale=sample_std_new, size=n1)\n",
"old = norm.rvs(loc=sample_mean_old, scale=sample_std_old, size=n2)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Ttest_indResult(statistic=-2.3131146830250087, pvalue=0.03274646800201609)"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ttest_ind(new, old)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c4797fc7",
"metadata": {},
"outputs": [],
"source": []
"source": [
"#We can reject null hypothesis with 95% confidence. \n",
"#Thus, we can say that the new machine is performing better (taking less time) on average compared to the old machine, and this difference is not due to chance."
]
},
{
"cell_type": "markdown",
"id": "501614ae",
"metadata": {},
"source": [
"## 2. An additional problem (not mandatory): In this case we can't assume that the population variances are equal. Hence in this case we cannot pool the variances.\n",
Expand All @@ -46,17 +327,16 @@
{
"cell_type": "code",
"execution_count": null,
"id": "3925f57e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "ironhack",
"display_name": "Python 3",
"language": "python",
"name": "ironhack"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -69,6 +349,19 @@
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
Expand Down