Skip to content
Open
Show file tree
Hide file tree
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
206 changes: 206 additions & 0 deletions .ipynb_checkpoints/Solutions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "e00a11f1",
"metadata": {},
"source": [
"# Lab | Inferential statistics"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "20e95bfe",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import math\n",
"from scipy.stats import ttest_1samp"
]
},
{
"cell_type": "markdown",
"id": "2c813f0a",
"metadata": {},
"source": [
"- It is assumed that the mean systolic blood pressure is `μ = 120 mm Hg`. In the Honolulu Heart Study, a sample of `n = 100` people had an average systolic blood pressure of 130.1 mm Hg with a standard deviation of 21.21 mm Hg. "
]
},
{
"cell_type": "markdown",
"id": "120875d3",
"metadata": {},
"source": [
"## 1. Is the group significantly different (with respect to systolic blood pressure!) from the regular population?"
]
},
{
"cell_type": "markdown",
"id": "d26c43d4",
"metadata": {},
"source": [
"### 1.1 Set up the hypothesis test."
]
},
{
"cell_type": "markdown",
"id": "07ffa509",
"metadata": {},
"source": [
"The null hypothesis is as follows:<br> \n",
"H0 : μ = 120<br>\n",
"The alternative hypothesis (two-tailed) is as follows:<br>\n",
"HA : μ != 120<br>"
]
},
{
"cell_type": "markdown",
"id": "17c9988a",
"metadata": {},
"source": [
"### 1.2 Write down all the steps followed for setting up the test."
]
},
{
"cell_type": "markdown",
"id": "9d420004",
"metadata": {},
"source": [
"- sample mean = 130.1\n",
"- mean population = 120\n",
"- standard deviation sample = 21.21\n",
"- sample size = 100\n",
"<br>\n",
"t = (130.1 - 120) / (21.21 / sqrt100) \n",
"<br>\n",
"- calculate/code the value of the test statistic \n",
"- find the T-critical from the T-table. Use the degrees of freedom (100 in this case) and the alpha level (0.05) to find the T-critical value. For a two-tailed t-test with a 100 dof the critical rejection region equals to t<=-1.984 and t=>1.984. \n",
"- check if the test statistic falls in the rejection region OR:\n",
"- calculate/code the p-value to check if the test statistic is significantly different from the population statistic so the null hypothesis should be rejected (p-value is less than 0.05 or 0.01) "
]
},
{
"cell_type": "markdown",
"id": "63df3700",
"metadata": {},
"source": [
"### 1.3 Calculate the test statistic by hand and also code it in Python. It should be 4.76190. We will take a look at how to make decisions based on this calculated value."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "97f73e20",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4.761904761904759"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t = (130.1-120) / (21.21 / (math.sqrt(100)))\n",
"t"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f25cbd29",
"metadata": {},
"outputs": [],
"source": [
"sample_mean = 130.1\n",
"sample_stdev = 21.21"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "0d01ae23",
"metadata": {},
"outputs": [],
"source": [
"x = np.random.normal(loc = sample_mean, scale = sample_stdev, size = 100)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d23efac6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4.82524640921731 5.07938051235794e-06\n"
]
}
],
"source": [
"popmean = 120\n",
"t_statistic, p_value = ttest_1samp(x, popmean)\n",
"print(t_statistic, p_value)"
]
},
{
"cell_type": "markdown",
"id": "ed8f9213",
"metadata": {},
"source": [
"Its not giving the right/same test statistic. How do i code the distribution in the correct way so the sample mean and sample standard deviation are fixed? "
]
},
{
"cell_type": "markdown",
"id": "4a7564e5",
"metadata": {},
"source": [
"## 2. If you finished the previous question, please go through the code for principal_component_analysis_example provided in the files_for_lab folder ."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.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,
"nbformat_minor": 5
}
129 changes: 115 additions & 14 deletions Solutions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
"# Lab | Inferential statistics"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "20e95bfe",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import math\n",
"from scipy.stats import ttest_1samp"
]
},
{
"cell_type": "markdown",
"id": "2c813f0a",
Expand All @@ -33,12 +45,15 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39c6f717",
"cell_type": "markdown",
"id": "07ffa509",
"metadata": {},
"outputs": [],
"source": []
"source": [
"The null hypothesis is as follows:<br> \n",
"H0 : μ = 120<br>\n",
"The alternative hypothesis (two-tailed) is as follows:<br>\n",
"HA : μ != 120<br>"
]
},
{
"cell_type": "markdown",
Expand All @@ -49,12 +64,22 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b09ce149",
"cell_type": "markdown",
"id": "9d420004",
"metadata": {},
"outputs": [],
"source": []
"source": [
"- sample mean = 130.1\n",
"- mean population = 120\n",
"- standard deviation sample = 21.21\n",
"- sample size = 100\n",
"<br>\n",
"t = (130.1 - 120) / (21.21 / sqrt100) \n",
"<br>\n",
"- calculate/code the value of the test statistic \n",
"- find the T-critical from the T-table. Use the degrees of freedom (100 in this case) and the alpha level (0.05) to find the T-critical value. For a two-tailed t-test with a 100 dof the critical rejection region equals to t<=-1.984 and t=>1.984. \n",
"- check if the test statistic falls in the rejection region OR:\n",
"- calculate/code the p-value to check if the test statistic is significantly different from the population statistic so the null hypothesis should be rejected (p-value is less than 0.05 or 0.01) "
]
},
{
"cell_type": "markdown",
Expand All @@ -66,11 +91,74 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "97f73e20",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4.761904761904759"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t = (130.1-120) / (21.21 / (math.sqrt(100)))\n",
"t"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f25cbd29",
"metadata": {},
"outputs": [],
"source": []
"source": [
"sample_mean = 130.1\n",
"sample_stdev = 21.21"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "0d01ae23",
"metadata": {},
"outputs": [],
"source": [
"x = np.random.normal(loc = sample_mean, scale = sample_stdev, size = 100)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d23efac6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4.82524640921731 5.07938051235794e-06\n"
]
}
],
"source": [
"popmean = 120\n",
"t_statistic, p_value = ttest_1samp(x, popmean)\n",
"print(t_statistic, p_value)"
]
},
{
"cell_type": "markdown",
"id": "ed8f9213",
"metadata": {},
"source": [
"Its not giving the right/same test statistic. How do i code the distribution in the correct way so the sample mean and sample standard deviation are fixed? "
]
},
{
"cell_type": "markdown",
Expand All @@ -83,7 +171,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -97,7 +185,20 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.9.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
Loading