Skip to content

Commit a0f977f

Browse files
committed
[WASM] make business_cycle WASM compatible
1 parent 1a7c13a commit a0f977f

File tree

1 file changed

+45
-60
lines changed

1 file changed

+45
-60
lines changed

lectures/business_cycle.md

Lines changed: 45 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jupytext:
44
extension: .md
55
format_name: myst
66
format_version: 0.13
7-
jupytext_version: 1.14.5
7+
jupytext_version: 1.16.7
88
kernelspec:
99
display_name: Python 3 (ipykernel)
1010
language: python
@@ -28,8 +28,7 @@ In addition to the packages already installed by Anaconda, this lecture requires
2828
```{code-cell} ipython3
2929
:tags: [hide-output]
3030
31-
!pip install wbgapi
32-
!pip install pandas-datareader
31+
!pip install pandas-datareader distutils requests pyodide_http
3332
```
3433

3534
We use the following imports
@@ -38,8 +37,15 @@ We use the following imports
3837
import matplotlib.pyplot as plt
3938
import pandas as pd
4039
import datetime
41-
import wbgapi as wb
4240
import pandas_datareader.data as web
41+
import requests
42+
```
43+
44+
We use the following library to help us read data from the http requests in WASM environment (Browser)
45+
46+
```{code-cell} ipython3
47+
import pyodide_http # makes it possible to read https links in pyodide
48+
pyodide_http.patch_all()
4349
```
4450

4551
Here's some minor code to help with colors in our plots.
@@ -53,41 +59,46 @@ cycler = plt.cycler(linestyle=['-', '-.', '--', ':'],
5359
plt.rc('axes', prop_cycle=cycler)
5460
```
5561

56-
5762
## Data acquisition
5863

59-
We will use the World Bank's data API `wbgapi` and `pandas_datareader` to retrieve data.
64+
We will use the World Bank's API data stored in [QuantEcon's data](https://github.com/QuantEcon/data) repository.
6065

6166
We can use `wb.series.info` with the argument `q` to query available data from
6267
the [World Bank](https://www.worldbank.org/en/home).
68+
For the following example, we have used `wb.series.info(q='GDP growth')`
69+
and stored the data in the above repository.
6370

64-
For example, let's retrieve the GDP growth data ID to query GDP growth data.
71+
Let's use the retrieved GDP growth data:
6572

6673
```{code-cell} ipython3
67-
wb.series.info(q='GDP growth')
74+
info_url = "https://raw.githubusercontent.com/QuantEcon/data/main/lecture-python-intro/dynamic/business_cycle_info.md"
75+
info_url_response = requests.get(info_url)
76+
wb_series_info_data = str(info_url_response.content, "utf-8")
77+
print(wb_series_info_data)
6878
```
6979

70-
71-
Now we use this series ID to obtain the data.
80+
Now we using this series ID and using API `wb.data.DataFrame('NY.GDP.MKTP.KD.ZG', ['USA', 'ARG', 'GBR', 'GRC', 'JPN'], labels=True)`
81+
we get the following dataframe that is stored in the QuantEcon's data:
7282

7383
```{code-cell} ipython3
74-
gdp_growth = wb.data.DataFrame('NY.GDP.MKTP.KD.ZG',
75-
['USA', 'ARG', 'GBR', 'GRC', 'JPN'],
76-
labels=True)
84+
gdp_growth_df_url = "https://raw.githubusercontent.com/QuantEcon/data/main/lecture-python-intro/dynamic/business_cycle_data.csv"
85+
gdp_growth = pd.read_csv(gdp_growth_df_url)
7786
gdp_growth
7887
```
7988

89+
We can look at the series' metadata to learn more about the series (click to expand) using the API `wb.series.metadata.get('NY.GDP.MKTP.KD.ZG')`.
8090

81-
We can look at the series' metadata to learn more about the series (click to expand).
91+
Here's the output of the metadata
8292

8393
```{code-cell} ipython3
8494
:tags: [hide-output]
8595
86-
wb.series.metadata.get('NY.GDP.MKTP.KD.ZG')
96+
metadata_url = "https://raw.githubusercontent.com/QuantEcon/data/main/lecture-python-intro/dynamic/business_cycle_metadata.md"
97+
metadata_url_response = requests.get(metadata_url)
98+
wb_metadata = str(metadata_url_response.content, "utf-8")
99+
print(wb_metadata)
87100
```
88101

89-
90-
91102
(gdp_growth)=
92103
## GDP growth rate
93104

@@ -96,11 +107,9 @@ First we look at GDP growth.
96107
Let's source our data from the World Bank and clean it.
97108

98109
```{code-cell} ipython3
99-
# Use the series ID retrieved before
100-
gdp_growth = wb.data.DataFrame('NY.GDP.MKTP.KD.ZG',
101-
['USA', 'ARG', 'GBR', 'GRC', 'JPN'],
102-
labels=True)
103-
gdp_growth = gdp_growth.set_index('Country')
110+
# Let's drop the economy column as we already have Country.
111+
# Set Country as the index.
112+
gdp_growth = gdp_growth.drop('economy', axis=1).set_index('Country')
104113
gdp_growth.columns = gdp_growth.columns.str.replace('YR', '').astype(int)
105114
```
106115

@@ -186,17 +195,15 @@ t_params = {'color':'grey', 'fontsize': 9,
186195
'va':'center', 'ha':'center'}
187196
```
188197

189-
190198
Let's start with the United States.
191199

192200
```{code-cell} ipython3
193201
---
194202
mystnb:
195203
figure:
196-
caption: "United States (GDP growth rate %)"
204+
caption: United States (GDP growth rate %)
197205
name: us_gdp
198206
---
199-
200207
fig, ax = plt.subplots()
201208
202209
country = 'United States'
@@ -207,8 +214,6 @@ plot_series(gdp_growth, country,
207214
plt.show()
208215
```
209216

210-
+++ {"user_expressions": []}
211-
212217
GDP growth is positive on average and trending slightly downward over time.
213218

214219
We also see fluctuations over GDP growth over time, some of which are quite large.
@@ -226,10 +231,9 @@ Notice the very large dip during the Covid-19 pandemic.
226231
---
227232
mystnb:
228233
figure:
229-
caption: "United Kingdom (GDP growth rate %)"
234+
caption: United Kingdom (GDP growth rate %)
230235
name: uk_gdp
231236
---
232-
233237
fig, ax = plt.subplots()
234238
235239
country = 'United Kingdom'
@@ -239,8 +243,6 @@ plot_series(gdp_growth, country,
239243
plt.show()
240244
```
241245

242-
+++ {"user_expressions": []}
243-
244246
Now let's consider Japan, which experienced rapid growth in the 1960s and
245247
1970s, followed by slowed expansion in the past two decades.
246248

@@ -251,10 +253,9 @@ Global Financial Crisis (GFC) and the Covid-19 pandemic.
251253
---
252254
mystnb:
253255
figure:
254-
caption: "Japan (GDP growth rate %)"
256+
caption: Japan (GDP growth rate %)
255257
name: jp_gdp
256258
---
257-
258259
fig, ax = plt.subplots()
259260
260261
country = 'Japan'
@@ -270,10 +271,9 @@ Now let's study Greece.
270271
---
271272
mystnb:
272273
figure:
273-
caption: "Greece (GDP growth rate %)"
274+
caption: Greece (GDP growth rate %)
274275
name: gc_gdp
275276
---
276-
277277
fig, ax = plt.subplots()
278278
279279
country = 'Greece'
@@ -292,10 +292,9 @@ Next let's consider Argentina.
292292
---
293293
mystnb:
294294
figure:
295-
caption: "Argentina (GDP growth rate %)"
295+
caption: Argentina (GDP growth rate %)
296296
name: arg_gdp
297297
---
298-
299298
fig, ax = plt.subplots()
300299
301300
country = 'Argentina'
@@ -343,11 +342,10 @@ defined by the NBER.
343342
---
344343
mystnb:
345344
figure:
346-
caption: "Long-run unemployment rate, US (%)"
345+
caption: Long-run unemployment rate, US (%)
347346
name: lrunrate
348347
tags: [hide-input]
349348
---
350-
351349
# We use the census bureau's estimate for the unemployment rate
352350
# between 1942 and 1948
353351
years = [datetime.datetime(year, 6, 1) for year in range(1942, 1948)]
@@ -390,7 +388,6 @@ ax.set_ylabel('unemployment rate (%)')
390388
plt.show()
391389
```
392390

393-
394391
The plot shows that
395392

396393
* expansions and contractions of the labor market have been highly correlated
@@ -418,9 +415,7 @@ With slight modifications, we can use our previous function to draw a plot
418415
that includes multiple countries.
419416

420417
```{code-cell} ipython3
421-
---
422-
tags: [hide-input]
423-
---
418+
:tags: [hide-input]
424419
425420
426421
def plot_comparison(data, countries,
@@ -497,17 +492,14 @@ t_params = {'color':'grey', 'fontsize': 9,
497492
Here we compare the GDP growth rate of developed economies and developing economies.
498493

499494
```{code-cell} ipython3
500-
---
501-
tags: [hide-input]
502-
---
495+
:tags: [hide-input]
503496
504497
# Obtain GDP growth rate for a list of countries
505498
gdp_growth = wb.data.DataFrame('NY.GDP.MKTP.KD.ZG',
506499
['CHN', 'USA', 'DEU', 'BRA', 'ARG', 'GBR', 'JPN', 'MEX'],
507500
labels=True)
508501
gdp_growth = gdp_growth.set_index('Country')
509502
gdp_growth.columns = gdp_growth.columns.str.replace('YR', '').astype(int)
510-
511503
```
512504

513505
We use the United Kingdom, United States, Germany, and Japan as examples of developed economies.
@@ -516,11 +508,10 @@ We use the United Kingdom, United States, Germany, and Japan as examples of deve
516508
---
517509
mystnb:
518510
figure:
519-
caption: "Developed economies (GDP growth rate %)"
511+
caption: Developed economies (GDP growth rate %)
520512
name: adv_gdp
521513
tags: [hide-input]
522514
---
523-
524515
fig, ax = plt.subplots()
525516
countries = ['United Kingdom', 'United States', 'Germany', 'Japan']
526517
ylabel = 'GDP growth rate (%)'
@@ -537,11 +528,10 @@ We choose Brazil, China, Argentina, and Mexico as representative developing econ
537528
---
538529
mystnb:
539530
figure:
540-
caption: "Developing economies (GDP growth rate %)"
531+
caption: Developing economies (GDP growth rate %)
541532
name: deve_gdp
542533
tags: [hide-input]
543534
---
544-
545535
fig, ax = plt.subplots()
546536
countries = ['Brazil', 'China', 'Argentina', 'Mexico']
547537
plot_comparison(gdp_growth.loc[countries, 1962:],
@@ -551,7 +541,6 @@ plot_comparison(gdp_growth.loc[countries, 1962:],
551541
plt.show()
552542
```
553543

554-
555544
The comparison of GDP growth rates above suggests that
556545
business cycles are becoming more synchronized in 21st-century recessions.
557546

@@ -571,11 +560,10 @@ the United Kingdom, Japan, and France.
571560
---
572561
mystnb:
573562
figure:
574-
caption: "Developed economies (unemployment rate %)"
563+
caption: Developed economies (unemployment rate %)
575564
name: adv_unemp
576565
tags: [hide-input]
577566
---
578-
579567
unempl_rate = wb.data.DataFrame('SL.UEM.TOTL.NE.ZS',
580568
['USA', 'FRA', 'GBR', 'JPN'], labels=True)
581569
unempl_rate = unempl_rate.set_index('Country')
@@ -623,11 +611,10 @@ year-on-year
623611
---
624612
mystnb:
625613
figure:
626-
caption: "Consumer sentiment index and YoY CPI change, US"
614+
caption: Consumer sentiment index and YoY CPI change, US
627615
name: csicpi
628616
tags: [hide-input]
629617
---
630-
631618
start_date = datetime.datetime(1978, 1, 1)
632619
end_date = datetime.datetime(2022, 12, 31)
633620
@@ -705,11 +692,10 @@ from 1919 to 2022 in the US to show this trend.
705692
---
706693
mystnb:
707694
figure:
708-
caption: "YoY real output change, US (%)"
695+
caption: YoY real output change, US (%)
709696
name: roc
710697
tags: [hide-input]
711698
---
712-
713699
start_date = datetime.datetime(1919, 1, 1)
714700
end_date = datetime.datetime(2022, 12, 31)
715701
@@ -753,11 +739,10 @@ percentage of GDP by banks from 1970 to 2022 in the UK.
753739
---
754740
mystnb:
755741
figure:
756-
caption: "Domestic credit to private sector by banks (% of GDP)"
742+
caption: Domestic credit to private sector by banks (% of GDP)
757743
name: dcpc
758744
tags: [hide-input]
759745
---
760-
761746
private_credit = wb.data.DataFrame('FS.AST.PRVT.GD.ZS',
762747
['GBR'], labels=True)
763748
private_credit = private_credit.set_index('Country')

0 commit comments

Comments
 (0)