Skip to content

Instantly share code, notes, and snippets.

@onpillow
Created January 25, 2019 09:33
Show Gist options
  • Save onpillow/80364240da8f9ee234925af388adc2fc to your computer and use it in GitHub Desktop.
Save onpillow/80364240da8f9ee234925af388adc2fc to your computer and use it in GitHub Desktop.
for_Medium06_3
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Bootstrap Simulation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Statistic: Mean"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([435, 205, 348, 264, 91, 319, 270, 484, 445, 102, 319, 330, 385,\n",
" 58, 419, 475, 343, 1, 34, 102, 443, 372, 445, 445, 80, 366,\n",
" 419, 149, 264, 330])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# draw a sample from population\n",
"sample = np.random.choice(pickups, size=30)\n",
"sample"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"# bootstrap for mean\n",
"boot_means = []\n",
"for _ in range(10000):\n",
" bootsample = np.random.choice(sample,size=30, replace=True)\n",
" boot_means.append(bootsample.mean())"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"# simulated mean of mean\n",
"bootmean = np.mean(boot_means)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"# simulated standard deviation of mean\n",
"bootmean_std = np.std(boot_means)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"(252.7, 291.44874000000004)"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# simulated mean VS true mean\n",
"(pickups.mean(), bootmean)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"(26.336951228264823, 26.85003412725247)"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# the theorical standard error and simulated standard error\n",
"(pickups.std()/(30 ** 0.5), bootmean_std)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:Anaconda3]",
"language": "python",
"name": "conda-env-Anaconda3-py"
},
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment