Skip to content

Instantly share code, notes, and snippets.

@drbh
Last active March 10, 2021 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drbh/261d08754a987166d627a0eecf04400d to your computer and use it in GitHub Desktop.
Save drbh/261d08754a987166d627a0eecf04400d to your computer and use it in GitHub Desktop.
A snippet to see how much money is added to USD M2 every year - data from the St. Louis Federal Reserve
import pandas as pd
# view original data here
# https://fred.stlouisfed.org/series/M2SL
url = "https://fred.stlouisfed.org/graph/fredgraph.csv?bgcolor=%23e1e9f0&chart_type=line&drp=0&fo=open%20sans&graph_bgcolor=%23ffffff&height=450&mode=fred&recession_bars=on&txtcolor=%23444444&ts=12&tts=12&width=968&nt=0&thu=0&trc=0&show_legend=yes&show_axis_titles=yes&show_tooltip=yes&id=M2SL&scale=left&cosd=1959-01-01&coed=2021-01-01&line_color=%234572a7&link_values=false&line_style=solid&mark_type=none&mw=3&lw=2&ost=-99999&oet=99999&mma=0&fml=a&fq=Monthly&fam=avg&fgst=lin&fgsnd=2020-02-01&line_index=1&transformation=lin&vintage_date=2021-03-10&revision_date=2021-03-10&nd=1959-01-01"
df = pd.read_csv(url)
#### UNCOMMENT TO ACCOUNT FOR 1.9T
#new_total = df.tail(1)["M2SL"].values[0] + 1900.0
#df = df.append(pd.DataFrame([["2021-03-10", new_total]], columns = ["DATE", "M2SL"]))
# add column and set datetimes
df["DATE"] = pd.to_datetime(df["DATE"])
df["newm"] = df["M2SL"].diff(1).shift(-1)
# group by year
grouped = df.groupby(df["DATE"].dt.year)
# make new values
percent_printed_of_total = grouped["newm"].sum() / df["M2SL"].max()
available_each_year = grouped["M2SL"].max()
new_each_year = grouped["newm"].sum()
# build output table
final = pd.concat([
available_each_year, new_each_year, percent_printed_of_total
], axis=1)
final.columns = ["M2", "NEW", "PERCENT"]
print(final)
# $ python3 percent_money_printed_fed.py
# M2 NEW PERCENT
# DATE
# 1959 297.8 11.6 0.000598
# 1960 312.4 15.9 0.000820
# 1961 335.5 23.4 0.001207
# 1962 362.7 27.7 0.001428
# 1963 393.2 30.0 0.001547
# ... ... ... ...
# 2017 13849.1 581.7 0.029993
# 2018 14365.6 576.7 0.029735
# 2019 15319.8 969.3 0.049978
# 2020 19088.8 3978.2 0.205119 <----- crazy
# 2021 19394.6 0.0 0.000000 <-------- about to add 1.9T
# [63 rows x 3 columns]
##### OUTPUT IF 1.9T is added
# M2 NEW PERCENT
# DATE
# 1959 297.8 11.6 0.000545
# 1960 312.4 15.9 0.000747
# 1961 335.5 23.4 0.001099
# 1962 362.7 27.7 0.001301
# 1963 393.2 30.0 0.001409
# ... ... ... ...
# 2017 13849.1 581.7 0.027317
# 2018 14365.6 576.7 0.027082
# 2019 15319.8 969.3 0.045519
# 2020 19088.8 3978.2 0.186817 <---- drops a couple percent (diluted by new money)
# 2021 21294.6 1900.0 0.089224 <---- sizable increase
# [63 rows x 3 columns]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment