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
@@ -139,15 +139,14 @@ In fact, a package is just a directory containing
139
139
1. possibly some compiled code that can be accessed by Python (e.g., functions compiled from C or FORTRAN code)
140
140
1. a file called `__init__.py` that specifies what will be executed when we type `import package_name`
141
141
142
-
In fact, you can find and explore the directory for NumPy on your computer
143
-
easily enough if you look around.
144
-
145
-
On this machine, it's located in
142
+
You can check the location of your `__init__.py` for NumPy in python by running the code:
146
143
147
144
```{code-block} ipython
148
145
:class: no-execute
149
146
150
-
anaconda3/lib/python3.7/site-packages/numpy
147
+
import numpy as np
148
+
149
+
print(np.__file__)
151
150
```
152
151
153
152
#### Subpackages
@@ -159,7 +158,9 @@ Consider the line `ϵ_values = np.random.randn(100)`.
159
158
160
159
Here `np` refers to the package NumPy, while `random` is a **subpackage** of NumPy.
161
160
162
-
Subpackages are just packages that are subdirectories of another package.
161
+
Subpackages are just packages that are subdirectories of another package.
162
+
163
+
For instance, you can find folder `random` under the directory of NumPy.
163
164
164
165
### Importing Names Directly
165
166
@@ -208,7 +209,7 @@ We can and will look at various ways to configure and improve this plot below.
208
209
209
210
## Alternative Implementations
210
211
211
-
Let's try writing some alternative versions of {ref}`our first program <ourfirstprog>`, which plotted IID draws from the normal distribution.
212
+
Let's try writing some alternative versions of {ref}`our first program <ourfirstprog>`, which plotted IID draws from the standard normal distribution.
212
213
213
214
The programs below are less efficient than the original one, and hence
214
215
somewhat artificial.
@@ -250,7 +251,9 @@ Let's study some parts of this program in more detail.
250
251
251
252
Consider the statement `ϵ_values = []`, which creates an empty list.
252
253
253
-
Lists are a *native Python data structure* used to group a collection of objects.
254
+
Lists are a *native Python data structure* used to group a collection of objects.
255
+
256
+
Items in lists are ordered, and duplicates are allowed in lists.
254
257
255
258
For example, try
256
259
@@ -259,7 +262,7 @@ x = [10, 'foo', False]
259
262
type(x)
260
263
```
261
264
262
-
The first element of `x` is an [integer](https://en.wikipedia.org/wiki/Integer_%28computer_science%29), the next is a [string](https://en.wikipedia.org/wiki/String_%28computer_science%29), and the third is a [Boolean value](https://en.wikipedia.org/wiki/Boolean_data_type).
265
+
The first element of `x` is an [integer](https://en.wikipedia.org/wiki/Integer_(computer_science)), the next is a [string](https://en.wikipedia.org/wiki/String_(computer_science)), and the third is a [Boolean value](https://en.wikipedia.org/wiki/Boolean_data_type).
263
266
264
267
When adding a value to a list, we can use the syntax `list_name.append(some_value)`
265
268
@@ -274,7 +277,7 @@ x
274
277
275
278
Here `append()` is what's called a *method*, which is a function "attached to" an object---in this case, the list `x`.
276
279
277
-
We'll learn all about methods later on, but just to give you some idea,
280
+
We'll learn all about methods {doc}`later on <oop_intro>`, but just to give you some idea,
278
281
279
282
* Python objects such as lists, strings, etc. all have methods that are used to manipulate the data contained in the object.
280
283
* String objects have [string methods](https://docs.python.org/3/library/stdtypes.html#string-methods), list objects have [list methods](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists), etc.
@@ -332,7 +335,7 @@ for animal in animals:
332
335
print("The plural of " + animal + " is " + animal + "s")
333
336
```
334
337
335
-
This example helps to clarify how the `for` loop works: When we execute a
338
+
This example helps to clarify how the `for` loop works: When we execute a
336
339
loop of the form
337
340
338
341
```{code-block} python3
@@ -397,10 +400,18 @@ plt.plot(ϵ_values)
397
400
plt.show()
398
401
```
399
402
403
+
A while loop will keep executing the code block delimited by indentation until the condition (```i < ts_length```) is satisfied.
404
+
405
+
In this case, the program will keep adding values to the list ```ϵ_values``` until ```i``` equals ```ts_length```:
406
+
407
+
```{code-cell} python3
408
+
i == ts_length #the ending condition for the while loop
409
+
```
410
+
400
411
Note that
401
412
402
-
* the code block for the `while` loop is again delimited only by indentation
403
-
* the statement `i = i + 1` can be replaced by `i += 1`
413
+
* the code block for the `while` loop is again delimited only by indentation.
414
+
* the statement `i = i + 1` can be replaced by `i += 1`.
404
415
405
416
## Another Application
406
417
@@ -478,8 +489,30 @@ Set $T=200$ and $\alpha = 0.9$.
478
489
```{exercise-end}
479
490
```
480
491
492
+
```{solution-start} pbe_ex1
493
+
:class: dropdown
494
+
```
481
495
482
-
```{exercise}
496
+
Here's one solution.
497
+
498
+
```{code-cell} python3
499
+
α = 0.9
500
+
T = 200
501
+
x = np.empty(T+1)
502
+
x[0] = 0
503
+
504
+
for t in range(T):
505
+
x[t+1] = α * x[t] + np.random.randn()
506
+
507
+
plt.plot(x)
508
+
plt.show()
509
+
```
510
+
511
+
```{solution-end}
512
+
```
513
+
514
+
515
+
```{exercise-start}
483
516
:label: pbe_ex2
484
517
485
518
Starting with your solution to exercise 1, plot three simulated time series,
@@ -495,87 +528,63 @@ Hints:
495
528
* For the legend, noted that the expression `'foo' + str(42)` evaluates to `'foo42'`.
496
529
```
497
530
498
-
```{exercise}
499
-
:label: pbe_ex3
500
-
501
-
Similar to the previous exercises, plot the time series
502
-
503
-
$$
504
-
x_{t+1} = \alpha \, |x_t| + \epsilon_{t+1}
505
-
\quad \text{where} \quad
506
-
x_0 = 0
507
-
\quad \text{and} \quad t = 0,\ldots,T
508
-
$$
509
-
510
-
Use $T=200$, $\alpha = 0.9$ and $\{\epsilon_t\}$ as before.
511
-
512
-
Search online for a function that can be used to compute the absolute value $|x_t|$.
531
+
```{exercise-end}
513
532
```
514
533
515
534
516
-
```{exercise-start}
517
-
:label: pbe_ex4
535
+
```{solution-start} pbe_ex2
536
+
:class: dropdown
518
537
```
519
538
520
-
One important aspect of essentially all programming languages is branching and
521
-
conditions.
522
-
523
-
In Python, conditions are usually implemented with if--else syntax.
539
+
```{code-cell} python3
540
+
α_values = [0.0, 0.8, 0.98]
541
+
T = 200
542
+
x = np.empty(T+1)
524
543
525
-
Here's an example, that prints -1 for each negative number in an array and 1
526
-
for each nonnegative number
544
+
for α in α_values:
545
+
x[0] = 0
546
+
for t in range(T):
547
+
x[t+1] = α * x[t] + np.random.randn()
548
+
plt.plot(x, label=f'$\\alpha = {α}$')
527
549
528
-
```{code-cell} python3
529
-
numbers = [-9, 2.3, -11, 0]
550
+
plt.legend()
551
+
plt.show()
530
552
```
531
553
532
-
```{code-cell} python3
533
-
for x in numbers:
534
-
if x < 0:
535
-
print(-1)
536
-
else:
537
-
print(1)
538
-
```
554
+
Note: `f'$\\alpha = {α}$'` in the solution is an application of [f-String](https://docs.python.org/3/tutorial/inputoutput.html#tut-f-strings), which allows you to use `{}` to contain an expression.
539
555
540
-
Now, write a new solution to Exercise 3 that does not use an existing function
541
-
to compute the absolute value.
556
+
The contained expression will be evaluated, and the result will be placed into the string.
542
557
543
-
Replace this existing function with an if--else condition.
544
558
545
-
```{exercise-end}
559
+
```{solution-end}
546
560
```
547
561
548
-
549
562
```{exercise-start}
550
-
:label: pbe_ex5
551
-
```
563
+
:label: pbe_ex3
552
564
553
-
Here's a harder exercise, that takes some thought and planning.
565
+
Similar to the previous exercises, plot the time series
554
566
555
-
The task is to compute an approximation to $\pi$ using [Monte Carlo](https://en.wikipedia.org/wiki/Monte_Carlo_method).
567
+
$$
568
+
x_{t+1} = \alpha \, |x_t| + \epsilon_{t+1}
569
+
\quad \text{where} \quad
570
+
x_0 = 0
571
+
\quad \text{and} \quad t = 0,\ldots,T
572
+
$$
556
573
557
-
Use no imports besides
574
+
Use $T=200$, $\alpha = 0.9$ and $\{\epsilon_t\}$ as before.
558
575
559
-
```{code-cell} python3
560
-
import numpy as np
576
+
Search online for a function that can be used to compute the absolute value $|x_t|$.
561
577
```
562
578
563
-
Your hints are as follows:
564
-
565
-
* If $U$ is a bivariate uniform random variable on the unit square $(0, 1)^2$, then the probability that $U$ lies in a subset $B$ of $(0,1)^2$ is equal to the area of $B$.
566
-
* If $U_1,\ldots,U_n$ are IID copies of $U$, then, as $n$ gets large, the fraction that falls in $B$, converges to the probability of landing in $B$.
567
-
* For a circle, $area = \pi * radius^2$.
568
-
569
579
```{exercise-end}
570
580
```
571
581
572
-
## Solutions
573
582
574
-
```{solution-start}pbe_ex1
583
+
```{solution-start}pbe_ex3
575
584
:class: dropdown
576
585
```
577
586
578
-
Here's one solution.
587
+
Here's one solution:
579
588
580
589
```{code-cell} python3
581
590
α = 0.9
@@ -584,7 +593,7 @@ x = np.empty(T+1)
584
593
x[0] = 0
585
594
586
595
for t in range(T):
587
-
x[t+1] = α * x[t] + np.random.randn()
596
+
x[t+1] = α * np.abs(x[t]) + np.random.randn()
588
597
589
598
plt.plot(x)
590
599
plt.show()
@@ -594,52 +603,38 @@ plt.show()
594
603
```
595
604
596
605
597
-
```{solution-start} pbe_ex2
598
-
:class: dropdown
606
+
```{exercise-start}
607
+
:label: pbe_ex4
599
608
```
600
609
601
-
```{code-cell} python3
602
-
α_values = [0.0, 0.8, 0.98]
603
-
T = 200
604
-
x = np.empty(T+1)
605
-
606
-
for α in α_values:
607
-
x[0] = 0
608
-
for t in range(T):
609
-
x[t+1] = α * x[t] + np.random.randn()
610
-
plt.plot(x, label=f'$\\alpha = {α}$')
611
-
612
-
plt.legend()
613
-
plt.show()
614
-
```
610
+
One important aspect of essentially all programming languages is branching and
611
+
conditions.
615
612
616
-
```{solution-end}
617
-
```
613
+
In Python, conditions are usually implemented with if--else syntax.
618
614
615
+
Here's an example, that prints -1 for each negative number in an array and 1
616
+
for each nonnegative number
619
617
620
-
```{solution-start} pbe_ex3
621
-
:class: dropdown
618
+
```{code-cell} python3
619
+
numbers = [-9, 2.3, -11, 0]
622
620
```
623
621
624
-
Here's one solution:
625
-
626
622
```{code-cell} python3
627
-
α = 0.9
628
-
T = 200
629
-
x = np.empty(T+1)
630
-
x[0] = 0
623
+
for x in numbers:
624
+
if x < 0:
625
+
print(-1)
626
+
else:
627
+
print(1)
628
+
```
631
629
632
-
for t in range(T):
633
-
x[t+1] = α * np.abs(x[t]) + np.random.randn()
630
+
Now, write a new solution to Exercise 3 that does not use an existing function
631
+
to compute the absolute value.
634
632
635
-
plt.plot(x)
636
-
plt.show()
637
-
```
633
+
Replace this existing function with an if--else condition.
638
634
639
-
```{solution-end}
635
+
```{exercise-end}
640
636
```
641
637
642
-
643
638
```{solution-start} pbe_ex4
644
639
:class: dropdown
645
640
```
@@ -683,6 +678,31 @@ plt.show()
683
678
```
684
679
685
680
681
+
682
+
```{exercise-start}
683
+
:label: pbe_ex5
684
+
```
685
+
686
+
Here's a harder exercise, that takes some thought and planning.
687
+
688
+
The task is to compute an approximation to $\pi$ using [Monte Carlo](https://en.wikipedia.org/wiki/Monte_Carlo_method).
689
+
690
+
Use no imports besides
691
+
692
+
```{code-cell} python3
693
+
import numpy as np
694
+
```
695
+
696
+
Your hints are as follows:
697
+
698
+
* If $U$ is a bivariate uniform random variable on the unit square $(0, 1)^2$, then the probability that $U$ lies in a subset $B$ of $(0,1)^2$ is equal to the area of $B$.
699
+
* If $U_1,\ldots,U_n$ are IID copies of $U$, then, as $n$ gets large, the fraction that falls in $B$, converges to the probability of landing in $B$.
700
+
* For a circle, $area = \pi * radius^2$.
701
+
702
+
```{exercise-end}
703
+
```
704
+
705
+
686
706
```{solution-start} pbe_ex5
687
707
:class: dropdown
688
708
```
@@ -704,12 +724,20 @@ We estimate the area by sampling bivariate uniforms and looking at the
704
724
fraction that falls into the circle.
705
725
706
726
```{code-cell} python3
707
-
n = 100000
727
+
n = 1000000 # sample size for Monte Carlo simulation
708
728
709
729
count = 0
710
730
for i in range(n):
731
+
732
+
# drawing random positions on the square
711
733
u, v = np.random.uniform(), np.random.uniform()
734
+
735
+
# check whether the point falls within the boundary
0 commit comments