In [1]:
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as dts
import os
from fredpy import series, window_equalize
from datetime import date
import runProcs
from dateutil.relativedelta import relativedelta
%matplotlib inline

In [6]:
# 0. Setup

# 0.1 general plot settings

font = {'weight' : 'bold',
        'size'   : 15}
plt.rc('font', **font)
plt.rcParams['xtick.major.pad']='8'
plt.rcParams['ytick.major.pad']='8'


# 0.2 Formatter for inserting commas in y axis labels with magnitudes in the thousands

def func(x, pos):  # formatter function takes tick label and tick position
   s = '{:0,d}'.format(int(x))
   return s

y_format = plt.FuncFormatter(func)  # make formatter

# 0.3 format the x axis ticksticks
years1,years2,years4,years5,years10,years15= dts.YearLocator(1),dts.YearLocator(2),dts.YearLocator(4),dts.YearLocator(5),dts.YearLocator(10),dts.YearLocator(15)


# 0.4 y label locator for vertical axes plotting gdp
majorLocator_y   = plt.MultipleLocator(3)
majorLocator_shares   = plt.MultipleLocator(0.2)

# 0.5 Index locator
def findDateIndex(dateStr,fredObj):
    for n,d in enumerate(fredObj.dates):
        if d == dateStr:
            return n
        
# 0.6 Today's date:
today = date.today()
today_string = today.strftime("%B %d, %Y")

In [3]:
# 1. Import Fed balance sheet data and define variables

# 1.1 Date windows for balance sheet graphs

win0 = ['2002-12-18','2050-06-01']
win1 = ['2007-05-01','2009-07-01']

# 1.2 Import data from FRED


# 1.2.1 Total assets
assets            = series('WALCL')

# 1.2.1.a Selected Primary Levels:

assets_gold       = series('WGCAL') 
assets_coin       = series('WACL')
assets_sec        = series('WSHOL')
assets_loans      = series('WALLNL')
assets_swaps      = series('WACBS')
assets_maid_i     = series('WAML1L')
assets_maid_ii    = series('WAML2L')
assets_maid_iii   = series('WAML3L')
assets_talf       = series('WATALFL')
assets_for        = series('WFCDA')

assets_maid       = series('WGCAL') 


# 1.2.1.b Construct a TAF series that conforms to other series
assets_TAF        = series('WGCAL')
TAF_temp          = series('WTERAUC')
Z = [0 for x in assets_TAF.data]
for n,z in enumerate(assets_TAF.datenumbers):
    if z==TAF_temp.datenumbers[0]:
        for m,x in enumerate(TAF_temp.data):
            Z[m+n] = x*1000
assets_TAF.data = np.array(Z)

# 1.2.1.c Construct a CPFF series that conforms to other series
assets_cpff       = series('WGCAL')
cpff_temp         = series('WACPFFL')
Z = [0 for x in assets_cpff.data]
for n,z in enumerate(cpff_temp.data):
    Z[n] = z
assets_cpff.data = np.array(Z)

# 1.2.1.d Use the following instead of the three maidens above.
assets_maid.data = np.array([a+b+c for a,b,c in zip(assets_maid_i.data,assets_maid_ii.data,assets_maid_iii.data)])

'''# 1.4 Creaste a measure of other assets for the table
assets_other_table= fred('WGCAL')
# assets_other_table.replace([a-b-c-d-e-f-g-h for a,b,c,d,e,f,g,h in zip(assets.data,assets_gold.data,assets_coin.data,assets_sec.data,assets_maid.data,assets_talf.data,assets_for.data,assets_loans.data)])
'''

# 1.2.1.e Securities held outright Components

assets_treas = series('WSHOTS')
assets_agency= series('WSHOFDSL')
assets_mbs   = series('WSHOMCB')


# 1.2.1.f Treasury securities components

assets_bills        = series('WSHOB')
assets_notes_nominal= series('WSHOSNB')
assets_notes_tips   = series('WSHOIIL')


# 1.2.2 Total Liabilities.

liabs = series('WLTLECL')


# 1.2.2.a  Selected Levels:

liabs_notes= series('WLFN')
liabs_repos= series('WLRRA')
liabs_deps = series('WLDLCL')
liabs_other_table= series('WLTLECL')
liabs_other_graph= series('WLTLECL')


# 1.2.2.b Deposits components
liabs_term_deps = series('WLDBTDL')
liabs_other_deps= series('WLODLL')
liabs_US_deps   = series('WLTGAL')
liabs_for_deps  = series('WLFOL')

# 1.2.2.c Other liabs
liabs_res = series('WRESBAL')
liabs_cur = series('WCURCIR')
liabs_res.window(win0)
liabs_cur.window(win0)
liabs_sfa = series('WLTLECL')
sfa_temp = series('WLSFAL')
Y = [0 for x in liabs_sfa.data]
for n,y in enumerate(liabs_sfa.datenumbers):
    if y==sfa_temp.datenumbers[0]:
        for m,x in enumerate(sfa_temp.data):
            Y[m+n] = x
liabs_sfa.data = np.array(Y)

# 1.2.3 Total capital

capital = series('WCTCL')

# 1.2.4 Other liabilities measure for graph
liabs_other_graph.data = np.array([a-b*1000-c*1000-d for a,b,c,d in zip(liabs.data,liabs_cur.data,liabs_res.data,liabs_sfa.data)])
# liabs_other_table.replace([a-b-c for a,b,c in zip(liabs_notes.data,liabs_repos.data,liabs_deps.data)])

In [7]:
assets_zero  = [0            for x in assets.data]
bills_stack  = [x/1000       for x     in assets_bills.data]
notes_stack  = [(x+y)/1000+z for x,y,z in zip(assets_notes_nominal.data,assets_notes_tips.data,bills_stack)]
TAF_stack    = [x/1000+y     for x,y   in zip(assets_TAF.data,notes_stack)]
swaps_stack  = [x/1000+y     for x,y   in zip(assets_swaps.data,TAF_stack)]
loans_stack  = [x/1000+y     for x,y   in zip(assets_loans.data,swaps_stack)]
cpff_stack   = [x/1000+y     for x,y   in zip(assets_cpff.data,loans_stack)]
agency_stack = [x/1000+y     for x,y   in zip(assets_agency.data,cpff_stack)]
mbs_stack    = [x/1000+y     for x,y   in zip(assets_mbs.data,agency_stack)]
assets_stack = [x/1000       for x     in assets.data]



fig = plt.figure(figsize=(7.5, 4.5))
ax = fig.add_subplot(1,1,1)
l = ax.fill_between(assets.datenumbers, bills_stack, assets_zero,facecolor='blue',      alpha=0.5)
l = ax.fill_between(assets.datenumbers, notes_stack, bills_stack,facecolor='red',       alpha=0.5)
l = ax.fill_between(assets.datenumbers, TAF_stack, notes_stack,  facecolor='green',     alpha=0.5)
l = ax.fill_between(assets.datenumbers, swaps_stack, TAF_stack,  facecolor='magenta',   alpha=0.5)
l = ax.fill_between(assets.datenumbers, loans_stack,swaps_stack, facecolor='yellow',    alpha=0.5)
l = ax.fill_between(assets.datenumbers, cpff_stack,loans_stack,  facecolor='cyan',      alpha=0.5)
l = ax.fill_between(assets.datenumbers, agency_stack,cpff_stack, facecolor='orange',    alpha=0.5)
l = ax.fill_between(assets.datenumbers, mbs_stack,agency_stack,  facecolor='blueviolet',alpha=0.5)
o = ax.fill_between(assets.datenumbers, assets_stack,mbs_stack,  facecolor='gray',      alpha=0.5)

a1 = plt.Rectangle((0, 0), 0, 0, fc="blue",      alpha = 0.5)
a2 = plt.Rectangle((0, 0), 0, 0, fc="red",       alpha = 0.5)
a3 = plt.Rectangle((0, 0), 0, 0, fc="green",     alpha = 0.5)
a4 = plt.Rectangle((0, 0), 0, 0, fc="magenta",   alpha = 0.5)
a5 = plt.Rectangle((0, 0), 0, 0, fc="yellow",    alpha = 0.5)
a6 = plt.Rectangle((0, 0), 0, 0, fc="cyan",      alpha = 0.5)
a7 = plt.Rectangle((0, 0), 0, 0, fc="orange",    alpha = 0.5)
a8 = plt.Rectangle((0, 0), 0, 0, fc="blueviolet",alpha = 0.5)
a9 = plt.Rectangle((0, 0), 0, 0, fc="gray",      alpha = 0.5)


ax.xaxis.set_major_locator(years2)
ax.yaxis.set_major_formatter(y_format)
ax.set_ylabel('Billions of $')
ax.set_title('Federal Reserve Assets')
fig.autofmt_xdate()
ax.grid(True)
plt.rc('legend',**{'fontsize':11})
ax.legend([a1,a2,a3,a4,a5,a6,a7,a8,a9],['T-bills','bonds','TAF','CB Swaps','Loans','CPFF','Agency','MBS','Other'],loc='upper left',ncol=2)
plt.savefig('econ161aImg.png',bbox_inches='tight',dpi=120)



In [ ]: