Beispiel #1
0
def test_calc_rets_missing_instr_rets_key_error():
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLG5')])
    irets = pd.Series([0.02, 0.01, 0.012], index=idx)
    vals = [1, 1 / 2, 1 / 2, 1]
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                      (TS('2015-01-04'), 'CLF5'),
                                      (TS('2015-01-04'), 'CLG5'),
                                      (TS('2015-01-05'), 'CLG5')])
    weights = pd.DataFrame(vals, index=widx, columns=["CL1"])
    with pytest.raises(KeyError):
        util.calc_rets(irets, weights)
Beispiel #2
0
def test_calc_rets_two_generics_non_unique_columns():
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                     (TS('2015-01-03'), 'CLG5'),
                                     (TS('2015-01-04'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLG5'),
                                     (TS('2015-01-04'), 'CLH5'),
                                     (TS('2015-01-05'), 'CLG5'),
                                     (TS('2015-01-05'), 'CLH5')])
    rets = pd.Series([0.1, 0.15, 0.05, 0.1, 0.8, -0.5, 0.2], index=idx)
    vals = [[1, 0], [0, 1], [0.5, 0], [0.5, 0.5], [0, 0.5], [1, 0], [0, 1]]
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                      (TS('2015-01-03'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLF5'),
                                      (TS('2015-01-04'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLH5'),
                                      (TS('2015-01-05'), 'CLG5'),
                                      (TS('2015-01-05'), 'CLH5')])
    weights = pd.DataFrame(vals, index=widx, columns=['CL1', 'CL1'])
    with pytest.raises(ValueError):
        util.calc_rets(rets, weights)
Beispiel #3
0
def test_calc_rets_one_generic():
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLG5'),
                                     (TS('2015-01-05'), 'CLG5')])
    rets = pd.Series([0.1, 0.05, 0.1, 0.8], index=idx)
    vals = [1, 0.5, 0.5, 1]
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                      (TS('2015-01-04'), 'CLF5'),
                                      (TS('2015-01-04'), 'CLG5'),
                                      (TS('2015-01-05'), 'CLG5')])
    weights = pd.DataFrame(vals, index=widx, columns=['CL1'])
    wrets = util.calc_rets(rets, weights)
    wrets_exp = pd.DataFrame([0.1, 0.075, 0.8],
                             index=weights.index.levels[0],
                             columns=['CL1'])
    assert_frame_equal(wrets, wrets_exp)
Beispiel #4
0
def test_calc_rets_two_generics_two_asts():
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                     (TS('2015-01-03'), 'CLG5'),
                                     (TS('2015-01-04'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLG5'),
                                     (TS('2015-01-04'), 'CLH5'),
                                     (TS('2015-01-05'), 'CLG5'),
                                     (TS('2015-01-05'), 'CLH5')])
    rets1 = pd.Series([0.1, 0.15, 0.05, 0.1, 0.8, -0.5, 0.2], index=idx)
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'COF5'),
                                     (TS('2015-01-03'), 'COG5'),
                                     (TS('2015-01-04'), 'COF5'),
                                     (TS('2015-01-04'), 'COG5'),
                                     (TS('2015-01-04'), 'COH5')])
    rets2 = pd.Series([0.1, 0.15, 0.05, 0.1, 0.4], index=idx)
    rets = {"CL": rets1, "CO": rets2}

    vals = [[1, 0], [0, 1], [0.5, 0], [0.5, 0.5], [0, 0.5], [1, 0], [0, 1]]
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                      (TS('2015-01-03'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLF5'),
                                      (TS('2015-01-04'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLH5'),
                                      (TS('2015-01-05'), 'CLG5'),
                                      (TS('2015-01-05'), 'CLH5')])
    weights1 = pd.DataFrame(vals, index=widx, columns=["CL0", "CL1"])
    vals = [[1, 0], [0, 1], [0.5, 0], [0.5, 0.5], [0, 0.5]]
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'COF5'),
                                      (TS('2015-01-03'), 'COG5'),
                                      (TS('2015-01-04'), 'COF5'),
                                      (TS('2015-01-04'), 'COG5'),
                                      (TS('2015-01-04'), 'COH5')])
    weights2 = pd.DataFrame(vals, index=widx, columns=["CO0", "CO1"])
    weights = {"CL": weights1, "CO": weights2}
    wrets = util.calc_rets(rets, weights)
    wrets_exp = pd.DataFrame(
        [[0.1, 0.15, 0.1, 0.15], [0.075, 0.45, 0.075, 0.25],
         [-0.5, 0.2, pd.np.NaN, pd.np.NaN]],
        index=weights["CL"].index.levels[0],
        columns=['CL0', 'CL1', 'CO0', 'CO1'])
    assert_frame_equal(wrets, wrets_exp)
Beispiel #5
0
def test_calc_rets_two_generics_nans_in_second_generic():
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                     (TS('2015-01-03'), 'CLG5'),
                                     (TS('2015-01-04'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLG5'),
                                     (TS('2015-01-04'), 'CLH5'),
                                     (TS('2015-01-05'), 'CLG5'),
                                     (TS('2015-01-05'), 'CLH5')])
    rets = pd.Series([0.1, np.NaN, 0.05, 0.1, np.NaN, -0.5, 0.2], index=idx)
    vals = [[1, 0], [0, 1], [0.5, 0], [0.5, 0.5], [0, 0.5], [1, 0], [0, 1]]
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                      (TS('2015-01-03'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLF5'),
                                      (TS('2015-01-04'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLH5'),
                                      (TS('2015-01-05'), 'CLG5'),
                                      (TS('2015-01-05'), 'CLH5')])
    weights = pd.DataFrame(vals, index=widx, columns=['CL1', 'CL2'])
    wrets = util.calc_rets(rets, weights)
    wrets_exp = pd.DataFrame([[0.1, np.NaN], [0.075, np.NaN], [-0.5, 0.2]],
                             index=weights.index.levels[0],
                             columns=['CL1', 'CL2'])
    assert_frame_equal(wrets, wrets_exp)
Beispiel #6
0
def test_calc_rets_missing_weight():
    # see https://github.com/matthewgilbert/mapping/issues/8

    # missing weight for return
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-02'), 'CLF5'),
                                     (TS('2015-01-03'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLF5')])
    rets = pd.Series([0.02, -0.03, 0.06], index=idx)
    vals = [1, 1]
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-02'), 'CLF5'),
                                      (TS('2015-01-04'), 'CLF5')])
    weights = pd.DataFrame(vals, index=widx, columns=["CL1"])
    with pytest.raises(ValueError):
        util.calc_rets(rets, weights)

    # extra instrument
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-02'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLF5')])
    weights1 = pd.DataFrame(1, index=idx, columns=["CL1"])
    idx = pd.MultiIndex.from_tuples([
        (TS('2015-01-02'), 'CLF5'),
        (TS('2015-01-02'), 'CLH5'),
        (TS('2015-01-03'), 'CLH5'),  # extra day for no weight instrument
        (TS('2015-01-04'), 'CLF5'),
        (TS('2015-01-04'), 'CLH5')
    ])
    rets = pd.Series([0.02, -0.03, 0.06, 0.05, 0.01], index=idx)
    with pytest.raises(ValueError):
        util.calc_rets(rets, weights1)

    # leading / trailing returns
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-02'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLF5')])
    weights2 = pd.DataFrame(1, index=idx, columns=["CL1"])
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-01'), 'CLF5'),
                                     (TS('2015-01-02'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLF5'),
                                     (TS('2015-01-05'), 'CLF5')])
    rets = pd.Series([0.02, -0.03, 0.06, 0.05], index=idx)
    with pytest.raises(ValueError):
        util.calc_rets(rets, weights2)