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
    def BK_gain(k):
        """
        This function computes the gain for the BK filter given a particular
        value of k.

        Inputs:
            k: The degree of approximation for the filter.

        Outputs:
            a: A NumPy array for the gain at each frequency when we force the
               w = 0 frequency to be completely filtered out.
            b: A NumPy array for the gain at each frequency when we don't
               force the w = 0 frequency to be completely filtered out.
        """
        # Getting the a_h and b_h coefficients
        low_w = np.pi * 2 / 32
        high_w = np.pi * 2 / 6

        b_low, a_low = bpf.gen_b(k, low_w)
        b_high, a_high = bpf.gen_b(k, high_w)

        aweights = a_high - a_low
        bweights = b_high - b_low

        # Populating the frequency series for both a and b.
        w = np.arange(0, 2 * np.pi + .01, .01)
        h = np.arange(-k, k+1)

        a = np.array([])
        b = np.array([])

        for freq in range(0, w.size):
            a_Scalar = abs(np.dot(aweights, np.exp(1j*w[freq] * h)))
            b_Scalar = abs(np.dot(bweights, np.exp(1j*w[freq] * h)))

            a = np.append(a, a_Scalar)
            b = np.append(b, b_Scalar)

        return a, b
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
    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]
bp_cpi = bpf.bandpass_filter(cpi, 16, 6, 32)[16:-16]