@@ -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
257258fig, ax = plt.subplots()
258259ax.hist(r, bins=60, alpha=0.4, label='bitcoin returns', density=True)
@@ -348,7 +349,7 @@ mystnb:
348349 name: draws-normal-cauchy
349350---
350351n = 120
351- np.random.seed(11 )
352+ rng = np.random.default_rng(10 )
352353
353354fig, axes = plt.subplots(3, 1, figsize=(6, 12))
354355
@@ -358,14 +359,14 @@ for ax in axes:
358359s_vals = 2, 12
359360
360361for 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
366367ax = axes[2]
367368distribution = cauchy()
368- data = distribution.rvs(n)
369+ data = distribution.rvs(n, random_state=rng )
369370ax.plot(list(range(n)), data, linestyle='', marker='o', alpha=0.5, ms=4)
370371ax.vlines(list(range(n)), 0, data, lw=0.2)
371372ax.set_title(f"draws from the Cauchy distribution", fontsize=11)
@@ -407,12 +408,12 @@ mystnb:
407408 name: draws-exponential
408409---
409410n = 120
410- np.random.seed (11)
411+ rng = np.random.default_rng (11)
411412
412413fig, ax = plt.subplots()
413414ax.set_ylim((0, 50))
414415
415- data = np.random .exponential(size=n)
416+ data = rng .exponential(size=n)
416417ax.plot(list(range(n)), data, linestyle='', marker='o', alpha=0.5, ms=4)
417418ax.vlines(list(range(n)), 0, data, lw=0.2)
418419
@@ -464,11 +465,11 @@ mystnb:
464465 name: draws-pareto
465466---
466467n = 120
467- np.random.seed (11)
468+ rng = np.random.default_rng (11)
468469
469470fig, ax = plt.subplots()
470471ax.set_ylim((0, 80))
471- exponential_data = np.random .exponential(size=n)
472+ exponential_data = rng .exponential(size=n)
472473pareto_data = np.exp(exponential_data)
473474ax.plot(list(range(n)), pareto_data, linestyle='', marker='o', alpha=0.5, ms=4)
474475ax.vlines(list(range(n)), 0, pareto_data, lw=0.2)
@@ -612,13 +613,13 @@ mystnb:
612613# Parameters and grid
613614x_grid = np.linspace(1, 1000, 1000)
614615sample_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)
620621data_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
623624data_list = [data_exp, data_logn, data_pareto]
624625
@@ -658,7 +659,7 @@ The [statsmodels](https://www.statsmodels.org/stable/index.html) package provide
658659If 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)
662663sm.qqplot(data_normal, line='45')
663664plt.show()
664665```
@@ -978,13 +979,13 @@ mystnb:
978979---
979980from scipy.stats import cauchy
980981
981- np.random.seed(1234 )
982+ rng = np.random.default_rng(9403 )
982983N = 1_000
983984
984985distribution = cauchy()
985986
986987fig, ax = plt.subplots()
987- data = distribution.rvs(N)
988+ data = distribution.rvs(N, random_state=rng )
988989
989990# Compute sample mean at each n
990991sample_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
11981199Cauchy) with three Pareto distributions using different choices of
11991200$\alpha$.
12001201
12011202For $\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
12121213from scipy.stats import pareto
12131214
1214- np.random.seed (11)
1215+ rng = np.random.default_rng (11)
12151216
12161217n = 120
12171218alphas = [1.15, 1.50, 1.75]
@@ -1220,7 +1221,7 @@ fig, axes = plt.subplots(3, 1, figsize=(6, 8))
12201221
12211222for (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
12401241There is an ongoing argument about whether the firm size distribution should
12411242be 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
12851286What differences do you observe?
12861287
@@ -1289,7 +1290,7 @@ try to track individual firms given the current distribution. We will discuss
12891290firm 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
13341335x_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
13531354choice 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
13701371num_reps = 100
1371- np.random.seed (1234)
1372+ rng = np.random.default_rng (1234)
13721373
13731374tax_rev_lognorm = np.empty(num_reps)
13741375tax_rev_pareto = np.empty(num_reps)
13751376
13761377for 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
13801381fig, ax = plt.subplots()
13811382
0 commit comments