Skip to content

Commit 246a7a7

Browse files
committed
put solutions below exercises
1 parent 5a28d48 commit 246a7a7

1 file changed

Lines changed: 98 additions & 93 deletions

File tree

lectures/python_by_example.md

Lines changed: 98 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,30 @@ Set $T=200$ and $\alpha = 0.9$.
497497
```{exercise-end}
498498
```
499499

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+
```
500518

501-
```{exercise}
519+
```{solution-end}
520+
```
521+
522+
523+
```{exercise-start}
502524
:label: pbe_ex2
503525
504526
Starting with your solution to exercise 1, plot three simulated time series,
@@ -514,87 +536,61 @@ Hints:
514536
* For the legend, noted that the expression `'foo' + str(42)` evaluates to `'foo42'`.
515537
```
516538

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}
532540
```
533541

534542

535-
```{exercise-start}
536-
:label: pbe_ex4
543+
```{solution-start} pbe_ex2
544+
:class: dropdown
537545
```
538546

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-
547547
```{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)
550551
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()
557560
```
558561

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.
561563

562-
Replace this existing function with an if--else condition.
563564

564-
```{exercise-end}
565+
```{solution-end}
565566
```
566567

567-
568568
```{exercise-start}
569-
:label: pbe_ex5
570-
```
569+
:label: pbe_ex3
571570
572-
Here's a harder exercise, that takes some thought and planning.
571+
Similar to the previous exercises, plot the time series
573572
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+
$$
575579
576-
Use no imports besides
580+
Use $T=200$, $\alpha = 0.9$ and $\{\epsilon_t\}$ as before.
577581
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|$.
580583
```
581584

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-
588585
```{exercise-end}
589586
```
590587

591-
## Solutions
592588

593-
```{solution-start} pbe_ex1
589+
```{solution-start} pbe_ex3
594590
:class: dropdown
595591
```
596592

597-
Here's one solution.
593+
Here's one solution:
598594

599595
```{code-cell} python3
600596
α = 0.9
@@ -603,7 +599,7 @@ x = np.empty(T+1)
603599
x[0] = 0
604600
605601
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()
607603
608604
plt.plot(x)
609605
plt.show()
@@ -613,55 +609,38 @@ plt.show()
613609
```
614610

615611

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
633614
```
634615

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.
637618

638-
```{solution-end}
639-
```
619+
In Python, conditions are usually implemented with if--else syntax.
640620

621+
Here's an example, that prints -1 for each negative number in an array and 1
622+
for each nonnegative number
641623

642-
```{solution-start} pbe_ex3
643-
:class: dropdown
624+
```{code-cell} python3
625+
numbers = [-9, 2.3, -11, 0]
644626
```
645627

646-
Here's one solution:
647-
648628
```{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+
```
653635

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.
656638

657-
plt.plot(x)
658-
plt.show()
659-
```
639+
Replace this existing function with an if--else condition.
660640

661-
```{solution-end}
641+
```{exercise-end}
662642
```
663643

664-
665644
```{solution-start} pbe_ex4
666645
:class: dropdown
667646
```
@@ -705,6 +684,31 @@ plt.show()
705684
```
706685

707686

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+
708712
```{solution-start} pbe_ex5
709713
:class: dropdown
710714
```
@@ -726,7 +730,7 @@ We estimate the area by sampling bivariate uniforms and looking at the
726730
fraction that falls into the circle.
727731

728732
```{code-cell} python3
729-
n = 100000 # sample size for Monte Carlo simulation
733+
n = 1000000 # sample size for Monte Carlo simulation
730734
731735
count = 0
732736
for i in range(n):
@@ -750,3 +754,4 @@ print(area_estimate * 4) # dividing by radius**2
750754

751755
```{solution-end}
752756
```
757+

0 commit comments

Comments
 (0)