Skip to content

Commit fb75a07

Browse files
[scipy.md] Update np.random → Generator API (#547)
* Update scipy to new API * Reuse main-text rng in Scipy lecture
1 parent 06af1a9 commit fb75a07

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

lectures/scipy.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@ The `scipy.stats` subpackage supplies
104104

105105
### Random Variables and Distributions
106106

107-
Recall that `numpy.random` provides functions for generating random variables
107+
Recall that `numpy.random` provides tools for generating random variables
108108

109109
```{code-cell} python3
110-
np.random.beta(5, 5, size=3)
110+
rng = np.random.default_rng()
111+
rng.beta(5, 5, size=3)
111112
```
112113

113114
This generates a draw from the distribution with the density function below when `a, b = 5, 5`
@@ -188,8 +189,8 @@ For example, `scipy.stats.linregress` implements simple linear regression
188189
```{code-cell} python3
189190
from scipy.stats import linregress
190191
191-
x = np.random.randn(200)
192-
y = 2 * x + 0.1 * np.random.randn(200)
192+
x = rng.standard_normal(200)
193+
y = 2 * x + 0.1 * rng.standard_normal(200)
193194
gradient, intercept, r_value, p_value, std_err = linregress(x, y)
194195
gradient, intercept
195196
```
@@ -572,8 +573,9 @@ Set `M = 10_000_000`
572573
Here is one solution:
573574

574575
```{code-cell} ipython3
576+
rng = np.random.default_rng()
575577
M = 10_000_000
576-
S = np.exp(μ + σ * np.random.randn(M))
578+
S = np.exp(μ + σ * rng.standard_normal(M))
577579
return_draws = np.maximum(S - K, 0)
578580
P = β**n * np.mean(return_draws)
579581
print(f"The Monte Carlo option price is {P:3f}")

0 commit comments

Comments
 (0)