Skip to content

Commit 33efc0b

Browse files
committed
numba exercise
1 parent 329b837 commit 33efc0b

File tree

1 file changed

+40
-44
lines changed

1 file changed

+40
-44
lines changed

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

0 commit comments

Comments
 (0)