In [2]:
# Required file: Z1_data.xml; download from: http://www.federalreserve.gov/datadownload/Choose.aspx?rel=Z.1

from __future__ import division
from lxml import etree
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import datetime
%matplotlib inline

In [3]:
# 0. Define a functions for managing parsed z1 data

# 0.1 Create a searchable legend in csv format
def createLegend(root):

    legend_df = pd.DataFrame(columns=['Series Name','Description','Frequency','Start','End'])
    x = 0

    for levelA in root:
        for levelB in levelA[4:-1]:
            entry = []
            ident = levelB.get('SERIES_NAME')
            freq = ident[-1]
            entry.append(ident)
            for levelC in levelB:
                for n,levelD in enumerate(levelC):
                    if n == 0:
                        entry.append(levelD[1].text)
                        entry.append(freq)      
            for levelC in [levelB[1],levelB[-1]]:
                d= levelC.get('TIME_PERIOD')
                d= datetime.datetime.strptime(d, '%Y-%m-%d').strftime('%m-%d-%Y')
                entry.append(d)

            legend_df.loc[x] = entry
            x+=1
            
    legend_df.to_csv('z1Legend.csv')

    return legend_df

# 0.2 Create a Pandas dataframe for the z1 series with code: name 
def getSeries(name):
    
    dates = []
    value= np.array([])
    for levelA in root:
        for levelB in levelA:
            ident = levelB.get('SERIES_NAME')
            if ident in [name]:
                for levelC in levelB:
                    for n,levelD in enumerate(levelC):
                        if n == 0:
                            description = levelD[1].text

                for levelC in levelB[1:-1]:
                    v = levelC.get('OBS_VALUE')
                    d= levelC.get('TIME_PERIOD')
                    dates = np.append(dates,d)
                    value = np.append(value,float(v))

    for n,d in enumerate(dates):
        dates[n]= datetime.datetime.strptime(d,'%Y-%m-%d').strftime('%m-%d-%Y')
        
    df = pd.DataFrame(value,index=dates,columns = [description])
    return df

# 0.3 Create a Pandas dataframe for the z1 series with codes in dataList
def getDataSet(dataList):
    df = pd.DataFrame([])
    for name in dataList:
        newSeries=getSeries(name)
        df = pd.concat([df,newSeries],axis=1, join_axes=[newSeries.index])
    return df

In [4]:
# 1. Import the xml data and create a legend

# 1.2 parse
tree = etree.parse("Z1_data.xml")
root = tree.getroot()

# 1.2 create a legend in csv format
# legend= createLegend(root)

In [4]:


In [5]:
# 4. Create data sets for money market, capital market, and intermeidary assets

# 4.1 Money market data
moneyMarketNames = [
'FL313161113.A',
'FL703135005.A',
'FL893169105.A',
'FL892150005.A']
moneyMarketData = getDataSet(moneyMarketNames)
moneyMarketData.columns = ['T-bills','Negotiable CDs','Commerical paper','Fed funds & repos']
moneyMarketData = moneyMarketData.convert_objects(convert_numeric=True)
moneyMarketData = np.round(moneyMarketData/1000)

In [6]:
# 4.2 Capital market data
capitalMarketNames = [
'FL893064105.A', # Corporate equities L.213 Line 1
'FL893065105.A', # Home mortgages L.217 Line 2
'FL103163003.A', # Nonfinancial Corporate bonds L.101 Line 22
'FL313161125.A', # Other Treas. securities L.209 Line 4
'FL893161705.A', # Agency- and GSE-Backed Securities L.210 Line 1
'FL213162005.A', # State and local governments Securities L.104 Line 20
'FL893065505.A', # Commerical Mortgages L.217 Line 4
'FL703066005.A', # Consumer credit L.109 Line 14
]
capitalMarketData = getDataSet(capitalMarketNames)
capitalMarketData.columns = ['Equities','Home mort','Corp bonds','Treas bonds','Agency/GSE sec','Muni bonds','Commercial mort','Consumer credit']
capitalMarketData = capitalMarketData.convert_objects(convert_numeric=True)
capitalMarketData = np.round(capitalMarketData/1000)

In [7]:
# 4.3 Intermediary data
intermediaryNames = [
'FL704090005.A', # depository inst. L.109 Line 1
# '', # savings and loans
'FL474090005.A', # credit unions L.13 line 1
'FL544090005.A', # life insurance L.115 line 1
'FL514090005.A', # fire and casualty L.114 line 1
'FL574090005.A', # pension funds (private) L.117 line 1
'FL344090005.A', # federal retirement funds L.119 line 1
'FL224090005.A', # state and local retirement funds L.118
'FL614090005.A', # finance companies L.126 Line 1
'FL654090005.A', # mutual funds L.121 Line 1
'FL634090005.A', # money market mutual funds Line.120 Line 1
]

intermediaryData = getDataSet(intermediaryNames)
# len(intermediaryData.columns)
# intermediaryData.columns
intermediaryData.columns = ['depInst','creditU','life','fire','pension','federalFunds','stateLocalFunds','finance','mutual','moneyMarketMutual']
intermediaryData = capitalMarketData.convert_objects(convert_numeric=True)
intermediaryData = np.round(capitalMarketData/1000)

In [8]:
endDate = moneyMarketData.index[-1]
endYear = int(endDate[6:])
midYear = endYear-10
midDate = '12-31-'+str(midYear)

In [9]:
nf = open('table_001_money_market_instruments.tex', 'w')

nf.write('\\begin{tabular}{p{4.5cm}ccccc} \\multicolumn{5}{l}{\\large Selected money market instruments outstanding (bil.~of dollars) \\normalsize} \\\\\\hline\n')
nf.write('                          & & \\textbf{'),nf.write(str(midYear)),nf.write('} & & \\textbf{'),nf.write(str(endYear)),nf.write('}\\\\\\hline\n')
nf.write('T-bills                   & & '),         nf.write(str('{:,g}'.format(moneyMarketData.loc[midDate]['T-bills']))),      nf.write(' & & '),      nf.write(str('{:,g}'.format(moneyMarketData.loc[endDate]['T-bills']))), nf.write('\\\\\n')
nf.write('Negotiable CDs            & & '),         nf.write(str('{:,g}'.format(moneyMarketData.loc[midDate]['Negotiable CDs']))),    nf.write(' & & '), nf.write(str('{:,g}'.format(moneyMarketData.loc[endDate]['Negotiable CDs']))), nf.write('\\\\\n')
nf.write('Commerical paper          & & '),         nf.write(str('{:,g}'.format(moneyMarketData.loc[midDate]['Commerical paper']))),  nf.write(' & & '), nf.write(str('{:,g}'.format(moneyMarketData.loc[endDate]['Commerical paper']))), nf.write('\\\\\n')
nf.write('Fed funds \& repos        & & '),         nf.write(str('{:,g}'.format(moneyMarketData.loc[midDate]['Fed funds & repos']))), nf.write(' & & '),nf.write(str('{:,g}'.format(moneyMarketData.loc[endDate]['Fed funds & repos']))), nf.write('\\\\\\hline\n')
nf.write('\\end{tabular}\n')

nf.close()

In [10]:
nf = open('table_001_capital_market_instruments.tex', 'w')

nf.write('\\begin{tabular}{p{4.5cm}ccccc} \\multicolumn{5}{l}{\\large Selected capital market instruments outstanding (bil.~of dollars) \\normalsize} \\\\\\hline\n')
nf.write('                      & & \\textbf{'),nf.write(str(midYear)),nf.write('} & & \\textbf{'),nf.write(str(endYear)),nf.write('}\\\\\\hline\n')
nf.write('Equities              & & '),         nf.write(str('{:,g}'.format(capitalMarketData.loc[midDate]['Equities']))),        nf.write(' & & '),nf.write(str('{:,g}'.format(capitalMarketData.loc[endDate]['Equities']))), nf.write('\\\\\n')
nf.write('Home mortgages        & & '),         nf.write(str('{:,g}'.format(capitalMarketData.loc[midDate]['Home mort']))),       nf.write(' & & '),nf.write(str('{:,g}'.format(capitalMarketData.loc[endDate]['Home mort']))), nf.write('\\\\\n')
nf.write('Corp.~bonds           & & '),         nf.write(str('{:,g}'.format(capitalMarketData.loc[midDate]['Corp bonds']))),      nf.write(' & & '),nf.write(str('{:,g}'.format(capitalMarketData.loc[endDate]['Corp bonds']))), nf.write('\\\\\n')
nf.write('Treas.~bonds          & & '),         nf.write(str('{:,g}'.format(capitalMarketData.loc[midDate]['Treas bonds']))),     nf.write(' & & '),nf.write(str('{:,g}'.format(capitalMarketData.loc[endDate]['Treas bonds']))), nf.write('\\\\\n')
nf.write('Agency/GDS sec.       & & '),         nf.write(str('{:,g}'.format(capitalMarketData.loc[midDate]['Agency/GSE sec']))),  nf.write(' & & '),nf.write(str('{:,g}'.format(capitalMarketData.loc[endDate]['Agency/GSE sec']))), nf.write('\\\\\n')
nf.write('Muni bonds            & & '),         nf.write(str('{:,g}'.format(capitalMarketData.loc[midDate]['Muni bonds']))),      nf.write(' & & '),nf.write(str('{:,g}'.format(capitalMarketData.loc[endDate]['Muni bonds']))), nf.write('\\\\\n')
nf.write('Commercial mort.      & & '),         nf.write(str('{:,g}'.format(capitalMarketData.loc[midDate]['Commercial mort']))), nf.write(' & & '),nf.write(str('{:,g}'.format(capitalMarketData.loc[endDate]['Commercial mort']))), nf.write('\\\\\n')
nf.write('Consumer credit       & & '),         nf.write(str('{:,g}'.format(capitalMarketData.loc[midDate]['Consumer credit']))), nf.write(' & & '),nf.write(str('{:,g}'.format(capitalMarketData.loc[endDate]['Consumer credit']))), nf.write('\\\\\\hline\n')
nf.write('\\end{tabular}\n')

nf.close()

In [11]:
# nf = open('table_slides_002_capital_market_instruments.tex', 'w')

# nf.write('\\fr{\n')
# nf.write('\\ft{Capital Market Instruments}\n')  
# nf.write('\\begin{tabular}{p{4.5cm}ccccc} \\multicolumn{5}{l}{\\large Selected capital market instruments outstanding (bil.~of dollars) \\normalsize} \\\\\\hline\n')
# nf.write('                      & & \\textbf{'),nf.write(str(midYear)),nf.write('} & & \\textbf{'),nf.write(str(endYear)),nf.write('}\\\\\\hline\n')
# nf.write('Equities              & & '),         nf.write(str('{:,}'.format(capitalMarketData.loc[midDate]['Equities']))),        nf.write(' & & '),nf.write(str('{:,}'.format(capitalMarketData.loc[endDate]['Equities']))), nf.write('\\\\\n')
# nf.write('Home mortgages        & & '),         nf.write(str('{:,}'.format(capitalMarketData.loc[midDate]['Home mort']))),       nf.write(' & & '),nf.write(str('{:,}'.format(capitalMarketData.loc[endDate]['Home mort']))), nf.write('\\\\\n')
# nf.write('Corp.~bonds           & & '),         nf.write(str('{:,}'.format(capitalMarketData.loc[midDate]['Corp bonds']))),      nf.write(' & & '),nf.write(str('{:,}'.format(capitalMarketData.loc[endDate]['Corp bonds']))), nf.write('\\\\\n')
# nf.write('Treas.~bonds          & & '),         nf.write(str('{:,}'.format(capitalMarketData.loc[midDate]['Treas bonds']))),     nf.write(' & & '),nf.write(str('{:,}'.format(capitalMarketData.loc[endDate]['Treas bonds']))), nf.write('\\\\\n')
# nf.write('Agency/GDS sec.       & & '),         nf.write(str('{:,}'.format(capitalMarketData.loc[midDate]['Agency/GSE sec']))),  nf.write(' & & '),nf.write(str('{:,}'.format(capitalMarketData.loc[endDate]['Agency/GSE sec']))), nf.write('\\\\\n')
# nf.write('Muni bonds            & & '),         nf.write(str('{:,}'.format(capitalMarketData.loc[midDate]['Muni bonds']))),      nf.write(' & & '),nf.write(str('{:,}'.format(capitalMarketData.loc[endDate]['Muni bonds']))), nf.write('\\\\\n')
# nf.write('Commercial mort.      & & '),         nf.write(str('{:,}'.format(capitalMarketData.loc[midDate]['Commercial mort']))), nf.write(' & & '),nf.write(str('{:,}'.format(capitalMarketData.loc[endDate]['Commercial mort']))), nf.write('\\\\\n')
# nf.write('Consumer credit       & & '),         nf.write(str('{:,}'.format(capitalMarketData.loc[midDate]['Consumer credit']))), nf.write(' & & '),nf.write(str('{:,}'.format(capitalMarketData.loc[endDate]['Consumer credit']))), nf.write('\\\\\\hline\n')
# nf.write('\\end{tabular}\n')
# nf.write('}')

# nf.close()

In [13]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [32]:
capitalMarketNames = [
'FL893064105.A', # Corporate equities L.213 Line 1
'FL893065105.A', # Home mortgages L.217 Line 2
'FL103163003.A', # Nonfinancial Corporate bonds L.101 Line 22
'FL313161125.A', # Other Treas. securities L.209 Line 4
'FL893161705.A', # Agency- and GSE-Backed Securities L.210 Line 1
'FL213162005.A', # State and local governments Securities L.104 Line 20
'FL893065505.A', # Commerical Mortgages L.217 Line 4
'FL703066005.A', # Consumer credit L.109 Line 14
]
capitalMarketData = getDataSet(capitalMarketNames)
capitalMarketData.columns = ['Equities','Home morts','Corp bonds','Treas bonds','Agency/GSE sec','Muni bonds','Commercial morts','Consumer credit']
# capitalMarketData.plot()
capitalMarketData = capitalMarketData.pct_change()
capitalMarketData.plot(legend=False)


Out[32]:
<matplotlib.axes.AxesSubplot at 0x110bb7150>

In [33]:
capitalMarketData


Out[33]:
Equities Home morts Corp bonds Treas bonds Agency/GSE sec Muni bonds Commercial morts Consumer credit
12-31-1945 NaN NaN NaN NaN NaN NaN NaN NaN
12-31-1946 -0.068229 0.234898 0.039866 -0.106438 0.077900 0.005688 0.174146 0.705602
12-31-1947 -0.019279 0.223680 0.113163 0.010127 0.062058 0.132060 0.150029 0.406709
12-31-1948 -0.010252 0.182368 0.155397 -0.076371 0.832840 0.096597 0.110714 0.211956
12-31-1949 0.098089 0.129615 0.090801 -0.017094 -0.008071 0.065108 0.059753 0.178576
12-31-1950 0.220012 0.201144 0.046568 0.176587 -0.087876 0.280533 0.052339 0.262926
12-31-1951 0.167277 0.142101 0.090252 -0.189825 0.045049 0.102397 0.080250 0.035249
12-31-1952 0.012227 0.129248 0.118240 0.114156 0.186940 0.302459 0.067393 0.218656
12-31-1953 -0.023559 0.128526 0.078994 -0.017095 0.586480 0.180564 0.081059 0.155632
12-31-1954 0.362830 0.141706 0.073627 -0.002666 0.015413 0.161402 0.112375 0.035510
12-31-1955 0.256197 0.166941 0.058529 0.073272 0.056250 0.123798 0.119101 0.208001
12-31-1956 0.089646 0.122919 0.063705 -0.083491 0.046703 0.091144 0.125316 0.108718
12-31-1957 -0.089215 0.087387 0.103928 -0.139783 0.352716 0.090389 0.112645 0.086163
12-31-1958 0.326510 0.091298 0.088073 0.042242 0.001940 0.100241 0.122140 0.038661
12-31-1959 0.115198 0.110278 0.044166 0.137665 0.232236 0.097661 0.110425 0.173917
12-31-1960 0.015969 0.086696 0.047009 -0.003733 0.057664 0.080870 0.105167 0.107462
12-31-1961 0.239894 0.089469 0.057665 0.113331 0.115442 0.075777 0.119616 0.063328
12-31-1962 -0.033144 0.092711 0.050018 -0.113267 0.223384 0.071396 0.128412 0.097398
12-31-1963 0.108475 0.099812 0.044552 0.087027 0.123712 0.064059 0.120789 0.135464
12-31-1964 0.158953 0.093076 0.045174 0.073241 0.101893 0.070312 0.081917 0.141682
12-31-1965 0.135462 0.084582 0.052506 -0.021122 0.176013 0.077684 0.087575 0.136602
12-31-1966 -0.101370 0.060574 0.105017 -0.058971 0.394478 0.062113 0.104328 0.073018
12-31-1967 0.264501 0.056853 0.136386 0.062233 0.176340 0.067146 0.077780 0.075599
12-31-1968 0.192763 0.068950 0.105566 0.024735 0.242899 0.073282 0.102190 0.130541
12-31-1969 -0.156087 0.059920 0.088687 -0.041355 0.209903 0.093227 0.082609 0.097021
12-31-1970 -0.011154 0.048624 0.134395 0.037547 0.205830 0.089748 0.094268 0.044572
12-31-1971 0.172801 0.089553 0.112782 0.093976 0.093847 0.110412 0.121152 0.129047
12-31-1972 0.234298 0.122389 0.065676 0.027945 0.164554 0.084288 0.177196 0.163587
12-31-1973 -0.220184 0.118806 0.046316 -0.000107 0.309095 0.083792 0.171134 0.171034
12-31-1974 -0.325944 0.088375 0.095066 -0.016724 0.230651 0.067196 0.108993 0.048851
12-31-1975 0.327097 0.089221 0.120064 0.267441 0.085202 0.054076 0.085919 0.058202
12-31-1976 0.232136 0.128731 0.089963 0.274487 0.131089 0.076951 0.081792 0.128490
12-31-1977 -0.107954 0.173268 0.082704 0.206817 0.171138 0.078822 0.109490 0.185130
12-31-1978 0.050988 0.176081 0.070506 0.152650 0.231640 0.165195 0.109955 0.185098
12-31-1979 0.183200 0.159193 0.053985 0.072793 0.251972 0.091373 0.116250 0.126047
12-31-1980 0.303046 0.119345 0.081840 0.104711 0.183185 0.067481 0.085426 -0.024432
12-31-1981 -0.075520 0.075478 0.067733 0.139009 0.162365 0.078320 0.153065 0.032281
12-31-1982 0.130574 0.038802 0.078564 0.189398 0.196964 0.112196 0.113436 0.044752
12-31-1983 0.187830 0.108471 0.061718 0.251187 0.174709 0.114426 0.167058 0.131322
12-31-1984 -0.035993 0.114017 0.107562 0.218923 0.163868 0.099474 0.211785 0.231237
12-31-1985 0.268975 0.155389 0.168013 0.213832 0.187811 0.315394 0.147835 0.156162
12-31-1986 0.181375 0.133117 0.219835 0.158353 0.285164 0.113286 0.111341 0.073014
12-31-1987 0.010477 0.114632 0.111668 0.133979 0.209776 0.128029 0.152986 0.044066
12-31-1988 0.134789 0.121513 0.131507 0.072054 0.141355 0.081097 0.103540 0.073797
12-31-1989 0.239736 0.095603 0.083233 0.076912 0.135409 0.053851 0.046166 0.041839
12-31-1990 -0.073866 0.100123 0.048995 0.079522 0.141138 0.049570 0.023067 -0.026547
12-31-1991 0.372799 0.064409 0.078130 0.119828 0.091322 0.092876 -0.016309 -0.039344
12-31-1992 0.118445 0.060318 0.062165 0.107100 0.099175 0.015573 -0.054165 -0.021262
12-31-1993 0.161361 0.054012 0.158836 0.078651 0.099790 0.054051 -0.032728 0.087897
12-31-1994 0.003276 0.057154 0.067703 0.053056 0.153430 -0.040161 -0.020371 0.152554
12-31-1995 0.342515 0.051084 0.045973 0.043478 0.093523 -0.055470 0.015316 0.093272
12-31-1996 0.149364 0.068566 0.058715 0.047998 0.095514 -0.015139 0.040383 0.062204
12-31-1997 0.294788 0.063713 0.080623 0.030720 0.081027 0.044664 0.082421 -0.002407
12-31-1998 0.216471 0.091035 0.121421 -0.010542 0.166202 0.062495 0.107979 0.007249
12-31-1999 0.263576 0.099511 0.107916 -0.041073 0.178498 0.034301 0.149418 0.024489
12-31-2000 -0.094117 0.090202 0.071116 -0.074385 0.110051 0.014076 0.102122 0.085249
12-31-2001 -0.110750 0.107978 0.128995 -0.069303 0.147720 0.088609 0.094737 0.036037
12-31-2002 -0.204132 0.133241 0.032035 0.074431 0.109669 0.111187 0.078861 0.045726
12-31-2003 0.337688 0.128129 0.027731 0.138354 0.078232 0.083749 0.092606 0.099330
12-31-2004 0.138326 0.142931 -0.004492 0.100677 0.019292 0.564178 0.117375 0.060656
... ... ... ... ... ... ... ...

68 rows × 8 columns


In [ ]: