Skip to content

Commit 1e3a89c

Browse files
authored
Merge branch 'main' into patch-2
2 parents 9c6606d + f47a535 commit 1e3a89c

1 file changed

Lines changed: 31 additions & 30 deletions

File tree

lectures/heavy_tails.md

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ mystnb:
252252
caption: Histogram (normal vs bitcoin returns)
253253
name: hist-normal-btc
254254
---
255-
r = np.random.standard_t(df=5, size=1000)
255+
rng = np.random.default_rng()
256+
r = rng.standard_t(df=5, size=1000)
256257
257258
fig, ax = plt.subplots()
258259
ax.hist(r, bins=60, alpha=0.4, label='bitcoin returns', density=True)
@@ -348,7 +349,7 @@ mystnb:
348349
name: draws-normal-cauchy
349350
---
350351
n = 120
351-
np.random.seed(11)
352+
rng = np.random.default_rng(10)
352353
353354
fig, axes = plt.subplots(3, 1, figsize=(6, 12))
354355
@@ -358,14 +359,14 @@ for ax in axes:
358359
s_vals = 2, 12
359360
360361
for ax, s in zip(axes[:2], s_vals):
361-
data = np.random.randn(n) * s
362+
data = rng.standard_normal(n) * s
362363
ax.plot(list(range(n)), data, linestyle='', marker='o', alpha=0.5, ms=4)
363364
ax.vlines(list(range(n)), 0, data, lw=0.2)
364365
ax.set_title(fr"draws from $N(0, \sigma^2)$ with $\sigma = {s}$", fontsize=11)
365366
366367
ax = axes[2]
367368
distribution = cauchy()
368-
data = distribution.rvs(n)
369+
data = distribution.rvs(n, random_state=rng)
369370
ax.plot(list(range(n)), data, linestyle='', marker='o', alpha=0.5, ms=4)
370371
ax.vlines(list(range(n)), 0, data, lw=0.2)
371372
ax.set_title(f"draws from the Cauchy distribution", fontsize=11)
@@ -407,12 +408,12 @@ mystnb:
407408
name: draws-exponential
408409
---
409410
n = 120
410-
np.random.seed(11)
411+
rng = np.random.default_rng(11)
411412
412413
fig, ax = plt.subplots()
413414
ax.set_ylim((0, 50))
414415
415-
data = np.random.exponential(size=n)
416+
data = rng.exponential(size=n)
416417
ax.plot(list(range(n)), data, linestyle='', marker='o', alpha=0.5, ms=4)
417418
ax.vlines(list(range(n)), 0, data, lw=0.2)
418419
@@ -464,11 +465,11 @@ mystnb:
464465
name: draws-pareto
465466
---
466467
n = 120
467-
np.random.seed(11)
468+
rng = np.random.default_rng(11)
468469
469470
fig, ax = plt.subplots()
470471
ax.set_ylim((0, 80))
471-
exponential_data = np.random.exponential(size=n)
472+
exponential_data = rng.exponential(size=n)
472473
pareto_data = np.exp(exponential_data)
473474
ax.plot(list(range(n)), pareto_data, linestyle='', marker='o', alpha=0.5, ms=4)
474475
ax.vlines(list(range(n)), 0, pareto_data, lw=0.2)
@@ -612,13 +613,13 @@ mystnb:
612613
# Parameters and grid
613614
x_grid = np.linspace(1, 1000, 1000)
614615
sample_size = 1000
615-
np.random.seed(13)
616-
z = np.random.randn(sample_size)
616+
rng = np.random.default_rng(13)
617+
z = rng.standard_normal(sample_size)
617618
618619
# Draws
619-
data_exp = np.random.exponential(size=sample_size)
620+
data_exp = rng.exponential(size=sample_size)
620621
data_logn = np.exp(z)
621-
data_pareto = np.exp(np.random.exponential(size=sample_size))
622+
data_pareto = np.exp(rng.exponential(size=sample_size))
622623
623624
data_list = [data_exp, data_logn, data_pareto]
624625
@@ -658,7 +659,7 @@ The [statsmodels](https://www.statsmodels.org/stable/index.html) package provide
658659
If the data is drawn from a normal distribution, the plot would look like:
659660

660661
```{code-cell} ipython3
661-
data_normal = np.random.normal(size=sample_size)
662+
data_normal = rng.normal(size=sample_size)
662663
sm.qqplot(data_normal, line='45')
663664
plt.show()
664665
```
@@ -978,13 +979,13 @@ mystnb:
978979
---
979980
from scipy.stats import cauchy
980981
981-
np.random.seed(1234)
982+
rng = np.random.default_rng(9403)
982983
N = 1_000
983984
984985
distribution = cauchy()
985986
986987
fig, ax = plt.subplots()
987-
data = distribution.rvs(N)
988+
data = distribution.rvs(N, random_state=rng)
988989
989990
# Compute sample mean at each n
990991
sample_mean = np.empty(N)
@@ -1194,13 +1195,13 @@ Since $r \geq \alpha$, we have $\mathbb E X^r = \infty$.
11941195
```{exercise}
11951196
:label: ht_ex3
11961197
1197-
Repeat exercise 1, but replace the three distributions (two normal, one
1198+
Repeat the simulation in {numref}`draws-normal-cauchy`, but replace the three distributions (two normal, one
11981199
Cauchy) with three Pareto distributions using different choices of
11991200
$\alpha$.
12001201
12011202
For $\alpha$, try 1.15, 1.5 and 1.75.
12021203
1203-
Use `np.random.seed(11)` to set the seed.
1204+
Use `rng = np.random.default_rng(11)` to set the seed.
12041205
```
12051206

12061207

@@ -1211,7 +1212,7 @@ Use `np.random.seed(11)` to set the seed.
12111212
```{code-cell} ipython3
12121213
from scipy.stats import pareto
12131214
1214-
np.random.seed(11)
1215+
rng = np.random.default_rng(11)
12151216
12161217
n = 120
12171218
alphas = [1.15, 1.50, 1.75]
@@ -1220,7 +1221,7 @@ fig, axes = plt.subplots(3, 1, figsize=(6, 8))
12201221
12211222
for (a, ax) in zip(alphas, axes):
12221223
ax.set_ylim((-5, 50))
1223-
data = pareto.rvs(size=n, scale=1, b=a)
1224+
data = pareto.rvs(size=n, scale=1, b=a, random_state=rng)
12241225
ax.plot(list(range(n)), data, linestyle='', marker='o', alpha=0.5, ms=4)
12251226
ax.vlines(list(range(n)), 0, data, lw=0.2)
12261227
ax.set_title(f"Pareto draws with $\\alpha = {a}$", fontsize=11)
@@ -1235,7 +1236,7 @@ plt.show()
12351236

12361237

12371238
```{exercise}
1238-
:label: ht_ex5
1239+
:label: ht_ex4
12391240
12401241
There is an ongoing argument about whether the firm size distribution should
12411242
be modeled as a Pareto distribution or a lognormal distribution (see, e.g.,
@@ -1280,7 +1281,7 @@ for each of the two distributions and compare the two samples by
12801281
* producing a [violin plot](https://en.wikipedia.org/wiki/Violin_plot) visualizing the two samples side-by-side and
12811282
* printing the mean and standard deviation of both samples.
12821283
1283-
For the seed use `np.random.seed(1234)`.
1284+
For the seed use `rng = np.random.default_rng(1234)`.
12841285
12851286
What differences do you observe?
12861287
@@ -1289,7 +1290,7 @@ try to track individual firms given the current distribution. We will discuss
12891290
firm dynamics in later lectures.)
12901291
```
12911292

1292-
```{solution-start} ht_ex5
1293+
```{solution-start} ht_ex4
12931294
:class: dropdown
12941295
```
12951296

@@ -1334,9 +1335,9 @@ r = 0.05
13341335
x_bar = 1.0
13351336
α = 1.05
13361337
1337-
def pareto_rvs(n):
1338+
def pareto_rvs(n, rng):
13381339
"Uses a standard method to generate Pareto draws."
1339-
u = np.random.uniform(size=n)
1340+
u = rng.uniform(size=n)
13401341
y = x_bar / (u**(1/α))
13411342
return y
13421343
```
@@ -1353,13 +1354,13 @@ Here's a function to compute a single estimate of tax revenue for a particular
13531354
choice of distribution `dist`.
13541355

13551356
```{code-cell} ipython3
1356-
def tax_rev(dist):
1357+
def tax_rev(dist, rng):
13571358
tax_raised = 0
13581359
for t in range(num_years):
13591360
if dist == 'pareto':
1360-
π = pareto_rvs(num_firms)
1361+
π = pareto_rvs(num_firms, rng)
13611362
else:
1362-
π = np.exp(μ + σ * np.random.randn(num_firms))
1363+
π = np.exp(μ + σ * rng.standard_normal(num_firms))
13631364
tax_raised += β**t * np.sum(π * tax_rate)
13641365
return tax_raised
13651366
```
@@ -1368,14 +1369,14 @@ Now let's generate the violin plot.
13681369

13691370
```{code-cell} ipython3
13701371
num_reps = 100
1371-
np.random.seed(1234)
1372+
rng = np.random.default_rng(1234)
13721373
13731374
tax_rev_lognorm = np.empty(num_reps)
13741375
tax_rev_pareto = np.empty(num_reps)
13751376
13761377
for i in range(num_reps):
1377-
tax_rev_pareto[i] = tax_rev('pareto')
1378-
tax_rev_lognorm[i] = tax_rev('lognorm')
1378+
tax_rev_pareto[i] = tax_rev('pareto', rng)
1379+
tax_rev_lognorm[i] = tax_rev('lognorm', rng)
13791380
13801381
fig, ax = plt.subplots()
13811382

0 commit comments

Comments
 (0)