Skip to content

Commit 7499474

Browse files
Update functions.md to New API
1 parent 3241a07 commit 7499474

1 file changed

Lines changed: 17 additions & 15 deletions

File tree

lectures/functions.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,13 @@ We will say more about this {doc}`later <writing_good_code>`.
281281
Consider again this code from the {doc}`previous lecture <python_by_example>`
282282

283283
```{code-cell} python3
284+
rng = np.random.default_rng()
285+
284286
ts_length = 100
285287
ϵ_values = [] # empty list
286288
287289
for i in range(ts_length):
288-
e = np.random.randn()
290+
e = rng.standard_normal()
289291
ϵ_values.append(e)
290292
291293
plt.plot(ϵ_values)
@@ -306,7 +308,7 @@ This is accomplished in the next program
306308
def generate_data(n):
307309
ϵ_values = []
308310
for i in range(n):
309-
e = np.random.randn()
311+
e = rng.standard_normal()
310312
ϵ_values.append(e)
311313
return ϵ_values
312314
@@ -336,9 +338,9 @@ def generate_data(n, generator_type):
336338
ϵ_values = []
337339
for i in range(n):
338340
if generator_type == 'U':
339-
e = np.random.uniform(0, 1)
341+
e = rng.uniform(0, 1)
340342
else:
341-
e = np.random.randn()
343+
e = rng.standard_normal()
342344
ϵ_values.append(e)
343345
return ϵ_values
344346
@@ -371,19 +373,19 @@ def generate_data(n, generator_type):
371373
ϵ_values.append(e)
372374
return ϵ_values
373375
374-
data = generate_data(100, np.random.uniform)
376+
data = generate_data(100, rng.uniform)
375377
plt.plot(data)
376378
plt.show()
377379
```
378380

379-
Now, when we call the function `generate_data()`, we pass `np.random.uniform`
381+
Now, when we call the function `generate_data()`, we pass `rng.uniform`
380382
as the second argument.
381383

382384
This object is a *function*.
383385

384-
When the function call `generate_data(100, np.random.uniform)` is executed, Python runs the function code block with `n` equal to 100 and the name `generator_type` "bound" to the function `np.random.uniform`.
386+
When the function call `generate_data(100, rng.uniform)` is executed, Python runs the function code block with `n` equal to 100 and the name `generator_type` "bound" to the function `rng.uniform`.
385387

386-
* While these lines are executed, the names `generator_type` and `np.random.uniform` are "synonyms", and can be used in identical ways.
388+
* While these lines are executed, the names `generator_type` and `rng.uniform` are "synonyms", and can be used in identical ways.
387389

388390
This principle works more generally---for example, consider the following piece of code
389391

@@ -507,7 +509,7 @@ factorial(4)
507509

508510
The [binomial random variable](https://en.wikipedia.org/wiki/Binomial_distribution) $Y \sim Bin(n, p)$ represents the number of successes in $n$ binary trials, where each trial succeeds with probability $p$.
509511

510-
Without any import besides `from numpy.random import uniform`, write a function
512+
Using `rng = np.random.default_rng()`, write a function
511513
`binomial_rv` such that `binomial_rv(n, p)` generates one draw of $Y$.
512514

513515
```{hint}
@@ -527,12 +529,12 @@ If $U$ is uniform on $(0, 1)$ and $p \in (0,1)$, then the expression `U < p` eva
527529
Here is one solution:
528530

529531
```{code-cell} python3
530-
from numpy.random import uniform
532+
rng = np.random.default_rng()
531533
532534
def binomial_rv(n, p):
533535
count = 0
534536
for i in range(n):
535-
U = uniform()
537+
U = rng.uniform()
536538
if U < p:
537539
count = count + 1 # Or count += 1
538540
return count
@@ -558,7 +560,7 @@ Second, write another function that does the same task except that the second ru
558560

559561
- If a head occurs `k` or more times within this sequence, pay one dollar.
560562

561-
Use no import besides `from numpy.random import uniform`.
563+
Use `rng = np.random.default_rng()` to generate random numbers.
562564

563565
```{exercise-end}
564566
```
@@ -573,15 +575,15 @@ Here's a function for the first random device.
573575

574576

575577
```{code-cell} python3
576-
from numpy.random import uniform
578+
rng = np.random.default_rng()
577579
578580
def draw(k): # pays if k consecutive successes in a sequence
579581
580582
payoff = 0
581583
count = 0
582584
583585
for i in range(10):
584-
U = uniform()
586+
U = rng.uniform()
585587
count = count + 1 if U < 0.5 else 0
586588
print(count) # print counts for clarity
587589
if count == k:
@@ -601,7 +603,7 @@ def draw_new(k): # pays if k successes in a sequence
601603
count = 0
602604
603605
for i in range(10):
604-
U = uniform()
606+
U = rng.uniform()
605607
count = count + ( 1 if U < 0.5 else 0 )
606608
print(count)
607609
if count == k:

0 commit comments

Comments
 (0)