Skip to content

Commit d151ab2

Browse files
[monte_carlo.md] Remove rng=None guard; use global rng as default
Per reviewer feedback, remove repetitive `if rng is None` checks and change `rng=None` to `rng=rng` across all 7 functions, following the convention established in lecture-python.myst PR #874. Also reformat `simulate_asset_price_path` signature to one-arg-per-line style (consistent with other multi-argument functions in the file) to fix the 80-character line length violation. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent d594d79 commit d151ab2

1 file changed

Lines changed: 13 additions & 21 deletions

File tree

lectures/monte_carlo.md

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,7 @@ S / n
181181
We can also construct a function that contains these operations:
182182

183183
```{code-cell} ipython3
184-
def compute_mean(n=1_000_000, rng=None):
185-
if rng is None:
186-
rng = np.random.default_rng()
184+
def compute_mean(n=1_000_000, rng=rng):
187185
S = 0.0
188186
for i in range(n):
189187
X_1 = np.exp(μ_1 + σ_1 * rng.standard_normal())
@@ -212,9 +210,7 @@ But the code above runs quite slowly.
212210
To make it faster, let's implement a vectorized routine using NumPy.
213211

214212
```{code-cell} ipython3
215-
def compute_mean_vectorized(n=1_000_000, rng=None):
216-
if rng is None:
217-
rng = np.random.default_rng()
213+
def compute_mean_vectorized(n=1_000_000, rng=rng):
218214
X_1 = np.exp(μ_1 + σ_1 * rng.standard_normal(n))
219215
X_2 = np.exp(μ_2 + σ_2 * rng.standard_normal(n))
220216
X_3 = np.exp(μ_3 + σ_3 * rng.standard_normal(n))
@@ -519,9 +515,13 @@ $$ s_{t+1} = s_t + \mu + \exp(h_t) \xi_{t+1} $$
519515
Here is a function to simulate a path using this equation:
520516

521517
```{code-cell} ipython3
522-
def simulate_asset_price_path(μ=default_μ, S0=default_S0, h0=default_h0, n=default_n, ρ=default_ρ, ν=default_ν, rng=None):
523-
if rng is None:
524-
rng = np.random.default_rng()
518+
def simulate_asset_price_path(μ=default_μ,
519+
S0=default_S0,
520+
h0=default_h0,
521+
n=default_n,
522+
ρ=default_ρ,
523+
ν=default_ν,
524+
rng=rng):
525525
s = np.empty(n+1)
526526
s[0] = np.log(S0)
527527
@@ -583,9 +583,7 @@ def compute_call_price(β=default_β,
583583
ρ=default_ρ,
584584
ν=default_ν,
585585
M=10_000,
586-
rng=None):
587-
if rng is None:
588-
rng = np.random.default_rng()
586+
rng=rng):
589587
current_sum = 0.0
590588
# For each sample path
591589
for m in range(M):
@@ -635,9 +633,7 @@ def compute_call_price_vector(β=default_β,
635633
ρ=default_ρ,
636634
ν=default_ν,
637635
M=10_000,
638-
rng=None):
639-
if rng is None:
640-
rng = np.random.default_rng()
636+
rng=rng):
641637
s = np.full(M, np.log(S0))
642638
h = np.full(M, h0)
643639
for t in range(n):
@@ -709,9 +705,7 @@ def compute_call_price_with_barrier(β=default_β,
709705
ν=default_ν,
710706
bp=default_bp,
711707
M=50_000,
712-
rng=None):
713-
if rng is None:
714-
rng = np.random.default_rng()
708+
rng=rng):
715709
current_sum = 0.0
716710
# For each sample path
717711
for m in range(M):
@@ -755,9 +749,7 @@ def compute_call_price_with_barrier_vector(β=default_β,
755749
ν=default_ν,
756750
bp=default_bp,
757751
M=50_000,
758-
rng=None):
759-
if rng is None:
760-
rng = np.random.default_rng()
752+
rng=rng):
761753
s = np.full(M, np.log(S0))
762754
h = np.full(M, h0)
763755
option_is_null = np.full(M, False)

0 commit comments

Comments
 (0)