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
Copy file name to clipboardExpand all lines: lectures/python_by_example.md
+98-93Lines changed: 98 additions & 93 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -497,8 +497,30 @@ Set $T=200$ and $\alpha = 0.9$.
497
497
```{exercise-end}
498
498
```
499
499
500
+
```{solution-start} pbe_ex1
501
+
:class: dropdown
502
+
```
503
+
504
+
Here's one solution.
505
+
506
+
```{code-cell} python3
507
+
α = 0.9
508
+
T = 200
509
+
x = np.empty(T+1)
510
+
x[0] = 0
511
+
512
+
for t in range(T):
513
+
x[t+1] = α * x[t] + np.random.randn()
514
+
515
+
plt.plot(x)
516
+
plt.show()
517
+
```
500
518
501
-
```{exercise}
519
+
```{solution-end}
520
+
```
521
+
522
+
523
+
```{exercise-start}
502
524
:label: pbe_ex2
503
525
504
526
Starting with your solution to exercise 1, plot three simulated time series,
@@ -514,87 +536,61 @@ Hints:
514
536
* For the legend, noted that the expression `'foo' + str(42)` evaluates to `'foo42'`.
515
537
```
516
538
517
-
```{exercise}
518
-
:label: pbe_ex3
519
-
520
-
Similar to the previous exercises, plot the time series
521
-
522
-
$$
523
-
x_{t+1} = \alpha \, |x_t| + \epsilon_{t+1}
524
-
\quad \text{where} \quad
525
-
x_0 = 0
526
-
\quad \text{and} \quad t = 0,\ldots,T
527
-
$$
528
-
529
-
Use $T=200$, $\alpha = 0.9$ and $\{\epsilon_t\}$ as before.
530
-
531
-
Search online for a function that can be used to compute the absolute value $|x_t|$.
539
+
```{exercise-end}
532
540
```
533
541
534
542
535
-
```{exercise-start}
536
-
:label: pbe_ex4
543
+
```{solution-start} pbe_ex2
544
+
:class: dropdown
537
545
```
538
546
539
-
One important aspect of essentially all programming languages is branching and
540
-
conditions.
541
-
542
-
In Python, conditions are usually implemented with if--else syntax.
543
-
544
-
Here's an example, that prints -1 for each negative number in an array and 1
545
-
for each nonnegative number
546
-
547
547
```{code-cell} python3
548
-
numbers = [-9, 2.3, -11, 0]
549
-
```
548
+
α_values = [0.0, 0.8, 0.98]
549
+
T = 200
550
+
x = np.empty(T+1)
550
551
551
-
```{code-cell} python3
552
-
for x in numbers:
553
-
if x < 0:
554
-
print(-1)
555
-
else:
556
-
print(1)
552
+
for α in α_values:
553
+
x[0] = 0
554
+
for t in range(T):
555
+
x[t+1] = α * x[t] + np.random.randn()
556
+
plt.plot(x, label=f'$\\alpha = {α}$')
557
+
558
+
plt.legend()
559
+
plt.show()
557
560
```
558
561
559
-
Now, write a new solution to Exercise 3 that does not use an existing function
560
-
to compute the absolute value.
562
+
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. The contained expression will be evaluated, and the result will be placed into the string.
561
563
562
-
Replace this existing function with an if--else condition.
563
564
564
-
```{exercise-end}
565
+
```{solution-end}
565
566
```
566
567
567
-
568
568
```{exercise-start}
569
-
:label: pbe_ex5
570
-
```
569
+
:label: pbe_ex3
571
570
572
-
Here's a harder exercise, that takes some thought and planning.
571
+
Similar to the previous exercises, plot the time series
573
572
574
-
The task is to compute an approximation to $\pi$ using [Monte Carlo](https://en.wikipedia.org/wiki/Monte_Carlo_method).
573
+
$$
574
+
x_{t+1} = \alpha \, |x_t| + \epsilon_{t+1}
575
+
\quad \text{where} \quad
576
+
x_0 = 0
577
+
\quad \text{and} \quad t = 0,\ldots,T
578
+
$$
575
579
576
-
Use no imports besides
580
+
Use $T=200$, $\alpha = 0.9$ and $\{\epsilon_t\}$ as before.
577
581
578
-
```{code-cell} python3
579
-
import numpy as np
582
+
Search online for a function that can be used to compute the absolute value $|x_t|$.
580
583
```
581
584
582
-
Your hints are as follows:
583
-
584
-
* 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$.
585
-
* 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$.
586
-
* For a circle, $area = \pi * radius^2$.
587
-
588
585
```{exercise-end}
589
586
```
590
587
591
-
## Solutions
592
588
593
-
```{solution-start}pbe_ex1
589
+
```{solution-start}pbe_ex3
594
590
:class: dropdown
595
591
```
596
592
597
-
Here's one solution.
593
+
Here's one solution:
598
594
599
595
```{code-cell} python3
600
596
α = 0.9
@@ -603,7 +599,7 @@ x = np.empty(T+1)
603
599
x[0] = 0
604
600
605
601
for t in range(T):
606
-
x[t+1] = α * x[t] + np.random.randn()
602
+
x[t+1] = α * np.abs(x[t]) + np.random.randn()
607
603
608
604
plt.plot(x)
609
605
plt.show()
@@ -613,55 +609,38 @@ plt.show()
613
609
```
614
610
615
611
616
-
```{solution-start} pbe_ex2
617
-
:class: dropdown
618
-
```
619
-
620
-
```{code-cell} python3
621
-
α_values = [0.0, 0.8, 0.98]
622
-
T = 200
623
-
x = np.empty(T+1)
624
-
625
-
for α in α_values:
626
-
x[0] = 0
627
-
for t in range(T):
628
-
x[t+1] = α * x[t] + np.random.randn()
629
-
plt.plot(x, label=f'$\\alpha = {α}$')
630
-
631
-
plt.legend()
632
-
plt.show()
612
+
```{exercise-start}
613
+
:label: pbe_ex4
633
614
```
634
615
635
-
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. The contained expression will be evaluated, and the result will be placed into the string.
636
-
616
+
One important aspect of essentially all programming languages is branching and
617
+
conditions.
637
618
638
-
```{solution-end}
639
-
```
619
+
In Python, conditions are usually implemented with if--else syntax.
640
620
621
+
Here's an example, that prints -1 for each negative number in an array and 1
622
+
for each nonnegative number
641
623
642
-
```{solution-start} pbe_ex3
643
-
:class: dropdown
624
+
```{code-cell} python3
625
+
numbers = [-9, 2.3, -11, 0]
644
626
```
645
627
646
-
Here's one solution:
647
-
648
628
```{code-cell} python3
649
-
α = 0.9
650
-
T = 200
651
-
x = np.empty(T+1)
652
-
x[0] = 0
629
+
for x in numbers:
630
+
if x < 0:
631
+
print(-1)
632
+
else:
633
+
print(1)
634
+
```
653
635
654
-
for t in range(T):
655
-
x[t+1] = α * np.abs(x[t]) + np.random.randn()
636
+
Now, write a new solution to Exercise 3 that does not use an existing function
637
+
to compute the absolute value.
656
638
657
-
plt.plot(x)
658
-
plt.show()
659
-
```
639
+
Replace this existing function with an if--else condition.
660
640
661
-
```{solution-end}
641
+
```{exercise-end}
662
642
```
663
643
664
-
665
644
```{solution-start} pbe_ex4
666
645
:class: dropdown
667
646
```
@@ -705,6 +684,31 @@ plt.show()
705
684
```
706
685
707
686
687
+
688
+
```{exercise-start}
689
+
:label: pbe_ex5
690
+
```
691
+
692
+
Here's a harder exercise, that takes some thought and planning.
693
+
694
+
The task is to compute an approximation to $\pi$ using [Monte Carlo](https://en.wikipedia.org/wiki/Monte_Carlo_method).
695
+
696
+
Use no imports besides
697
+
698
+
```{code-cell} python3
699
+
import numpy as np
700
+
```
701
+
702
+
Your hints are as follows:
703
+
704
+
* 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$.
705
+
* 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$.
706
+
* For a circle, $area = \pi * radius^2$.
707
+
708
+
```{exercise-end}
709
+
```
710
+
711
+
708
712
```{solution-start} pbe_ex5
709
713
:class: dropdown
710
714
```
@@ -726,7 +730,7 @@ We estimate the area by sampling bivariate uniforms and looking at the
726
730
fraction that falls into the circle.
727
731
728
732
```{code-cell} python3
729
-
n = 100000 # sample size for Monte Carlo simulation
733
+
n = 1000000 # sample size for Monte Carlo simulation
0 commit comments