4 |
5 | int main() {
6 | double x;
7 | int i = 3;
8 | x = 1.0 / (double) i; /* cast i to double */
9 | double y = sin(x);
10 | printf("i = %d, x = %f, y = %f\n", i, x, y);
11 | return 0;
12 | }
13 |
--------------------------------------------------------------------------------
/lecture2/git_intro/Makefile:
--------------------------------------------------------------------------------
1 | all: reveal beamer pdf
2 |
3 | reveal:
4 | pandoc --self-contained --variable theme="simple" -t revealjs -s github.md -o github.html
5 |
6 | beamer:
7 | pandoc -t beamer -s github.md -o github_slides.pdf
8 |
9 | pdf:
10 | pandoc -t beamer -s github.md -o github.pdf
11 |
--------------------------------------------------------------------------------
/lecture2/git_intro/github.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: 'Intro to GitHub'
3 | author: "Spencer Lyon"
4 | date : "2016-02-05"
5 | ---
6 |
7 | # Git `remote`s
8 |
9 | ## What's a `remote`?
10 |
11 | - A git repo can be on your hard drive: called local
12 | - A `remote` is a copy of the repository on someone else's hard drive or server
13 | - You `git push` commits from local to remote
14 | - `git pull` commits from remote to local
15 |
16 | ## Collaboration
17 |
18 | - Working with remotes enables many [workflows](https://git-scm.com/book/en/v2/Distributed-Git-Distributed-Workflows)
19 | - One common workflow (image taken from [here](https://git-scm.com/book/en/v2/Distributed-Git-Distributed-Workflows))
20 |
21 | 
22 |
23 | # [GitHub](https://github.com)
24 |
25 | ## Facts
26 |
27 | - GitHub is a common place to have a `remote`
28 | - [Over 2.2 million](http://githut.info) active repositories
29 | - Unlimited free public repositories
30 | - 5 free private repositories for academics
31 |
32 | ## GitHub Collaboration
33 |
34 | - Permission management: only certain users can `push`
35 | - Forking: anyone can create a copy of any (visible) repository under their account
36 | - Pull requests: system for project maintainers to review proposed changes before accepting them
37 | - Automated testing (CI) and coverage
38 |
39 | # Example
40 |
41 | ## quantecon_nyu_2016
42 |
43 | - Add this lecture to course repo
44 | - Steps:
45 | 1. Login to github (online)
46 | 2. Clone repository
47 | ```sh
48 | git clone https://github.com/jstac/quantecon_nyu_2016.git
49 | ```
50 | 3. Fork repository (online)
51 | 4. Add `remote` that points to fork
52 | ```sh
53 | git remote add fork https://github.com/spencerlyon2/quantecon_nyu_2016.git
54 | ```
55 |
56 | ## Steps (continued)
57 |
58 | - More steps
59 | 5. `add` + `commit` files
60 | ```
61 | git add files
62 | git commit -m "Adding github intro slides"
63 | ```
64 | 6. `push` to fork
65 | ```
66 | git push fork master
67 | ```
68 | 7. Open pull request (online)
69 |
70 | # Exercise
71 |
72 | ## Pushing homework
73 |
74 | - We will let you practice hw submission process
75 | - Steps
76 | - Create account on github
77 | - Clone [homework repository](https://github.com/jstac/quantecon_nyu_2016_homework)
78 | - Fork homework repo
79 | - Add your fork as remote
80 | - Create file `firstname_lastname` in folder `hw_github_intro`
81 | - Leave short message in file
82 | - Add and commit the file
83 | - Push to fork
84 | - Submit pull request
85 |
86 |
87 | # Extras
88 |
89 | ## SSH keys
90 |
91 | - Two modes of authenticating to github: https, SSH
92 | - https will require you to enter password on every push
93 | - Adding ssh-key removes this requirement
94 | - Follow these steps:
95 | - `ssh-keygen`: follow prompts
96 | - Copy the output of `cat ~/.ssh/id_rsa.pub` (use shift-c)
97 | - NOTE: path `~/.ssh/id_rsa.pub` might be different depending on your answers to previous step
98 | - Important part is to get the `.pub` version
99 | - Go to github account [settings](https://github.com/settings/profile)
100 | - Click "ssh keys" link on left, then "New ssh key"
101 | - Give it a title (name) and paste clipboard contents in key
102 | - Click big green "add SSH key button"
103 |
--------------------------------------------------------------------------------
/lecture2/git_intro/gitnotes.html:
--------------------------------------------------------------------------------
1 | Version Control with Git
2 |
3 |

4 |
5 | I suspect that the most of us have participated in some sort of "home-rolled" version of version control. For example, below are three forms of "collaboration" that we might have engaged in
6 |
7 | - Tediously "careful" approach: This is when a person (or people) maintain a type of version control by placing the date edited at the end of the file name and then maintaining a full folder of these types of files.
8 | - Stake your claim approach: This appraoch takes the form of calling "dibs" on the use of a specific file for the time being by shouting across the office or sending emails to your collaborators and informing others that the file is "currently under your control" and that others should stay away!
9 | - Where's that email approach: Perhaps worst of all is the approach in which collaborators each maintain their own file and email it back and forth with changes as they make them.
10 |
11 | All of these are dangerous workflows and will result in mistakes and unwanted material showing up in your files (or wanted material being erased). Luckily for us, a variety of version control products have evolved in order to help us eliminate the need for these types of behaviors. Dropbox, Google Drives, SVN, Mercurial, and git are all examples of version control systems.
12 | Version control allows us to keep track of what changes have been made over time. Careful maintenance of code and data is vital to reproducibility which "is the hallmark of good science." While Dropbox and Google Drives may be useful for sharing certain types of files (I am not an advocate of their complete abandonment), they are not suitable for the fine tuned version control that we need to maintain good science (or software).
13 | What is git
?
14 | Git is a distributed version control system. Distributed version control means that the entire history of every file is kept on your computer. It was originally written by Linus Torvalds (creator of Linux) to help maintain the Linux project (Fun Fact: Git was originally written because Torvalds found all other alternatives of version control to be too slow to manage a project as large as Linux, so he decided to write his own version which he began on 3 April 2005 and started being used on 7 April 2005. Read more about the history).
15 | Using git
16 | Many people associate git
with the cloud based repository service Github, but git
can be run independently either just on your own computer or on a self-hosted server. In this short tutorial, we are going to create a git
repository hosted on our computer. We will then talk about some of the day to day commads that will be used in git
.
17 | WARNING: Until you know what you're doing and exactly how they work, NEVER use the -f
or --force
flags no matter what the internet tells you.
18 | We are now going to walk through some of the basic settings you should set and an example of some commands.
19 | Configuration
20 | Here we deal with configuration details such as our default editor, user name, email, colors, etc...
21 |
22 | - Set name:
git config --global user.name FirstName LastName
23 | - Set email:
git config --global user.email email@email.com
24 | - Sets git colors:
git config --global color.ui "auto"
25 | - Sets editor to vim:
git config --global core.editor "vim"
26 |
27 | Creating a folder
28 | We will create a folder called <MyFirstGitRepo>
using mkdir <MyFirstGitRepo>
29 | Initializing a git
repository
30 | Now enter that directory using cd <MyFirstGitRepo>
. To initialize this directory as a git
repository (which in the background will create a series of directories and files) we will use the command git init
.
31 | We can see the things that were created by typing ls .git
, but don't worry too much about what is being kept inside yet.
32 | Four Stages of Files
33 | Files in a git repository can be in four different stages: untracked, unstaged, staged, and committed.
34 |
35 | - Untracked: This is a new file that your repository hasn't seen before.
36 | - Unstaged: A file that has previously been tracked and saved, but has changed since its last version.
37 | - Staged: A file that has been changed and is prepared to be committed. Nothing is set in stone yet though and new changes can be made.
38 | - Committed: When a file is committed it becomes a piece of the history of the repository. This moment of time in the file will be able to be referenced or referred to in the future.
39 |
40 | The picture below illustrates this "life cycle"
41 |
42 |

43 |
44 | Let's illustrate this through an example and to expose ourselves to the commands that will be helpful: git add
, git commit
, git diff
, and git status
.
45 | First let's check what is new in our repository. Type git status
. What does it say? It should say that there is nothing to commit because we haven't done anything yet.
46 | Now let's create a file called README.md
. Open this file and type your name in it (could also use the command echo "FirstName LastName" >> README.md
). If you type git status
now, what do you see? It should list README.md
as an untracked file.
47 | We can move this file from untracked to staged by using git add README.md
. Type this command and then once again type git status
. Our README.md
file now shows up in green as a change to be committed. This means it is staged.
48 | We can take our "snapshot" of the file by using the command git commit -m "Type a meaningful msg here"
. Commit the file and then once again type git status
. What do you see now? The repository should be clean again.
49 | Add something new to the README.md
file. Once again, we can check the status of our git repository by typing git status
. It will tell us that README.md
is unstaged because we have made changes to it.
50 | Imagine we wanted to double check the things that we changed. We could type git diff README.md
which will show us the changes that have been made to that file since its previous commit.
51 | These commands will be the core of your git
workflow so I suggest familiarizing yourself with what they do.
52 | Commit History
53 | Remember how we can leave ourselves commit messages. It is useful to leave meaningful commit messages because they are left as a guide for yourself. We can see our history of commits by typing git log
. We can do this in several formats: Try git log --pretty=oneline
, git log --stat
, git log --since=2.weeks
, etc... See the Git Pro book for more options.
54 | If we want to return to a previous commit in our history we can use the information from git log
to get back. There is a sequence of numbers and letters, which we will call commit hash, at the top of each entry in your history. If you copy this and type git reset <commit hash>
then it will return us to that moment of our history. All of our more recent changes will be there, but they will be as if they had been just staged. Can use git reset <commit hash> --hard
to reset to a previous point in time and delete all changes, but be very careful with that command as it can erase your history (in fact, I suggest not using it until you really know enough that you know it is what you want).
55 | Branching
56 | A branch is essentially an specific version of your folder. You start with a main branch which is called master. When you create another branch, it is an exact replica of the branch it is being created from (typically master) and includes the full history of the repository. After creating a new branch you can make changes and this new branch will develop its own history (without changing the history of the original branch -- such as master). If you decide you like the changes that you made then you can bring them into the original branch.
57 | The command git checkout
is used for a few different things, but we will mostly use it for switching between branches. When used with the -b
flag, it creates a new branch and switches to it.
58 | Let's create a new branch in our repository. git checkout -b test
. What has changed? Let's look at the output of git branch
. Now let's make some changes in our branch.
59 | > echo "New line" >> README.md
60 | > git add README.md
61 | > git commit -m "Branch update"
62 | > git log
63 | We can see the history has changed and we have a new commit. Let's return to the master branch by typing git checkout master
. Now check git log
, notice that we no longer have the changes from our test branch.
64 | Imagine that we decided to bring those changes into our master branch. We could use the git merge
command by typing git merge test
which will merge the branch test
into the current branch.
65 | Ignore Files
66 | Sometimes there are files that get created via an intermediate process (such as .aux
, .synctex
, etc... in latex). We often don't care about keeping track of these files. Another wonderful thing about git
is that it allows us to ignore files we don't care about!
67 | We do this by creating a file called .gitignore
at the initial directory of our git repository.
68 | For example, echo *.garbage >> .gitignore
.
69 | Then create a file called foo.garbage
with touch foo.garbage
.
70 | Now type git status
. Notice that this file doesn't show up! It is because it recognizes that anything with an ending of .garbage
should be ignored (this is because *
is treated as a wildcard. Read more about regular expressions or wild cards for more information on how to use them).
71 | Resources and References
72 | Below is a sequence of references to things that I have found useful. I suggest reading a few of them and at least skimming the majority of them. In particular, the software carpentry lectures on git are very useful for learning the basics -- The Pro Git book is the biblical reference for git and can likely answer any question you will have for at least a few years to come. I have organized both sections by how relevant/convincing I found the documents.
73 | Git Technical References
74 | Software Carpentry: Git Lectures Pro Git Git Tower
75 | Why Git References
76 | Difference between git and Dropbox Version control for scientific research Why Physicists Should Use Git Or Why Everyone Should Try Git Git can facilitate greater reproducibility and increased transparency in science
77 |
--------------------------------------------------------------------------------
/lecture2/git_intro/gitnotes.md:
--------------------------------------------------------------------------------
1 | # Version Control with Git
2 |
3 |
4 |

5 |
6 |
7 | I suspect that the most of us have participated in some sort of "home-rolled" version of version control. For example, below are three forms of "collaboration" that we might have engaged in
8 |
9 | * Tediously "careful" approach: This is when a person (or people) maintain a type of version control by placing the date edited at the end of the file name and then maintaining a full folder of these types of files.
10 | * Stake your claim approach: This appraoch takes the form of calling "dibs" on the use of a specific file for the time being by shouting across the office or sending emails to your collaborators and informing others that the file is "currently under your control" and that others should stay away!
11 | * Where's that email approach: Perhaps worst of all is the approach in which collaborators each maintain their own file and email it back and forth with changes as they make them.
12 |
13 | All of these are dangerous workflows and will result in mistakes and unwanted material showing up in your files (or wanted material being erased). Luckily for us, a variety of version control products have evolved in order to help us eliminate the need for these types of behaviors. Dropbox, Google Drives, SVN, Mercurial, and git are all examples of version control systems.
14 |
15 | Version control allows us to keep track of what changes have been made over time. Careful maintenance of code and data is vital to reproducibility which "is the hallmark of good science." While Dropbox and Google Drives may be useful for sharing certain types of files (I am not an advocate of their complete abandonment), they are not suitable for the fine tuned version control that we need to maintain good science (or software).
16 |
17 | ## What is `git`?
18 |
19 | Git is a _distributed_ version control system. _Distributed version control_ means that the entire history of every file is kept on your computer. It was originally written by Linus Torvalds (creator of Linux) to help maintain the Linux project (Fun Fact: Git was originally written because Torvalds found all other alternatives of version control to be too slow to manage a project as large as Linux, so he decided to write his own version which he began on 3 April 2005 and started being used on 7 April 2005. Read more about the [history](https://en.wikipedia.org/wiki/Git_(software))).
20 |
21 | ## Using `git`
22 |
23 | Many people associate `git` with the cloud based repository service Github, but `git` can be run independently either just on your own computer or on a self-hosted server. In this short tutorial, we are going to create a `git` repository hosted on our computer. We will then talk about some of the day to day commads that will be used in `git`.
24 |
25 | WARNING: Until you know what you're doing and exactly how they work, NEVER use the `-f` or `--force` flags no matter what the internet tells you.
26 |
27 | We are now going to walk through some of the basic settings you should set and an example of some commands.
28 |
29 | ### Configuration
30 |
31 | Here we deal with configuration details such as our default editor, user name, email, colors, etc...
32 |
33 | * Set name: `git config --global user.name FirstName LastName`
34 | * Set email: `git config --global user.email email@email.com`
35 | * Sets git colors: `git config --global color.ui "auto"`
36 | * Sets editor to vim: `git config --global core.editor "vim"`
37 |
38 | ### Creating a folder
39 |
40 | We will create a folder called `` using `mkdir `
41 |
42 | ### Initializing a `git` repository
43 |
44 | Now enter that directory using `cd `. To initialize this directory as a `git` repository (which in the background will create a series of directories and files) we will use the command `git init`.
45 |
46 | We can see the things that were created by typing `ls .git`, but don't worry too much about what is being kept inside yet.
47 |
48 | ### Four Stages of Files
49 |
50 | Files in a git repository can be in four different stages: untracked, unstaged, staged, and committed.
51 |
52 | * Untracked: This is a new file that your repository hasn't seen before.
53 | * Unstaged: A file that has previously been tracked and saved, but has changed since its last version.
54 | * Staged: A file that has been changed and is prepared to be committed. Nothing is set in stone yet though and new changes can be made.
55 | * Committed: When a file is committed it becomes a piece of the history of the repository. This moment of time in the file will be able to be referenced or referred to in the future.
56 |
57 | The picture below illustrates this "life cycle"
58 |
59 |
60 |

61 |
62 |
63 | Let's illustrate this through an example and to expose ourselves to the commands that will be helpful: `git add`, `git commit`, `git diff`, and `git status`.
64 |
65 | First let's check what is new in our repository. Type `git status`. What does it say? It should say that there is nothing to commit because we haven't done anything yet.
66 |
67 | Now let's create a file called `README.md`. Open this file and type your name in it (could also use the command `echo "FirstName LastName" >> README.md`). If you type `git status` now, what do you see? It should list `README.md` as an untracked file.
68 |
69 | We can move this file from untracked to staged by using `git add README.md`. Type this command and then once again type `git status`. Our `README.md` file now shows up in green as a change to be committed. This means it is staged.
70 |
71 | We can take our "snapshot" of the file by using the command `git commit -m "Type a meaningful msg here"`. Commit the file and then once again type `git status`. What do you see now? The repository should be clean again.
72 |
73 | Add something new to the `README.md` file. Once again, we can check the status of our git repository by typing `git status`. It will tell us that `README.md` is unstaged because we have made changes to it.
74 |
75 | Imagine we wanted to double check the things that we changed. We could type `git diff README.md` which will show us the changes that have been made to that file since its previous commit.
76 |
77 | These commands will be the core of your `git` workflow so I suggest familiarizing yourself with what they do.
78 |
79 | ### Commit History
80 |
81 | Remember how we can leave ourselves commit messages. It is useful to leave meaningful commit messages because they are left as a guide for yourself. We can see our history of commits by typing `git log`. We can do this in several formats: Try `git log --pretty=oneline`, `git log --stat`, `git log --since=2.weeks`, etc... See the Git Pro book for more options.
82 |
83 | If we want to return to a previous commit in our history we can use the information from `git log` to get back. There is a sequence of numbers and letters, which we will call commit hash, at the top of each entry in your history. If you copy this and type `git reset ` then it will return us to that moment of our history. All of our more recent changes will be there, but they will be as if they had been just staged. Can use `git reset --hard` to reset to a previous point in time and delete all changes, but be very careful with that command as it can erase your history (in fact, I suggest not using it until you really know enough that you know it is what you want).
84 |
85 | ### Branching
86 |
87 | A branch is essentially an specific version of your folder. You start with a main branch which is called master. When you create another branch, it is an exact replica of the branch it is being created from (typically master) and includes the full history of the repository. After creating a new branch you can make changes and this new branch will develop its own history (without changing the history of the original branch -- such as master). If you decide you like the changes that you made then you can bring them into the original branch.
88 |
89 | The command `git checkout` is used for a few different things, but we will mostly use it for switching between branches. When used with the `-b` flag, it creates a new branch and switches to it.
90 |
91 | Let's create a new branch in our repository. `git checkout -b test`. What has changed? Let's look at the output of `git branch`. Now let's make some changes in our branch.
92 |
93 | ```
94 | > echo "New line" >> README.md
95 | > git add README.md
96 | > git commit -m "Branch update"
97 | > git log
98 | ```
99 |
100 | We can see the history has changed and we have a new commit. Let's return to the master branch by typing `git checkout master`. Now check `git log`, notice that we no longer have the changes from our test branch.
101 |
102 | Imagine that we decided to bring those changes into our master branch. We could use the `git merge` command by typing `git merge test` which will merge the branch `test` into the current branch.
103 |
104 | ### Ignore Files
105 |
106 | Sometimes there are files that get created via an intermediate process (such as `.aux`, `.synctex`, etc... in latex). We often don't care about keeping track of these files. Another wonderful thing about `git` is that it allows us to ignore files we don't care about!
107 |
108 | We do this by creating a file called `.gitignore` at the initial directory of our git repository.
109 |
110 | For example, `echo *.garbage >> .gitignore`.
111 |
112 | Then create a file called `foo.garbage` with `touch foo.garbage`.
113 |
114 | Now type `git status`. Notice that this file doesn't show up! It is because it recognizes that anything with an ending of `.garbage` should be ignored (this is because `*` is treated as a wildcard. Read more about regular expressions or [wild cards](http://tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm) for more information on how to use them).
115 |
116 | ## Resources and References
117 |
118 | Below is a sequence of references to things that I have found useful. I suggest reading a few of them and at least skimming the majority of them. In particular, the software carpentry lectures on git are very useful for learning the basics -- The Pro Git book is the biblical reference for git and can likely answer any question you will have for at least a few years to come. I have organized both sections by how relevant/convincing I found the documents.
119 |
120 | ### Git Technical References
121 | [Software Carpentry: Git Lectures](http://swcarpentry.github.io/git-novice/02-setup.html)
122 | [Pro Git](http://git-scm.com/)
123 | [Git Tower](https://www.git-tower.com/learn)
124 |
125 | ### Why Git References
126 | [Difference between git and Dropbox](https://gist.github.com/magicseth/1951984)
127 | [Version control for scientific research](http://blogs.biomedcentral.com/bmcblog/2013/02/28/version-control-for-scientific-research/)
128 | [Why Physicists Should Use Git Or Why Everyone Should Try Git](http://openmetric.org/assets/slides/whygit/#/)
129 | [Git can facilitate greater reproducibility and increased transparency in science](http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3639880/)
130 |
131 |
--------------------------------------------------------------------------------
/lecture2/git_intro/images/gitlifecycle.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture2/git_intro/images/gitlifecycle.png
--------------------------------------------------------------------------------
/lecture2/git_intro/images/phd101212s.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture2/git_intro/images/phd101212s.gif
--------------------------------------------------------------------------------
/lecture2/lecture2.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture2/lecture2.pdf
--------------------------------------------------------------------------------
/lecture3/lecture3.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture3/lecture3.pdf
--------------------------------------------------------------------------------
/lecture4/lecture4.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture4/lecture4.pdf
--------------------------------------------------------------------------------
/lecture5/.ipynb_checkpoints/numpy_timing-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "## Speed Comparisons: NumPy vs Pure Python"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "Speed comparisons for two different routines. \n",
15 | "\n",
16 | "First, some imports:"
17 | ]
18 | },
19 | {
20 | "cell_type": "code",
21 | "execution_count": 1,
22 | "metadata": {
23 | "collapsed": true
24 | },
25 | "outputs": [],
26 | "source": [
27 | "import numpy as np\n",
28 | "import random"
29 | ]
30 | },
31 | {
32 | "cell_type": "markdown",
33 | "metadata": {},
34 | "source": [
35 | "Fix data size"
36 | ]
37 | },
38 | {
39 | "cell_type": "code",
40 | "execution_count": 2,
41 | "metadata": {
42 | "collapsed": true
43 | },
44 | "outputs": [],
45 | "source": [
46 | "n = int(10**6)"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {},
52 | "source": [
53 | "Make an evenly spaced grid of n points between 0 and 1."
54 | ]
55 | },
56 | {
57 | "cell_type": "code",
58 | "execution_count": 7,
59 | "metadata": {
60 | "collapsed": false
61 | },
62 | "outputs": [
63 | {
64 | "name": "stdout",
65 | "output_type": "stream",
66 | "text": [
67 | "10 loops, best of 3: 117 ms per loop\n"
68 | ]
69 | }
70 | ],
71 | "source": [
72 | "%%timeit\n",
73 | "x = []\n",
74 | "a = 0\n",
75 | "step = 1 / (n - 1)\n",
76 | "for i in range(n):\n",
77 | " x.append(a)\n",
78 | " a += step"
79 | ]
80 | },
81 | {
82 | "cell_type": "markdown",
83 | "metadata": {},
84 | "source": [
85 | "Do the same using `np.linspace`."
86 | ]
87 | },
88 | {
89 | "cell_type": "code",
90 | "execution_count": 8,
91 | "metadata": {
92 | "collapsed": false
93 | },
94 | "outputs": [
95 | {
96 | "name": "stdout",
97 | "output_type": "stream",
98 | "text": [
99 | "100 loops, best of 3: 3.92 ms per loop\n"
100 | ]
101 | }
102 | ],
103 | "source": [
104 | "%%timeit\n",
105 | "x = np.linspace(0, 1, n)"
106 | ]
107 | },
108 | {
109 | "cell_type": "markdown",
110 | "metadata": {},
111 | "source": [
112 | "Take the maximum of n standard normals."
113 | ]
114 | },
115 | {
116 | "cell_type": "code",
117 | "execution_count": 5,
118 | "metadata": {
119 | "collapsed": false
120 | },
121 | "outputs": [
122 | {
123 | "name": "stdout",
124 | "output_type": "stream",
125 | "text": [
126 | "1 loop, best of 3: 1 s per loop\n"
127 | ]
128 | }
129 | ],
130 | "source": [
131 | "%%timeit\n",
132 | "running_max = 0\n",
133 | "for i in range(n):\n",
134 | " x = random.normalvariate(0, 1)\n",
135 | " if x > running_max:\n",
136 | " running_max = x"
137 | ]
138 | },
139 | {
140 | "cell_type": "markdown",
141 | "metadata": {},
142 | "source": [
143 | "Do the same in NumPy."
144 | ]
145 | },
146 | {
147 | "cell_type": "code",
148 | "execution_count": 6,
149 | "metadata": {
150 | "collapsed": false
151 | },
152 | "outputs": [
153 | {
154 | "name": "stdout",
155 | "output_type": "stream",
156 | "text": [
157 | "10 loops, best of 3: 38.4 ms per loop\n"
158 | ]
159 | }
160 | ],
161 | "source": [
162 | "%%timeit\n",
163 | "all_max = np.max(np.random.randn(n))"
164 | ]
165 | },
166 | {
167 | "cell_type": "code",
168 | "execution_count": null,
169 | "metadata": {
170 | "collapsed": true
171 | },
172 | "outputs": [],
173 | "source": []
174 | }
175 | ],
176 | "metadata": {
177 | "kernelspec": {
178 | "display_name": "Python 3",
179 | "language": "python",
180 | "name": "python3"
181 | },
182 | "language_info": {
183 | "codemirror_mode": {
184 | "name": "ipython",
185 | "version": 3
186 | },
187 | "file_extension": ".py",
188 | "mimetype": "text/x-python",
189 | "name": "python",
190 | "nbconvert_exporter": "python",
191 | "pygments_lexer": "ipython3",
192 | "version": "3.5.1"
193 | }
194 | },
195 | "nbformat": 4,
196 | "nbformat_minor": 0
197 | }
198 |
--------------------------------------------------------------------------------
/lecture5/fast_loop_examples/ar1_sample_mean.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 |
7 |
8 | double ar1_ts (double * params, int n, unsigned long int seed)
9 | {
10 | double beta = params[0];
11 | double alpha = params[1];
12 | double s = params[2];
13 | /* create a generator chosen by the environment variable GSL_RNG_TYPE */
14 | const gsl_rng_type * T;
15 | gsl_rng * r;
16 | gsl_rng_env_setup();
17 | T = gsl_rng_default;
18 | r = gsl_rng_alloc(T);
19 | gsl_rng_set(r, seed);
20 |
21 | int i;
22 | double x = beta / (1 - alpha); // Start at mean of stationary dist
23 | double sum = 0;
24 | for (i = 1; i <= n; i++) {
25 | sum += x;
26 | x = beta + alpha * x + gsl_ran_gaussian(r, s);
27 | }
28 |
29 | gsl_rng_free (r);
30 | return sum / n;
31 | }
32 |
33 | int main(void)
34 | {
35 | clock_t start, end;
36 | double cpu_time_used;
37 |
38 | int N = 10000000;
39 | double beta = 1.0;
40 | double alpha = 0.9;
41 | double s = 1;
42 | double params[3] = {beta, alpha, s};
43 |
44 | start = clock();
45 | double sample_mean = ar1_ts(params, N, 1);
46 | end = clock();
47 | cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
48 |
49 | printf("mean = %g\n", sample_mean);
50 | printf("time elapsed = %g\n", cpu_time_used);
51 | return 0;
52 | }
53 |
54 |
--------------------------------------------------------------------------------
/lecture5/fast_loop_examples/ar1_sample_mean.jl:
--------------------------------------------------------------------------------
1 | function ar1_sample_mean(N, beta, alpha, s)
2 | sm = 0.0
3 | x = beta / (1 - alpha)
4 | for i in 1:N
5 | sm += x
6 | x = beta + alpha * x + s * randn()
7 | end
8 | return sm / N
9 | end
10 |
11 | N = 10000000
12 | beta = 1.0
13 | alpha = 0.9
14 | s = 1.0
15 | tic()
16 | result = ar1_sample_mean(N, beta, alpha, s)
17 | println("mean = $result")
18 | toc()
19 |
20 |
--------------------------------------------------------------------------------
/lecture5/fast_loop_examples/ar1_sample_mean.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | import time
3 | from numba import jit
4 |
5 | @jit
6 | def ar1_sample_mean(N, alpha, beta, s):
7 | x = beta / (1 - alpha)
8 | sm = 0.0
9 | for i in range(N):
10 | x = beta + alpha * x + s * np.random.randn()
11 | sm += x
12 | return sm / N
13 |
14 | N = 10000000
15 | alpha = 0.9
16 | beta = 1.0
17 | s = 1.0
18 |
19 | t = time.time()
20 | result = ar1_sample_mean(N, alpha, beta, s)
21 | elapsed = time.time() - t
22 |
23 | print("mean = {}".format(result))
24 | print("elapsed time = {}".format(elapsed))
25 |
--------------------------------------------------------------------------------
/lecture5/fast_loop_examples/foo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture5/fast_loop_examples/foo
--------------------------------------------------------------------------------
/lecture5/fast_loop_examples/makefile:
--------------------------------------------------------------------------------
1 | maketest:
2 | gcc -Wall ar1_sample_mean.c -lgsl -lgslcblas -lm -o foo
3 |
4 |
--------------------------------------------------------------------------------
/lecture5/lecture5.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture5/lecture5.pdf
--------------------------------------------------------------------------------
/lecture5/numpy_timing.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "## Speed Comparisons: NumPy vs Pure Python"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "Speed comparisons for two different routines. \n",
15 | "\n",
16 | "First, some imports:"
17 | ]
18 | },
19 | {
20 | "cell_type": "code",
21 | "execution_count": 1,
22 | "metadata": {
23 | "collapsed": true
24 | },
25 | "outputs": [],
26 | "source": [
27 | "import numpy as np\n",
28 | "import random"
29 | ]
30 | },
31 | {
32 | "cell_type": "markdown",
33 | "metadata": {},
34 | "source": [
35 | "Fix data size"
36 | ]
37 | },
38 | {
39 | "cell_type": "code",
40 | "execution_count": 2,
41 | "metadata": {
42 | "collapsed": true
43 | },
44 | "outputs": [],
45 | "source": [
46 | "n = int(10**6)"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {},
52 | "source": [
53 | "Make an evenly spaced grid of n points between 0 and 1."
54 | ]
55 | },
56 | {
57 | "cell_type": "code",
58 | "execution_count": 7,
59 | "metadata": {
60 | "collapsed": false
61 | },
62 | "outputs": [
63 | {
64 | "name": "stdout",
65 | "output_type": "stream",
66 | "text": [
67 | "10 loops, best of 3: 117 ms per loop\n"
68 | ]
69 | }
70 | ],
71 | "source": [
72 | "%%timeit\n",
73 | "x = []\n",
74 | "a = 0\n",
75 | "step = 1 / (n - 1)\n",
76 | "for i in range(n):\n",
77 | " x.append(a)\n",
78 | " a += step"
79 | ]
80 | },
81 | {
82 | "cell_type": "markdown",
83 | "metadata": {},
84 | "source": [
85 | "Do the same using `np.linspace`."
86 | ]
87 | },
88 | {
89 | "cell_type": "code",
90 | "execution_count": 8,
91 | "metadata": {
92 | "collapsed": false
93 | },
94 | "outputs": [
95 | {
96 | "name": "stdout",
97 | "output_type": "stream",
98 | "text": [
99 | "100 loops, best of 3: 3.92 ms per loop\n"
100 | ]
101 | }
102 | ],
103 | "source": [
104 | "%%timeit\n",
105 | "x = np.linspace(0, 1, n)"
106 | ]
107 | },
108 | {
109 | "cell_type": "markdown",
110 | "metadata": {},
111 | "source": [
112 | "Take the maximum of n standard normals."
113 | ]
114 | },
115 | {
116 | "cell_type": "code",
117 | "execution_count": 5,
118 | "metadata": {
119 | "collapsed": false
120 | },
121 | "outputs": [
122 | {
123 | "name": "stdout",
124 | "output_type": "stream",
125 | "text": [
126 | "1 loop, best of 3: 1 s per loop\n"
127 | ]
128 | }
129 | ],
130 | "source": [
131 | "%%timeit\n",
132 | "running_max = 0\n",
133 | "for i in range(n):\n",
134 | " x = random.normalvariate(0, 1)\n",
135 | " if x > running_max:\n",
136 | " running_max = x"
137 | ]
138 | },
139 | {
140 | "cell_type": "markdown",
141 | "metadata": {},
142 | "source": [
143 | "Do the same in NumPy."
144 | ]
145 | },
146 | {
147 | "cell_type": "code",
148 | "execution_count": 6,
149 | "metadata": {
150 | "collapsed": false
151 | },
152 | "outputs": [
153 | {
154 | "name": "stdout",
155 | "output_type": "stream",
156 | "text": [
157 | "10 loops, best of 3: 38.4 ms per loop\n"
158 | ]
159 | }
160 | ],
161 | "source": [
162 | "%%timeit\n",
163 | "all_max = np.max(np.random.randn(n))"
164 | ]
165 | },
166 | {
167 | "cell_type": "code",
168 | "execution_count": null,
169 | "metadata": {
170 | "collapsed": true
171 | },
172 | "outputs": [],
173 | "source": []
174 | }
175 | ],
176 | "metadata": {
177 | "kernelspec": {
178 | "display_name": "Python 3",
179 | "language": "python",
180 | "name": "python3"
181 | },
182 | "language_info": {
183 | "codemirror_mode": {
184 | "name": "ipython",
185 | "version": 3
186 | },
187 | "file_extension": ".py",
188 | "mimetype": "text/x-python",
189 | "name": "python",
190 | "nbconvert_exporter": "python",
191 | "pygments_lexer": "ipython3",
192 | "version": "3.5.1"
193 | }
194 | },
195 | "nbformat": 4,
196 | "nbformat_minor": 0
197 | }
198 |
--------------------------------------------------------------------------------
/lecture6/.ipynb_checkpoints/IntroToJulia-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "slideshow": {
7 | "slide_type": "slide"
8 | }
9 | },
10 | "source": [
11 | "## Introduction to Julia\n",
12 | "\n",
13 | "**Chase Coleman & Spencer Lyon**\n",
14 | "\n",
15 | "3-4-16"
16 | ]
17 | },
18 | {
19 | "cell_type": "markdown",
20 | "metadata": {
21 | "slideshow": {
22 | "slide_type": "slide"
23 | }
24 | },
25 | "source": [
26 | "## Opening Example"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": 4,
32 | "metadata": {
33 | "collapsed": false,
34 | "slideshow": {
35 | "slide_type": "fragment"
36 | }
37 | },
38 | "outputs": [
39 | {
40 | "data": {
41 | "text/plain": [
42 | "bisect (generic function with 3 methods)"
43 | ]
44 | },
45 | "execution_count": 4,
46 | "metadata": {},
47 | "output_type": "execute_result"
48 | }
49 | ],
50 | "source": [
51 | "# *1 *2 *3\n",
52 | "function bisect(f, a, b, maxit=100, tol::Float64=1e-9)\n",
53 | " fa, fb = f(a), f(b)\n",
54 | " # *4 *5\n",
55 | " for it in 1:maxit \n",
56 | " mid = (a + b)/2\n",
57 | " fmid = f(mid)\n",
58 | " \n",
59 | " # *6\n",
60 | " if abs(fmid) < tol \n",
61 | " # *7\n",
62 | " return mid\n",
63 | " end\n",
64 | "\n",
65 | " if fa*fmid > 0\n",
66 | " fa, a = fmid, mid \n",
67 | " else\n",
68 | " fb, b = fmid, mid\n",
69 | " end\n",
70 | " end\n",
71 | " \n",
72 | " # *8\n",
73 | " error(\"maximum iterations exceeded\")\n",
74 | "end"
75 | ]
76 | },
77 | {
78 | "cell_type": "markdown",
79 | "metadata": {
80 | "slideshow": {
81 | "slide_type": "subslide"
82 | }
83 | },
84 | "source": [
85 | "1. Define new functions with `function ... end`\n",
86 | "2. Default arugments `(..., arg=default_value)`\n",
87 | "3. Typed arguments `(..., arg::Type)`\n",
88 | "4. For loop `for X (in|=) SOMETHING ... end`\n",
89 | "5. Create ranges `A:B` (not dense like Matlab)\n",
90 | "6. If statement `if CONDITION BLOCK end`\n",
91 | "7. Return statement `return STUFF` (optional, see next example)\n",
92 | "8. Throwing error `error(MESSAGE)`"
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": 5,
98 | "metadata": {
99 | "collapsed": false,
100 | "slideshow": {
101 | "slide_type": "subslide"
102 | }
103 | },
104 | "outputs": [
105 | {
106 | "data": {
107 | "text/plain": [
108 | "f3 (generic function with 1 method)"
109 | ]
110 | },
111 | "execution_count": 5,
112 | "metadata": {},
113 | "output_type": "execute_result"
114 | }
115 | ],
116 | "source": [
117 | "# shorthand function syntax\n",
118 | "f(x) = x^2-2\n",
119 | "\n",
120 | "# longer syntax -- equivalent to above\n",
121 | "function f2(x)\n",
122 | " x^2-2\n",
123 | "end\n",
124 | "\n",
125 | "# much longer syntax -- still equivalent to above\n",
126 | "function f3(x)\n",
127 | " return x^2-2\n",
128 | "end"
129 | ]
130 | },
131 | {
132 | "cell_type": "code",
133 | "execution_count": 6,
134 | "metadata": {
135 | "collapsed": false,
136 | "slideshow": {
137 | "slide_type": "fragment"
138 | }
139 | },
140 | "outputs": [
141 | {
142 | "name": "stdout",
143 | "output_type": "stream",
144 | "text": [
145 | "1.4142135623842478\n",
146 | "1.4142135623842478\n",
147 | "1.4142135623842478\n"
148 | ]
149 | }
150 | ],
151 | "source": [
152 | "println(bisect(f, 0, 4))\n",
153 | "println(bisect(f2, 0, 4))\n",
154 | "println(bisect(f3, 0, 4))"
155 | ]
156 | },
157 | {
158 | "cell_type": "markdown",
159 | "metadata": {
160 | "slideshow": {
161 | "slide_type": "slide"
162 | }
163 | },
164 | "source": [
165 | "## Types\n",
166 | "\n",
167 | "Everything in Julia has a `type`.\n",
168 | "\n",
169 | "You can inspect the `type` of an object using the `typeof` function:"
170 | ]
171 | },
172 | {
173 | "cell_type": "code",
174 | "execution_count": 18,
175 | "metadata": {
176 | "collapsed": false,
177 | "slideshow": {
178 | "slide_type": "fragment"
179 | }
180 | },
181 | "outputs": [
182 | {
183 | "name": "stdout",
184 | "output_type": "stream",
185 | "text": [
186 | "1.0 is a Float64\n",
187 | "1 is a Int64\n",
188 | "foo is a ASCIIString\n",
189 | "φ is a UTF8String\n",
190 | "bisect is a Function\n",
191 | "4 is a Int8\n",
192 | "Float64 is a DataType\n",
193 | "true is a Bool\n"
194 | ]
195 | }
196 | ],
197 | "source": [
198 | "for obj in [1.0, 1, \"foo\", \"φ\", bisect, Int8(4), Float64, true]\n",
199 | " # Notice string interpolation syntax `$`\n",
200 | " println(\"$obj is a $(typeof(obj))\")\n",
201 | "end"
202 | ]
203 | },
204 | {
205 | "cell_type": "markdown",
206 | "metadata": {
207 | "slideshow": {
208 | "slide_type": "subslide"
209 | }
210 | },
211 | "source": [
212 | "### Type Parameters\n",
213 | "\n",
214 | "Types can have parameters. \n",
215 | "\n",
216 | "This concept is best understood by example"
217 | ]
218 | },
219 | {
220 | "cell_type": "code",
221 | "execution_count": 20,
222 | "metadata": {
223 | "collapsed": false,
224 | "slideshow": {
225 | "slide_type": "fragment"
226 | }
227 | },
228 | "outputs": [
229 | {
230 | "data": {
231 | "text/plain": [
232 | "Array{Int64,1}"
233 | ]
234 | },
235 | "execution_count": 20,
236 | "metadata": {},
237 | "output_type": "execute_result"
238 | }
239 | ],
240 | "source": [
241 | "x = [1, 2, 3]\n",
242 | "typeof(x) # whats the `{` and `}` stuff all about?"
243 | ]
244 | },
245 | {
246 | "cell_type": "markdown",
247 | "metadata": {
248 | "slideshow": {
249 | "slide_type": "fragment"
250 | }
251 | },
252 | "source": [
253 | "`Int` and `1` are type parameters. In this case it tells us that the array\n",
254 | "is filled with `Int`s and has `1` dimension (it is a vector, yes Julia has\n",
255 | "vectors).\n",
256 | "\n",
257 | "Allow you to do all sorts of magic that will become clear later. For now just\n",
258 | "recognize the `{` `}` syntax."
259 | ]
260 | },
261 | {
262 | "cell_type": "markdown",
263 | "metadata": {
264 | "collapsed": true,
265 | "slideshow": {
266 | "slide_type": "subslide"
267 | }
268 | },
269 | "source": [
270 | "### User defined types\n",
271 | "\n",
272 | "You can define your own types (you _should_ do this a lot).\n",
273 | "\n",
274 | "Two forms of types:\n",
275 | "\n",
276 | "- abstract: you can't create these, but they help you group related types together\n",
277 | "- composite: you do create these, they are the actual data of your program"
278 | ]
279 | },
280 | {
281 | "cell_type": "code",
282 | "execution_count": 21,
283 | "metadata": {
284 | "collapsed": true,
285 | "slideshow": {
286 | "slide_type": "subslide"
287 | }
288 | },
289 | "outputs": [],
290 | "source": [
291 | "abstract Exog\n",
292 | "\n",
293 | "type AR1 <: Exog\n",
294 | " rho::Float64 # you should put types on the fields of your types\n",
295 | " sigma::Float64\n",
296 | "end\n",
297 | "\n",
298 | "\"\"\"\n",
299 | "I'm docstring. I describe the `MarkovChain` type.\n",
300 | "\n",
301 | "`x0` is the initial distribution\n",
302 | "\"\"\"\n",
303 | "type MarkovChain{T} <: Exog\n",
304 | " Π::Matrix{Float64}\n",
305 | " vals::AbstractVector{T}\n",
306 | " x0::Vector{Float64}\n",
307 | "end\n",
308 | "\n",
309 | "# functions to give `vals` and `x0` default arguments\n",
310 | "MarkovChain(Π, v) = MarkovChain(Π, v, fill(1/length(v), length(v)))\n",
311 | "MarkovChain(Π) = MarkovChain(Π, 1:size(Π, 1))"
312 | ]
313 | },
314 | {
315 | "cell_type": "code",
316 | "execution_count": 24,
317 | "metadata": {
318 | "collapsed": false,
319 | "slideshow": {
320 | "slide_type": "fragment"
321 | }
322 | },
323 | "outputs": [
324 | {
325 | "data": {
326 | "text/plain": [
327 | "AR1(0.9,0.1)"
328 | ]
329 | },
330 | "execution_count": 24,
331 | "metadata": {},
332 | "output_type": "execute_result"
333 | }
334 | ],
335 | "source": [
336 | "AR1(0.9, 0.1)"
337 | ]
338 | },
339 | {
340 | "cell_type": "code",
341 | "execution_count": 23,
342 | "metadata": {
343 | "collapsed": false,
344 | "slideshow": {
345 | "slide_type": "fragment"
346 | }
347 | },
348 | "outputs": [
349 | {
350 | "name": "stdout",
351 | "output_type": "stream",
352 | "text": [
353 | "search: "
354 | ]
355 | },
356 | {
357 | "data": {
358 | "text/latex": [
359 | "I'm docstring. I describe the \\texttt{MarkovChain} type.\n",
360 | "\\texttt{x0} is the initial distribution\n"
361 | ],
362 | "text/markdown": [
363 | "I'm docstring. I describe the `MarkovChain` type.\n",
364 | "\n",
365 | "`x0` is the initial distribution\n"
366 | ],
367 | "text/plain": [
368 | "I'm docstring. I describe the `MarkovChain` type.\n",
369 | "\n",
370 | "`x0` is the initial distribution\n"
371 | ]
372 | },
373 | "execution_count": 23,
374 | "metadata": {},
375 | "output_type": "execute_result"
376 | },
377 | {
378 | "name": "stdout",
379 | "output_type": "stream",
380 | "text": [
381 | "MarkovChain\n",
382 | "\n"
383 | ]
384 | }
385 | ],
386 | "source": [
387 | "?MarkovChain"
388 | ]
389 | },
390 | {
391 | "cell_type": "markdown",
392 | "metadata": {
393 | "slideshow": {
394 | "slide_type": "slide"
395 | }
396 | },
397 | "source": [
398 | "## Multiple Dispatch\n",
399 | "\n",
400 | "Function behavior can be specialized based on the type (and number) of all function arguments\n",
401 | "\n",
402 | "Let's see some examples"
403 | ]
404 | },
405 | {
406 | "cell_type": "code",
407 | "execution_count": 41,
408 | "metadata": {
409 | "collapsed": false,
410 | "slideshow": {
411 | "slide_type": "fragment"
412 | }
413 | },
414 | "outputs": [
415 | {
416 | "name": "stdout",
417 | "output_type": "stream",
418 | "text": [
419 | "g(hello) I have something\n",
420 | "g(1) I have an integer\n",
421 | "g(1.0) I have a float\n",
422 | "g(1//2) I have some kind of number\n",
423 | "g([1,2,3]) I have an array\n"
424 | ]
425 | }
426 | ],
427 | "source": [
428 | "g(x) = \"I have something\"\n",
429 | "g(x::Int) = \"I have an integer\"\n",
430 | "g(x::Float64) = \"I have a float\"\n",
431 | "g(x::Number) = \"I have some kind of number\"\n",
432 | "g(x::Array) = \"I have an array\"\n",
433 | "\n",
434 | "for x in (\"hello\", 1, 1.0, 1//2, [1, 2, 3])\n",
435 | " @printf \"%-12s%s\\n\" \"g($x)\" g(x)\n",
436 | "end"
437 | ]
438 | },
439 | {
440 | "cell_type": "markdown",
441 | "metadata": {
442 | "slideshow": {
443 | "slide_type": "subslide"
444 | }
445 | },
446 | "source": [
447 | "We can add methods to the `g` function that take multiple arguments\n",
448 | "\n",
449 | "Notice how the return value depends on the types of both arguments"
450 | ]
451 | },
452 | {
453 | "cell_type": "code",
454 | "execution_count": 51,
455 | "metadata": {
456 | "collapsed": false
457 | },
458 | "outputs": [
459 | {
460 | "data": {
461 | "text/plain": [
462 | "g (generic function with 13 methods)"
463 | ]
464 | },
465 | "execution_count": 51,
466 | "metadata": {},
467 | "output_type": "execute_result"
468 | }
469 | ],
470 | "source": [
471 | "g(x, y) = \"I have two things\"\n",
472 | "g(x::Int, y) = \"I have an integer and something else\"\n",
473 | "g(x::Int, y::Number) = \"I have an integer and a number\"\n",
474 | "g(x::Int, y::Int) = \"I have two integers\"\n",
475 | "g(x::Array, y::Array) = \"I have two arrays\"\n",
476 | "g(x::Array{Float64}, y::Array{Float64}) = \"I have two arrays that have floats\""
477 | ]
478 | },
479 | {
480 | "cell_type": "code",
481 | "execution_count": 52,
482 | "metadata": {
483 | "collapsed": false,
484 | "slideshow": {
485 | "slide_type": "fragment"
486 | }
487 | },
488 | "outputs": [
489 | {
490 | "name": "stdout",
491 | "output_type": "stream",
492 | "text": [
493 | "g(x, y) I have two things\n",
494 | "g(1, x) I have an integer and something else\n",
495 | "g(1, 1//2) I have an integer and a number\n",
496 | "g(1, 2) I have two integers\n",
497 | "g(1, 2.0) I have an integer and a number\n",
498 | "g([1], [2]) I have two arrays\n",
499 | "g([1.0], [2.0]) I have two arrays that have floats\n"
500 | ]
501 | }
502 | ],
503 | "source": [
504 | "stuff = ((\"x\", \"y\"), (1, \"x\"), (1, 1//2), \n",
505 | " (1, 2), (1, 2.0), ([1], [2]), \n",
506 | " ([1.0], [2.0]))\n",
507 | "\n",
508 | "for (x1, x2) in stuff\n",
509 | " @printf \"%-18s%s\\n\" \"g($x1, $x2)\" g(x1, x2)\n",
510 | "end"
511 | ]
512 | },
513 | {
514 | "cell_type": "markdown",
515 | "metadata": {
516 | "slideshow": {
517 | "slide_type": "slide"
518 | }
519 | },
520 | "source": [
521 | "## Example\n",
522 | "\n",
523 | "Let's construct routines that will allow us to simulate any subtype of `Exog`\n",
524 | "\n",
525 | "To do this we will need each subtype of `Exog` to implement a `iter` method\n",
526 | "\n",
527 | "This method should take two arguments:\n",
528 | "\n",
529 | "- The `Exog` subtype\n",
530 | "- The current state\n",
531 | "\n",
532 | "It should return the state on the next `iter`ation"
533 | ]
534 | },
535 | {
536 | "cell_type": "code",
537 | "execution_count": 58,
538 | "metadata": {
539 | "collapsed": false,
540 | "slideshow": {
541 | "slide_type": "subslide"
542 | }
543 | },
544 | "outputs": [
545 | {
546 | "data": {
547 | "text/plain": [
548 | "iter (generic function with 4 methods)"
549 | ]
550 | },
551 | "execution_count": 58,
552 | "metadata": {},
553 | "output_type": "execute_result"
554 | }
555 | ],
556 | "source": [
557 | "iter(ar1::AR1, x) = ar1.rho*x + ar1.sigma*randn()\n",
558 | "\n",
559 | "function iter(mc::MarkovChain, s::Int, v)\n",
560 | " ind = searchsortedfirst(cumsum(vec(mc.Π[s, :])), rand())\n",
561 | " return mc.vals[ind]\n",
562 | "end\n",
563 | "\n",
564 | "iter{T}(mc::MarkovChain{T}, v::T) = iter(mc, findfirst(mc.vals, v), v)\n",
565 | "iter(::Exog, x) = error(\"iter should be implemented by each Exog subtype\")"
566 | ]
567 | },
568 | {
569 | "cell_type": "markdown",
570 | "metadata": {
571 | "slideshow": {
572 | "slide_type": "subslide"
573 | }
574 | },
575 | "source": [
576 | "Now we will define single `simulate` function for all `Exog` subtypes"
577 | ]
578 | },
579 | {
580 | "cell_type": "code",
581 | "execution_count": 64,
582 | "metadata": {
583 | "collapsed": false
584 | },
585 | "outputs": [
586 | {
587 | "data": {
588 | "text/plain": [
589 | "simulate (generic function with 2 methods)"
590 | ]
591 | },
592 | "execution_count": 64,
593 | "metadata": {},
594 | "output_type": "execute_result"
595 | }
596 | ],
597 | "source": [
598 | "# NOTE `;` for keyword argument\n",
599 | "function simulate(ex::Exog, x0; capT::Int=10)\n",
600 | " out = Array(typeof(x0), capT)\n",
601 | " out[1] = x0\n",
602 | "\n",
603 | " for t = 2:capT\n",
604 | " out[t] = iter(ex, out[t-1])\n",
605 | " end\n",
606 | " out\n",
607 | "end\n",
608 | "\n",
609 | "# for MarkovChain we have more info, so we don't need to give it an x0\n",
610 | "# define another method that hands off to the method above\n",
611 | "function simulate(mc::MarkovChain; capT::Int=100)\n",
612 | " v = mc.vals[searchsortedfirst(cumsum(mc.x0), rand())]\n",
613 | " simulate(mc, v; capT=capT)\n",
614 | "end"
615 | ]
616 | },
617 | {
618 | "cell_type": "code",
619 | "execution_count": 70,
620 | "metadata": {
621 | "collapsed": false,
622 | "slideshow": {
623 | "slide_type": "subslide"
624 | }
625 | },
626 | "outputs": [
627 | {
628 | "data": {
629 | "text/plain": [
630 | "10-element Array{Float64,1}:\n",
631 | " 0.5 \n",
632 | " 0.415777\n",
633 | " 0.364054\n",
634 | " 0.320674\n",
635 | " 0.300484\n",
636 | " 0.456054\n",
637 | " 0.316475\n",
638 | " 0.303914\n",
639 | " 0.35384 \n",
640 | " 0.282838"
641 | ]
642 | },
643 | "execution_count": 70,
644 | "metadata": {},
645 | "output_type": "execute_result"
646 | }
647 | ],
648 | "source": [
649 | "ar1 = AR1(0.9, 0.1)\n",
650 | "simulate(ar1, 0.5)"
651 | ]
652 | },
653 | {
654 | "cell_type": "code",
655 | "execution_count": 69,
656 | "metadata": {
657 | "collapsed": false,
658 | "slideshow": {
659 | "slide_type": "fragment"
660 | }
661 | },
662 | "outputs": [
663 | {
664 | "data": {
665 | "text/plain": [
666 | "5-element Array{Float64,1}:\n",
667 | " 0.5\n",
668 | " 0.5\n",
669 | " 0.5\n",
670 | " 0.9\n",
671 | " 0.5"
672 | ]
673 | },
674 | "execution_count": 69,
675 | "metadata": {},
676 | "output_type": "execute_result"
677 | }
678 | ],
679 | "source": [
680 | "mc = MarkovChain([0.7 0.3; 0.4 0.6], [0.5, 0.9], [0.2, 0.8])\n",
681 | "simulate(mc; capT=5)"
682 | ]
683 | }
684 | ],
685 | "metadata": {
686 | "kernelspec": {
687 | "display_name": "Julia 0.4.2",
688 | "language": "julia",
689 | "name": "julia-0.4"
690 | },
691 | "language_info": {
692 | "file_extension": ".jl",
693 | "mimetype": "application/julia",
694 | "name": "julia",
695 | "version": "0.4.2"
696 | },
697 | "livereveal": {
698 | "scroll": true,
699 | "start_slideshow_at": "selected",
700 | "theme": "white",
701 | "transition": "fade"
702 | }
703 | },
704 | "nbformat": 4,
705 | "nbformat_minor": 0
706 | }
707 |
--------------------------------------------------------------------------------
/lecture6/IntroToJulia.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "slideshow": {
7 | "slide_type": "slide"
8 | }
9 | },
10 | "source": [
11 | "## Introduction to Julia\n",
12 | "\n",
13 | "**Chase Coleman & Spencer Lyon**\n",
14 | "\n",
15 | "3-4-16"
16 | ]
17 | },
18 | {
19 | "cell_type": "markdown",
20 | "metadata": {
21 | "slideshow": {
22 | "slide_type": "slide"
23 | }
24 | },
25 | "source": [
26 | "## Opening Example"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": 4,
32 | "metadata": {
33 | "collapsed": false,
34 | "slideshow": {
35 | "slide_type": "fragment"
36 | }
37 | },
38 | "outputs": [
39 | {
40 | "data": {
41 | "text/plain": [
42 | "bisect (generic function with 3 methods)"
43 | ]
44 | },
45 | "execution_count": 4,
46 | "metadata": {},
47 | "output_type": "execute_result"
48 | }
49 | ],
50 | "source": [
51 | "# *1 *2 *3\n",
52 | "function bisect(f, a, b, maxit=100, tol::Float64=1e-9)\n",
53 | " fa, fb = f(a), f(b)\n",
54 | " # *4 *5\n",
55 | " for it in 1:maxit \n",
56 | " mid = (a + b)/2\n",
57 | " fmid = f(mid)\n",
58 | " \n",
59 | " # *6\n",
60 | " if abs(fmid) < tol \n",
61 | " # *7\n",
62 | " return mid\n",
63 | " end\n",
64 | "\n",
65 | " if fa*fmid > 0\n",
66 | " fa, a = fmid, mid \n",
67 | " else\n",
68 | " fb, b = fmid, mid\n",
69 | " end\n",
70 | " end\n",
71 | " \n",
72 | " # *8\n",
73 | " error(\"maximum iterations exceeded\")\n",
74 | "end"
75 | ]
76 | },
77 | {
78 | "cell_type": "markdown",
79 | "metadata": {
80 | "slideshow": {
81 | "slide_type": "subslide"
82 | }
83 | },
84 | "source": [
85 | "1. Define new functions with `function ... end`\n",
86 | "2. Default arugments `(..., arg=default_value)`\n",
87 | "3. Typed arguments `(..., arg::Type)`\n",
88 | "4. For loop `for X (in|=) SOMETHING ... end`\n",
89 | "5. Create ranges `A:B` (not dense like Matlab)\n",
90 | "6. If statement `if CONDITION BLOCK end`\n",
91 | "7. Return statement `return STUFF` (optional, see next example)\n",
92 | "8. Throwing error `error(MESSAGE)`"
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": 5,
98 | "metadata": {
99 | "collapsed": false,
100 | "slideshow": {
101 | "slide_type": "subslide"
102 | }
103 | },
104 | "outputs": [
105 | {
106 | "data": {
107 | "text/plain": [
108 | "f3 (generic function with 1 method)"
109 | ]
110 | },
111 | "execution_count": 5,
112 | "metadata": {},
113 | "output_type": "execute_result"
114 | }
115 | ],
116 | "source": [
117 | "# shorthand function syntax\n",
118 | "f(x) = x^2-2\n",
119 | "\n",
120 | "# longer syntax -- equivalent to above\n",
121 | "function f2(x)\n",
122 | " x^2-2\n",
123 | "end\n",
124 | "\n",
125 | "# much longer syntax -- still equivalent to above\n",
126 | "function f3(x)\n",
127 | " return x^2-2\n",
128 | "end"
129 | ]
130 | },
131 | {
132 | "cell_type": "code",
133 | "execution_count": 6,
134 | "metadata": {
135 | "collapsed": false,
136 | "slideshow": {
137 | "slide_type": "fragment"
138 | }
139 | },
140 | "outputs": [
141 | {
142 | "name": "stdout",
143 | "output_type": "stream",
144 | "text": [
145 | "1.4142135623842478\n",
146 | "1.4142135623842478\n",
147 | "1.4142135623842478\n"
148 | ]
149 | }
150 | ],
151 | "source": [
152 | "println(bisect(f, 0, 4))\n",
153 | "println(bisect(f2, 0, 4))\n",
154 | "println(bisect(f3, 0, 4))"
155 | ]
156 | },
157 | {
158 | "cell_type": "markdown",
159 | "metadata": {
160 | "slideshow": {
161 | "slide_type": "slide"
162 | }
163 | },
164 | "source": [
165 | "## Types\n",
166 | "\n",
167 | "Everything in Julia has a `type`.\n",
168 | "\n",
169 | "You can inspect the `type` of an object using the `typeof` function:"
170 | ]
171 | },
172 | {
173 | "cell_type": "code",
174 | "execution_count": 18,
175 | "metadata": {
176 | "collapsed": false,
177 | "slideshow": {
178 | "slide_type": "fragment"
179 | }
180 | },
181 | "outputs": [
182 | {
183 | "name": "stdout",
184 | "output_type": "stream",
185 | "text": [
186 | "1.0 is a Float64\n",
187 | "1 is a Int64\n",
188 | "foo is a ASCIIString\n",
189 | "φ is a UTF8String\n",
190 | "bisect is a Function\n",
191 | "4 is a Int8\n",
192 | "Float64 is a DataType\n",
193 | "true is a Bool\n"
194 | ]
195 | }
196 | ],
197 | "source": [
198 | "for obj in [1.0, 1, \"foo\", \"φ\", bisect, Int8(4), Float64, true]\n",
199 | " # Notice string interpolation syntax `$`\n",
200 | " println(\"$obj is a $(typeof(obj))\")\n",
201 | "end"
202 | ]
203 | },
204 | {
205 | "cell_type": "markdown",
206 | "metadata": {
207 | "slideshow": {
208 | "slide_type": "subslide"
209 | }
210 | },
211 | "source": [
212 | "### Type Parameters\n",
213 | "\n",
214 | "Types can have parameters. \n",
215 | "\n",
216 | "This concept is best understood by example"
217 | ]
218 | },
219 | {
220 | "cell_type": "code",
221 | "execution_count": 20,
222 | "metadata": {
223 | "collapsed": false,
224 | "slideshow": {
225 | "slide_type": "fragment"
226 | }
227 | },
228 | "outputs": [
229 | {
230 | "data": {
231 | "text/plain": [
232 | "Array{Int64,1}"
233 | ]
234 | },
235 | "execution_count": 20,
236 | "metadata": {},
237 | "output_type": "execute_result"
238 | }
239 | ],
240 | "source": [
241 | "x = [1, 2, 3]\n",
242 | "typeof(x) # whats the `{` and `}` stuff all about?"
243 | ]
244 | },
245 | {
246 | "cell_type": "markdown",
247 | "metadata": {
248 | "slideshow": {
249 | "slide_type": "fragment"
250 | }
251 | },
252 | "source": [
253 | "`Int` and `1` are type parameters. In this case it tells us that the array\n",
254 | "is filled with `Int`s and has `1` dimension (it is a vector, yes Julia has\n",
255 | "vectors).\n",
256 | "\n",
257 | "Allow you to do all sorts of magic that will become clear later. For now just\n",
258 | "recognize the `{` `}` syntax."
259 | ]
260 | },
261 | {
262 | "cell_type": "markdown",
263 | "metadata": {
264 | "collapsed": true,
265 | "slideshow": {
266 | "slide_type": "subslide"
267 | }
268 | },
269 | "source": [
270 | "### User defined types\n",
271 | "\n",
272 | "You can define your own types (you _should_ do this a lot).\n",
273 | "\n",
274 | "Two forms of types:\n",
275 | "\n",
276 | "- abstract: you can't create these, but they help you group related types together\n",
277 | "- composite: you do create these, they are the actual data of your program"
278 | ]
279 | },
280 | {
281 | "cell_type": "code",
282 | "execution_count": 21,
283 | "metadata": {
284 | "collapsed": true,
285 | "slideshow": {
286 | "slide_type": "subslide"
287 | }
288 | },
289 | "outputs": [],
290 | "source": [
291 | "abstract Exog\n",
292 | "\n",
293 | "type AR1 <: Exog\n",
294 | " rho::Float64 # you should put types on the fields of your types\n",
295 | " sigma::Float64\n",
296 | "end\n",
297 | "\n",
298 | "\"\"\"\n",
299 | "I'm docstring. I describe the `MarkovChain` type.\n",
300 | "\n",
301 | "`x0` is the initial distribution\n",
302 | "\"\"\"\n",
303 | "type MarkovChain{T} <: Exog\n",
304 | " Π::Matrix{Float64}\n",
305 | " vals::AbstractVector{T}\n",
306 | " x0::Vector{Float64}\n",
307 | "end\n",
308 | "\n",
309 | "# functions to give `vals` and `x0` default arguments\n",
310 | "MarkovChain(Π, v) = MarkovChain(Π, v, fill(1/length(v), length(v)))\n",
311 | "MarkovChain(Π) = MarkovChain(Π, 1:size(Π, 1))"
312 | ]
313 | },
314 | {
315 | "cell_type": "code",
316 | "execution_count": 24,
317 | "metadata": {
318 | "collapsed": false,
319 | "slideshow": {
320 | "slide_type": "fragment"
321 | }
322 | },
323 | "outputs": [
324 | {
325 | "data": {
326 | "text/plain": [
327 | "AR1(0.9,0.1)"
328 | ]
329 | },
330 | "execution_count": 24,
331 | "metadata": {},
332 | "output_type": "execute_result"
333 | }
334 | ],
335 | "source": [
336 | "AR1(0.9, 0.1)"
337 | ]
338 | },
339 | {
340 | "cell_type": "code",
341 | "execution_count": 23,
342 | "metadata": {
343 | "collapsed": false,
344 | "slideshow": {
345 | "slide_type": "fragment"
346 | }
347 | },
348 | "outputs": [
349 | {
350 | "name": "stdout",
351 | "output_type": "stream",
352 | "text": [
353 | "search: "
354 | ]
355 | },
356 | {
357 | "data": {
358 | "text/latex": [
359 | "I'm docstring. I describe the \\texttt{MarkovChain} type.\n",
360 | "\\texttt{x0} is the initial distribution\n"
361 | ],
362 | "text/markdown": [
363 | "I'm docstring. I describe the `MarkovChain` type.\n",
364 | "\n",
365 | "`x0` is the initial distribution\n"
366 | ],
367 | "text/plain": [
368 | "I'm docstring. I describe the `MarkovChain` type.\n",
369 | "\n",
370 | "`x0` is the initial distribution\n"
371 | ]
372 | },
373 | "execution_count": 23,
374 | "metadata": {},
375 | "output_type": "execute_result"
376 | },
377 | {
378 | "name": "stdout",
379 | "output_type": "stream",
380 | "text": [
381 | "MarkovChain\n",
382 | "\n"
383 | ]
384 | }
385 | ],
386 | "source": [
387 | "?MarkovChain"
388 | ]
389 | },
390 | {
391 | "cell_type": "markdown",
392 | "metadata": {
393 | "slideshow": {
394 | "slide_type": "slide"
395 | }
396 | },
397 | "source": [
398 | "## Multiple Dispatch\n",
399 | "\n",
400 | "Function behavior can be specialized based on the type (and number) of all function arguments\n",
401 | "\n",
402 | "Let's see some examples"
403 | ]
404 | },
405 | {
406 | "cell_type": "code",
407 | "execution_count": 41,
408 | "metadata": {
409 | "collapsed": false,
410 | "slideshow": {
411 | "slide_type": "fragment"
412 | }
413 | },
414 | "outputs": [
415 | {
416 | "name": "stdout",
417 | "output_type": "stream",
418 | "text": [
419 | "g(hello) I have something\n",
420 | "g(1) I have an integer\n",
421 | "g(1.0) I have a float\n",
422 | "g(1//2) I have some kind of number\n",
423 | "g([1,2,3]) I have an array\n"
424 | ]
425 | }
426 | ],
427 | "source": [
428 | "g(x) = \"I have something\"\n",
429 | "g(x::Int) = \"I have an integer\"\n",
430 | "g(x::Float64) = \"I have a float\"\n",
431 | "g(x::Number) = \"I have some kind of number\"\n",
432 | "g(x::Array) = \"I have an array\"\n",
433 | "\n",
434 | "for x in (\"hello\", 1, 1.0, 1//2, [1, 2, 3])\n",
435 | " @printf \"%-12s%s\\n\" \"g($x)\" g(x)\n",
436 | "end"
437 | ]
438 | },
439 | {
440 | "cell_type": "markdown",
441 | "metadata": {
442 | "slideshow": {
443 | "slide_type": "subslide"
444 | }
445 | },
446 | "source": [
447 | "We can add methods to the `g` function that take multiple arguments\n",
448 | "\n",
449 | "Notice how the return value depends on the types of both arguments"
450 | ]
451 | },
452 | {
453 | "cell_type": "code",
454 | "execution_count": 51,
455 | "metadata": {
456 | "collapsed": false
457 | },
458 | "outputs": [
459 | {
460 | "data": {
461 | "text/plain": [
462 | "g (generic function with 13 methods)"
463 | ]
464 | },
465 | "execution_count": 51,
466 | "metadata": {},
467 | "output_type": "execute_result"
468 | }
469 | ],
470 | "source": [
471 | "g(x, y) = \"I have two things\"\n",
472 | "g(x::Int, y) = \"I have an integer and something else\"\n",
473 | "g(x::Int, y::Number) = \"I have an integer and a number\"\n",
474 | "g(x::Int, y::Int) = \"I have two integers\"\n",
475 | "g(x::Array, y::Array) = \"I have two arrays\"\n",
476 | "g(x::Array{Float64}, y::Array{Float64}) = \"I have two arrays that have floats\""
477 | ]
478 | },
479 | {
480 | "cell_type": "code",
481 | "execution_count": 52,
482 | "metadata": {
483 | "collapsed": false,
484 | "slideshow": {
485 | "slide_type": "fragment"
486 | }
487 | },
488 | "outputs": [
489 | {
490 | "name": "stdout",
491 | "output_type": "stream",
492 | "text": [
493 | "g(x, y) I have two things\n",
494 | "g(1, x) I have an integer and something else\n",
495 | "g(1, 1//2) I have an integer and a number\n",
496 | "g(1, 2) I have two integers\n",
497 | "g(1, 2.0) I have an integer and a number\n",
498 | "g([1], [2]) I have two arrays\n",
499 | "g([1.0], [2.0]) I have two arrays that have floats\n"
500 | ]
501 | }
502 | ],
503 | "source": [
504 | "stuff = ((\"x\", \"y\"), (1, \"x\"), (1, 1//2), \n",
505 | " (1, 2), (1, 2.0), ([1], [2]), \n",
506 | " ([1.0], [2.0]))\n",
507 | "\n",
508 | "for (x1, x2) in stuff\n",
509 | " @printf \"%-18s%s\\n\" \"g($x1, $x2)\" g(x1, x2)\n",
510 | "end"
511 | ]
512 | },
513 | {
514 | "cell_type": "markdown",
515 | "metadata": {
516 | "slideshow": {
517 | "slide_type": "slide"
518 | }
519 | },
520 | "source": [
521 | "## Example\n",
522 | "\n",
523 | "Let's construct routines that will allow us to simulate any subtype of `Exog`\n",
524 | "\n",
525 | "To do this we will need each subtype of `Exog` to implement a `iter` method\n",
526 | "\n",
527 | "This method should take two arguments:\n",
528 | "\n",
529 | "- The `Exog` subtype\n",
530 | "- The current state\n",
531 | "\n",
532 | "It should return the state on the next `iter`ation"
533 | ]
534 | },
535 | {
536 | "cell_type": "code",
537 | "execution_count": 58,
538 | "metadata": {
539 | "collapsed": false,
540 | "slideshow": {
541 | "slide_type": "subslide"
542 | }
543 | },
544 | "outputs": [
545 | {
546 | "data": {
547 | "text/plain": [
548 | "iter (generic function with 4 methods)"
549 | ]
550 | },
551 | "execution_count": 58,
552 | "metadata": {},
553 | "output_type": "execute_result"
554 | }
555 | ],
556 | "source": [
557 | "iter(ar1::AR1, x) = ar1.rho*x + ar1.sigma*randn()\n",
558 | "\n",
559 | "function iter(mc::MarkovChain, s::Int, v)\n",
560 | " ind = searchsortedfirst(cumsum(vec(mc.Π[s, :])), rand())\n",
561 | " return mc.vals[ind]\n",
562 | "end\n",
563 | "\n",
564 | "iter{T}(mc::MarkovChain{T}, v::T) = iter(mc, findfirst(mc.vals, v), v)\n",
565 | "iter(::Exog, x) = error(\"iter should be implemented by each Exog subtype\")"
566 | ]
567 | },
568 | {
569 | "cell_type": "markdown",
570 | "metadata": {
571 | "slideshow": {
572 | "slide_type": "subslide"
573 | }
574 | },
575 | "source": [
576 | "Now we will define single `simulate` function for all `Exog` subtypes"
577 | ]
578 | },
579 | {
580 | "cell_type": "code",
581 | "execution_count": 64,
582 | "metadata": {
583 | "collapsed": false
584 | },
585 | "outputs": [
586 | {
587 | "data": {
588 | "text/plain": [
589 | "simulate (generic function with 2 methods)"
590 | ]
591 | },
592 | "execution_count": 64,
593 | "metadata": {},
594 | "output_type": "execute_result"
595 | }
596 | ],
597 | "source": [
598 | "# NOTE `;` for keyword argument\n",
599 | "function simulate(ex::Exog, x0; capT::Int=10)\n",
600 | " out = Array(typeof(x0), capT)\n",
601 | " out[1] = x0\n",
602 | "\n",
603 | " for t = 2:capT\n",
604 | " out[t] = iter(ex, out[t-1])\n",
605 | " end\n",
606 | " out\n",
607 | "end\n",
608 | "\n",
609 | "# for MarkovChain we have more info, so we don't need to give it an x0\n",
610 | "# define another method that hands off to the method above\n",
611 | "function simulate(mc::MarkovChain; capT::Int=100)\n",
612 | " v = mc.vals[searchsortedfirst(cumsum(mc.x0), rand())]\n",
613 | " simulate(mc, v; capT=capT)\n",
614 | "end"
615 | ]
616 | },
617 | {
618 | "cell_type": "code",
619 | "execution_count": 70,
620 | "metadata": {
621 | "collapsed": false,
622 | "slideshow": {
623 | "slide_type": "subslide"
624 | }
625 | },
626 | "outputs": [
627 | {
628 | "data": {
629 | "text/plain": [
630 | "10-element Array{Float64,1}:\n",
631 | " 0.5 \n",
632 | " 0.415777\n",
633 | " 0.364054\n",
634 | " 0.320674\n",
635 | " 0.300484\n",
636 | " 0.456054\n",
637 | " 0.316475\n",
638 | " 0.303914\n",
639 | " 0.35384 \n",
640 | " 0.282838"
641 | ]
642 | },
643 | "execution_count": 70,
644 | "metadata": {},
645 | "output_type": "execute_result"
646 | }
647 | ],
648 | "source": [
649 | "ar1 = AR1(0.9, 0.1)\n",
650 | "simulate(ar1, 0.5)"
651 | ]
652 | },
653 | {
654 | "cell_type": "code",
655 | "execution_count": 69,
656 | "metadata": {
657 | "collapsed": false,
658 | "slideshow": {
659 | "slide_type": "fragment"
660 | }
661 | },
662 | "outputs": [
663 | {
664 | "data": {
665 | "text/plain": [
666 | "5-element Array{Float64,1}:\n",
667 | " 0.5\n",
668 | " 0.5\n",
669 | " 0.5\n",
670 | " 0.9\n",
671 | " 0.5"
672 | ]
673 | },
674 | "execution_count": 69,
675 | "metadata": {},
676 | "output_type": "execute_result"
677 | }
678 | ],
679 | "source": [
680 | "mc = MarkovChain([0.7 0.3; 0.4 0.6], [0.5, 0.9], [0.2, 0.8])\n",
681 | "simulate(mc; capT=5)"
682 | ]
683 | }
684 | ],
685 | "metadata": {
686 | "kernelspec": {
687 | "display_name": "Julia 0.4.2",
688 | "language": "julia",
689 | "name": "julia-0.4"
690 | },
691 | "language_info": {
692 | "file_extension": ".jl",
693 | "mimetype": "application/julia",
694 | "name": "julia",
695 | "version": "0.4.2"
696 | },
697 | "livereveal": {
698 | "scroll": true,
699 | "start_slideshow_at": "selected",
700 | "theme": "white",
701 | "transition": "fade"
702 | }
703 | },
704 | "nbformat": 4,
705 | "nbformat_minor": 0
706 | }
707 |
--------------------------------------------------------------------------------
/lecture6/ParallelJulia.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "slideshow": {
7 | "slide_type": "slide"
8 | }
9 | },
10 | "source": [
11 | "## Parallel programming in Julia\n",
12 | "\n",
13 | "**Chase Coleman & Spencer Lyon**\n",
14 | "\n",
15 | "3-4-16"
16 | ]
17 | },
18 | {
19 | "cell_type": "markdown",
20 | "metadata": {
21 | "slideshow": {
22 | "slide_type": "slide"
23 | }
24 | },
25 | "source": [
26 | "## Basics\n",
27 | "\n",
28 | "Julia has built in support for parallel programming\n",
29 | "\n",
30 | "To add more computing processes use the `addprocs` function"
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "execution_count": 2,
36 | "metadata": {
37 | "collapsed": false,
38 | "scrolled": true,
39 | "slideshow": {
40 | "slide_type": "fragment"
41 | }
42 | },
43 | "outputs": [
44 | {
45 | "data": {
46 | "text/plain": [
47 | "4-element Array{Int64,1}:\n",
48 | " 2\n",
49 | " 3\n",
50 | " 4\n",
51 | " 5"
52 | ]
53 | },
54 | "execution_count": 2,
55 | "metadata": {},
56 | "output_type": "execute_result"
57 | }
58 | ],
59 | "source": [
60 | "addprocs(4)"
61 | ]
62 | },
63 | {
64 | "cell_type": "markdown",
65 | "metadata": {
66 | "slideshow": {
67 | "slide_type": "fragment"
68 | }
69 | },
70 | "source": [
71 | "Each process has a unique `id` (integer)\n",
72 | "\n",
73 | "You can see that we added processes with id from 2 to 5"
74 | ]
75 | },
76 | {
77 | "cell_type": "markdown",
78 | "metadata": {
79 | "slideshow": {
80 | "slide_type": "fragment"
81 | }
82 | },
83 | "source": [
84 | "You can also add processes on remote machines.\n",
85 | "\n",
86 | "See the docstring for `addprocs` for more info"
87 | ]
88 | },
89 | {
90 | "cell_type": "markdown",
91 | "metadata": {
92 | "slideshow": {
93 | "slide_type": "subslide"
94 | }
95 | },
96 | "source": [
97 | "To get the number of active processes use the `nprocs` function "
98 | ]
99 | },
100 | {
101 | "cell_type": "code",
102 | "execution_count": 3,
103 | "metadata": {
104 | "collapsed": false,
105 | "slideshow": {
106 | "slide_type": "fragment"
107 | }
108 | },
109 | "outputs": [
110 | {
111 | "data": {
112 | "text/plain": [
113 | "5"
114 | ]
115 | },
116 | "execution_count": 3,
117 | "metadata": {},
118 | "output_type": "execute_result"
119 | }
120 | ],
121 | "source": [
122 | "nprocs()"
123 | ]
124 | },
125 | {
126 | "cell_type": "markdown",
127 | "metadata": {
128 | "slideshow": {
129 | "slide_type": "subslide"
130 | }
131 | },
132 | "source": [
133 | "When you have `n` active processes, typically `n-1` will be used for computation\n",
134 | "\n",
135 | "The first process (with id 1) is used to direct the computation\n",
136 | "\n",
137 | "Other processes are called workers:\n"
138 | ]
139 | },
140 | {
141 | "cell_type": "code",
142 | "execution_count": 4,
143 | "metadata": {
144 | "collapsed": false,
145 | "slideshow": {
146 | "slide_type": "fragment"
147 | }
148 | },
149 | "outputs": [
150 | {
151 | "data": {
152 | "text/plain": [
153 | "4-element Array{Int64,1}:\n",
154 | " 2\n",
155 | " 3\n",
156 | " 4\n",
157 | " 5"
158 | ]
159 | },
160 | "execution_count": 4,
161 | "metadata": {},
162 | "output_type": "execute_result"
163 | }
164 | ],
165 | "source": [
166 | "workers()"
167 | ]
168 | },
169 | {
170 | "cell_type": "markdown",
171 | "metadata": {
172 | "slideshow": {
173 | "slide_type": "slide"
174 | }
175 | },
176 | "source": [
177 | "## `pmap`\n",
178 | "\n",
179 | "\n",
180 | "One of the easiest ways to get started parallel programming in Julia is the `pmap` function\n",
181 | "\n",
182 | "In its simplest form `pmap` takes two arguments: a function and a collection (array or tuple)\n",
183 | "\n",
184 | "The function is applied in parallel to each item in the collection"
185 | ]
186 | },
187 | {
188 | "cell_type": "code",
189 | "execution_count": 7,
190 | "metadata": {
191 | "collapsed": false,
192 | "slideshow": {
193 | "slide_type": "fragment"
194 | }
195 | },
196 | "outputs": [
197 | {
198 | "name": "stdout",
199 | "output_type": "stream",
200 | "text": [
201 | " 0.201885 seconds (98 allocations: 21.513 MB, 1.02% gc time)\n"
202 | ]
203 | }
204 | ],
205 | "source": [
206 | "# TODO find a more compelling econ example\n",
207 | "\n",
208 | "args = (rand(200, 200), rand(400, 400), rand(200, 200), rand(400, 400))\n",
209 | "\n",
210 | "# first a serial version\n",
211 | "@time for X in args\n",
212 | " svd(X)\n",
213 | "end"
214 | ]
215 | },
216 | {
217 | "cell_type": "code",
218 | "execution_count": 8,
219 | "metadata": {
220 | "collapsed": false,
221 | "slideshow": {
222 | "slide_type": "fragment"
223 | }
224 | },
225 | "outputs": [
226 | {
227 | "name": "stdout",
228 | "output_type": "stream",
229 | "text": [
230 | " 0.105647 seconds (1.28 k allocations: 6.189 MB)\n"
231 | ]
232 | }
233 | ],
234 | "source": [
235 | "# now in parallel\n",
236 | "@time pmap(svd, args);"
237 | ]
238 | },
239 | {
240 | "cell_type": "markdown",
241 | "metadata": {
242 | "slideshow": {
243 | "slide_type": "subslide"
244 | }
245 | },
246 | "source": [
247 | "We have 4 workers and had 4 arrays, why didn't get get a 4x speedup?"
248 | ]
249 | },
250 | {
251 | "cell_type": "markdown",
252 | "metadata": {
253 | "slideshow": {
254 | "slide_type": "fragment"
255 | }
256 | },
257 | "source": [
258 | "Notice that 2 arrays were `200x200` and two were `400x400` (computational load is _unbalanced_)\n",
259 | "\n",
260 | "Julia gave each worker one array, but some had less work than others so they finished first"
261 | ]
262 | },
263 | {
264 | "cell_type": "markdown",
265 | "metadata": {
266 | "slideshow": {
267 | "slide_type": "fragment"
268 | }
269 | },
270 | "source": [
271 | "This means some processes were inactive during the total computation time\n",
272 | "\n",
273 | "Also, there is (small) overhead in passing data to the worker and passing the result back to process 1"
274 | ]
275 | },
276 | {
277 | "cell_type": "markdown",
278 | "metadata": {
279 | "slideshow": {
280 | "slide_type": "slide"
281 | }
282 | },
283 | "source": [
284 | "## `@parallel` loops\n",
285 | "\n",
286 | "Julia also has the ability to make a for loop run in parallel\n",
287 | "\n",
288 | "To do this use the `@parallel` macro"
289 | ]
290 | },
291 | {
292 | "cell_type": "markdown",
293 | "metadata": {
294 | "slideshow": {
295 | "slide_type": "fragment"
296 | }
297 | },
298 | "source": [
299 | "> There are subtleties to using `@parallel` \n",
300 | ">\n",
301 | "> We will cover only basic examples here\n",
302 | ">\n",
303 | "> Consult the [documentation](http://docs.julialang.org/en/latest/manual/parallel-computing/#parallel-map-and-loops) for more information"
304 | ]
305 | },
306 | {
307 | "cell_type": "markdown",
308 | "metadata": {
309 | "slideshow": {
310 | "slide_type": "subslide"
311 | }
312 | },
313 | "source": [
314 | "There are two possible syntaxes for `@parallel`:"
315 | ]
316 | },
317 | {
318 | "cell_type": "markdown",
319 | "metadata": {
320 | "slideshow": {
321 | "slide_type": "fragment"
322 | }
323 | },
324 | "source": [
325 | "The first is\n",
326 | "\n",
327 | "```julia\n",
328 | "@parallel for ...\n",
329 | "end\n",
330 | "```\n",
331 | "\n",
332 | "and simply executes the body of the for loop in parallel"
333 | ]
334 | },
335 | {
336 | "cell_type": "markdown",
337 | "metadata": {
338 | "slideshow": {
339 | "slide_type": "fragment"
340 | }
341 | },
342 | "source": [
343 | "The second is\n",
344 | "\n",
345 | "```julia\n",
346 | "@parallel (f) for ...\n",
347 | "end\n",
348 | "```\n",
349 | "\n",
350 | "It the same, but also applies a reduction function `f` to the result of each iteration\n",
351 | "\n",
352 | "The result of each iteration is the result of the last statement in the loop's body\n",
353 | "\n",
354 | "The function `f` should take two elements and output one"
355 | ]
356 | },
357 | {
358 | "cell_type": "markdown",
359 | "metadata": {
360 | "slideshow": {
361 | "slide_type": "subslide"
362 | }
363 | },
364 | "source": [
365 | "Let's see this in practice"
366 | ]
367 | },
368 | {
369 | "cell_type": "code",
370 | "execution_count": 10,
371 | "metadata": {
372 | "collapsed": false,
373 | "slideshow": {
374 | "slide_type": "fragment"
375 | }
376 | },
377 | "outputs": [
378 | {
379 | "name": "stdout",
380 | "output_type": "stream",
381 | "text": [
382 | "\tFrom worker 3:\trandn() = -1.4823226833572487\n",
383 | "\tFrom worker 3:\trandn() = 0.008485042894568029\n",
384 | "\tFrom worker 2:\trandn() = -1.4372949820146432\n",
385 | "\tFrom worker 2:\trandn() = 0.14678523736405727\n",
386 | "\tFrom worker 2:\trandn() = -1.293961883026427\n",
387 | "\tFrom worker 5:\trandn() = -0.39493431470157886\n",
388 | "\tFrom worker 5:\trandn() = 0.2494670221840125\n",
389 | "\tFrom worker 4:\trandn() = -0.6202425665423612\n",
390 | "\tFrom worker 4:\trandn() = -1.853569956218026\n",
391 | "\tFrom worker 3:\trandn() = 1.5581361796835485\n"
392 | ]
393 | }
394 | ],
395 | "source": [
396 | "@parallel for i = 1:10\n",
397 | " @show randn()\n",
398 | "end;"
399 | ]
400 | },
401 | {
402 | "cell_type": "code",
403 | "execution_count": 21,
404 | "metadata": {
405 | "collapsed": false,
406 | "slideshow": {
407 | "slide_type": "fragment"
408 | }
409 | },
410 | "outputs": [
411 | {
412 | "name": "stdout",
413 | "output_type": "stream",
414 | "text": [
415 | "\tFrom worker 2:\trandn() = -1.1271781195591901\n",
416 | "\tFrom worker 2:\trandn() = 0.9025559215992444\n",
417 | "\tFrom worker 3:\trandn() = -0.912358216201542\n",
418 | "\tFrom worker 2:\trandn() = 0.009225014074008112\n",
419 | "\tFrom worker 3:\trandn() = 0.8497725800879736\n",
420 | "\tFrom worker 3:\trandn() = -1.005123841699878\n",
421 | "\tFrom worker 5:\trandn() = -0.3685509736298961\n",
422 | "\tFrom worker 5:\trandn() = -1.637375058113114\n",
423 | "\tFrom worker 4:\trandn() = -1.8505401231390373\n",
424 | "\tFrom worker 4:\trandn() = -0.7754764372191437\n",
425 | "-5.915049253800575\n"
426 | ]
427 | }
428 | ],
429 | "source": [
430 | "# now with a (+)\n",
431 | "total = @parallel (+) for i=1:10\n",
432 | " @show randn()\n",
433 | "end\n",
434 | "println(total)"
435 | ]
436 | },
437 | {
438 | "cell_type": "markdown",
439 | "metadata": {
440 | "slideshow": {
441 | "slide_type": "subslide"
442 | }
443 | },
444 | "source": [
445 | "One issue with `@parallel` is that all variables used in the loop are copied to the working process, but not copied back to process 1\n",
446 | "\n",
447 | "That means code like this will not work as you might expect:"
448 | ]
449 | },
450 | {
451 | "cell_type": "code",
452 | "execution_count": 22,
453 | "metadata": {
454 | "collapsed": false,
455 | "slideshow": {
456 | "slide_type": "fragment"
457 | }
458 | },
459 | "outputs": [
460 | {
461 | "data": {
462 | "text/plain": [
463 | "10-element Array{Float64,1}:\n",
464 | " 0.0\n",
465 | " 0.0\n",
466 | " 0.0\n",
467 | " 0.0\n",
468 | " 0.0\n",
469 | " 0.0\n",
470 | " 0.0\n",
471 | " 0.0\n",
472 | " 0.0\n",
473 | " 0.0"
474 | ]
475 | },
476 | "execution_count": 22,
477 | "metadata": {},
478 | "output_type": "execute_result"
479 | }
480 | ],
481 | "source": [
482 | "a = zeros(10)\n",
483 | "@parallel for i=1:10\n",
484 | " a[i] = i\n",
485 | "end\n",
486 | "a"
487 | ]
488 | },
489 | {
490 | "cell_type": "markdown",
491 | "metadata": {
492 | "slideshow": {
493 | "slide_type": "fragment"
494 | }
495 | },
496 | "source": [
497 | "For that to work we need an array that provides shared memory access across processes..."
498 | ]
499 | },
500 | {
501 | "cell_type": "markdown",
502 | "metadata": {
503 | "slideshow": {
504 | "slide_type": "slide"
505 | }
506 | },
507 | "source": [
508 | "## `SharedArray`\n",
509 | "\n",
510 | "A `SharedArray` is an array whose memory can be shared across all processes on the same machine"
511 | ]
512 | },
513 | {
514 | "cell_type": "markdown",
515 | "metadata": {
516 | "slideshow": {
517 | "slide_type": "fragment"
518 | }
519 | },
520 | "source": [
521 | "This comes with a number of benefits over `Array` for parallel computing.\n",
522 | "\n",
523 | "Some of them are:\n",
524 | "\n",
525 | "- Save on the overhead of passing arrays to worker processes\n",
526 | "- Update arrays in a predictable way from `@parallel` loops"
527 | ]
528 | },
529 | {
530 | "cell_type": "markdown",
531 | "metadata": {
532 | "slideshow": {
533 | "slide_type": "subslide"
534 | }
535 | },
536 | "source": [
537 | "Let's see an example"
538 | ]
539 | },
540 | {
541 | "cell_type": "code",
542 | "execution_count": 30,
543 | "metadata": {
544 | "collapsed": false
545 | },
546 | "outputs": [
547 | {
548 | "name": "stdout",
549 | "output_type": "stream",
550 | "text": [
551 | "[0,0,0,0,0,0,0,0,0,0,0]\n"
552 | ]
553 | }
554 | ],
555 | "source": [
556 | "a = SharedArray(Int, 1000)\n",
557 | "@parallel for i in eachindex(a)\n",
558 | " a[i] = i\n",
559 | "end\n",
560 | "println(a[end-10:end])"
561 | ]
562 | },
563 | {
564 | "cell_type": "markdown",
565 | "metadata": {
566 | "slideshow": {
567 | "slide_type": "fragment"
568 | }
569 | },
570 | "source": [
571 | "I know what you're thinking \"wait, you told me that this example should work\""
572 | ]
573 | },
574 | {
575 | "cell_type": "markdown",
576 | "metadata": {
577 | "slideshow": {
578 | "slide_type": "fragment"
579 | }
580 | },
581 | "source": [
582 | "Well it did..."
583 | ]
584 | },
585 | {
586 | "cell_type": "code",
587 | "execution_count": 31,
588 | "metadata": {
589 | "collapsed": false
590 | },
591 | "outputs": [
592 | {
593 | "name": "stdout",
594 | "output_type": "stream",
595 | "text": [
596 | "[990,991,992,993,994,995,996,997,998,999,1000]\n"
597 | ]
598 | }
599 | ],
600 | "source": [
601 | "println(a[end-10:end])"
602 | ]
603 | },
604 | {
605 | "cell_type": "markdown",
606 | "metadata": {
607 | "slideshow": {
608 | "slide_type": "fragment"
609 | }
610 | },
611 | "source": [
612 | "... but there's a caveat"
613 | ]
614 | },
615 | {
616 | "cell_type": "markdown",
617 | "metadata": {
618 | "slideshow": {
619 | "slide_type": "subslide"
620 | }
621 | },
622 | "source": [
623 | "An `@parallel` loop with a `SharedArray` will run _asynchronously_\n",
624 | "\n",
625 | "This means the instructions will be sent to the workers, and then the main process will just continue without waiting for workers to finish"
626 | ]
627 | },
628 | {
629 | "cell_type": "markdown",
630 | "metadata": {
631 | "slideshow": {
632 | "slide_type": "fragment"
633 | }
634 | },
635 | "source": [
636 | "To fix this problem we need to tell Julia to `@sync` all computations that happen in the loop:"
637 | ]
638 | },
639 | {
640 | "cell_type": "code",
641 | "execution_count": 32,
642 | "metadata": {
643 | "collapsed": false
644 | },
645 | "outputs": [
646 | {
647 | "name": "stdout",
648 | "output_type": "stream",
649 | "text": [
650 | "[990,991,992,993,994,995,996,997,998,999,1000]\n"
651 | ]
652 | }
653 | ],
654 | "source": [
655 | "b = SharedArray(Int, 1000)\n",
656 | "@sync @parallel for i in eachindex(b)\n",
657 | " b[i] = i\n",
658 | "end\n",
659 | "println(b[end-10:end])"
660 | ]
661 | },
662 | {
663 | "cell_type": "markdown",
664 | "metadata": {
665 | "slideshow": {
666 | "slide_type": "fragment"
667 | }
668 | },
669 | "source": [
670 | "Because `SharedArray` data is available to multiple processes, you need to be careful about how and when it is accessed\n",
671 | "\n",
672 | "For more details see the [`SharedArray` docs](http://docs.julialang.org/en/latest/manual/parallel-computing/#id2)"
673 | ]
674 | },
675 | {
676 | "cell_type": "markdown",
677 | "metadata": {
678 | "slideshow": {
679 | "slide_type": "slide"
680 | }
681 | },
682 | "source": [
683 | "## More info\n",
684 | "\n",
685 | "We've only scratched the surface of Julia's parallel computing capabilities. \n",
686 | "\n",
687 | "For more information see these references:\n",
688 | "\n",
689 | "- The offical [documentation](http://docs.julialang.org/en/latest/manual/parallel-computing/) on parallel computing\n",
690 | "- [Parallel Julia](https://github.com/JuliaParallel) github organization\n",
691 | "- Other packages:\n",
692 | " - [MPI.jl](https://github.com/JuliaParallel/MPI.jl)\n",
693 | " - [ParallelAccelerator.jl](https://github.com/IntelLabs/ParallelAccelerator.jl)\n",
694 | " - [DistributedArrays.jl](https://github.com/JuliaParallel/DistributedArrays.jl)"
695 | ]
696 | }
697 | ],
698 | "metadata": {
699 | "celltoolbar": "Slideshow",
700 | "kernelspec": {
701 | "display_name": "Julia 0.4.3-pre",
702 | "language": "julia",
703 | "name": "julia-0.4"
704 | },
705 | "language_info": {
706 | "file_extension": ".jl",
707 | "mimetype": "application/julia",
708 | "name": "julia",
709 | "version": "0.4.3"
710 | },
711 | "livereveal": {
712 | "scroll": true,
713 | "start_slideshow_at": "selected",
714 | "theme": "white",
715 | "transition": "fade"
716 | }
717 | },
718 | "nbformat": 4,
719 | "nbformat_minor": 0
720 | }
721 |
--------------------------------------------------------------------------------
/lecture6/install_julia:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | wget -O julia_binary.tar.gz https://julialang.s3.amazonaws.com/bin/linux/x64/0.4/julia-0.4-latest-linux-x86_64.tar.gz
4 |
5 | mkdir -p $HOME/src
6 |
7 | rm -rf $HOME/src/julia*
8 | mkdir -p $HOME/src/julia
9 | tar -C $HOME/src/julia -zxf julia_binary.tar.gz --strip-components=1
10 | rm julia_binary.tar.gz
11 |
12 | # prepend julia to path
13 | echo "Adding julia to path"
14 | echo "export PATH=$HOME/src/julia/bin:$PATH" >> $HOME/.bashrc
15 |
--------------------------------------------------------------------------------
/lecture7/Intro_to_pymc_pres.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture7/Intro_to_pymc_pres.pdf
--------------------------------------------------------------------------------
/lecture7/background_linalg.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture7/background_linalg.pdf
--------------------------------------------------------------------------------
/lecture7/lecture7.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture7/lecture7.pdf
--------------------------------------------------------------------------------
/lecture8/analysis_intermediate.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture8/analysis_intermediate.pdf
--------------------------------------------------------------------------------
/lecture8/analysis_introductory.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture8/analysis_introductory.pdf
--------------------------------------------------------------------------------
/lecture8/lecture8.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture8/lecture8.pdf
--------------------------------------------------------------------------------
/lecture8/presentation.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture8/presentation.pdf
--------------------------------------------------------------------------------
/lecture9/lecture9.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/lecture9/lecture9.pdf
--------------------------------------------------------------------------------
/other_PDFS/benhabib_wealth_distribution.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/other_PDFS/benhabib_wealth_distribution.pdf
--------------------------------------------------------------------------------
/other_PDFS/lp_spaces.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jstac/quantecon_nyu_2016/25face5e85627cb3e94b0885210619867bc5e648/other_PDFS/lp_spaces.pdf
--------------------------------------------------------------------------------