my-markdown-library 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. checksums.yaml +7 -0
  2. data/F24LS_md/ Lecture 4 - Public.md +347 -0
  3. data/F24LS_md/Lecture 1 - Introduction and Overview.md +327 -0
  4. data/F24LS_md/Lecture 10 - Development_.md +631 -0
  5. data/F24LS_md/Lecture 11 - Econometrics.md +345 -0
  6. data/F24LS_md/Lecture 12 - Finance.md +692 -0
  7. data/F24LS_md/Lecture 13 - Environmental Economics.md +299 -0
  8. data/F24LS_md/Lecture 15 - Conclusion.md +272 -0
  9. data/F24LS_md/Lecture 2 - Demand.md +349 -0
  10. data/F24LS_md/Lecture 3 - Supply.md +329 -0
  11. data/F24LS_md/Lecture 5 - Production C-D.md +291 -0
  12. data/F24LS_md/Lecture 6 - Utility and Latex.md +440 -0
  13. data/F24LS_md/Lecture 7 - Inequality.md +607 -0
  14. data/F24LS_md/Lecture 8 - Macroeconomics.md +704 -0
  15. data/F24LS_md/Lecture 8 - Macro.md +700 -0
  16. data/F24LS_md/Lecture 9 - Game Theory_.md +436 -0
  17. data/F24LS_md/summary.yaml +105 -0
  18. data/F24Lec_MD/LecNB_summary.yaml +206 -0
  19. data/F24Lec_MD/lec01/lec01.md +267 -0
  20. data/F24Lec_MD/lec02/Avocados_demand.md +425 -0
  21. data/F24Lec_MD/lec02/Demand_Steps_24.md +126 -0
  22. data/F24Lec_MD/lec02/PriceElasticity.md +83 -0
  23. data/F24Lec_MD/lec02/ScannerData_Beer.md +171 -0
  24. data/F24Lec_MD/lec02/demand-curve-Fa24.md +213 -0
  25. data/F24Lec_MD/lec03/3.0-CubicCostCurve.md +239 -0
  26. data/F24Lec_MD/lec03/3.1-Supply.md +274 -0
  27. data/F24Lec_MD/lec03/3.2-sympy.md +332 -0
  28. data/F24Lec_MD/lec03/3.3a-california-energy.md +120 -0
  29. data/F24Lec_MD/lec03/3.3b-a-really-hot-tuesday.md +121 -0
  30. data/F24Lec_MD/lec04/lec04-CSfromSurvey-closed.md +335 -0
  31. data/F24Lec_MD/lec04/lec04-CSfromSurvey.md +331 -0
  32. data/F24Lec_MD/lec04/lec04-Supply-Demand-closed.md +519 -0
  33. data/F24Lec_MD/lec04/lec04-Supply-Demand.md +514 -0
  34. data/F24Lec_MD/lec04/lec04-four-plot-24.md +34 -0
  35. data/F24Lec_MD/lec04/lec04-four-plot.md +34 -0
  36. data/F24Lec_MD/lec05/Lec5-Cobb-Douglas.md +131 -0
  37. data/F24Lec_MD/lec05/Lec5-CobbD-AER1928.md +283 -0
  38. data/F24Lec_MD/lec06/6.1-Sympy-Differentiation.md +253 -0
  39. data/F24Lec_MD/lec06/6.2-3D-utility.md +287 -0
  40. data/F24Lec_MD/lec06/6.3-QuantEcon-Optimization.md +399 -0
  41. data/F24Lec_MD/lec06/6.4-latex.md +138 -0
  42. data/F24Lec_MD/lec06/6.5-Edgeworth.md +269 -0
  43. data/F24Lec_MD/lec07/7.1-inequality.md +283 -0
  44. data/F24Lec_MD/lec07/7.2-historical-inequality.md +237 -0
  45. data/F24Lec_MD/lec08/macro-fred-api.md +313 -0
  46. data/F24Lec_MD/lec09/lecNB-prisoners-dilemma.md +88 -0
  47. data/F24Lec_MD/lec10/Lec10.2-waterguard.md +401 -0
  48. data/F24Lec_MD/lec10/lec10.1-mapping.md +199 -0
  49. data/F24Lec_MD/lec11/11.1-slr.md +305 -0
  50. data/F24Lec_MD/lec11/11.2-mlr.md +171 -0
  51. data/F24Lec_MD/lec12/Lec12-4-PersonalFinance.md +590 -0
  52. data/F24Lec_MD/lec12/lec12-1_Interest_Payments.md +267 -0
  53. data/F24Lec_MD/lec12/lec12-2-stocks-options.md +235 -0
  54. data/F24Lec_MD/lec13/Co2_ClimateChange.md +139 -0
  55. data/F24Lec_MD/lec13/ConstructingMAC.md +213 -0
  56. data/F24Lec_MD/lec13/EmissionsTracker.md +170 -0
  57. data/F24Lec_MD/lec13/KuznetsHypothesis.md +219 -0
  58. data/F24Lec_MD/lec13/RoslingPlots.md +217 -0
  59. data/F24Lec_MD/lec15/vibecession.md +485 -0
  60. data/F24Textbook_MD/00-intro/index.md +292 -0
  61. data/F24Textbook_MD/01-demand/01-demand.md +152 -0
  62. data/F24Textbook_MD/01-demand/02-example.md +131 -0
  63. data/F24Textbook_MD/01-demand/03-log-log.md +284 -0
  64. data/F24Textbook_MD/01-demand/04-elasticity.md +248 -0
  65. data/F24Textbook_MD/01-demand/index.md +15 -0
  66. data/F24Textbook_MD/02-supply/01-supply.md +203 -0
  67. data/F24Textbook_MD/02-supply/02-eep147-example.md +86 -0
  68. data/F24Textbook_MD/02-supply/03-sympy.md +138 -0
  69. data/F24Textbook_MD/02-supply/04-market-equilibria.md +204 -0
  70. data/F24Textbook_MD/02-supply/index.md +16 -0
  71. data/F24Textbook_MD/03-public/govt-intervention.md +73 -0
  72. data/F24Textbook_MD/03-public/index.md +10 -0
  73. data/F24Textbook_MD/03-public/surplus.md +351 -0
  74. data/F24Textbook_MD/03-public/taxes-subsidies.md +282 -0
  75. data/F24Textbook_MD/04-production/index.md +15 -0
  76. data/F24Textbook_MD/04-production/production.md +178 -0
  77. data/F24Textbook_MD/04-production/shifts.md +296 -0
  78. data/F24Textbook_MD/05-utility/budget-constraints.md +166 -0
  79. data/F24Textbook_MD/05-utility/index.md +15 -0
  80. data/F24Textbook_MD/05-utility/utility.md +136 -0
  81. data/F24Textbook_MD/06-inequality/historical-inequality.md +253 -0
  82. data/F24Textbook_MD/06-inequality/index.md +15 -0
  83. data/F24Textbook_MD/06-inequality/inequality.md +226 -0
  84. data/F24Textbook_MD/07-game-theory/bertrand.md +257 -0
  85. data/F24Textbook_MD/07-game-theory/cournot.md +333 -0
  86. data/F24Textbook_MD/07-game-theory/equilibria-oligopolies.md +96 -0
  87. data/F24Textbook_MD/07-game-theory/expected-utility.md +61 -0
  88. data/F24Textbook_MD/07-game-theory/index.md +19 -0
  89. data/F24Textbook_MD/07-game-theory/python-classes.md +340 -0
  90. data/F24Textbook_MD/08-development/index.md +35 -0
  91. data/F24Textbook_MD/09-macro/CentralBanks.md +101 -0
  92. data/F24Textbook_MD/09-macro/Indicators.md +77 -0
  93. data/F24Textbook_MD/09-macro/fiscal_policy.md +36 -0
  94. data/F24Textbook_MD/09-macro/index.md +14 -0
  95. data/F24Textbook_MD/09-macro/is_curve.md +76 -0
  96. data/F24Textbook_MD/09-macro/phillips_curve.md +70 -0
  97. data/F24Textbook_MD/10-finance/index.md +10 -0
  98. data/F24Textbook_MD/10-finance/options.md +178 -0
  99. data/F24Textbook_MD/10-finance/value-interest.md +60 -0
  100. data/F24Textbook_MD/11-econometrics/index.md +16 -0
  101. data/F24Textbook_MD/11-econometrics/multivariable.md +218 -0
  102. data/F24Textbook_MD/11-econometrics/reading-econ-papers.md +25 -0
  103. data/F24Textbook_MD/11-econometrics/single-variable.md +483 -0
  104. data/F24Textbook_MD/11-econometrics/statsmodels.md +58 -0
  105. data/F24Textbook_MD/12-environmental/KuznetsHypothesis-Copy1.md +187 -0
  106. data/F24Textbook_MD/12-environmental/KuznetsHypothesis.md +187 -0
  107. data/F24Textbook_MD/12-environmental/MAC.md +254 -0
  108. data/F24Textbook_MD/12-environmental/index.md +36 -0
  109. data/F24Textbook_MD/LICENSE.md +11 -0
  110. data/F24Textbook_MD/intro.md +26 -0
  111. data/F24Textbook_MD/references.md +25 -0
  112. data/F24Textbook_MD/summary.yaml +414 -0
  113. metadata +155 -0
@@ -0,0 +1,237 @@
1
+ ---
2
+ title: "7.2-historical-inequality"
3
+ type: lecture-notebook
4
+ week: 7
5
+ source_path: "/Users/ericvandusen/Documents/Data88E-ForTraining/F24Lec_NBs/lec07/7.2-historical-inequality.ipynb"
6
+ ---
7
+
8
+ ```python
9
+ import pandas as pd
10
+ import numpy as np
11
+ import matplotlib as mpl
12
+ import matplotlib.pyplot as plt
13
+ from datascience import *
14
+ %matplotlib inline
15
+ plt.style.use('seaborn-v0_8-muted')
16
+ mpl.rcParams['figure.figsize'] = (10.0, 10.0)
17
+ ```
18
+
19
+ # Income Inequality Historically
20
+
21
+ <!-- Written by Amal Bhatnagar -->
22
+
23
+ In the last chart on the previous page, you may have noticed that income inequality was rising in the United States in the last few decades. We will examine this in more detail, and also observe global trends in inequality.
24
+
25
+ ## The United States
26
+
27
+ Let's look at historical trends of income inequality in the US over the last 100 years. The data has been collected from [The World Inequality Database](https://wid.world/), which is co-directed by Berkeley Economics professors Emanuel Saez and Gabriel Zucman. Specifically, we will observe income distributions for the bottom 50 percent, top 10 percent, and top 1 percent.
28
+
29
+ ```python
30
+ us_hist = Table.read_table("US_inequality.csv")
31
+ us_hist.show(5)
32
+ ```
33
+
34
+ ```python
35
+ us_hist.take(np.arange(100,105))
36
+ ```
37
+
38
+ Let's begin with some data cleaning: it seems like our 3 brackets are 'vertically stacked' on top of each other. Instead, we would like a table with 5 columns: `Year`, `bottom 50% income share`, `top 10% income share`, and `top 1% income share`.
39
+
40
+ ```python
41
+ bottom_50_us = us_hist.where("Percentile", "p0p50").drop("Percentile").relabeled("Income Share", "Bottom 50% Share")
42
+ top_10_us = us_hist.where("Percentile", "p90p100").drop("Percentile").relabeled("Income Share", "Top 10% Share")
43
+ top_1_us = us_hist.where("Percentile", "p99p100").drop("Percentile").relabeled("Income Share", "Top 1% Share")
44
+ us_hist_joined = bottom_50_us.join("Year", top_10_us).join("Year", top_1_us)
45
+ us_hist_joined
46
+ ```
47
+
48
+ Oh no, there are some `nan` values! NaN (not a number) values are very common in real world datasets: often, we may not have some observations simply because no data was collected, or perhaps the data collected was faulty. Sometimes, we can try to impute or replace NaN values in order to avoid having gaps in our data, but for now let's ignore NaNs and when plotting to see what's going on:
49
+
50
+ ```python
51
+ # mpl.rcParams['figure.dpi'] = 120
52
+ us_hist_joined.plot("Year", width=11, height=7)
53
+ plt.title("Income Share over Time", fontsize = 16)
54
+ plt.ylabel("Proportion", fontsize = 14)
55
+ plt.xlabel("Year", fontsize = 14)
56
+ plt.show()
57
+ ```
58
+
59
+ # Income Inequality for the Rest of the World
60
+
61
+ Now let's examine the trends of income inequality in other parts of the world.
62
+
63
+ ```python
64
+ world_hist = Table.read_table("World_Inequality.csv")
65
+ bottom_50_world = world_hist.where("Percentile", "p0p50").drop("Percentile")
66
+ top_10_world = world_hist.where("Percentile", "p90p100").drop("Percentile")
67
+ top_1_world = world_hist.where("Percentile", "p99p100").drop("Percentile")
68
+ top_10_world
69
+ ```
70
+
71
+ ```python
72
+ top_10_world.plot("Year", width=11, height=7)
73
+ plt.ylabel("Gini Coefficient", fontsize=14)
74
+ plt.xlabel("Year", fontsize=14)
75
+ plt.title("Income Inequality over Time", fontsize=18);
76
+ ```
77
+
78
+ Just like the US, it seems global inequality has been rising around the world, especially in China, India, Russia, and across Europe. However, in absolute terms, the level of income inequality in Europe is much lower than that in the United States.
79
+
80
+ Also look at Russia: income inequality spiked up around 1991. This was likely caused by the fall of the USSR: the failing Soviet state left the ownership of state assets uncontested, which allowed former USSR officials to acquire state property through informal deals. This led to the rise of many Russian oligarchs - those who rapidly accumulated wealth during the era of Russian privatization directly follwing the dissolution of the Soviet Union.
81
+
82
+ ```python
83
+ top_10_world.select("Year", "USA", "Europe").plot("Year", width=11, height=7)
84
+ plt.ylabel("Gini Coefficient", fontsize=14)
85
+ plt.xlabel("Year", fontsize=14)
86
+ plt.title("Income Inequality over Time", fontsize=18);
87
+ ```
88
+
89
+ ## The Elephant Graph
90
+
91
+ ```{figure} elephant_curve.jpg
92
+ ---
93
+ width: 500px
94
+ name: elephant-curve
95
+ ---
96
+ The elephant curve {cite}`10inequality-elephantCurve`
97
+ ```
98
+
99
+ The elephant curve is a graph that shows the real income growth per adult across each income group’s percentile around the world.
100
+
101
+ There are 3 key features of the elephant curve: a hump for the world’s poorest, valley for the middle class, and trunk for the upper class. The thump is made of the world’s poorest countries, most likely those from developing countries. The valley comprises the working class from the developed world and upper class from developing countries. The trunk is made of people from the upper class from developed countries. The hump and valley indicate growth among emerging countries, and the top global 1%’s growth is higher than any other income group, thus explaining the positively sloped shape of the trunk.
102
+
103
+
104
+ A study done by the Brookings Institution] found that “poorer countries, and the lower income groups within those countries, have grown most rapidly in the past 20 years” {cite}`10inequality-brookings`. This supports the World Bank’s claim that inequality between countries and within countries is decreasing. The Brookings Institution used only household surveys, however, which usually excludes the top and bottom percentile of the population, due to non-response bias. Still, the study is useful in corroborating the trends and growth in global income inequality.
105
+
106
+ # Factors that Affect Income Inequality
107
+
108
+ Economists have isolated multiple factors that influence a country's income inequality
109
+ - top marginal tax rates
110
+ - unemployment rates
111
+ - population growth
112
+
113
+ We will look at each of these scenarios independently and see its overall trends
114
+
115
+ ## Top marginal tax rates
116
+
117
+ Let's also take a look at the top marginal tax rates in the United States throughout this time. Overall, the United States (and most of the rest of the world) has a progressive tax system, which means that the more income you earn, the higher percentage you will be taxed at. A good way to reduce income inequality is through progressive taxation; having the richer paying a higher portion of their income will help increase equality. Currently, the top marginal tax rate is 37%, as we can see in the table below.
118
+
119
+ ```{figure} MTR.png
120
+ ---
121
+ width: 500px
122
+ name: irs
123
+ ---
124
+ Marginal tax rates. Image from the IRS
125
+ ```
126
+
127
+ The top marginal tax rate only applies to the portion of your income above a certain income level. For example, if you earned 19501 dollars in 2019, then you will pay 1940 dollars plus 12% of $19501-19401$, i.e. 12 dollars. For another example, if you earned 80000 dollars, then you will pay $9086 + 0.22(80000-78950) = 9317$ dollars in tax, effectively a $\frac{9317}{80000} = 11.6\%$ tax rate.
128
+
129
+ In general, the idea is you will pay a lower tax rate for your first $x$ dollars, but a higher rate for dollars earned over $x$.
130
+
131
+ Now let's look at the historical trends in marginal top tax rates, which is the % taxed at the highest tax bracket.
132
+
133
+ ```python
134
+ toptax = Table.read_table("toptaxrate.csv")
135
+ toptax
136
+ ```
137
+
138
+ ```python
139
+ # mpl.rcParams['figure.dpi'] = 120
140
+ toptax.plot(0,1, width=11, height=7)
141
+ plt.title("Top Marginal Tax Rate over Time", fontsize = 18)
142
+ plt.show()
143
+ ```
144
+
145
+ ```python
146
+ # mpl.rcParams['figure.dpi'] = 120
147
+ us_hist_joined.plot("Year", width=11, height=7)
148
+ plt.title("Income Share over Time", fontsize = 18)
149
+ plt.ylabel("Proportion", fontsize = 14)
150
+ plt.xlabel("Year", fontsize = 14)
151
+ plt.show()
152
+ ```
153
+
154
+ This graph depicts income inequality decreasing between 1910 and 1970 and increasing from 1970 to present.
155
+
156
+ In 1913, Congress implemented the current income tax to promote equality. Originally meant to help compensate for revenue lost from reducing high tariffs, the new policy essentially made the top 1% start contributing to taxes. Additionally, the top marginal tax rate increased from 7% in 1913 to 73% in 1918, thus helping reduce income inequality. Right before the Great Depression, income inequality peaked, where the richest 1% possessed 19.6% of all income. During the Great Depression, top marginal tax rates increased, peaking at 94% in 1944. The top marginal tax rate decreased but remained high over subsequent decades, where it was 70% in 1965 and 50% in 1982. These high top marginal tax rates are correlated with low income inequality. During the Great Depression, the richest 1% had about 15% of total income. For the 10 years after the Great Depression, the top 1% had below 10% of total income and 8% for the 30 years afterwards. This period was known as the Great Compression, as income differentials between the top 1% and the rest of the country decreased.
157
+
158
+ In the 1970s, the economy took a turn for the worse with high unemployment and inflation (stagflation) and low growth. In order to stimulate economic growth, the government reduced top marginal tax rates (70% to 38.5% in 1980s), deregulated corporate institutions, and attacked labor union memberships (membership decreased by half within 30 years). Although these policies improved economic growth, it resulted in higher income inequality.
159
+
160
+ The graph below better shows that the share of income earned by the bottom 50% percentile steadily decreased, while the share earned by the top 1% increased steadily. This means that the top 1% has more wealth than the entire bottom 50% of the population. Suppose a class of 100 people has \$100 in aggregate. In a world with perfect equality, each person would have \$1. With this current level of income inequality, one person would have more wealth than 50 people combined.
161
+
162
+ The media continues to report on the nation's significant income disparity. [The Washington Post wrote a story](https://www.washingtonpost.com/business/2019/09/26/income-inequality-america-highest-its-been-since-census-started-tracking-it-data-show/) and found that “The number of families earning \$15,000 or less has fallen since 2007, according to the latest census data, while the number of households bringing in \$250,000 a year or more has grown more than 15 percent.”
163
+
164
+ Can we conclude that high marginal tax rates lead to low income inequality but slow economic growth?
165
+
166
+ ## Unemployment rates
167
+
168
+ Economists believe that unemployment is one of the leading factors that leads to income inequality. When looking at what influences the Gini coefficient, a paper from [Princeton](https://rpds.princeton.edu/sites/rpds/files/media/menendez_unemployment_ar.pdf) found that the unemployment rate had the largest effect on the income inequality rates
169
+
170
+ Below, we look at the unemployment rates for the past 20 years across many different countries. These are the same countries and regions that we will further study below.
171
+
172
+ ```python
173
+ unemployment = Table.read_table("Unemployment.csv")
174
+ unemployment
175
+ ```
176
+
177
+ As we can see from the graph, the unemployment rates for China, India and the rest of the world have stayed somewhat steady. On the other hand, Brazil, the US, Russia and Europe are encountering drastically different unemployment situations than before.
178
+
179
+ ```python
180
+ # mpl.rcParams['figure.dpi'] = 120
181
+ unemployment.plot("Year", width=11, height=7)
182
+ plt.ylabel("Unemployment Rate (%)", fontsize = 14)
183
+ plt.xlabel("Year", fontsize =14)
184
+ plt.title("Unemployment Rate over Time", fontsize = 18)
185
+ plt.show()
186
+ ```
187
+
188
+ ```python
189
+ top_10_world.plot("Year", width=11, height=7)
190
+ plt.ylabel("Gini Coefficient", size=14)
191
+ plt.xlabel("Year", fontsize=14)
192
+ plt.title("International Income Inequality over Time", fontsize=18)
193
+ plt.show()
194
+ ```
195
+
196
+ The graphs above show a positive correlation between unemployment and income inequality. As unemployment increases, income inequality also increases. In 2011, Hanan Morsy, an Egyptian economist who serves as the Director of Macroeconomic Policy, Forecasting and Research at the African Development Bank, actually researched this topic {cite}`10inequality-morsy`. Her group examined the member nations of the Organization for Economic Cooperation and Development (OECD) between 1980 and 2005. She found that specific groups that were vulnerable to the economic shocks that led to an increase in income inequality:
197
+ - young workers
198
+ - low-skilled workers
199
+ - workers who had been out of work for a long time
200
+
201
+ Her solution was to increase job creation opportunities for temporary, recently fired, and recently hired workers, provide job assistance and training to prevent long-term unemployment, and improve incentives for working by aligning incentives with productivity of labor. Morsy's research found that the most vulnerable groups to economic shocks were young, low-skilled and temporary workers. Creating opportunities for these different demographics would help them be more protected from potential shocks and thus decrease income inequality.
202
+
203
+ ## Population growth
204
+
205
+ As the number of people in a country's population increase, it becomes more difficult for a country to distribute its public goods to everyone. This leads to many social consequences in which resources are not fairly distributed to all members of the population, cause inaccessibility for different parts of the population
206
+
207
+ The table below shows how the population growth has changed for the same countries we saw above. We are only looking at data for the past 10 years.
208
+
209
+ ```python
210
+ pop_growth = Table.read_table("Population Growth.csv")
211
+ pop_growth
212
+ ```
213
+
214
+ ```python
215
+ pop_growth.plot("Year", width=11, height=7)
216
+ plt.title("Population Growth over Time", fontsize = 18)
217
+ plt.ylabel("Population Growth (%)", fontsize = 14)
218
+ plt.xlabel("Year", fontsize = 14)
219
+ plt.show()
220
+ ```
221
+
222
+ ```python
223
+ top_10_world.plot("Year", width=11, height=7)
224
+ plt.ylabel("Gini Coefficient", fontsize=14)
225
+ plt.xlabel("Year", fontsize=14)
226
+ plt.title("International Income Inequality over Time", fontsize=18)
227
+ plt.show()
228
+ ```
229
+
230
+ The graphs above show that most countries high population growth between 1-2% during the 1980's. The effects of this can be seen in the rising income inequality during the 90's.
231
+
232
+ Recent research by University of Toronto's Marijn Bolhuis and Univeristy of Oxford's Alexandra de Pleijt shows that there is a strong correlation between a country's population growth (measured by birth rates) and its income inequality {cite}`10inequality-popGrowth`. Their most recent study in 2016 analyzed income inequality and birth rates data between 1870 and 2000 across 67 countries. They concluded that if a country had 50% higher income inequality, then that country's birth rate would be about twice as high as another country with the same level of economic development. Bolhuis says that these higher birth rates mean that economic growth has to be equal to or greater than the birth rate to offset the implications of higher birth rates.
233
+
234
+ This is part of a larger debate about the relationship between birth rates and income inequality. Economist Thomas Piketty finds that low birth rates, rather than high birth rates, are causing today's income inequality. With lower birth rates, fewer children per couple are being borne, so these children get more of their parents' inheritance.
235
+
236
+
237
+
@@ -0,0 +1,313 @@
1
+ ---
2
+ title: "macro-fred-api"
3
+ type: lecture-notebook
4
+ week: 8
5
+ source_path: "/Users/ericvandusen/Documents/Data88E-ForTraining/F24Lec_NBs/lec08/macro-fred-api.ipynb"
6
+ ---
7
+
8
+ <table style="width: 100%;" id="nb-header">
9
+ <tr style="background-color: transparent;"><td>
10
+ <img src="https://data-88e.github.io/assets/images/blue_text.png" width="250px" style="margin-left: 0;" />
11
+ </td><td>
12
+ <p style="text-align: right; font-size: 10pt;"><strong>Economic Models</strong>, EdX<br>
13
+ Dr. Eric Van Dusen <br>
14
+ Alan Liang <br>
15
+ Umar Maniku <br>
16
+ Matthew Yep <br>
17
+ Yiyang Chen <br>
18
+ Bennett Somerville <br>
19
+ Akhil Venkatesh <br>
20
+ </table>
21
+
22
+ # Lecture Notebook 4.3: Macroeconomic Indicators
23
+
24
+ ```python
25
+ import os
26
+ import pandas as pd
27
+ import numpy as np
28
+ import matplotlib.pyplot as plt
29
+ %matplotlib inline
30
+ ```
31
+
32
+ ```python
33
+ # Load our API key from the .env file
34
+ def load_environment_variables(filename):
35
+ with open(filename) as f:
36
+ for line in f:
37
+ (key, value) = line.replace('\n', '').split('=')
38
+ os.environ[key] = value
39
+
40
+ load_environment_variables('.env')
41
+ ```
42
+
43
+ **Learning Objectives:**
44
+ In this notebook, we will download macroeconomic data and examine how the models and theories we discussed in lecture fit with these real life data. This notebook will introduce the basic concept of an API, the way to use it for economic research and study.
45
+
46
+ ## Macroeconomic Data and Fred API
47
+
48
+ We will start by first download some macroeconomic data from FRED. In this notebook, we will introduce two ways of interacting with online sources of data with API (Application Programming Interface).
49
+
50
+ **What is an API?**
51
+ In contrast to a user interface, which connects a computer to a person, an application programming interface (API) connects computers or pieces of software to each other. It is not intended to be used directly by a person (the end user) other than a computer programmer who is incorporating it into the software. An API is often made up of different parts which act as tools or services that are available to the programmer. A program or a programmer that uses one of these parts is said to call that portion of the API. The calls that make up the API are also known as subroutines, methods, requests, or endpoints. An API specification defines these calls, meaning that it explains how to use or implement them.
52
+
53
+ In basic terms, APIs just allow applications to communicate with one another. For the APIs we are concerned right now--web based APIs that return data in response to a request made by us--**they allow us to get data from outside sources by sending an API a request detailing the information we want. Then the API will "respond" with the requested data to us.**
54
+
55
+ ![What is an API](what_is_an_API.png)
56
+ Source: Perry Eising, "What exactly is an API?"
57
+
58
+ ### General Way of Interacting with an API
59
+
60
+ First, we will use the most common way to access the data through an API without using any packages other may have already built for a particular site. Usually this is the way you would interact with an API.
61
+
62
+ #### Step 1: Get the API Key
63
+ **In most cases, you will need to get an API key in order to access an API.** For many resources, it involves some paperwork to apply and/or limited free usage, so it is good practice to keep your API keys private as long as it is possible. In this lecture notebook, we will use macroeconomic data from FRED, which is one of the most famous and convenient sources of economic data. For FRED, the process of getting an API key is simple. Request the API key [here](https://fred.stlouisfed.org/docs/api/api_key.html).
64
+
65
+ ```python
66
+ # Because API keys are private, we store our API key in the .env file in this directory, which is not published on GitHub.
67
+ api_key = os.environ['API_KEY']
68
+ ```
69
+
70
+ #### Step 2: Learn to use the API
71
+ Using an API is like ordering food at a restaurant with a menu. To have a delicious meal, we have to know what food the restaurant offers, and any other additional information (for example, how would you like your steak). Similarly, it is very important for us to know what requests an API take through the API documentation. **The API documentation will inform us about how we can use specific instructions to obtain the data that we want, and what the returned data would look like.** Look up the Fred API's documentation [here](https://fred.stlouisfed.org/docs/api/fred/series_observations.html).
72
+
73
+ <div class="alert alert-info">
74
+ <b> Example: "api.stlouisfed.org/fred/series/observations?series_id=GNPCA&api_key=abcdefghijklmnopqrstuvwxyz123456" <br>
75
+
76
+ Endpoint: "api.stlouisfed.org/fred/series/observations" <br>
77
+ Parameters: series_id=GNPCA, api_key=abcdefghijklmnopqrstuvwxyz123456
78
+ </div>
79
+
80
+ Try the following link, replacing REDACTED with your API key to see what the API will return!
81
+
82
+ https://api.stlouisfed.org/fred/series/observations?series_id=GNPCA&api_key=REDACTED&file_type=json
83
+
84
+ #### Step 3: Make the fetch
85
+ Now we are ready to start writing code to fetch the data we want through the API.
86
+
87
+ ```python
88
+ import requests
89
+ from urllib.parse import urlencode
90
+ endpoint = "https://api.stlouisfed.org/fred/series/observations"
91
+ ```
92
+
93
+ Parameters that we would like to include in our query (these can be found in the FRED API documentations!):
94
+ - `series_id`: The Economic data series we want (e.g. GDP)
95
+ - `observation_start`: The earliest date we want our data to include
96
+ - `observation_end`: The latest date we want our data to include
97
+ - `frequency`: The frequency of observations (e.g. Annually, Quarterly, Monthly)
98
+ - `units`: The units of observations (e.g. plain numbers, percentage change from a year ago, etc.)
99
+ - `api_key`: The API key
100
+ - `file_type`: The file data that will returned from FRED (e.g. json, xml)
101
+
102
+ ```python
103
+ def fetch(series_id, unit="lin"):
104
+ # specifies parameters
105
+ params = {"series_id": series_id,
106
+ "observation_start": "1958-01-01",
107
+ "observation_end": "2023-10-01",
108
+ "frequency": "q", # quarterly
109
+ "units": unit,
110
+ "api_key": api_key,
111
+ "file_type": "json"
112
+ }
113
+
114
+ # forms API request
115
+ url_params = urlencode(params)
116
+ url = f"{endpoint}?{url_params}"
117
+
118
+ # fires off the request
119
+ res = requests.get(url)
120
+
121
+ # checks if the request encounters an error
122
+ if res.status_code not in range(200, 299):
123
+ raise Exception(f'Fetch request for "{series_id}" failed (Error: {res.status_code})')
124
+
125
+ # return the content of the response
126
+ return res.json()
127
+ ```
128
+
129
+ Let's try downloading the real GDP data from Fred now.
130
+
131
+ ```python
132
+ res_json_GDP = fetch("GDPC1")
133
+
134
+ # process the response
135
+ gdp = pd.DataFrame(res_json_GDP["observations"])[["date", "value"]]
136
+ gdp.rename(columns={"value": "GDP"}, inplace=True)
137
+ gdp['date'] = pd.to_datetime(gdp['date'], format='%Y-%m-%d')
138
+ gdp['GDP'] = pd.to_numeric(gdp['GDP'])
139
+ gdp
140
+ ```
141
+
142
+ ```python
143
+ plt.plot(gdp['date'], gdp['GDP'])
144
+ ```
145
+
146
+ **Congratulations! We have just made our first request to Fred API and obtain some useful data.**
147
+
148
+ Now let's try download more macroeconomic indicators.
149
+
150
+ ```python
151
+ # this function allows us to fetch and process the data together
152
+ def fetch_and_process(series_id, unit="lin"):
153
+ res_json = fetch(series_id, unit)
154
+ return pd.DataFrame(res_json["observations"])[["date", "value"]].rename(columns={"date":"DATE", "value": f"{series_id}_{unit}"})
155
+ ```
156
+
157
+ ```python
158
+ gdp = fetch_and_process("GDPC1")
159
+ gdp_growth = fetch_and_process("GDPC1", "pc1")
160
+ inflation = fetch_and_process("CPIAUCSL", "pc1")
161
+ unemployment = fetch_and_process("UNRATE")
162
+ ffr = fetch_and_process("DFF")
163
+ nrou = fetch_and_process("NROU")
164
+ core_inflation = fetch_and_process("CPILFESL", "pc1")
165
+ potential_gdp = fetch_and_process("GDPPOT")
166
+
167
+ gdp_growth
168
+ ```
169
+
170
+ ### Site-Specific Prebuilt Packages
171
+
172
+ Now we will switch gears to use some prebuilt packages to access FRED API. Notice that the prebuilt packages are not necessarily available for every API. But here we will use the "FredAPI" package developed by Mortada Mehyar. Documentation [here](https://github.com/mortada/fredapi).
173
+
174
+ ```python
175
+ # may need to install the package and its dependencies
176
+ !pip install fredapi
177
+ from fredapi import Fred
178
+ fred = Fred(api_key=api_key)
179
+ GDP_fredapi = pd.DataFrame(fred.get_series('GDP'))
180
+ GDP_fredapi
181
+ ```
182
+
183
+ You will get more practice with FREDAPI package in the lab!
184
+
185
+ ## Macroeconomic Indicators and Data
186
+
187
+ **Now we're off to use the data we have just got!**
188
+
189
+ ```python
190
+ # merge all macroeconomic data we have got!
191
+ macroeconomics = gdp.merge(gdp_growth)\
192
+ .merge(inflation)\
193
+ .merge(unemployment)\
194
+ .merge(ffr)\
195
+ .merge(core_inflation)\
196
+ .merge(nrou)\
197
+ .merge(potential_gdp)\
198
+
199
+
200
+ macroeconomics = macroeconomics.set_index("DATE").apply(pd.to_numeric).reset_index("DATE")
201
+
202
+ potential_gdp = potential_gdp[potential_gdp["DATE"] <= max(macroeconomics["DATE"])]
203
+ ```
204
+
205
+ ```python
206
+ macroeconomics.head()
207
+ ```
208
+
209
+ ---
210
+
211
+ ### Macroeconomics Indicators and their Time Series
212
+ In this section, we will see how the value of each macroeconomic indicator varied from 1958 to present.
213
+
214
+ ```python
215
+ series_names = {"GDPC1_lin": "GDP",
216
+ "GDPC1_pc1": "GDP Growth",
217
+ "CPIAUCSL_pc1": "Inflation",
218
+ "UNRATE_lin": "Unemployment Rate",
219
+ "DFF_lin": "Fed Funds Rate",
220
+ "CPILFESL_pc1": "Core Inflation Rate",
221
+ "NROU_lin": "Noncyclical Rate of Unemployment" # aka NAIRU, Natural Rate of Unemployment (Long-term)
222
+ }
223
+
224
+ # generate a time series plot for "series"
225
+ def plot_time_series(data, series, title="1958 to present", figsize=(14, 8)):
226
+
227
+ plt.figure(figsize=figsize)
228
+
229
+ plt.plot(data["DATE"], data[series])
230
+
231
+ # plot a horizontal line at zero if the variable is in percent
232
+ if ("pc1" in series):
233
+ plt.axhline(y=0, color='black', linestyle='-.')
234
+
235
+ plt.xticks(data.index[np.arange(0, data.shape[0], 16)], rotation=45)
236
+ plt.grid(visible=True)
237
+
238
+ plt.xlabel("DATE")
239
+ plt.ylabel(series)
240
+ plt.title(f"{series_names[series]}: {title}", fontsize=16)
241
+ ```
242
+
243
+ #### GDP
244
+
245
+ ```python
246
+ plot_time_series(macroeconomics, "GDPC1_lin")
247
+ ```
248
+
249
+ #### GDP Growth
250
+
251
+ ```python
252
+ plot_time_series(macroeconomics, "GDPC1_pc1")
253
+ ```
254
+
255
+ #### Inflation Rate
256
+
257
+ ```python
258
+ plot_time_series(macroeconomics, "CPIAUCSL_pc1")
259
+ ```
260
+
261
+ #### Core Inflation Rate
262
+
263
+ ```python
264
+ plot_time_series(macroeconomics, "CPILFESL_pc1")
265
+ ```
266
+
267
+ #### Unemployment Rate
268
+
269
+ ```python
270
+ plot_time_series(macroeconomics, "UNRATE_lin")
271
+ ```
272
+
273
+ #### Fed Funds Rate
274
+
275
+ ```python
276
+ plot_time_series(macroeconomics, "DFF_lin")
277
+ ```
278
+
279
+ #### Noncyclical Rate of Unemployment
280
+
281
+ ```python
282
+ plot_time_series(macroeconomics, "NROU_lin")
283
+ ```
284
+
285
+ What interesting pattern do you notice?
286
+
287
+ ## Making an empirical Phillips curve
288
+
289
+ The above plots are interesting, but they can be found on the FRED website. Let's take the FRED data and do some data science with it.
290
+
291
+ ```python
292
+ plt.scatter(macroeconomics['UNRATE_lin'], macroeconomics['CPILFESL_pc1'])
293
+ plt.title('Empirical Phillips Curve')
294
+ plt.xlabel('Unemployment Rate')
295
+ plt.ylabel('Inflation');
296
+ ```
297
+
298
+ We can see a somewhat negative relationship, but it's not as strong as the relationship we saw during lecture, which used pre-WWI data. In the lab for this week, you'll use the FRED API to dive deeper into the relationship between the unemployment rate and inflation and see how it has changed over time.
299
+
300
+ ```python
301
+ plt.scatter(macroeconomics['UNRATE_lin'], macroeconomics['GDPC1_lin'])
302
+ plt.title('Okun\'s Law With Emprirical Data')
303
+ plt.xlabel('Unemployment Rate')
304
+ plt.ylabel('Output (GDP)');
305
+ ```
306
+
307
+ ```python
308
+ plt.scatter(macroeconomics['DFF_lin'], macroeconomics['GDPC1_lin'])
309
+ plt.title('Empirical IS Curve')
310
+ plt.xlabel('Real Interest Rate')
311
+ plt.ylabel('Output (GDP)');
312
+ ```
313
+
@@ -0,0 +1,88 @@
1
+ ---
2
+ title: "lecNB-prisoners-dilemma"
3
+ type: lecture-notebook
4
+ week: 9
5
+ source_path: "/Users/ericvandusen/Documents/Data88E-ForTraining/F24Lec_NBs/lec09/lecNB-prisoners-dilemma.ipynb"
6
+ ---
7
+
8
+ <table style="width: 100%;" id="nb-header">
9
+ <tr style="background-color: transparent;"><td>
10
+ <img src="https://data-88e.github.io/assets/images/blue_text.png" width="250px" style="margin-left: 0;" />
11
+ </td><td>
12
+ <p style="text-align: right; font-size: 10pt;"><strong>Economic Models</strong>, EdX<br>
13
+ Dr. Eric Van Dusen <br>
14
+ Chris Pyles <br>
15
+ Akhil Venkatesh <br>
16
+ </table>
17
+
18
+ ```python
19
+ from players import *
20
+ ```
21
+
22
+ # Lecture Notebook: Iterated Prisoner's Dilemma
23
+
24
+ ## Iterated Prisoner's Dilemma
25
+
26
+ ```python
27
+ def human_play(self, opponent):
28
+ if len(opponent.history):
29
+ print(f"Your opponents last move: {'D' if opponent.history.item(-1) else 'C'}")
30
+ move = input("Your move: (d/c) ").strip().upper()
31
+ assert move in ["D", "C"], f"invalid move: {move}"
32
+ return move == "D"
33
+
34
+ Human = create_player_class("Human", human_play)
35
+ ```
36
+
37
+ ```python
38
+ n_rounds = input("How many rounds would you like to play? [10] ")
39
+ if n_rounds:
40
+ n_rounds = int(n_rounds)
41
+ else:
42
+ n_rounds = 10
43
+
44
+ show_opponent = input("Show opponent? (y/n) [n] ").strip().upper() == "Y"
45
+
46
+ human = Human()
47
+ opponent = np.random.choice(make_array(
48
+ Alternator(), Backstabber(), Bully(), Desperate(), FoolMeOnce(), Forgiver(),
49
+ Grudger(), OnceBitten()
50
+ ))
51
+
52
+ if show_opponent:
53
+ print(f"Your opponent is: {opponent}")
54
+
55
+ winner = run_match(human, opponent, n_rounds)
56
+ if winner == Human():
57
+ winner = "you!"
58
+ print(f"The winner is: {winner}")
59
+ ```
60
+
61
+ ### Using `create_player_class`
62
+
63
+ **From the project:**
64
+
65
+ In this project, we will be creating some custom Python classes to represent different playing strategies. We'll be using the `create_player_class` function (provided) to create these classes for us. Here are a few important things to know about the classes:
66
+
67
+ Consider a player class instance `player = Player()`.
68
+ * `player.play()` is a method that represents a single move. It returns `True` if the player **defects** and `False` if they cooperate.
69
+ * `player.history` is an array of the previous moves of the player instance. `player.history.item(-1)`, for example, is the most recent move (the last element of the array).
70
+
71
+ Don't worry about the other methods that are defined for players; these are there for the code below to work but you don't need to concern yourself with them. Here's how the `create_player_class` function works:
72
+
73
+ 1. Define a function, `f`, that takes two arguments: `self` and `opponent` (more about these later), and returns `True` if the player defects and `False` otherwise.
74
+ 2. Call `create_player_class` with two arguments: a string for the class name, e.g. `"Player"`, and the play method `f`.
75
+
76
+ Let's illustrate this by creating `Defector`, a player that always defects. The `defector_play` function below always returns `True`, since the player always defects. We then create `Defector` using `create_player_class`.
77
+
78
+ **Example:** Defector
79
+
80
+ ```python
81
+ def defector_play(self, opponent):
82
+ return True
83
+
84
+ Defector = create_player_class("Defector", defector_play)
85
+ ```
86
+
87
+
88
+