Skip to content

Commit 5a1ab00

Browse files
authored
Merge pull request #208 from QuantEcon/solution-position
Moving All Solutions Below Corresponding Exercises
2 parents 1da76ec + dfd9124 commit 5a1ab00

File tree

8 files changed

+162
-184
lines changed

8 files changed

+162
-184
lines changed

lectures/matplotlib.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,6 @@ The output should look like this
301301
```{exercise-end}
302302
```
303303

304-
## Solutions
305-
306304
```{solution-start} mpl_ex1
307305
:class: dropdown
308306
```

lectures/numba.md

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -486,50 +486,6 @@ Use the same idea here, but make the code efficient using Numba.
486486
Compare speed with and without Numba when the sample size is large.
487487
```
488488

489-
490-
```{exercise-start}
491-
:label: speed_ex2
492-
```
493-
494-
In the [Introduction to Quantitative Economics with Python](https://python-intro.quantecon.org) lecture series you can
495-
learn all about finite-state Markov chains.
496-
497-
For now, let's just concentrate on simulating a very simple example of such a chain.
498-
499-
Suppose that the volatility of returns on an asset can be in one of two regimes --- high or low.
500-
501-
The transition probabilities across states are as follows
502-
503-
```{figure} /_static/lecture_specific/sci_libs/nfs_ex1.png
504-
```
505-
506-
For example, let the period length be one day, and suppose the current state is high.
507-
508-
We see from the graph that the state tomorrow will be
509-
510-
* high with probability 0.8
511-
* low with probability 0.2
512-
513-
Your task is to simulate a sequence of daily volatility states according to this rule.
514-
515-
Set the length of the sequence to `n = 1_000_000` and start in the high state.
516-
517-
Implement a pure Python version and a Numba version, and compare speeds.
518-
519-
To test your code, evaluate the fraction of time that the chain spends in the low state.
520-
521-
If your code is correct, it should be about 2/3.
522-
523-
Hints:
524-
525-
* Represent the low state as 0 and the high state as 1.
526-
* If you want to store integers in a NumPy array and then apply JIT compilation, use `x = np.empty(n, dtype=np.int_)`.
527-
528-
```{exercise-end}
529-
```
530-
531-
## Solutions
532-
533489
```{solution-start} speed_ex1
534490
:class: dropdown
535491
```
@@ -571,6 +527,46 @@ characters.
571527
```{solution-end}
572528
```
573529

530+
```{exercise-start}
531+
:label: speed_ex2
532+
```
533+
534+
In the [Introduction to Quantitative Economics with Python](https://python-intro.quantecon.org) lecture series you can
535+
learn all about finite-state Markov chains.
536+
537+
For now, let's just concentrate on simulating a very simple example of such a chain.
538+
539+
Suppose that the volatility of returns on an asset can be in one of two regimes --- high or low.
540+
541+
The transition probabilities across states are as follows
542+
543+
```{figure} /_static/lecture_specific/sci_libs/nfs_ex1.png
544+
```
545+
546+
For example, let the period length be one day, and suppose the current state is high.
547+
548+
We see from the graph that the state tomorrow will be
549+
550+
* high with probability 0.8
551+
* low with probability 0.2
552+
553+
Your task is to simulate a sequence of daily volatility states according to this rule.
554+
555+
Set the length of the sequence to `n = 1_000_000` and start in the high state.
556+
557+
Implement a pure Python version and a Numba version, and compare speeds.
558+
559+
To test your code, evaluate the fraction of time that the chain spends in the low state.
560+
561+
If your code is correct, it should be about 2/3.
562+
563+
Hints:
564+
565+
* Represent the low state as 0 and the high state as 1.
566+
* If you want to store integers in a NumPy array and then apply JIT compilation, use `x = np.empty(n, dtype=np.int_)`.
567+
568+
```{exercise-end}
569+
```
574570

575571
```{solution-start} speed_ex2
576572
:class: dropdown

lectures/numpy.md

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,12 @@ For a comprehensive list of what's available in NumPy see [this documentation](h
710710

711711
## Exercises
712712

713+
```{code-cell} ipython
714+
%matplotlib inline
715+
import matplotlib.pyplot as plt
716+
plt.rcParams['figure.figsize'] = (10,6)
717+
```
718+
713719
```{exercise-start}
714720
:label: np_ex1
715721
```
@@ -733,6 +739,35 @@ Now write a new function that does the same job, but uses NumPy arrays and array
733739
```{exercise-end}
734740
```
735741

742+
```{solution-start} np_ex1
743+
:class: dropdown
744+
```
745+
746+
This code does the job
747+
748+
```{code-cell} python3
749+
def p(x, coef):
750+
X = np.ones_like(coef)
751+
X[1:] = x
752+
y = np.cumprod(X) # y = [1, x, x**2,...]
753+
return coef @ y
754+
```
755+
756+
Let's test it
757+
758+
```{code-cell} python3
759+
x = 2
760+
coef = np.linspace(2, 4, 3)
761+
print(coef)
762+
print(p(x, coef))
763+
# For comparison
764+
q = np.poly1d(np.flip(coef))
765+
print(q(x))
766+
```
767+
768+
```{solution-end}
769+
```
770+
736771

737772
```{exercise-start}
738773
:label: np_ex2
@@ -784,56 +819,6 @@ If you can, write the method so that `draw(k)` returns `k` draws from `q`.
784819
```{exercise-end}
785820
```
786821

787-
788-
```{exercise}
789-
:label: np_ex3
790-
791-
Recall our {ref}`earlier discussion <oop_ex1>` of the empirical cumulative distribution function.
792-
793-
Your task is to
794-
795-
1. Make the `__call__` method more efficient using NumPy.
796-
1. Add a method that plots the ECDF over $[a, b]$, where $a$ and $b$ are method parameters.
797-
```
798-
799-
## Solutions
800-
801-
```{code-cell} ipython
802-
%matplotlib inline
803-
import matplotlib.pyplot as plt
804-
plt.rcParams['figure.figsize'] = (10,6)
805-
```
806-
807-
```{solution-start} np_ex1
808-
:class: dropdown
809-
```
810-
811-
This code does the job
812-
813-
```{code-cell} python3
814-
def p(x, coef):
815-
X = np.ones_like(coef)
816-
X[1:] = x
817-
y = np.cumprod(X) # y = [1, x, x**2,...]
818-
return coef @ y
819-
```
820-
821-
Let's test it
822-
823-
```{code-cell} python3
824-
x = 2
825-
coef = np.linspace(2, 4, 3)
826-
print(coef)
827-
print(p(x, coef))
828-
# For comparison
829-
q = np.poly1d(np.flip(coef))
830-
print(q(x))
831-
```
832-
833-
```{solution-end}
834-
```
835-
836-
837822
```{solution-start} np_ex2
838823
:class: dropdown
839824
```
@@ -899,6 +884,17 @@ using descriptors that behaves as we desire can be found
899884
```
900885

901886

887+
```{exercise}
888+
:label: np_ex3
889+
890+
Recall our {ref}`earlier discussion <oop_ex1>` of the empirical cumulative distribution function.
891+
892+
Your task is to
893+
894+
1. Make the `__call__` method more efficient using NumPy.
895+
1. Add a method that plots the ECDF over $[a, b]$, where $a$ and $b$ are method parameters.
896+
```
897+
902898
```{solution-start} np_ex3
903899
:class: dropdown
904900
```

lectures/parallelization.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,6 @@ For the size of the Monte Carlo simulation, use something substantial, such as
442442
`n = 100_000_000`.
443443
```
444444

445-
## Solutions
446-
447445
```{solution-start} parallel_ex1
448446
:class: dropdown
449447
```

lectures/python_advanced_features.md

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,6 +1667,31 @@ Write a function to recursively compute the $t$-th Fibonacci number for any $t$.
16671667
```{exercise-end}
16681668
```
16691669

1670+
```{solution-start} paf_ex1
1671+
:class: dropdown
1672+
```
1673+
1674+
Here's the standard solution
1675+
1676+
```{code-cell} python3
1677+
def x(t):
1678+
if t == 0:
1679+
return 0
1680+
if t == 1:
1681+
return 1
1682+
else:
1683+
return x(t-1) + x(t-2)
1684+
```
1685+
1686+
Let's test it
1687+
1688+
```{code-cell} python3
1689+
print([x(i) for i in range(10)])
1690+
```
1691+
1692+
```{solution-end}
1693+
```
1694+
16701695

16711696
```{exercise-start}
16721697
:label: paf_ex2
@@ -1695,58 +1720,6 @@ for date in dates:
16951720
```{exercise-end}
16961721
```
16971722

1698-
1699-
```{exercise-start}
1700-
:label: paf_ex3
1701-
```
1702-
1703-
Suppose we have a text file `numbers.txt` containing the following lines
1704-
1705-
```{code-block} none
1706-
:class: no-execute
1707-
1708-
prices
1709-
3
1710-
8
1711-
1712-
7
1713-
21
1714-
```
1715-
1716-
Using `try` -- `except`, write a program to read in the contents of the file and sum the numbers, ignoring lines without numbers.
1717-
1718-
```{exercise-end}
1719-
```
1720-
1721-
## Solutions
1722-
1723-
1724-
```{solution-start} paf_ex1
1725-
:class: dropdown
1726-
```
1727-
1728-
Here's the standard solution
1729-
1730-
```{code-cell} python3
1731-
def x(t):
1732-
if t == 0:
1733-
return 0
1734-
if t == 1:
1735-
return 1
1736-
else:
1737-
return x(t-1) + x(t-2)
1738-
```
1739-
1740-
Let's test it
1741-
1742-
```{code-cell} python3
1743-
print([x(i) for i in range(10)])
1744-
```
1745-
1746-
```{solution-end}
1747-
```
1748-
1749-
17501723
```{solution-start} paf_ex2
17511724
:class: dropdown
17521725
```
@@ -1781,6 +1754,29 @@ for date in dates:
17811754

17821755

17831756

1757+
```{exercise-start}
1758+
:label: paf_ex3
1759+
```
1760+
1761+
Suppose we have a text file `numbers.txt` containing the following lines
1762+
1763+
```{code-block} none
1764+
:class: no-execute
1765+
1766+
prices
1767+
3
1768+
8
1769+
1770+
7
1771+
21
1772+
```
1773+
1774+
Using `try` -- `except`, write a program to read in the contents of the file and sum the numbers, ignoring lines without numbers.
1775+
1776+
```{exercise-end}
1777+
```
1778+
1779+
17841780
```{solution-start} paf_ex3
17851781
:class: dropdown
17861782
```

0 commit comments

Comments
 (0)