def problem_6():
    """
    This function uses the HP filter created before to filter the data
    before repeating the same process as in problem 5. We will plot both the
    trend and cycle produced by the HP filter.

    Inputs:
        None

    Outputs:
        This function automatically generates plots of the spectra for the
        filtered data.

    Notes:
        This function uses the function hp_filter found in the file hp_filter.py
    """
    gdpT, gdpC = hp.hp_filter(gdp)
    cpiT, cpiC = hp.hp_filter(cpi)
    consT, consC = hp.hp_filter(cons)
    invT, invC = hp.hp_filter(inv)

    plt.figure()
    plt.psd(gdpT)
    plt.title('GDP Trend Spectrum')
    plt.show()

    plt.figure()
    plt.psd(cpiT)
    plt.title('CPI Trend Spectrum')
    plt.show()

    plt.figure()
    plt.psd(consT)
    plt.title('Consumption Trend Spectrum')
    plt.show()

    plt.figure()
    plt.psd(invT)
    plt.title('Investment Trend Spectrum')
    plt.show()

    plt.figure()
    plt.psd(gdpC)
    plt.title('GDP Cycle Spectrum')
    plt.show()

    plt.figure()
    plt.psd(cpiC)
    plt.title('CPI Cycle Spectrum')
    plt.show()

    plt.figure()
    plt.psd(consC)
    plt.title('Consumption Cycle Spectrum')
    plt.show()

    plt.figure()
    plt.psd(invC)
    plt.title('Investment Cycle Spectrum')
    plt.show()
예제 #2
0
def problem_4():
    """
    This function competes problem three from Ben's assignment.
    """
    data = sp.loadtxt('SP500.txt', skiprows=1, dtype=float,
                      converters={0:pylab.datestr2num})
    yData = data[:,1]

    # Differencing (y_t - y_t-1)
    ytemp = yData.copy()
    ytemp2 = yData.copy()
    ytemp2 = np.insert(ytemp2, 0,0)
    ytemp = np.insert(ytemp, ytemp.size, 0)
    yhat = ytemp - ytemp2
    c_diff = yhat[1:-1]
    five_percent_diff = round(yhat.size * .05)
    c_diff = c_diff[five_percent_diff:-five_percent_diff]

    # OLS (quadratic)
    x = np.zeros((yData.size,3))
    x[:, 0] = np.arange(1, yData.size + 1)**2
    x[:, 1] = np.arange(1, yData.size + 1)
    x[:, 2] = np.ones(yData.size)
    beta = la.lstsq(x, yData)[0]
    time = np.arange(1, yData.size+1 )
    y_trend = beta[0]*time**2 + beta[1] * time + beta[2] * np.ones(time.size)

    c_ols = yData - y_trend
    five_percent_ols = round(c_ols.size * .05)

    c_ols  = c_ols[five_percent_ols:-five_percent_ols]


    # HP filter
    T_hp, c_hp = hpf.hp_filter(yData, 1600)
    five_percent_hp = round(c_hp.size * .05)
    c_hp  = c_hp[five_percent_hp:-five_percent_hp]

    # Band-Pass
    c_bp = bpf.bandpass_filter(yData, 6, 6, 32)
    five_percent_bp = round(c_bp.size * .05)
    c_bp  = c_bp[five_percent_bp:-five_percent_bp]

    all_c = np.array([c_diff, c_ols, c_hp, c_bp])
    the_table = np.zeros((3,all_c.shape[0]))

    for i in range(all_c.shape[0]):
        the_table[0, i] = np.mean(all_c[i])
        the_table[1, i] = np.var(all_c[i])
        the_table[2, i] = np.corrcoef(all_c[i][:-1], all_c[i][1:])[0,1]

    return the_table
예제 #3
0
def problem_2():
    """
    This function completes problem two from Ben's assignment
    """
    data = sp.loadtxt('GPDIC96.txt', skiprows=1, dtype=float,
                      converters={0:pylab.datestr2num})
    yData = data[:,1]
    T_1000, c_1000  = hpf.hp_filter(yData, 1000)
    T_1600, c_1600  = hpf.hp_filter(yData, 1600)
    T_1800, c_1800  = hpf.hp_filter(yData, 1800)
    T_16000, c_16000  = hpf.hp_filter(yData, 16000)

    all_c = np.array([c_1000, c_1600, c_1800, c_16000])

    the_table = np.zeros((3,all_c.shape[0]))

    for i in range(all_c.shape[0]):
        the_table[0, i] = np.mean(all_c[i])
        the_table[1, i] = np.var(all_c[i])
        the_table[2, i] = np.corrcoef(all_c[i][:-1], all_c[i][1:])[0,1]

    return the_table
예제 #4
0
def problem_1():
    """
    This function uses the file GPDIC96.txt as the data for this problem.
    It produces the T, cycle sets and plots what Ben asked for in 1.d.
    """
    data = sp.loadtxt('GPDIC96.txt', skiprows=1, dtype=float,
                      converters={0:pylab.datestr2num})
    yData = data[:,1]
    T, c  = hpf.hp_filter(yData)
    time = sp.linspace(0, T.size, T.size)
    plt.plot(time, yData)
    plt.plot(time, T)
    plt.figure()
    plt.plot(time, c)
    plt.show()
def problem_10():
    """
    This function provides the solution to problem 10. We use the 4 data
    sets from problem 5 to generate moments regarding data filtered with
    an HP filter, BK filter, and first-difference filter.

    Inputs:
        None

    Outputs:
        the_tables: This is the tables containing mean, standard deviation,
                    autocorrelation, and correlation with GDP for the
                    following filters for each data set:
                        HP(1600)
                        BK(16, 6, 32)
                        first difference.
    """
    # Generate GDP filtered data
    first_diff_gdp = gdp[1:] - gdp[:-1]
    hp_gdp = hp.hp_filter(gdp)[1]
    bp_gdp = bpf.bandpass_filter(gdp, 16, 6, 32)[16:-16]
    all_gdp = np.array([first_diff_gdp, hp_gdp, bp_gdp])

    # Generate cpi filtered data
    first_diff_cpi = cpi[1:] - cpi[:-1]
    hp_cpi = hp.hp_filter(cpi)[1]
    bp_cpi = bpf.bandpass_filter(cpi, 16, 6, 32)[16:-16]
    all_cpi = np.array([first_diff_cpi, hp_cpi, bp_cpi])

    # Generate consumption filtered data
    first_diff_cons = cons[1:] - cons[:-1]
    hp_cons = hp.hp_filter(cons)[1]
    bp_cons = bpf.bandpass_filter(cons, 16, 6, 32)[16:-16]
    all_cons = np.array([first_diff_cons, hp_cons, bp_cons])

    # Generate investment filtered data
    first_diff_inv = inv[1:] - inv[:-1]
    hp_inv = hp.hp_filter(inv)[1]
    bp_inv = bpf.bandpass_filter(inv, 16, 6, 32)[16:-16]
    all_inv = np.array([first_diff_inv, hp_inv, bp_inv])

    all_data = np.vstack([all_gdp, all_cpi, all_cons, all_inv])


    all_tables = np.empty((4, 4, 3))

    data_titles = ['GDP', "CPI", 'Consumption', 'Investment']
    filter_labels = ['First Diff', 'HP', 'BP']


    for data_set in range(all_data.shape[0]):
        plt.figure()
        for filt in range(all_data.shape[1]):
            plt.plot(
                range(all_data[data_set, filt].size),
                all_data[data_set,filt],
                label = filter_labels[filt])
        plt.title(data_titles[data_set])
        plt.legend(loc=0)
        plt.show()


    for data_set in range(all_data.shape[0]):
        for i in range(all_data.shape[1]):
            all_tables[data_set, 0, i] = np.mean(all_data[data_set, i])
            all_tables[data_set, 1, i] = np.std(all_data[data_set, i])
            all_tables[data_set, 2, i] = np.corrcoef(all_data[data_set, i][:-1],
                                                all_data[data_set, i][1:])[0,1]
            all_tables[data_set, 3, i] = np.corrcoef(all_data[data_set, i],
                                                     all_data[0, i])[0,1]

    titles = ['Parameters [GDP]', 'Parameters [CPI]',
              'Parameters [cons]', 'Parameters [inv]']
    col_names = ['First Diff', 'HP (1600)', 'BK (16)']
    params = ['Mean', 'Std', 'Autocorrelation', 'Corr w/GDP']

    pretty_gdp = pt()
    pretty_cpi = pt()
    pretty_cons = pt()
    pretty_inv = pt()

    pretty_all = [pretty_gdp, pretty_cpi, pretty_cons, pretty_inv]

    for tab in range(4):
        pretty_all[tab].add_column(titles[tab], params)
        for col in range(3):
            pretty_all[tab].add_column(col_names[col], all_tables[tab, :, col])
        print pretty_all[tab]

    return pretty_all
예제 #6
0
import UhligSolve as lig
import pandas as pan
from pandas.io.data import DataReader as dr
import datetime

##-------------------- Step 1. Get data and filter it.------------------------##
# We will use Wage data: ECIWAG
# Also business labor: PRS84006173
# and Consumption of fixed capital: COFC

start_date = datetime.datetime(2000, 1, 1)
wage_Data= np.asarray(dr('ECIWAG', 'fred', start = start_date)['VALUE'])
labor_Data = np.asarray(dr('PRS84006173', 'fred', start = start_date)['VALUE'])
consump_Data = np.asarray(dr('COFC', 'fred', start = start_date)['VALUE'])

filt_wage = hp.hp_filter(wage_Data)[1]
filt_labor = hp.hp_filter(labor_Data)[1]
filt_consump = hp.hp_filter(consump_Data)[1]

moments = np.array([np.mean(filt_consump), np.mean(filt_labor),
                    np.mean(filt_wage)   ,np.var(filt_consump)])

##------------------------Step 2. Find steady state --------------------------##
def get_current_ss(beta):
    """
    This calls SolveSS.py to get the steady state for the particular parameter
    vector beta.

    Inputs:
        beta: This is a 3 element array with the value for gamma in the first
              place, xsi in the second, and a in the third.
예제 #7
0
cons = np.asarray(
    dr('PCECC96', 'fred', start = datetime.datetime(1947,1,1))['VALUE'])
inv = np.asarray(
    dr('GCEC96', 'fred', start = datetime.datetime(1947,1,1))['VALUE'])

mask = np.arange(1,cpi.size, 3)
cpi = cpi[mask]

# <codecell>


# <codecell>

# Generate GDP filtered datas
first_diff_gdp = gdp[1:] - gdp[:-1]
hp_gdp = hp.hp_filter(gdp)[1]
bp_gdp = bpf.bandpass_filter(gdp, 16, 6, 32)[16:-16]
all_gdp = np.array([first_diff_gdp, hp_gdp, bp_gdp])

gdp_table = np.zeros((4,all_gdp.shape[0]))

for i in range(all_gdp.shape[0]):
    gdp_table[0, i] = np.mean(all_gdp[i])
    gdp_table[1, i] = np.std(all_gdp[i])
    gdp_table[2, i] = np.corrcoef(all_gdp[i][:-1], all_gdp[i][1:])[0,1]
    cpi_table[3, i] = np.corrcoef(all_gdp[i], all_gdp[i])[0,1]


# Generate cpi filtered datas
first_diff_cpi = cpi[1:] - cpi[:-1]
hp_cpi = hp.hp_filter(cpi)[1]