Skip to content

Commit e809a43

Browse files
longye-tianmmcky
andauthored
[prob_dist] Update suggestions (#519)
* update prob_dist * add x y label * Update lectures/prob_dist.md Co-authored-by: Matt McKay <[email protected]> * update xy label using lower case * Update lectures/prob_dist.md --------- Co-authored-by: Matt McKay <[email protected]>
1 parent 7fa9a60 commit e809a43

File tree

1 file changed

+50
-12
lines changed

1 file changed

+50
-12
lines changed

lectures/prob_dist.md

+50-12
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ S = np.arange(1, n+1)
124124
ax.plot(S, u.pmf(S), linestyle='', marker='o', alpha=0.8, ms=4)
125125
ax.vlines(S, 0, u.pmf(S), lw=0.2)
126126
ax.set_xticks(S)
127+
ax.set_xlabel('S')
128+
ax.set_ylabel('PMF')
127129
plt.show()
128130
```
129131

@@ -136,6 +138,8 @@ S = np.arange(1, n+1)
136138
ax.step(S, u.cdf(S))
137139
ax.vlines(S, 0, u.cdf(S), lw=0.2)
138140
ax.set_xticks(S)
141+
ax.set_xlabel('S')
142+
ax.set_ylabel('CDF')
139143
plt.show()
140144
```
141145

@@ -232,6 +236,8 @@ S = np.arange(1, n+1)
232236
ax.plot(S, u.pmf(S), linestyle='', marker='o', alpha=0.8, ms=4)
233237
ax.vlines(S, 0, u.pmf(S), lw=0.2)
234238
ax.set_xticks(S)
239+
ax.set_xlabel('S')
240+
ax.set_ylabel('PMF')
235241
plt.show()
236242
```
237243

@@ -244,6 +250,8 @@ S = np.arange(1, n+1)
244250
ax.step(S, u.cdf(S))
245251
ax.vlines(S, 0, u.cdf(S), lw=0.2)
246252
ax.set_xticks(S)
253+
ax.set_xlabel('S')
254+
ax.set_ylabel('CDF')
247255
plt.show()
248256
```
249257

@@ -267,6 +275,8 @@ u_sum = np.cumsum(u.pmf(S))
267275
ax.step(S, u_sum)
268276
ax.vlines(S, 0, u_sum, lw=0.2)
269277
ax.set_xticks(S)
278+
ax.set_xlabel('S')
279+
ax.set_ylabel('CDF')
270280
plt.show()
271281
```
272282

@@ -289,21 +299,13 @@ The mean and variance are:
289299
```{code-cell} ipython3
290300
λ = 2
291301
u = scipy.stats.poisson(λ)
292-
```
293-
294-
```{code-cell} ipython3
295302
u.mean(), u.var()
296303
```
297-
298-
The the expectation of Poisson distribution is $\lambda$ and the variance is also $\lambda$.
304+
305+
The expectation of the Poisson distribution is $\lambda$ and the variance is also $\lambda$.
299306

300307
Here's the PMF:
301308

302-
```{code-cell} ipython3
303-
λ = 2
304-
u = scipy.stats.poisson(λ)
305-
```
306-
307309
```{code-cell} ipython3
308310
u.pmf(1)
309311
```
@@ -314,6 +316,8 @@ S = np.arange(1, n+1)
314316
ax.plot(S, u.pmf(S), linestyle='', marker='o', alpha=0.8, ms=4)
315317
ax.vlines(S, 0, u.pmf(S), lw=0.2)
316318
ax.set_xticks(S)
319+
ax.set_xlabel('S')
320+
ax.set_ylabel('PMF')
317321
plt.show()
318322
```
319323

@@ -386,7 +390,8 @@ for μ, σ in zip(μ_vals, σ_vals):
386390
ax.plot(x_grid, u.pdf(x_grid),
387391
alpha=0.5, lw=2,
388392
label=f'$\mu={μ}, \sigma={σ}$')
389-
393+
ax.set_xlabel('x')
394+
ax.set_ylabel('PDF')
390395
plt.legend()
391396
plt.show()
392397
```
@@ -402,6 +407,8 @@ for μ, σ in zip(μ_vals, σ_vals):
402407
alpha=0.5, lw=2,
403408
label=f'$\mu={μ}, \sigma={σ}$')
404409
ax.set_ylim(0, 1)
410+
ax.set_xlabel('x')
411+
ax.set_ylabel('CDF')
405412
plt.legend()
406413
plt.show()
407414
```
@@ -446,7 +453,8 @@ for μ, σ in zip(μ_vals, σ_vals):
446453
ax.plot(x_grid, u.pdf(x_grid),
447454
alpha=0.5, lw=2,
448455
label=f'$\mu={μ}, \sigma={σ}$')
449-
456+
ax.set_xlabel('x')
457+
ax.set_ylabel('PDF')
450458
plt.legend()
451459
plt.show()
452460
```
@@ -461,6 +469,8 @@ for σ in σ_vals:
461469
label=f'$\mu={μ}, \sigma={σ}$')
462470
ax.set_ylim(0, 1)
463471
ax.set_xlim(0, 3)
472+
ax.set_xlabel('x')
473+
ax.set_ylabel('CDF')
464474
plt.legend()
465475
plt.show()
466476
```
@@ -500,6 +510,8 @@ for λ in λ_vals:
500510
ax.plot(x_grid, u.pdf(x_grid),
501511
alpha=0.5, lw=2,
502512
label=f'$\lambda={λ}$')
513+
ax.set_xlabel('x')
514+
ax.set_ylabel('PDF')
503515
plt.legend()
504516
plt.show()
505517
```
@@ -512,6 +524,8 @@ for λ in λ_vals:
512524
alpha=0.5, lw=2,
513525
label=f'$\lambda={λ}$')
514526
ax.set_ylim(0, 1)
527+
ax.set_xlabel('x')
528+
ax.set_ylabel('CDF')
515529
plt.legend()
516530
plt.show()
517531
```
@@ -557,6 +571,8 @@ for α, β in zip(α_vals, β_vals):
557571
ax.plot(x_grid, u.pdf(x_grid),
558572
alpha=0.5, lw=2,
559573
label=fr'$\alpha={α}, \beta={β}$')
574+
ax.set_xlabel('x')
575+
ax.set_ylabel('PDF')
560576
plt.legend()
561577
plt.show()
562578
```
@@ -569,6 +585,8 @@ for α, β in zip(α_vals, β_vals):
569585
alpha=0.5, lw=2,
570586
label=fr'$\alpha={α}, \beta={β}$')
571587
ax.set_ylim(0, 1)
588+
ax.set_xlabel('x')
589+
ax.set_ylabel('CDF')
572590
plt.legend()
573591
plt.show()
574592
```
@@ -614,6 +632,8 @@ for α, β in zip(α_vals, β_vals):
614632
ax.plot(x_grid, u.pdf(x_grid),
615633
alpha=0.5, lw=2,
616634
label=fr'$\alpha={α}, \beta={β}$')
635+
ax.set_xlabel('x')
636+
ax.set_ylabel('PDF')
617637
plt.legend()
618638
plt.show()
619639
```
@@ -626,6 +646,8 @@ for α, β in zip(α_vals, β_vals):
626646
alpha=0.5, lw=2,
627647
label=fr'$\alpha={α}, \beta={β}$')
628648
ax.set_ylim(0, 1)
649+
ax.set_xlabel('x')
650+
ax.set_ylabel('CDF')
629651
plt.legend()
630652
plt.show()
631653
```
@@ -720,6 +742,8 @@ We can histogram the income distribution we just constructed as follows
720742
x = df['income']
721743
fig, ax = plt.subplots()
722744
ax.hist(x, bins=5, density=True, histtype='bar')
745+
ax.set_xlabel('income')
746+
ax.set_ylabel('density')
723747
plt.show()
724748
```
725749

@@ -760,6 +784,8 @@ x_amazon = np.asarray(data)
760784
```{code-cell} ipython3
761785
fig, ax = plt.subplots()
762786
ax.hist(x_amazon, bins=20)
787+
ax.set_xlabel('monthly return (percent change)')
788+
ax.set_ylabel('density')
763789
plt.show()
764790
```
765791

@@ -774,6 +800,8 @@ KDE will generate a smooth curve that approximates the PDF.
774800
```{code-cell} ipython3
775801
fig, ax = plt.subplots()
776802
sns.kdeplot(x_amazon, ax=ax)
803+
ax.set_xlabel('monthly return (percent change)')
804+
ax.set_ylabel('KDE')
777805
plt.show()
778806
```
779807

@@ -784,6 +812,8 @@ fig, ax = plt.subplots()
784812
sns.kdeplot(x_amazon, ax=ax, bw_adjust=0.1, alpha=0.5, label="bw=0.1")
785813
sns.kdeplot(x_amazon, ax=ax, bw_adjust=0.5, alpha=0.5, label="bw=0.5")
786814
sns.kdeplot(x_amazon, ax=ax, bw_adjust=1, alpha=0.5, label="bw=1")
815+
ax.set_xlabel('monthly return (percent change)')
816+
ax.set_ylabel('KDE')
787817
plt.legend()
788818
plt.show()
789819
```
@@ -802,6 +832,8 @@ Yet another way to display an observed distribution is via a violin plot.
802832
```{code-cell} ipython3
803833
fig, ax = plt.subplots()
804834
ax.violinplot(x_amazon)
835+
ax.set_ylabel('monthly return (percent change)')
836+
ax.set_xlabel('KDE')
805837
plt.show()
806838
```
807839

@@ -822,6 +854,8 @@ x_apple = np.asarray(data)
822854
```{code-cell} ipython3
823855
fig, ax = plt.subplots()
824856
ax.violinplot([x_amazon, x_apple])
857+
ax.set_ylabel('monthly return (percent change)')
858+
ax.set_xlabel('KDE')
825859
plt.show()
826860
```
827861

@@ -855,6 +889,8 @@ x_grid = np.linspace(-50, 65, 200)
855889
fig, ax = plt.subplots()
856890
ax.plot(x_grid, u.pdf(x_grid))
857891
ax.hist(x_amazon, density=True, bins=40)
892+
ax.set_xlabel('monthly return (percent change)')
893+
ax.set_ylabel('density')
858894
plt.show()
859895
```
860896

@@ -882,6 +918,8 @@ x_grid = np.linspace(-4, 4, 200)
882918
fig, ax = plt.subplots()
883919
ax.plot(x_grid, u.pdf(x_grid))
884920
ax.hist(x_draws, density=True, bins=40)
921+
ax.set_xlabel('x')
922+
ax.set_ylabel('density')
885923
plt.show()
886924
```
887925

0 commit comments

Comments
 (0)