You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now, there are several ways that we can simplify the code above.
360
362
361
-
For example, we can get rid of the conditionals all together by just passing the desired generator type *as a function*.
363
+
For example, we can get rid of the conditionals all together by just passing the desired generator type as a function, method, or other [callable](https://typing.python.org/en/latest/spec/callables.html) object.
362
364
363
365
To understand this, consider the following version.
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`
380
382
as the second argument.
381
383
382
-
This object is a *function*.
384
+
This object is a *callable* — that is, an object that can be called using parentheses.
383
385
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 callable `rng.uniform`.
385
387
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.
387
389
388
390
This principle works more generally---for example, consider the following piece of code
389
391
@@ -399,9 +401,7 @@ m(7, 2, 4)
399
401
Here we created another name for the built-in function `max()`, which could
400
402
then be used in identical ways.
401
403
402
-
In the context of our program, the ability to bind new names to functions
403
-
means that there is no problem *passing a function as an argument to another
404
-
function*---as we did above.
404
+
In the context of our program, the ability to bind names to functions, or more generally to callable objects, means that there is no problem passing one callable object as an argument to another callable --- as we did with `rng.uniform` above.
405
405
406
406
407
407
(recursive_functions)=
@@ -507,7 +507,7 @@ factorial(4)
507
507
508
508
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$.
509
509
510
-
Without any import besides `from numpy.random import uniform`, write a function
510
+
Using `rng = np.random.default_rng()`, write a function
511
511
`binomial_rv` such that `binomial_rv(n, p)` generates one draw of $Y$.
512
512
513
513
```{hint}
@@ -527,12 +527,12 @@ If $U$ is uniform on $(0, 1)$ and $p \in (0,1)$, then the expression `U < p` eva
527
527
Here is one solution:
528
528
529
529
```{code-cell} python3
530
-
from numpy.random import uniform
530
+
rng = np.random.default_rng()
531
531
532
532
def binomial_rv(n, p):
533
533
count = 0
534
534
for i in range(n):
535
-
U = uniform()
535
+
U = rng.uniform()
536
536
if U < p:
537
537
count = count + 1 # Or count += 1
538
538
return count
@@ -558,7 +558,7 @@ Second, write another function that does the same task except that the second ru
558
558
559
559
- If a head occurs `k` or more times within this sequence, pay one dollar.
560
560
561
-
Use no import besides `from numpy.random import uniform`.
561
+
Use `rng = np.random.default_rng()` to generate random numbers.
562
562
563
563
```{exercise-end}
564
564
```
@@ -573,15 +573,15 @@ Here's a function for the first random device.
573
573
574
574
575
575
```{code-cell} python3
576
-
from numpy.random import uniform
576
+
rng = np.random.default_rng()
577
577
578
578
def draw(k): # pays if k consecutive successes in a sequence
579
579
580
580
payoff = 0
581
581
count = 0
582
582
583
583
for i in range(10):
584
-
U = uniform()
584
+
U = rng.uniform()
585
585
count = count + 1 if U < 0.5 else 0
586
586
print(count) # print counts for clarity
587
587
if count == k:
@@ -601,7 +601,7 @@ def draw_new(k): # pays if k successes in a sequence
0 commit comments