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
152 changes: 144 additions & 8 deletions your-code/main.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Libraries"
"import pandas as pd\n",
"import numpy as np\n",
"import scipy.stats as st\n"
]
},
{
Expand All @@ -30,13 +32,96 @@
"**Hint**: function `stats.norm.interval` from `scipy` can help you get through this exercise. "
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"173.46666666666667"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"heights = [167, 167, 168, 168, 168, 169, 171, 172, 173, 175, 175, 175, 177, 182, 195]\n",
"\n",
"heights_sum = sum(heights)\n",
"\n",
"heights_sum/15"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(172.14308590115726, 174.79024743217607)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"heights = [167, 167, 168, 168, 168, 169, 171, 172, 173, 175, 175, 175, 177, 182, 195]\n",
"\n",
"alpha = 0.8\n",
"\n",
"mean = heights_sum/15\n",
"\n",
"std = 4\n",
"\n",
"n = 15\n",
"\n",
"st.norm.interval(0.8, loc=mean, scale = std/np.sqrt(n))"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(170.9117270472475, 176.02160628608584)"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"## same using t-distribution\n",
"\n",
"heights = np.array([167, 167, 168, 168, 168, 169, 171, 172, 173, 175, 175, 175, 177, 182, 195])\n",
"\n",
"s = heights.std(ddof=1)\n",
"mean = heights.mean()\n",
"n = len(heights)\n",
"\n",
"st.t.interval(0.8, n-1, loc = mean, scale = s/np.sqrt(n))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# your code here"
"## t-interval are fatter on the tail, allow more extreme values"
]
},
{
Expand All @@ -51,11 +136,62 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 27,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Our CI with 80% level of confidence for proportion is [0.20248138545542083, 0.3118043288302934]\n"
]
}
],
"source": [
"# your code here"
"sample_size = 105\n",
"losses = 27\n",
"\n",
"p = losses/sample_size\n",
"\n",
"n = 105\n",
"\n",
"# 80% confidence\n",
"\n",
"z_value = st.norm.ppf( 1 - (1 - 0.8)/2)\n",
"\n",
"margin_of_error = z_value * np.sqrt((p * (1- p))/n)\n",
"\n",
"lower_bound = p - margin_of_error\n",
"upper_bound = p + margin_of_error\n",
"\n",
"print(f\"Our CI with 80% level of confidence for proportion is [{lower_bound}, {upper_bound}]\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Our CI with 80% level of confidence for proportion is [0.18698561776452813, 0.3273000965211861]\n"
]
}
],
"source": [
"# 90% confidence\n",
"\n",
"z_value = st.norm.ppf( 1 - (1 - 0.9)/2)\n",
"\n",
"margin_of_error = z_value * np.sqrt((p * (1- p))/n)\n",
"\n",
"lower_bound = p - margin_of_error\n",
"upper_bound = p + margin_of_error\n",
"\n",
"print(f\"Our CI with 80% level of confidence for proportion is [{lower_bound}, {upper_bound}]\")"
]
},
{
Expand Down Expand Up @@ -131,7 +267,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -145,7 +281,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.10.9"
}
},
"nbformat": 4,
Expand Down