Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[prob_dist] Update suggestions #519

Merged
merged 5 commits into from
Jul 23, 2024
Merged
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
62 changes: 50 additions & 12 deletions lectures/prob_dist.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ S = np.arange(1, n+1)
ax.plot(S, u.pmf(S), linestyle='', marker='o', alpha=0.8, ms=4)
ax.vlines(S, 0, u.pmf(S), lw=0.2)
ax.set_xticks(S)
ax.set_xlabel('S')
ax.set_ylabel('PMF')
plt.show()
```

Expand All @@ -136,6 +138,8 @@ S = np.arange(1, n+1)
ax.step(S, u.cdf(S))
ax.vlines(S, 0, u.cdf(S), lw=0.2)
ax.set_xticks(S)
ax.set_xlabel('S')
ax.set_ylabel('CDF')
plt.show()
```

Expand Down Expand Up @@ -232,6 +236,8 @@ S = np.arange(1, n+1)
ax.plot(S, u.pmf(S), linestyle='', marker='o', alpha=0.8, ms=4)
ax.vlines(S, 0, u.pmf(S), lw=0.2)
ax.set_xticks(S)
ax.set_xlabel('S')
ax.set_ylabel('PMF')
plt.show()
```

Expand All @@ -244,6 +250,8 @@ S = np.arange(1, n+1)
ax.step(S, u.cdf(S))
ax.vlines(S, 0, u.cdf(S), lw=0.2)
ax.set_xticks(S)
ax.set_xlabel('S')
ax.set_ylabel('CDF')
plt.show()
```

Expand All @@ -267,6 +275,8 @@ u_sum = np.cumsum(u.pmf(S))
ax.step(S, u_sum)
ax.vlines(S, 0, u_sum, lw=0.2)
ax.set_xticks(S)
ax.set_xlabel('S')
ax.set_ylabel('CDF')
plt.show()
```

Expand All @@ -289,21 +299,13 @@ The mean and variance are:
```{code-cell} ipython3
λ = 2
u = scipy.stats.poisson(λ)
```

```{code-cell} ipython3
u.mean(), u.var()
```

The the expectation of Poisson distribution is $\lambda$ and the variance is also $\lambda$.
The expectation of the Poisson distribution is $\lambda$ and the variance is also $\lambda$.

Here's the PMF:

```{code-cell} ipython3
λ = 2
u = scipy.stats.poisson(λ)
```

```{code-cell} ipython3
u.pmf(1)
```
Expand All @@ -314,6 +316,8 @@ S = np.arange(1, n+1)
ax.plot(S, u.pmf(S), linestyle='', marker='o', alpha=0.8, ms=4)
ax.vlines(S, 0, u.pmf(S), lw=0.2)
ax.set_xticks(S)
ax.set_xlabel('S')
ax.set_ylabel('PMF')
plt.show()
```

Expand Down Expand Up @@ -386,7 +390,8 @@ for μ, σ in zip(μ_vals, σ_vals):
ax.plot(x_grid, u.pdf(x_grid),
alpha=0.5, lw=2,
label=f'$\mu={μ}, \sigma={σ}$')

ax.set_xlabel('x')
ax.set_ylabel('PDF')
plt.legend()
plt.show()
```
Expand All @@ -402,6 +407,8 @@ for μ, σ in zip(μ_vals, σ_vals):
alpha=0.5, lw=2,
label=f'$\mu={μ}, \sigma={σ}$')
ax.set_ylim(0, 1)
ax.set_xlabel('x')
ax.set_ylabel('CDF')
plt.legend()
plt.show()
```
Expand Down Expand Up @@ -446,7 +453,8 @@ for μ, σ in zip(μ_vals, σ_vals):
ax.plot(x_grid, u.pdf(x_grid),
alpha=0.5, lw=2,
label=f'$\mu={μ}, \sigma={σ}$')

ax.set_xlabel('x')
ax.set_ylabel('PDF')
plt.legend()
plt.show()
```
Expand All @@ -461,6 +469,8 @@ for σ in σ_vals:
label=f'$\mu={μ}, \sigma={σ}$')
ax.set_ylim(0, 1)
ax.set_xlim(0, 3)
ax.set_xlabel('x')
ax.set_ylabel('CDF')
plt.legend()
plt.show()
```
Expand Down Expand Up @@ -500,6 +510,8 @@ for λ in λ_vals:
ax.plot(x_grid, u.pdf(x_grid),
alpha=0.5, lw=2,
label=f'$\lambda={λ}$')
ax.set_xlabel('x')
ax.set_ylabel('PDF')
plt.legend()
plt.show()
```
Expand All @@ -512,6 +524,8 @@ for λ in λ_vals:
alpha=0.5, lw=2,
label=f'$\lambda={λ}$')
ax.set_ylim(0, 1)
ax.set_xlabel('x')
ax.set_ylabel('CDF')
plt.legend()
plt.show()
```
Expand Down Expand Up @@ -557,6 +571,8 @@ for α, β in zip(α_vals, β_vals):
ax.plot(x_grid, u.pdf(x_grid),
alpha=0.5, lw=2,
label=fr'$\alpha={α}, \beta={β}$')
ax.set_xlabel('x')
ax.set_ylabel('PDF')
plt.legend()
plt.show()
```
Expand All @@ -569,6 +585,8 @@ for α, β in zip(α_vals, β_vals):
alpha=0.5, lw=2,
label=fr'$\alpha={α}, \beta={β}$')
ax.set_ylim(0, 1)
ax.set_xlabel('x')
ax.set_ylabel('CDF')
plt.legend()
plt.show()
```
Expand Down Expand Up @@ -614,6 +632,8 @@ for α, β in zip(α_vals, β_vals):
ax.plot(x_grid, u.pdf(x_grid),
alpha=0.5, lw=2,
label=fr'$\alpha={α}, \beta={β}$')
ax.set_xlabel('x')
ax.set_ylabel('PDF')
plt.legend()
plt.show()
```
Expand All @@ -626,6 +646,8 @@ for α, β in zip(α_vals, β_vals):
alpha=0.5, lw=2,
label=fr'$\alpha={α}, \beta={β}$')
ax.set_ylim(0, 1)
ax.set_xlabel('x')
ax.set_ylabel('CDF')
plt.legend()
plt.show()
```
Expand Down Expand Up @@ -720,6 +742,8 @@ We can histogram the income distribution we just constructed as follows
x = df['income']
fig, ax = plt.subplots()
ax.hist(x, bins=5, density=True, histtype='bar')
ax.set_xlabel('income')
ax.set_ylabel('density')
plt.show()
```

Expand Down Expand Up @@ -760,6 +784,8 @@ x_amazon = np.asarray(data)
```{code-cell} ipython3
fig, ax = plt.subplots()
ax.hist(x_amazon, bins=20)
ax.set_xlabel('monthly return (percent change)')
ax.set_ylabel('density')
plt.show()
```

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

Expand All @@ -784,6 +812,8 @@ fig, ax = plt.subplots()
sns.kdeplot(x_amazon, ax=ax, bw_adjust=0.1, alpha=0.5, label="bw=0.1")
sns.kdeplot(x_amazon, ax=ax, bw_adjust=0.5, alpha=0.5, label="bw=0.5")
sns.kdeplot(x_amazon, ax=ax, bw_adjust=1, alpha=0.5, label="bw=1")
ax.set_xlabel('monthly return (percent change)')
ax.set_ylabel('KDE')
plt.legend()
plt.show()
```
Expand All @@ -802,6 +832,8 @@ Yet another way to display an observed distribution is via a violin plot.
```{code-cell} ipython3
fig, ax = plt.subplots()
ax.violinplot(x_amazon)
ax.set_ylabel('monthly return (percent change)')
ax.set_xlabel('KDE')
plt.show()
```

Expand All @@ -822,6 +854,8 @@ x_apple = np.asarray(data)
```{code-cell} ipython3
fig, ax = plt.subplots()
ax.violinplot([x_amazon, x_apple])
ax.set_ylabel('monthly return (percent change)')
ax.set_xlabel('KDE')
plt.show()
```

Expand Down Expand Up @@ -855,6 +889,8 @@ x_grid = np.linspace(-50, 65, 200)
fig, ax = plt.subplots()
ax.plot(x_grid, u.pdf(x_grid))
ax.hist(x_amazon, density=True, bins=40)
ax.set_xlabel('monthly return (percent change)')
ax.set_ylabel('density')
plt.show()
```

Expand Down Expand Up @@ -882,6 +918,8 @@ x_grid = np.linspace(-4, 4, 200)
fig, ax = plt.subplots()
ax.plot(x_grid, u.pdf(x_grid))
ax.hist(x_draws, density=True, bins=40)
ax.set_xlabel('x')
ax.set_ylabel('density')
plt.show()
```

Expand Down