def FredList(ticker, win, freq): i = fp.series(ticker) if i.frequency_short != freq: i = i.as_frequency(freq, 'mean') i2 = i.window(win) i_list = i2.data.tolist() return i_list
# Approximately when the program started startTime = time.time() # In[ ]: # start date, end date, and name for the .mp4 and .ogv files startDate = '2010-01-01' endDate = '2500-01-01' fileName = 'USTreasuryYieldCurveAnimation10Present' # In[ ]: # Create Fred objects y1m= series('DTB4WK') y3m= series('DTB3') y6m= series('DTB6') y1 = series('DGS1') y5 = series('DGS5') y10= series('DGS10') y20= series('DGS20') y30= series('DGS30') win = [startDate,endDate] for y in [y1m,y3m,y6m,y1,y5,y10,y20,y30]: y.window(win) y1m = pd.DataFrame(y1m.data,index = y1m.datenumbers,columns=['1 mo']) y3m = pd.DataFrame(y3m.data,index = y3m.datenumbers,columns=['3 mo']) y6m = pd.DataFrame(y6m.data,index = y6m.datenumbers,columns=['6 mo'])
# ## Unemployment Rate # # We construct an unemployment series from April 1929 through the most recent date available by concatenating four U.S. unemployment rate series; all of which are available from FRED (https://fred.stlouisfed.org/). Specifically: # # 1. Seasonally adjusted unemployment rate for the United States from April 1929 through February 1940. FRED series ID: M0892AUSM156SNBR. NBER Indicator: m08292a. # 2. Seasonally adjusted unemployment rate for the United States from March 1940 through December 1946. FRED series ID: M0892BUSM156SNBR. NBER Indicator: m08292b. # 3. Seasonally adjusted unemployment rate for the United States from January 1947 through December 1947. FRED series ID: M0892CUSM156NNBR. NBER Indicator: m08292c. Note: The source data are not seasonally adjusted and contain observations through December 1966. We seasonally adjust the entire series through December 1966 using the U.S. Census Bureau's X-12-ARIMA seasonal adjustment program. We then discard values after December 1947. # 4. Seasonally adjusted unemployment rate for the United States from January 1948 through the most recent date available. FRED series ID: UNRATE. # In[25]: # Historical US unemployment rate from the NBER Macrohistory Database: 1929-04-01 to 1940-02-01; # Seasonallyadjusted # Download from FRED and save as a Pandas series unemp_1 = fp.series('M0892AUSM156SNBR') unemp_1 = unemp_1.window(['04-01-1929', '02-01-1940']).data # In[26]: # Historical US unemployment rate from the NBER Macrohistory Database: 1940-03-01 to 1946-12-01; # Seasonally adjusted # Download from FRED and save as a Pandas series unemp_2 = fp.series('M0892BUSM156SNBR') unemp_2 = unemp_2.window(['03-01-1940', '12-01-1946']).data # In[27]: # Historical US unemployment rate from the NBER Macrohistory Database: 1947-01-01 to 1966-12-01; # Raw series is *not* seasonally adjusted
frequency='Quarterly') deflator_frame = pd.DataFrame({ 'deflator inflation - 3mo forecast': defl_forecast_1q.data, 'deflator inflation - 6mo forecast': defl_forecast_2q.data, 'deflator inflation - 1yr forecast': defl_forecast_1y.data }) # ## Actual data # In[5]: interest3mo = fp.series('TB3MS').as_frequency('Q') interest6mo = fp.series('TB6MS').as_frequency('Q') interest1yr = fp.series('GS1').as_frequency('Q') interest3mo, interest6mo, interest1yr = fp.window_equalize( [interest3mo, interest6mo, interest1yr]) interest_frame = pd.DataFrame({ 'nominal interest - 3mo': interest3mo.data, 'nominal interest - 6mo': interest6mo.data, 'nominal interest - 1yr': interest1yr.data }) # In[6]: defl_3mo = fp.series('GDPDEF')
# # * average annual real monetary base (trillions of $) # * average annual GDP deflator inflation # * change in real monetary base from preceding year for 1960 only # * inflation tax from preceding year for 1960 only # * total seigniorage for 1960 only # # The purpose behind providing incomplete columns is to give students a starting point for completing the columns using Excel or a comparable tool. # # ## Download and manage data # In[2]: # Download monetary base and GDP deflator data m_base = fp.series('BOGMBASE') gdp_deflator = fp.series('A191RD3A086NBEA') # Convert monetary base data to annual frequency m_base = m_base.as_frequency('A') # Equalize data ranges for monetary base and GDP deflator data m_base, gdp_deflator = fp.window_equalize([m_base, gdp_deflator]) # GDP deflator base year base_year = gdp_deflator.units.split(' ')[-1].split('=')[0] # In[3]:
# In[1]: import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt import matplotlib.animation as animation import matplotlib.cm as cm import numpy as np from fredpy import series,window_equalize import subprocess # In[2]: # Dowload data from FRED u = series('LNS14000028') p = series('CPIAUCSL') # Construct the inflation series p.pc(annualized=True) p.ma2side(length=6) p.data = p.ma2data p.datenumbers = p.ma2datenumbers p.dates = p.ma2dates window_equalize([p,u]) p.bpfilter(low=24,high=84,K=84) u.bpfilter(low=24,high=84,K=84) # Set data for animation x = u.bpcycle
from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from matplotlib import cm, rc import matplotlib.colors as colors import matplotlib.dates as mdates import numpy as np import pandas as pd from fredpy import series, window_equalize import collections # get_ipython().magic('matplotlib inline') # In[2]: # # Import daily interest rate data y1m = series('DTB4WK') y3m = series('DTB3') y6m = series('DTB6') y1 = series('DGS1') y5 = series('DGS5') y10 = series('DGS10') y20 = series('DGS20') y30 = series('DGS30') # Set the date ranges for the data for x in [y1m, y3m, y6m, y1, y5, y10, y20, y30]: x.window(['2003-01-01', '2016-03-31']) # In[3]: # Form individual data frames and the full data frame
def capitalSeries(i, k0, delta): t0 = len(i) - 1 k = [k0] for t in range(t0): k.append(i[t] + (1 - delta) * k[t]) return np.array(k) # In[4]: # 2. Import and manage data from FRED # 2.1 Annual data investmentA = series('GPDIA') consumptionA = series('PCECA') governmentA = series('GCEA') exportsA = series('EXPGSA') importsA = series('IMPGSA') netExportsA = series('A019RC1A027NBEA') deflatorA = series('A191RD3A086NBEA') depreciationA = series('Y0000C1A027NBEA') gdpA = series('GDPA') tfpA = series('GDPA') capitalA = series('GDPA') laborA = series('B4701C0A222NBEA' ) # BEA index: fred('HOANBS') / .quartertoannual(method='AVG') # annualSeries = [investmentA,consumptionA,governmentA,exportsA,importsA,netExportsA,deflatorA,depreciationA,gdpA,tfpA,capitalA,laborA] investmentA, consumptionA, governmentA, netExportsA, exportsA, importsA, deflatorA, depreciationA, gdpA, tfpA, capitalA, laborA = window_equalize(
# In[2]: # 1. Import the most recent inflation forecast data from the Philadelphia Fed, Survey of Professional Forecasters url = "https://www.philadelphiafed.org/-/media/research-and-data/real-time-center/survey-of-professional-forecasters/historical-data/inflation.xls?la=en" r = requests.get(url, verify=False) with open("../xls/inflation_forecasts.xls", "wb") as code: code.write(r.content) # dls = "http://www.philadelphiafed.org/research-and-data/real-time-center/survey-of-professional-forecasters/historical-data/inflation.xls" # urllib.urlretrieve(dls, "inflation_forecasts.xls") # In[3]: # 2. Download and manage data from FRED gdp_deflator_Q = fp.series('GDPDEF') gdp_deflator_A = fp.series('A191RD3A086NBEA') gdp_deflator_Q = gdp_deflator_Q.apc(method='forward') gdp_deflator_A = gdp_deflator_A.apc(method='forward') gdp_deflator_Q = gdp_deflator_Q.window(['07-01-1970', '01-01-2200']) gdp_deflator_A = gdp_deflator_A.window(['07-01-1970', '01-01-2200']) interest_Q = fp.series('GS1') interest_A = fp.series('GS1') interest_Q = interest_Q.as_frequency(freq='Q', method='mean') interest_A = interest_A.as_frequency(freq='A', method='mean') interest_Q = interest_Q.window(['07-01-1970', '01-01-2200']) interest_A = interest_A.window(['07-01-1970', '01-01-2200'])
# 1.4 Define the function for computing the capital series def capitalSeries(i, k0, delta): t0 = len(i) - 1 k = [k0] for t in range(t0): k.append(i[t] + (1 - delta) * k[t]) return np.array(k) # In[9]: # 2. Import and manage data from FRED # 2.1 Annual data investmentA = fp.series('GPDIA') consumptionA = fp.series('PCECA') governmentA = fp.series('GCEA') exportsA = fp.series('EXPGSA') importsA = fp.series('IMPGSA') netExportsA = fp.series('A019RC1A027NBEA') deflatorA = fp.series('A191RD3A086NBEA') # depreciationA = fp.series('Y0000C1A027NBEA') # Current-Cost Depreciation of Fixed Assets depreciationA = fp.series( 'M1TTOTL1ES000' ) # Consumption of fixed capital: Private: Domestic business: Capital consumption allowances gdpA = fp.series('GDPA') tfpA = fp.series('GDPA') capitalA = fp.series('GDPA') laborA = fp.series('B4701C0A222NBEA')
from fredpy import series, window_equalize # ## Data # # ### Importing the data # # As his measure of the unemployment rate, Sargent uses the unemployment rate for white men age 20 and over (FRED code: LNS14000028). The results are essentially identical if the unemployment rate of the over 16 non-institutional population (FRED code: UNRATE) is used. His measure of the inflation rate is a 13-month two-sided moving average of the annualized monthly percentage change in the CPI. The unemployment and inflation rate data are monthly. # # ### Detrending procedures # # Sargent isolates the business cycle components of the data using the bandpass filter of Baxter and King (1995). Since the data are monthly, the minimum frequency is set to 24 months, the maximum is set to 84 months, and the lag-lead truncation to 84. Additionally, I also detrend the data using the Hodrick-Prescott filter (1997). The striking loops in Sargent's Figure 1.5 *are sensitive* to the filtering procedure used. # In[2]: # Dowload data u = series('LNS14000028') p = series('CPIAUCSL') # Construct the inflation series p.pc(annualized=True) p.ma2side(length=6) p.data = p.ma2data p.datenumbers = p.ma2datenumbers p.dates = p.ma2dates # Make sure that the data inflation and unemployment series cver the same time interval window_equalize([p, u]) # Filter the data p.bpfilter(low=24, high=84, K=84) p.hpfilter(lamb=129600)
# In[2]: # 1. Import the most recent inflation forecast data from the Philadelphia Fed, Survey of Professional Forecasters url = "https://www.philadelphiafed.org/-/media/research-and-data/real-time-center/survey-of-professional-forecasters/historical-data/inflation.xls?la=en" r = requests.get(url, verify=False) with open("inflationForecasts.xls", "wb") as code: code.write(r.content) # dls = "http://www.philadelphiafed.org/research-and-data/real-time-center/survey-of-professional-forecasters/historical-data/inflation.xls" # urllib.urlretrieve(dls, "inflationForecasts.xls") # In[3]: # 2. Download and manage data from FRED gdpDeflatorQ = series('GDPDEF') gdpDeflatorA = series('A191RD3A086NBEA') gdpDeflatorQ.apc(method='forward') gdpDeflatorA.apc(method='forward') gdpDeflatorQ.window(['07-01-1970', '01-01-2200']) gdpDeflatorA.window(['07-01-1970', '01-01-2200']) interestQ = series('GS1') interestA = series('GS1') interestQ.monthtoquarter(method='average') interestA.monthtoannual(method='average') interestQ.window(['07-01-1970', '01-01-2200']) interestA.window(['07-01-1970', '01-01-2200']) # In[4]:
deflatorForecasts = deflatorForecasts.iloc[5:] # deflatorForecasts.iloc[0:5] # In[6]: # cpiForecasts = pd.read_excel('medianLevel.xls',sheetname = 'CPI') # cpiForecasts = cpiForecasts.iloc[5:] # cpiForecasts=cpiForecasts.interpolate() # # cpiForecasts.iloc[0:5] # ## GDP deflator inflation forecasts # In[7]: # Create some fredpy instances defl_forecast_1q = series('GDPDEF') defl_forecast_2q = series('GDPDEF') defl_forecast_1y = series('GDPDEF') # Initialize forecast lists forecast_1q = [] forecast_2q = [] forecast_1y = [] # Associate forecasts with dates. The date should coincide with the start of the period for which the forecast applies. dates = [] for i, ind in enumerate(deflatorForecasts.index): year = int(deflatorForecasts.iloc[i]['YEAR']) quart = int(deflatorForecasts.iloc[i]['QUARTER']) if quart == 1: month = '01'