Skip to content

Commit b22495a

Browse files
committed
made student notebooks up to 2a
1 parent 0436629 commit b22495a

10 files changed

+2196
-15
lines changed

notebooks/01a-student-probability-simulation.ipynb

Lines changed: 1249 additions & 0 deletions
Large diffs are not rendered by default.

notebooks/01b-Instructor-Probability_a_simulated_introduction.ipynb renamed to notebooks/01b-instructor-joint-conditional-probability.ipynb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@
110110
"# Solution: Calculate P(A,B)\n",
111111
"x_0 = np.random.binomial(2, 0.5, 10000)\n",
112112
"p_ab = sum(x_0==2)/len(x_0)\n",
113+
"\n",
114+
"# Now, plot the histogram of the results\n",
113115
"plt.hist(x_0);\n",
114116
"print(p_ab)"
115117
]

notebooks/01b-student-joint-conditional-probability.ipynb

Lines changed: 577 additions & 0 deletions
Large diffs are not rendered by default.

notebooks/02a-Instructor-Parameter_estimation_hypothesis_testing.ipynb renamed to notebooks/02a-instructor-parameter-estimation.ipynb

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,25 @@
129129
"def plot_posterior(p=0.6, N=0):\n",
130130
" \"\"\"Plot the posterior given a uniform prior; Bernoulli trials\n",
131131
" with probability p; sample size N\"\"\"\n",
132+
" # Set seed\n",
132133
" np.random.seed(42)\n",
134+
"\n",
133135
" # Flip coins \n",
134136
" n_successes = np.random.binomial(N, p)\n",
137+
" \n",
135138
" # X-axis for PDF\n",
136139
" x = np.linspace(0, 1, 100)\n",
137-
" # Prior\n",
140+
" \n",
141+
" # Write out equation for uniform prior\n",
138142
" prior = 1\n",
139-
" # Compute posterior, given the likelihood (analytic form)\n",
140-
" posterior = x**n_successes*(1-x)**(N-n_successes)*prior\n",
141-
" posterior /= np.max(posterior) # so that peak always at 1\n",
143+
" \n",
144+
" # Write out equation for posterior, which is likelihood * prior.\n",
145+
" posterior = (x**n_successes) * ((1-x)**(N-n_successes)) * prior\n",
146+
" \n",
147+
" # Pseudo-normalize the posterior so that we can compare them on the same scale.\n",
148+
" posterior /= np.max(posterior) \n",
149+
" \n",
150+
" # Plot posterior\n",
142151
" plt.plot(x, posterior)\n",
143152
" plt.show()"
144153
]
@@ -245,29 +254,37 @@
245254
},
246255
{
247256
"cell_type": "code",
248-
"execution_count": 5,
257+
"execution_count": 7,
249258
"metadata": {},
250259
"outputs": [],
251260
"source": [
252-
"# Solution\n",
261+
"# Write the plotting function, as above\n",
253262
"def plot_posteriors(p=0.6, N=0):\n",
254263
" np.random.seed(42)\n",
255264
" n_successes = np.random.binomial(N, p)\n",
256265
" x = np.linspace(0.01, 0.99, 100)\n",
257-
" posterior1 = x**n_successes*(1-x)**(N-n_successes) # w/ uniform prior\n",
258-
" posterior1 /= np.max(posterior1) # so that peak always at 1\n",
259-
" plt.plot(x, posterior1, label='Uniform prior')\n",
260-
" jp = np.sqrt(x*(1-x))**(-1) # Jeffreys prior\n",
261-
" posterior2 = posterior1*jp # w/ Jeffreys prior\n",
262-
" posterior2 /= np.max(posterior2) # so that peak always at 1 (not quite correct to do; see below)\n",
263-
" plt.plot(x, posterior2, label='Jeffreys prior')\n",
266+
"\n",
267+
" # Write out the likelihood for the data\n",
268+
" likelihood = x**n_successes*(1-x)**(N-n_successes) \n",
269+
" \n",
270+
" # Write out equation for posterior given uniform prior\n",
271+
" prior_uniform = 1 \n",
272+
" posterior_uniform = likelihood * prior_uniform\n",
273+
" posterior_uniform /= np.max(posterior_uniform)\n",
274+
" plt.plot(x, posterior_uniform, label='Uniform prior')\n",
275+
" \n",
276+
" # Write out equation for posterior given Jeffreys prior\n",
277+
" prior_jeffreys = np.sqrt(x*(1-x))**(-1)\n",
278+
" posterior_jeffreys = likelihood * prior_jeffreys\n",
279+
" posterior_jeffreys /= np.max(posterior_jeffreys)\n",
280+
" plt.plot(x, posterior_jeffreys, label='Jeffreys prior')\n",
264281
" plt.legend()\n",
265282
" plt.show()"
266283
]
267284
},
268285
{
269286
"cell_type": "code",
270-
"execution_count": 6,
287+
"execution_count": 8,
271288
"metadata": {},
272289
"outputs": [
273290
{
@@ -286,6 +303,13 @@
286303
"source": [
287304
"interact(plot_posteriors, p=(0, 1, 0.01), N=(0, 100));"
288305
]
306+
},
307+
{
308+
"cell_type": "code",
309+
"execution_count": null,
310+
"metadata": {},
311+
"outputs": [],
312+
"source": []
289313
}
290314
],
291315
"metadata": {

notebooks/02a-student-parameter-estimation.ipynb

Lines changed: 329 additions & 0 deletions
Large diffs are not rendered by default.

notebooks/01-Student-Probability_a_simulated_introduction.ipynb renamed to notebooks/archive/01-Student-Probability_a_simulated_introduction.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@
13211321
"name": "python",
13221322
"nbconvert_exporter": "python",
13231323
"pygments_lexer": "ipython3",
1324-
"version": "3.7.3"
1324+
"version": "3.7.2"
13251325
}
13261326
},
13271327
"nbformat": 4,

0 commit comments

Comments
 (0)