Skip to content

Commit 6ef26b0

Browse files
committed
markov_chains_I
1 parent f83ecee commit 6ef26b0

File tree

5 files changed

+334
-80
lines changed

5 files changed

+334
-80
lines changed

lectures/_static/lecture_specific/graphviz/.ipynb_checkpoints/graphviz_generation-checkpoint.ipynb

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
},
6060
{
6161
"cell_type": "code",
62-
"execution_count": 12,
62+
"execution_count": 3,
6363
"id": "16e9379d-bf94-4c79-9a34-4dbc2cc41b0e",
6464
"metadata": {},
6565
"outputs": [
@@ -69,7 +69,7 @@
6969
"'../lake_model/lake_model_worker.png'"
7070
]
7171
},
72-
"execution_count": 12,
72+
"execution_count": 3,
7373
"metadata": {},
7474
"output_type": "execute_result"
7575
}
@@ -92,9 +92,110 @@
9292
"G.edge('2', '2', label='(1-λ)(1-d)')\n",
9393
"G.edge('3', '3', label='(1-α)(1-d)')\n",
9494
"\n",
95-
"# Show graphviz\n",
95+
"# Save Plot\n",
9696
"G.render(filename='../lake_model/lake_model_worker')"
9797
]
98+
},
99+
{
100+
"cell_type": "markdown",
101+
"id": "194ca10b-dd02-4210-adbc-c2bb8b699d45",
102+
"metadata": {},
103+
"source": [
104+
"## Markov Chains I\n",
105+
"\n",
106+
"### Example 1\n",
107+
"\n",
108+
"Hamilton on US unemployment data"
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": 4,
114+
"id": "75e86ad6-d11c-4c36-920d-cbab0e3d97d2",
115+
"metadata": {},
116+
"outputs": [
117+
{
118+
"data": {
119+
"text/plain": [
120+
"'../markov_chains_I/hamilton.pdf'"
121+
]
122+
},
123+
"execution_count": 4,
124+
"metadata": {},
125+
"output_type": "execute_result"
126+
}
127+
],
128+
"source": [
129+
"dot = Digraph(comment='Graph')\n",
130+
"dot.attr(rankdir='LR')\n",
131+
"dot.node(\"ng\")\n",
132+
"dot.node(\"mr\")\n",
133+
"dot.node(\"sr\")\n",
134+
"\n",
135+
"dot.edge(\"ng\", \"ng\", label=\"0.971\")\n",
136+
"dot.edge(\"ng\", \"mr\", label=\"0.029\")\n",
137+
"dot.edge(\"mr\", \"ng\", label=\"0.145\")\n",
138+
"\n",
139+
"dot.edge(\"mr\", \"mr\", label=\"0.778\")\n",
140+
"dot.edge(\"mr\", \"sr\", label=\"0.077\")\n",
141+
"dot.edge(\"sr\", \"mr\", label=\"0.508\")\n",
142+
"\n",
143+
"dot.edge(\"sr\", \"sr\", label=\"0.492\")\n",
144+
"dot\n",
145+
"\n",
146+
"dot.render(filename='../markov_chains_I/hamilton')"
147+
]
148+
},
149+
{
150+
"cell_type": "markdown",
151+
"id": "1b7b4b45-f6a4-495d-9115-638aafe9acd8",
152+
"metadata": {},
153+
"source": [
154+
"### Exercise 1\n",
155+
"\n",
156+
"Solution 2:"
157+
]
158+
},
159+
{
160+
"cell_type": "code",
161+
"execution_count": 5,
162+
"id": "46c4612f-3f8b-4c83-b02a-81fe67545e8f",
163+
"metadata": {},
164+
"outputs": [
165+
{
166+
"data": {
167+
"text/plain": [
168+
"'../markov_chains_I/Temple.pdf'"
169+
]
170+
},
171+
"execution_count": 5,
172+
"metadata": {},
173+
"output_type": "execute_result"
174+
}
175+
],
176+
"source": [
177+
"dot = Digraph(comment='Graph')\n",
178+
"dot.attr(rankdir='LR')\n",
179+
"dot.node(\"Growth\")\n",
180+
"dot.node(\"Stagnation\")\n",
181+
"dot.node(\"Collapse\")\n",
182+
"\n",
183+
"dot.edge(\"Growth\", \"Growth\", label=\"0.68\")\n",
184+
"dot.edge(\"Growth\", \"Stagnation\", label=\"0.12\")\n",
185+
"dot.edge(\"Growth\", \"Collapse\", label=\"0.20\")\n",
186+
"\n",
187+
"dot.edge(\"Stagnation\", \"Stagnation\", label=\"0.24\")\n",
188+
"dot.edge(\"Stagnation\", \"Growth\", label=\"0.50\")\n",
189+
"dot.edge(\"Stagnation\", \"Collapse\", label=\"0.26\")\n",
190+
"\n",
191+
"dot.edge(\"Collapse\", \"Collapse\", label=\"0.46\")\n",
192+
"dot.edge(\"Collapse\", \"Stagnation\", label=\"0.18\")\n",
193+
"dot.edge(\"Collapse\", \"Growth\", label=\"0.36\")\n",
194+
"\n",
195+
"dot\n",
196+
"\n",
197+
"dot.render(filename='../markov_chains_I/Temple')"
198+
]
98199
}
99200
],
100201
"metadata": {
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "ccd1c797-db85-4293-8436-0cd442d1d9ae",
6+
"metadata": {},
7+
"source": [
8+
"# Code for Graphviz Plots "
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 1,
14+
"id": "6f6a377d-1fc8-4f2a-8e92-b550a7c92828",
15+
"metadata": {},
16+
"outputs": [
17+
{
18+
"name": "stdout",
19+
"output_type": "stream",
20+
"text": [
21+
"Requirement already satisfied: graphviz in /Users/humphreyyang/anaconda3/envs/quantecon/lib/python3.9/site-packages (0.20.1)\n"
22+
]
23+
}
24+
],
25+
"source": [
26+
"!pip install graphviz"
27+
]
28+
},
29+
{
30+
"cell_type": "markdown",
31+
"id": "ad403a5a-868c-45ed-a360-a81aee9cfd38",
32+
"metadata": {},
33+
"source": [
34+
"```{admonition} graphviz\n",
35+
":class: warning\n",
36+
"If you are running this lecture locally it requires [graphviz](https://www.graphviz.org)\n",
37+
"to be installed on your computer. Installation instructions for graphviz can be found\n",
38+
"[here](https://www.graphviz.org/download/) \n",
39+
"```"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 2,
45+
"id": "428161a2-e0d5-402e-bf16-1d1e460a30e7",
46+
"metadata": {},
47+
"outputs": [],
48+
"source": [
49+
"from graphviz import Digraph\n"
50+
]
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"id": "75d07327-0fcc-41f1-8d36-b8b8d4eb1060",
55+
"metadata": {},
56+
"source": [
57+
"## Lake Model"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": 3,
63+
"id": "16e9379d-bf94-4c79-9a34-4dbc2cc41b0e",
64+
"metadata": {},
65+
"outputs": [
66+
{
67+
"data": {
68+
"text/plain": [
69+
"'../lake_model/lake_model_worker.png'"
70+
]
71+
},
72+
"execution_count": 3,
73+
"metadata": {},
74+
"output_type": "execute_result"
75+
}
76+
],
77+
"source": [
78+
"# Create Digraph object\n",
79+
"G = Digraph(format='png')\n",
80+
"G.attr(rankdir='LR')\n",
81+
"\n",
82+
"# Add nodes\n",
83+
"G.attr('node', shape='circle')\n",
84+
"G.node('1', 'New entrants', color='blue')\n",
85+
"G.node('2', 'Unemployed')\n",
86+
"G.node('3', 'Employed')\n",
87+
"\n",
88+
"# Add edges\n",
89+
"G.edge('1', '2', label='b')\n",
90+
"G.edge('2', '3', label='λ(1-d)')\n",
91+
"G.edge('3', '2', label='α(1-d)')\n",
92+
"G.edge('2', '2', label='(1-λ)(1-d)')\n",
93+
"G.edge('3', '3', label='(1-α)(1-d)')\n",
94+
"\n",
95+
"# Save Plot\n",
96+
"G.render(filename='../lake_model/lake_model_worker')"
97+
]
98+
},
99+
{
100+
"cell_type": "markdown",
101+
"id": "194ca10b-dd02-4210-adbc-c2bb8b699d45",
102+
"metadata": {},
103+
"source": [
104+
"## Markov Chains I\n",
105+
"\n",
106+
"### Example 1\n",
107+
"\n",
108+
"Hamilton on US unemployment data"
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": 6,
114+
"id": "75e86ad6-d11c-4c36-920d-cbab0e3d97d2",
115+
"metadata": {},
116+
"outputs": [
117+
{
118+
"data": {
119+
"text/plain": [
120+
"'../markov_chains_I/Hamilton.png'"
121+
]
122+
},
123+
"execution_count": 6,
124+
"metadata": {},
125+
"output_type": "execute_result"
126+
}
127+
],
128+
"source": [
129+
"dot = Digraph(format='png')\n",
130+
"dot.attr(rankdir='LR')\n",
131+
"dot.node(\"ng\")\n",
132+
"dot.node(\"mr\")\n",
133+
"dot.node(\"sr\")\n",
134+
"\n",
135+
"dot.edge(\"ng\", \"ng\", label=\"0.971\")\n",
136+
"dot.edge(\"ng\", \"mr\", label=\"0.029\")\n",
137+
"dot.edge(\"mr\", \"ng\", label=\"0.145\")\n",
138+
"\n",
139+
"dot.edge(\"mr\", \"mr\", label=\"0.778\")\n",
140+
"dot.edge(\"mr\", \"sr\", label=\"0.077\")\n",
141+
"dot.edge(\"sr\", \"mr\", label=\"0.508\")\n",
142+
"\n",
143+
"dot.edge(\"sr\", \"sr\", label=\"0.492\")\n",
144+
"dot\n",
145+
"\n",
146+
"dot.render(filename='../markov_chains_I/Hamilton')"
147+
]
148+
},
149+
{
150+
"cell_type": "markdown",
151+
"id": "1b7b4b45-f6a4-495d-9115-638aafe9acd8",
152+
"metadata": {},
153+
"source": [
154+
"### Exercise 1\n",
155+
"\n",
156+
"Solution 2:"
157+
]
158+
},
159+
{
160+
"cell_type": "code",
161+
"execution_count": 5,
162+
"id": "46c4612f-3f8b-4c83-b02a-81fe67545e8f",
163+
"metadata": {},
164+
"outputs": [
165+
{
166+
"data": {
167+
"text/plain": [
168+
"'../markov_chains_I/Temple.png'"
169+
]
170+
},
171+
"execution_count": 5,
172+
"metadata": {},
173+
"output_type": "execute_result"
174+
}
175+
],
176+
"source": [
177+
"dot = Digraph(format='png')\n",
178+
"dot.attr(rankdir='LR')\n",
179+
"dot.node(\"Growth\")\n",
180+
"dot.node(\"Stagnation\")\n",
181+
"dot.node(\"Collapse\")\n",
182+
"\n",
183+
"dot.edge(\"Growth\", \"Growth\", label=\"0.68\")\n",
184+
"dot.edge(\"Growth\", \"Stagnation\", label=\"0.12\")\n",
185+
"dot.edge(\"Growth\", \"Collapse\", label=\"0.20\")\n",
186+
"\n",
187+
"dot.edge(\"Stagnation\", \"Stagnation\", label=\"0.24\")\n",
188+
"dot.edge(\"Stagnation\", \"Growth\", label=\"0.50\")\n",
189+
"dot.edge(\"Stagnation\", \"Collapse\", label=\"0.26\")\n",
190+
"\n",
191+
"dot.edge(\"Collapse\", \"Collapse\", label=\"0.46\")\n",
192+
"dot.edge(\"Collapse\", \"Stagnation\", label=\"0.18\")\n",
193+
"dot.edge(\"Collapse\", \"Growth\", label=\"0.36\")\n",
194+
"\n",
195+
"dot\n",
196+
"\n",
197+
"dot.render(filename='../markov_chains_I/Temple')"
198+
]
199+
}
200+
],
201+
"metadata": {
202+
"kernelspec": {
203+
"display_name": "Python 3 (ipykernel)",
204+
"language": "python",
205+
"name": "python3"
206+
},
207+
"language_info": {
208+
"codemirror_mode": {
209+
"name": "ipython",
210+
"version": 3
211+
},
212+
"file_extension": ".py",
213+
"mimetype": "text/x-python",
214+
"name": "python",
215+
"nbconvert_exporter": "python",
216+
"pygments_lexer": "ipython3",
217+
"version": "3.9.16"
218+
}
219+
},
220+
"nbformat": 4,
221+
"nbformat_minor": 5
222+
}
Loading
Loading

0 commit comments

Comments
 (0)