def test_train_valid_test_split(fn):
    columns = ['test column 1', 'test column 2', 'test column 3']
    dates = generate_random_dates(10)
    assets = get_assets(3)
    index = pd.MultiIndex.from_product([dates, assets])
    values = np.arange(len(index) * len(columns)).reshape(
        [len(columns), len(index)]).T
    targets = np.arange(len(index))

    fn_inputs = {
        'all_x': pd.DataFrame(values, index, columns),
        'all_y': pd.Series(targets, index, name='target'),
        'train_size': 0.6,
        'valid_size': 0.2,
        'test_size': 0.2
    }
    fn_correct_outputs = OrderedDict([
        ('X_train', pd.DataFrame(values[:18], index[:18], columns=columns)),
        ('X_valid', pd.DataFrame(values[18:24], index[18:24],
                                 columns=columns)),
        ('X_test', pd.DataFrame(values[24:], index[24:], columns=columns)),
        ('y_train', pd.Series(targets[:18], index[:18])),
        ('y_valid', pd.Series(targets[18:24], index[18:24])),
        ('y_test', pd.Series(targets[24:], index[24:]))
    ])

    assert_output(fn,
                  fn_inputs,
                  fn_correct_outputs,
                  check_parameter_changes=False)
def test_non_overlapping_samples(fn):
    columns = ['test column 1', 'test column 2']
    dates = generate_random_dates(8)
    assets = get_assets(3)
    index = pd.MultiIndex.from_product([dates, assets])
    values = np.arange(len(index) * len(columns)).reshape(
        [len(columns), len(index)]).T
    targets = np.arange(len(index))

    fn_inputs = {
        'x': pd.DataFrame(values, index, columns),
        'y': pd.Series(targets, index),
        'n_skip_samples': 2,
        'start_i': 1
    }

    new_index = pd.MultiIndex.from_product(
        [dates[fn_inputs['start_i']::fn_inputs['n_skip_samples'] + 1], assets])
    fn_correct_outputs = OrderedDict([
        ('non_overlapping_x',
         pd.DataFrame([[3, 27], [4, 28], [5, 29], [12, 36], [13, 37], [14, 38],
                       [21, 45], [22, 46], [23, 47]], new_index, columns)),
        ('non_overlapping_y',
         pd.Series([3, 4, 5, 12, 13, 14, 21, 22, 23], new_index))
    ])

    assert_output(fn,
                  fn_inputs,
                  fn_correct_outputs,
                  check_parameter_changes=False)
Esempio n. 3
0
def test_get_long_short(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        'close':
        pd.DataFrame([[25.6788, 35.1392, 34.0527], [25.1884, 14.3453, 39.9373],
                      [78.2803, 34.3854, 23.2932], [88.8725, 52.223, 34.4107]],
                     dates, tickers),
        'lookback_high':
        pd.DataFrame(
            [[np.nan, np.nan, np.nan], [92.11310000, 91.05430000, 90.95720000],
             [35.4411, 34.1799, 34.0223],
             [92.11310000, 91.05430000, 90.95720000]], dates, tickers),
        'lookback_low':
        pd.DataFrame([[np.nan, np.nan, np.nan], [34.1705, 92.453, 58.5107],
                      [15.67180000, 12.34530000, 34.05270000],
                      [27.18340000, 12.34530000, 23.29320000]], dates, tickers)
    }
    fn_correct_outputs = OrderedDict([
        ('long_short',
         pd.DataFrame([[0, 0, 0], [-1, -1, -1], [1, 1, -1], [0, 0, 0]], dates,
                      tickers))
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
Esempio n. 4
0
def test_get_signal_return(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(5)

    fn_inputs = {
        'signal':
        pd.DataFrame(
            [[0, 0, 0], [-1, -1, -1], [1, 0, 0], [0, 0, 0], [0, 1, 0]], dates,
            tickers),
        'lookahead_returns':
        pd.DataFrame(
            [[0.88702896, 0.96521098, 0.65854789],
             [1.13391240, 0.87420969, -0.53914925],
             [0.35450805, -0.56900529, -0.64808965],
             [0.38572896, -0.94655617, 0.123564379], [np.nan, np.nan, np.nan]],
            dates, tickers)
    }
    fn_correct_outputs = OrderedDict([
        ('signal_return',
         pd.DataFrame(
             [[0, 0, 0], [-1.13391240, -0.87420969, 0.53914925],
              [0.35450805, 0, 0], [0, 0, 0], [np.nan, np.nan, np.nan]], dates,
             tickers))
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
Esempio n. 5
0
def test_get_return_lookahead(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(5)

    fn_inputs = {
        'close':
        pd.DataFrame([[25.6788, 35.1392, 34.0527], [25.1884, 14.3453, 39.9373],
                      [62.3457, 92.2524, 65.7893], [78.2803, 34.3854, 23.2932],
                      [88.8725, 52.223, 34.4107]], dates, tickers),
        'lookahead_prices':
        pd.DataFrame([[62.34570000, 92.25240000, 65.78930000],
                      [78.28030000, 34.38540000, 23.29320000],
                      [88.87250000, 52.22300000, 34.41070000],
                      [np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan]],
                     dates, tickers)
    }
    fn_correct_outputs = OrderedDict([
        ('lookahead_returns',
         pd.DataFrame([[0.88702896, 0.96521098, 0.65854789],
                       [1.13391240, 0.87420969, -0.53914925],
                       [0.35450805, -0.56900529, -0.64808965],
                       [np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan]],
                      dates, tickers))
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_generate_positions(fn):
    tickers = generate_random_tickers(5)
    dates = generate_random_dates(6)

    fn_inputs = {
        'prices': pd.DataFrame(
            [
                [65.40757705426432, 27.556319958924323, 50.59935209411175, 56.274712269629134, 99.9873070881051],
                [47.82126720752715, 56.812865745668375, 40.75685814634723, 27.469680989736023, 41.449858088448735],
                [88.20038097315815, 45.573972499280494, 36.592711369868724, 21.36570423559795, 0.698919959739297],
                [14.670236824202721, 49.557949251949054, 18.935364730808935, 23.163368660093298, 8.075599541367884],
                [41.499140208637705, 9.75987296846733, 66.08677766062186, 37.927861417544385, 10.792730405945827],
                [86.26923464863536, 32.12679487375028, 15.621592524570282, 77.1908860965619, 52.733950486350444]],
            dates, tickers)}
    fn_correct_outputs = OrderedDict([
        (
            'final_positions',
            pd.DataFrame(
                [
                    [30, 0, 30, 30, 30],
                    [0, 30, 0, 0, 0],
                    [30, 0, 0, 0, -10],
                    [-10, 0, -10, 0, -10],
                    [0, -10, 30, 0, -10],
                    [30, 0, -10, 30, 30]],
                dates, tickers))])

    assert_output(fn, fn_inputs, fn_correct_outputs)
Esempio n. 7
0
def test_factor_betas(fn):
    n_components = 3
    dates = generate_random_dates(4)
    assets = get_assets(3)

    pca = PCA(n_components)
    pca.fit(
        pd.DataFrame([[0.21487253, 0.12342312, -0.13245215],
                      [0.23423439, -0.23434532, 1.67834324],
                      [0.23432445, -0.23563226, 0.23423523],
                      [0.24824535, -0.23523435, 0.36235236]], dates, assets))

    fn_inputs = {
        'pca': pca,
        'factor_beta_indices': np.array(assets),
        'factor_beta_columns': np.arange(n_components)
    }
    fn_correct_outputs = OrderedDict([
        ('factor_betas',
         pd.DataFrame([[0.00590170, -0.07759542, 0.99696746],
                       [-0.13077609, 0.98836246, 0.07769983],
                       [0.99139436, 0.13083807, 0.00431461]],
                      fn_inputs['factor_beta_indices'],
                      fn_inputs['factor_beta_columns']))
    ])

    assert_output(fn,
                  fn_inputs,
                  fn_correct_outputs,
                  check_parameter_changes=False)
def test_generate_dollar_volume_weights(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        'close': pd.DataFrame(
            [
                [35.4411, 34.1799, 34.0223],
                [92.1131, 91.0543, 90.9572],
                [57.9708, 57.7814, 58.1982],
                [34.1705, 92.453, 58.5107]],
            dates, tickers),
        'volume': pd.DataFrame(
            [
                [9.83683e+06, 1.78072e+07, 8.82982e+06],
                [8.22427e+07, 6.85315e+07, 4.81601e+07],
                [1.62348e+07, 1.30527e+07, 9.51201e+06],
                [1.06742e+07, 5.68313e+07, 9.31601e+06]],
            dates, tickers)}
    fn_correct_outputs = OrderedDict([
        (
            'dollar_volume_weights',
            pd.DataFrame(
                [
                    [0.27719777, 0.48394253, 0.23885970],
                     [0.41632975, 0.34293308, 0.24073717],
                     [0.41848548, 0.33536102, 0.24615350],
                     [0.05917255, 0.85239760, 0.08842984]],
                dates, tickers))])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_idiosyncratic_var_matrix(fn):
    dates = generate_random_dates(4)
    assets = get_assets(3)

    fn_inputs = {
        'returns': pd.DataFrame(
            [
                [ 0.02769242,  1.34872387,  0.23460972],
                [-0.94728692,  0.68386883, -1.23987235],
                [ 1.93769376, -0.48275934,  0.34957348],
                [ 0.23985234,  0.35897345,  0.34598734]],
            dates, assets),
        'factor_returns': pd.DataFrame([
                [-0.49503261,  1.45332369, -0.08980631],
                [-1.87563271,  0.67894147, -1.11984992],
                [-0.13027172, -0.49001128,  1.67259298],
                [-0.25392567,  0.47320133,  0.04528734]],
                dates),
        'factor_betas': pd.DataFrame([
            [ 0.00590170, -0.07759542, 0.99696746],
            [-0.13077609,  0.98836246, 0.07769983],
            [ 0.99139436,  0.13083807, 0.00431461]]),
        'ann_factor': 250}
    fn_correct_outputs = OrderedDict([
        (
            'idiosyncratic_var_matrix', pd.DataFrame(np.full([3,3], 0.0), assets, assets))])

    assert_output(fn, fn_inputs, fn_correct_outputs)
Esempio n. 10
0
def test_calculate_simple_moving_average(fn):
    tickers = generate_random_tickers(5)
    dates = generate_random_dates(6)

    fn_inputs = {
        'close': pd.DataFrame(
            [
                [21.050810483942833, 17.013843810658827, 10.984503755486879, 11.248093428369392, 12.961712733997235],
                [15.63570258751384, 14.69054309070934, 11.353027688995159, 475.74195118202061, 11.959640427803022],
                [482.34539247360806, 35.202580592515041, 3516.5416782257166, 66.405314327318209, 13.503960481087077],
                [10.918933017418304, 17.9086438675435, 24.801265417692324, 12.488954191854916, 10.52435923388642],
                [10.675971965144655, 12.749401436636365, 11.805257579935713, 21.539039489843024, 19.99766036804861],
                [11.545495378369814, 23.981468434099405, 24.974763062186504, 36.031962102997689, 14.304332320024963]],
            dates, tickers),
    'rolling_window': 3}
    fn_correct_outputs = OrderedDict([
        (
            'returns',
            pd.DataFrame(
                [
                    [np.nan, np.nan, np.nan, np.nan, np.nan],
                    [np.nan, np.nan, np.nan, np.nan, np.nan],
                    [173.01063518,22.30232250,1179.62640322,184.46511965,12.80843788],
                    [169.63334269,22.60058918,1184.23199044,184.87873990,11.99598671],
                    [167.98009915,21.95354197,1184.38273374,33.47776934,14.67532669],
                    [11.04680012,18.21317125,20.52709535,23.35331859,14.94211731]],
                dates, tickers))])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_get_signal_return(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(5)

    fn_inputs = {
        'signal': pd.DataFrame(
            [
                [0, 0, 0],
                [-1, -1, -1],
                [1, 0, 0],
                [0, 0, 0],
                [0, 1, 0]],
            dates, tickers),
        'lookahead_returns': pd.DataFrame(
            [
                [0.88702896, 0.96521098, 0.65854789],
                [1.13391240, 0.87420969, -0.53914925],
                [0.35450805, -0.56900529, -0.64808965],
                [0.38572896, -0.94655617, 0.123564379],
                [np.nan, np.nan, np.nan]],
            dates, tickers)}
    fn_correct_outputs = OrderedDict([
        (
            'signal_return',
            pd.DataFrame(
                [
                    [0, 0, 0],
                    [-1.13391240, -0.87420969,  0.53914925],
                    [0.35450805, 0, 0],
                    [0, 0, 0],
                    [np.nan, np.nan, np.nan]],
                dates, tickers))])

    assert_output(fn, fn_inputs, fn_correct_outputs)
Esempio n. 12
0
def test_calculate_dividend_weights(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        "dividends":
        pd.DataFrame(
            [[0.0, 0.0, 0.0], [0.0, 0.0, 0.1], [0.0, 1.0, 0.3],
             [0.0, 0.2, 0.0]],
            dates,
            tickers,
        )
    }
    fn_correct_outputs = OrderedDict([(
        "dividend_weights",
        pd.DataFrame(
            [
                [np.nan, np.nan, np.nan],
                [0.00000000, 0.00000000, 1.00000000],
                [0.00000000, 0.71428571, 0.28571429],
                [0.00000000, 0.75000000, 0.25000000],
            ],
            dates,
            tickers,
        ),
    )])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_get_lookahead_prices(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(5)

    fn_inputs = {
        'close': pd.DataFrame(
            [
                [25.6788, 35.1392, 34.0527],
                [25.1884, 14.3453, 39.9373],
                [62.3457, 92.2524, 65.7893],
                [78.2803, 34.3854, 23.2932],
                [88.8725, 52.223, 34.4107]],
            dates, tickers),
        'lookahead_days': 2}
    fn_correct_outputs = OrderedDict([
        (
            'lookahead_prices',
            pd.DataFrame(
                [
                    [62.34570000, 92.25240000, 65.78930000],
                    [78.28030000, 34.38540000, 23.29320000],
                    [88.87250000, 52.22300000, 34.41070000],
                    [np.nan, np.nan, np.nan],
                    [np.nan, np.nan, np.nan]],
                dates, tickers))])

    assert_output(fn, fn_inputs, fn_correct_outputs)
Esempio n. 14
0
def test_factor_cov_matrix(fn):
    dates = generate_random_dates(4)

    fn_inputs = {
        "factor_returns":
        pd.DataFrame(
            [
                [-0.49503261, 1.45332369, -0.08980631],
                [-1.87563271, 0.67894147, -1.11984992],
                [-0.13027172, -0.49001128, 1.67259298],
                [-0.25392567, 0.47320133, 0.04528734],
            ],
            dates,
        ),
        "ann_factor":
        250,
    }
    fn_correct_outputs = OrderedDict([(
        "factor_cov_matrix",
        np.array([
            [162.26559808, 0.0, 0.0],
            [0.0, 159.86284454, 0.0],
            [0.0, 0.0, 333.09785876],
        ]),
    )])

    assert_output(fn, fn_inputs, fn_correct_outputs)
Esempio n. 15
0
def test_idiosyncratic_var_vector(fn):
    dates = generate_random_dates(4)
    assets = get_assets(3)

    fn_inputs = {
        "returns":
        pd.DataFrame(
            [
                [0.02769242, 1.34872387, 0.23460972],
                [-0.94728692, 0.68386883, -1.23987235],
                [1.93769376, -0.48275934, 0.34957348],
                [0.23985234, 0.35897345, 0.34598734],
            ],
            dates,
            assets,
        ),
        "idiosyncratic_var_matrix":
        pd.DataFrame(
            [
                [0.02272535, 0.0, 0.0],
                [0.0, 0.05190083, 0.0],
                [0.0, -0.49001128, 0.05431181],
            ],
            assets,
            assets,
        ),
    }
    fn_correct_outputs = OrderedDict([(
        "idiosyncratic_var_vector",
        pd.DataFrame([0.02272535, 0.05190083, 0.05431181], assets),
    )])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_generate_weighted_returns(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        'returns': pd.DataFrame(
            [
                [np.nan, np.nan, np.nan],
                [1.59904743, 1.66397210, 1.67345829],
                [-0.37065629, -0.36541822, -0.36015840],
                [-0.41055669, 0.60004777, 0.00536958]],
            dates, tickers),
        'weights': pd.DataFrame(
            [
                [0.03777059, 0.04733924, 0.05197790],
                [0.82074874, 0.48533938, 0.75792752],
                [0.10196420, 0.05866016, 0.09578226],
                [0.03951647, 0.40866122, 0.09431233]],
            dates, tickers)}
    fn_correct_outputs = OrderedDict([
        (
            'weighted_returns',
                pd.DataFrame(
                    [
                        [np.nan, np.nan, np.nan],
                        [1.31241616, 0.80759119, 1.26836009],
                        [-0.03779367, -0.02143549, -0.03449679],
                        [-0.01622375, 0.24521625, 0.00050642]],
                    dates, tickers))])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_factor_betas(fn):
    n_components = 3
    dates = generate_random_dates(4)
    assets = get_assets(3)

    pca = PCA(n_components)
    pca.fit(pd.DataFrame(
        [
            [0.21487253,  0.12342312, -0.13245215],
            [0.23423439, -0.23434532,  1.67834324],
            [0.23432445, -0.23563226,  0.23423523],
            [0.24824535, -0.23523435,  0.36235236]],
        dates, assets))

    fn_inputs = {
        'pca': pca,
        'factor_beta_indices': np.array(assets),
        'factor_beta_columns': np.arange(n_components)}
    fn_correct_outputs = OrderedDict([
        (
            'factor_betas', pd.DataFrame(
                [
                    [ 0.00590170, -0.07759542, 0.99696746],
                    [-0.13077609,  0.98836246, 0.07769983],
                    [ 0.99139436,  0.13083807, 0.00431461]],
                fn_inputs['factor_beta_indices'],
                fn_inputs['factor_beta_columns']))])

    assert_output(fn, fn_inputs, fn_correct_outputs, check_parameter_changes=False)
Esempio n. 18
0
def test_get_high_lows_lookback(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        "high":
        pd.DataFrame(
            [
                [35.4411, 34.1799, 34.0223],
                [92.1131, 91.0543, 90.9572],
                [57.9708, 57.7814, 58.1982],
                [34.1705, 92.453, 58.5107],
            ],
            dates,
            tickers,
        ),
        "low":
        pd.DataFrame(
            [
                [15.6718, 75.1392, 34.0527],
                [27.1834, 12.3453, 95.9373],
                [28.2503, 24.2854, 23.2932],
                [86.3725, 32.223, 38.4107],
            ],
            dates,
            tickers,
        ),
        "lookback_days":
        2,
    }
    fn_correct_outputs = OrderedDict([
        (
            "lookback_high",
            pd.DataFrame(
                [
                    [np.nan, np.nan, np.nan],
                    [np.nan, np.nan, np.nan],
                    [92.11310000, 91.05430000, 90.95720000],
                    [92.11310000, 91.05430000, 90.95720000],
                ],
                dates,
                tickers,
            ),
        ),
        (
            "lookback_low",
            pd.DataFrame(
                [
                    [np.nan, np.nan, np.nan],
                    [np.nan, np.nan, np.nan],
                    [15.67180000, 12.34530000, 34.05270000],
                    [27.18340000, 12.34530000, 23.29320000],
                ],
                dates,
                tickers,
            ),
        ),
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
Esempio n. 19
0
def test_idiosyncratic_var_matrix(fn):
    dates = generate_random_dates(4)
    assets = get_assets(3)

    fn_inputs = {
        'returns':
        pd.DataFrame([[0.02769242, 1.34872387, 0.23460972],
                      [-0.94728692, 0.68386883, -1.23987235],
                      [1.93769376, -0.48275934, 0.34957348],
                      [0.23985234, 0.35897345, 0.34598734]], dates, assets),
        'factor_returns':
        pd.DataFrame([[-0.49503261, 1.45332369, -0.08980631],
                      [-1.87563271, 0.67894147, -1.11984992],
                      [-0.13027172, -0.49001128, 1.67259298],
                      [-0.25392567, 0.47320133, 0.04528734]], dates),
        'factor_betas':
        pd.DataFrame([[0.00590170, -0.07759542, 0.99696746],
                      [-0.13077609, 0.98836246, 0.07769983],
                      [0.99139436, 0.13083807, 0.00431461]]),
        'ann_factor':
        250
    }
    fn_correct_outputs = OrderedDict([('idiosyncratic_var_matrix',
                                       pd.DataFrame(np.full([3, 3], 0.0),
                                                    assets, assets))])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_date_top_industries(fn):
    tickers = generate_random_tickers(10)
    dates = generate_random_dates(2)

    fn_inputs = {
        'prices':
        pd.DataFrame(
            [[
                21.050810483942833, 17.013843810658827, 10.984503755486879,
                11.248093428369392, 12.961712733997235, 482.34539247360806,
                35.202580592515041, 3516.5416782257166, 66.405314327318209,
                13.503960481087077
            ],
             [
                 15.63570258751384, 14.69054309070934, 11.353027688995159,
                 475.74195118202061, 11.959640427803022, 10.918933017418304,
                 17.9086438675435, 24.801265417692324, 12.488954191854916,
                 15.63570258751384
             ]], dates, tickers),
        'sector':
        pd.Series([
            'ENERGY', 'MATERIALS', 'ENERGY', 'ENERGY', 'TELECOM', 'FINANCIALS',
            'TECHNOLOGY', 'HEALTH', 'MATERIALS', 'REAL ESTATE'
        ], tickers),
        'date':
        dates[-1],
        'top_n':
        4
    }
    fn_correct_outputs = OrderedDict([('top_industries',
                                       {'ENERGY', 'HEALTH', 'TECHNOLOGY'})])

    assert_output(fn, fn_inputs, fn_correct_outputs)
Esempio n. 21
0
def test_generate_returns(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        "prices":
        pd.DataFrame(
            [
                [35.4411, 34.1799, 34.0223],
                [92.1131, 91.0543, 90.9572],
                [57.9708, 57.7814, 58.1982],
                [34.1705, 92.453, 58.5107],
            ],
            dates,
            tickers,
        )
    }
    fn_correct_outputs = OrderedDict([(
        "returns",
        pd.DataFrame(
            [
                [np.nan, np.nan, np.nan],
                [1.59904743, 1.66397210, 1.67345829],
                [-0.37065629, -0.36541822, -0.36015840],
                [-0.41055669, 0.60004777, 0.00536958],
            ],
            dates,
            tickers,
        ),
    )])

    assert_output(fn, fn_inputs, fn_correct_outputs)
Esempio n. 22
0
def test_get_covariance_returns(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        "returns":
        pd.DataFrame(
            [
                [np.nan, np.nan, np.nan],
                [1.59904743, 1.66397210, 1.67345829],
                [-0.37065629, -0.36541822, -0.36015840],
                [-0.41055669, 0.60004777, 0.00536958],
            ],
            dates,
            tickers,
        )
    }
    fn_correct_outputs = OrderedDict([(
        "returns_covariance",
        np.array([
            [0.89856076, 0.7205586, 0.8458721],
            [0.7205586, 0.78707297, 0.76450378],
            [0.8458721, 0.76450378, 0.83182775],
        ]),
    )])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_get_return_lookahead(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(5)

    fn_inputs = {
        'close': pd.DataFrame(
            [
                [25.6788, 35.1392, 34.0527],
                [25.1884, 14.3453, 39.9373],
                [62.3457, 92.2524, 65.7893],
                [78.2803, 34.3854, 23.2932],
                [88.8725, 52.223, 34.4107]],
            dates, tickers),
        'lookahead_prices': pd.DataFrame(
            [
                [62.34570000, 92.25240000, 65.78930000],
                [78.28030000, 34.38540000, 23.29320000],
                [88.87250000, 52.22300000, 34.41070000],
                [np.nan, np.nan, np.nan],
                [np.nan, np.nan, np.nan]],
            dates, tickers)}
    fn_correct_outputs = OrderedDict([
        (
            'lookahead_returns',
            pd.DataFrame(
                [
                    [0.88702896,  0.96521098,  0.65854789],
                    [1.13391240,  0.87420969, -0.53914925],
                    [0.35450805, -0.56900529, -0.64808965],
                    [np.nan, np.nan, np.nan],
                    [np.nan, np.nan, np.nan]],
                dates, tickers))])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_non_overlapping_estimators(fn):
    n_estimators = 3
    columns = ['test column 1', 'test column 2']
    dates = generate_random_dates(8)
    assets = get_assets(3)
    index = pd.MultiIndex.from_product([dates, assets])
    noise = np.random.RandomState(0).random_sample([len(index)]) * len(index)
    values = np.arange(len(index) * len(columns)).reshape([len(columns), len(index)]).T
    targets = np.sum(values, axis=-1) + noise

    classifiers = [
        RandomForestRegressor(300, oob_score=True, n_jobs=-1, random_state=101)
        for _ in range(n_estimators)]

    fn_inputs = {
        'x': pd.DataFrame(values, index, columns),
        'y': pd.Series(targets, index),
        'classifiers': classifiers,
        'n_skip_samples': 3}

    random_forest_regressor_fit = RandomForestRegressor.fit
    with patch.object(RandomForestRegressor, 'fit', autospec=True) as mock_fit:
        mock_fit.side_effect = random_forest_regressor_fit
        fn_return_value = fn(**fn_inputs)

        assert_structure(fn_return_value, [RandomForestRegressor for _ in range(n_estimators)], 'PCA')

        for classifier in fn_return_value:
            try:
                classifier.fit.assert_called()
            except AssertionError:
                raise Exception('Test Failure: RandomForestRegressor.fit not called on all classifiers')
def test_fit_pca(fn):
    dates = generate_random_dates(4)
    assets = get_assets(3)

    fn_inputs = {
        'returns':
        pd.DataFrame([[0.02769242, 1.34872387, 0.23460972],
                      [-0.94728692, 0.68386883, -1.23987235],
                      [1.93769376, -0.48275934, 0.34957348],
                      [0.23985234, 0.35897345, 0.34598734]], dates, assets),
        'num_factor_exposures':
        2,
        'svd_solver':
        'full'
    }
    fn_correct_values = {
        'PCA':
        PCA(),
        'PCA.components_':
        np.array([[0.81925896, -0.40427891, 0.40666118],
                  [-0.02011128, 0.68848693, 0.72496985]])
    }

    pca_fit = PCA.fit
    with patch.object(PCA, 'fit', autospec=True) as mock_fit:
        mock_fit.side_effect = pca_fit

        fn_return_value = fn(**fn_inputs)

        assert_structure(fn_return_value, fn_correct_values['PCA'], 'PCA')

        try:
            # print(dir(fn_return_value.fit))
            fn_return_value.fit.assert_called()
            # old python:
            # fn_return_value.fit.assert_any_call()

        except AssertionError:
            raise Exception('Test Failure: PCA.fit not called')

        try:
            fn_return_value.fit.assert_called_with(self=fn_return_value,
                                                   X=fn_inputs['returns'])
        except Exception:
            raise Exception(
                'Test Failure: PCA.fit called with the wrong arguments')

        assert_structure(fn_return_value.components_,
                         fn_correct_values['PCA.components_'],
                         'PCA.components_')

        if not does_data_match(fn_return_value.components_,
                               fn_correct_values['PCA.components_']):
            raise Exception('Test Failure: PCA not fitted correctly\n\n'
                            'PCA.components_:\n'
                            '{}\n\n'
                            'Expected PCA.components_:\n'
                            '{}'.format(fn_return_value.components_,
                                        fn_correct_values['PCA.components_']))
Esempio n. 26
0
def test_factor_returns(fn):
    n_components = 3
    dates = generate_random_dates(4)
    assets = get_assets(3)

    pca = PCA(n_components)
    pca.fit(
        pd.DataFrame(
            [
                [0.21487253, 0.12342312, -0.13245215],
                [0.23423439, -0.23434532, 1.67834324],
                [0.23432445, -0.23563226, 0.23423523],
                [0.24824535, -0.23523435, 0.36235236],
            ],
            dates,
            assets,
        ))

    fn_inputs = {
        "pca":
        pca,
        "returns":
        pd.DataFrame(
            [
                [0.02769242, 1.34872387, 0.23460972],
                [-0.94728692, 0.68386883, -1.23987235],
                [1.93769376, -0.48275934, 0.34957348],
                [0.23985234, 0.35897345, 0.34598734],
            ],
            dates,
            assets,
        ),
        "factor_return_indices":
        np.array(dates),
        "factor_return_columns":
        np.arange(n_components),
    }
    fn_correct_outputs = OrderedDict([(
        "factor_returns",
        pd.DataFrame(
            [
                [-0.49503261, 1.45332369, -0.08980631],
                [-1.87563271, 0.67894147, -1.11984992],
                [-0.13027172, -0.49001128, 1.67259298],
                [-0.25392567, 0.47320133, 0.04528734],
            ],
            fn_inputs["factor_return_indices"],
            fn_inputs["factor_return_columns"],
        ),
    )])

    assert_output(fn,
                  fn_inputs,
                  fn_correct_outputs,
                  check_parameter_changes=False)
def test_tracking_error(fn):
    dates = generate_random_dates(4)

    fn_inputs = {
        'benchmark_returns_by_date':
        pd.Series([np.nan, 0.99880148, 0.99876653, 1.00024411], dates),
        'etf_returns_by_date':
        pd.Series([np.nan, 0.63859274, 0.93475823, 2.57295727], dates)
    }
    fn_correct_outputs = OrderedDict([('tracking_error', 16.5262431971)])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_rebalance_portfolio(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(11)

    fn_inputs = {
        'returns':
        pd.DataFrame(
            [[np.nan, np.nan, np.nan], [-0.02202381, 0.02265285, 0.01441961],
             [0.01947657, 0.00551985, 0.00047382],
             [0.00537313, -0.00803232, 0.01160313],
             [0.00593824, -0.00567773, 0.02247191],
             [0.02479339, 0.01758824, -0.00824176],
             [-0.0109447, -0.00383568, 0.01361958],
             [0.01164822, 0.01558719, 0.00614894],
             [0.0109384, -0.00182079, 0.02900868],
             [0.01138952, 0.00218049, -0.00954495],
             [0.0106982, 0.00644535, -0.01815329]], dates, tickers),
        'index_weights':
        pd.DataFrame([[0.00449404, 0.11586048, 0.00359727],
                      [
                          0.00403487,
                          0.12534048,
                          0.0034428,
                      ], [0.00423485, 0.12854258, 0.00347404],
                      [0.00395679, 0.1243466, 0.00335064],
                      [0.00368729, 0.11750295, 0.00333929],
                      [0.00369562, 0.11447422, 0.00325973],
                      [
                          0.00379612,
                          0.11088075,
                          0.0031734,
                      ], [0.00366501, 0.10806014, 0.00314648],
                      [0.00361268, 0.10376514, 0.00323257],
                      [0.00358844, 0.10097531, 0.00319009],
                      [0.00362045, 0.09791232, 0.00318071]], dates, tickers),
        'shift_size':
        2,
        'chunk_size':
        4
    }
    fn_correct_outputs = OrderedDict([('all_rebalance_weights', [
        np.array([0.29341237, 0.41378419, 0.29280344]),
        np.array([0.29654088, 0.40731481, 0.29614432]),
        np.array([0.29868214, 0.40308791, 0.29822995]),
        np.array([0.30100044, 0.39839644, 0.30060312])
    ])])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_tracking_error(fn):
    dates = generate_random_dates(4)

    fn_inputs = {
        'benchmark_returns_by_date': pd.Series(
                [np.nan, 0.99880148, 0.99876653, 1.00024411],
                dates),
        'etf_returns_by_date': pd.Series(
                [np.nan, 0.63859274, 0.93475823, 2.57295727],
                dates)}
    fn_correct_outputs = OrderedDict([
        (
            'tracking_error',
            16.5262431971)])

    assert_output(fn, fn_inputs, fn_correct_outputs)
Esempio n. 30
0
def test_filter_signals(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(10)

    fn_inputs = {
        "signal":
        pd.DataFrame(
            [
                [0, 0, 0],
                [-1, -1, -1],
                [1, 0, -1],
                [0, 0, 0],
                [1, 0, 0],
                [0, 1, 0],
                [0, 0, 1],
                [0, -1, 1],
                [-1, 0, 0],
                [0, 0, 0],
            ],
            dates,
            tickers,
        ),
        "lookahead_days":
        3,
    }
    fn_correct_outputs = OrderedDict([(
        "filtered_signal",
        pd.DataFrame(
            [
                [0, 0, 0],
                [-1, -1, -1],
                [1, 0, 0],
                [0, 0, 0],
                [0, 0, 0],
                [0, 1, 0],
                [0, 0, 1],
                [0, -1, 0],
                [-1, 0, 0],
                [0, 0, 0],
            ],
            dates,
            tickers,
        ),
    )])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_fit_pca(fn):
    dates = generate_random_dates(4)
    assets = get_assets(3)

    fn_inputs = {
        'returns': pd.DataFrame(
            [
                [0.02769242, 1.34872387, 0.23460972],
                [-0.94728692, 0.68386883, -1.23987235],
                [1.93769376, -0.48275934, 0.34957348],
                [0.23985234, 0.35897345, 0.34598734]],
            dates, assets),
        'num_factor_exposures': 2,
        'svd_solver': 'full'}
    fn_correct_values = {
        'PCA': PCA(),
        'PCA.components_': np.array([
            [0.81925896, -0.40427891, 0.40666118],
            [-0.02011128, 0.68848693, 0.72496985]])}

    pca_fit = PCA.fit
    with patch.object(PCA, 'fit', autospec=True) as mock_fit:
        mock_fit.side_effect = pca_fit

        fn_return_value = fn(**fn_inputs)

        assert_structure(fn_return_value, fn_correct_values['PCA'], 'PCA')

        try:
            fn_return_value.fit.assert_called()
        except AssertionError:
            raise Exception('Test Failure: PCA.fit not called')

        try:
            fn_return_value.fit.assert_called_with(self=fn_return_value, X=fn_inputs['returns'])
        except Exception:
            raise Exception('Test Failure: PCA.fit called with the wrong arguments')

        assert_structure(fn_return_value.components_, fn_correct_values['PCA.components_'], 'PCA.components_')

        if not does_data_match(fn_return_value.components_, fn_correct_values['PCA.components_']):
            raise Exception('Test Failure: PCA not fitted correctly\n\n'
                            'PCA.components_:\n'
                            '{}\n\n'
                            'Expected PCA.components_:\n'
                            '{}'.format(fn_return_value.components_, fn_correct_values['PCA.components_']))
def test_rebalance_portfolio(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(11)

    fn_inputs = {
        'returns': pd.DataFrame(
            [
                [np.nan, np.nan, np.nan],
                [-0.02202381, 0.02265285, 0.01441961],
                [0.01947657, 0.00551985, 0.00047382],
                [0.00537313, -0.00803232, 0.01160313],
                [0.00593824, -0.00567773, 0.02247191],
                [0.02479339, 0.01758824, -0.00824176],
                [-0.0109447, -0.00383568, 0.01361958],
                [0.01164822, 0.01558719, 0.00614894],
                [0.0109384, -0.00182079, 0.02900868],
                [0.01138952, 0.00218049, -0.00954495],
                [0.0106982, 0.00644535, -0.01815329]],
            dates, tickers),
        'index_weights': pd.DataFrame(
            [
                [0.00449404, 0.11586048, 0.00359727],
                [0.00403487, 0.12534048, 0.0034428, ],
                [0.00423485, 0.12854258, 0.00347404],
                [0.00395679, 0.1243466, 0.00335064],
                [0.00368729, 0.11750295, 0.00333929],
                [0.00369562, 0.11447422, 0.00325973],
                [0.00379612, 0.11088075, 0.0031734, ],
                [0.00366501, 0.10806014, 0.00314648],
                [0.00361268, 0.10376514, 0.00323257],
                [0.00358844, 0.10097531, 0.00319009],
                [0.00362045, 0.09791232, 0.00318071]],
            dates, tickers),
        'shift_size': 2,
        'chunk_size': 4}
    fn_correct_outputs = OrderedDict([
        (
            'all_rebalance_weights',
            [
                np.array([0.29341237, 0.41378419, 0.29280344]),
                np.array([0.29654088, 0.40731481, 0.29614432]),
                np.array([0.29868214, 0.40308791, 0.29822995]),
                np.array([0.30100044, 0.39839644, 0.30060312])]
        )])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_calculate_cumulative_returns(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        'returns':
        pd.DataFrame(
            [[np.nan, np.nan, np.nan], [1.59904743, 1.66397210, 1.67345829],
             [-0.37065629, -0.36541822, -0.36015840],
             [-0.41055669, 0.60004777, 0.00536958]], dates, tickers)
    }
    fn_correct_outputs = OrderedDict([
        ('cumulative_returns',
         pd.Series([np.nan, 5.93647782, -0.57128454, -0.68260542], dates))
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
Esempio n. 34
0
def test_generate_weighted_returns(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        "returns": pd.DataFrame(
            [
                [np.nan, np.nan, np.nan],
                [1.59904743, 1.66397210, 1.67345829],
                [-0.37065629, -0.36541822, -0.36015840],
                [-0.41055669, 0.60004777, 0.00536958],
            ],
            dates,
            tickers,
        ),
        "weights": pd.DataFrame(
            [
                [0.03777059, 0.04733924, 0.05197790],
                [0.82074874, 0.48533938, 0.75792752],
                [0.10196420, 0.05866016, 0.09578226],
                [0.03951647, 0.40866122, 0.09431233],
            ],
            dates,
            tickers,
        ),
    }
    fn_correct_outputs = OrderedDict(
        [
            (
                "weighted_returns",
                pd.DataFrame(
                    [
                        [np.nan, np.nan, np.nan],
                        [1.31241616, 0.80759119, 1.26836009],
                        [-0.03779367, -0.02143549, -0.03449679],
                        [-0.01622375, 0.24521625, 0.00050642],
                    ],
                    dates,
                    tickers,
                ),
            )
        ]
    )

    assert_output(fn, fn_inputs, fn_correct_outputs)
Esempio n. 35
0
def test_generate_dollar_volume_weights(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        "close": pd.DataFrame(
            [
                [35.4411, 34.1799, 34.0223],
                [92.1131, 91.0543, 90.9572],
                [57.9708, 57.7814, 58.1982],
                [34.1705, 92.453, 58.5107],
            ],
            dates,
            tickers,
        ),
        "volume": pd.DataFrame(
            [
                [9.83683e06, 1.78072e07, 8.82982e06],
                [8.22427e07, 6.85315e07, 4.81601e07],
                [1.62348e07, 1.30527e07, 9.51201e06],
                [1.06742e07, 5.68313e07, 9.31601e06],
            ],
            dates,
            tickers,
        ),
    }
    fn_correct_outputs = OrderedDict(
        [
            (
                "dollar_volume_weights",
                pd.DataFrame(
                    [
                        [0.27719777, 0.48394253, 0.23885970],
                        [0.41632975, 0.34293308, 0.24073717],
                        [0.41848548, 0.33536102, 0.24615350],
                        [0.05917255, 0.85239760, 0.08842984],
                    ],
                    dates,
                    tickers,
                ),
            )
        ]
    )

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_calculate_returns(fn):
    tickers = generate_random_tickers(5)
    dates = generate_random_dates(6)

    fn_inputs = {
        'close':
        pd.DataFrame(
            [[
                21.050810483942833, 17.013843810658827, 10.984503755486879,
                11.248093428369392, 12.961712733997235
            ],
             [
                 15.63570258751384, 14.69054309070934, 11.353027688995159,
                 475.74195118202061, 11.959640427803022
             ],
             [
                 482.34539247360806, 35.202580592515041, 3516.5416782257166,
                 66.405314327318209, 13.503960481087077
             ],
             [
                 10.918933017418304, 17.9086438675435, 24.801265417692324,
                 12.488954191854916, 10.52435923388642
             ],
             [
                 10.675971965144655, 12.749401436636365, 11.805257579935713,
                 21.539039489843024, 19.99766036804861
             ],
             [
                 11.545495378369814, 23.981468434099405, 24.974763062186504,
                 36.031962102997689, 14.304332320024963
             ]], dates, tickers)
    }
    fn_correct_outputs = OrderedDict([
        ('returns',
         pd.DataFrame([
             [np.nan, np.nan, np.nan, np.nan, np.nan],
             [-0.25723988, -0.13655355, 0.03354944, 41.29534136, -0.07731018],
             [29.84897463, 1.39627496, 308.74483411, -0.86041737, 0.12912763],
             [-0.97736283, -0.49126900, -0.99294726, -0.81192839, -0.22064647],
             [-0.02225135, -0.28808672, -0.52400584, 0.72464717, 0.90013092],
             [0.08144677, 0.88098779, 1.11556274, 0.67286764, -0.28469971]
         ], dates, tickers))
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_sharpe_ratio(fn):
    dates = generate_random_dates(4)
    factor_names = ['Factor {}'.format(i) for i in range(3)]

    fn_inputs = {
        'factor_returns': pd.DataFrame(
            [
                [ 0.00069242,  0.00072387,  0.00002972],
                [-0.00028692,  0.00086883, -0.00007235],
                [-0.00066376, -0.00045934,  0.00007348],
                [ 0.00085234,  0.00093345,  0.00008734]],
            dates, factor_names),
        'annualization_factor': 16.0}
    fn_correct_outputs = OrderedDict([
        (
            'sharpe_ratio', pd.Series([3.21339895, 12.59157330, 6.54485802], factor_names))])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_factor_cov_matrix(fn):
    dates = generate_random_dates(4)

    fn_inputs = {
        'factor_returns': pd.DataFrame([
                [-0.49503261,  1.45332369, -0.08980631],
                [-1.87563271,  0.67894147, -1.11984992],
                [-0.13027172, -0.49001128,  1.67259298],
                [-0.25392567,  0.47320133,  0.04528734]],
                dates),
        'ann_factor': 250}
    fn_correct_outputs = OrderedDict([
        (
            'factor_cov_matrix', np.array([
                [162.26559808, 0.0, 0.0],
                [0.0, 159.86284454, 0.0],
                [0.0, 0.0, 333.09785876]]))])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_get_high_lows_lookback(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        'high': pd.DataFrame(
            [
                [35.4411, 34.1799, 34.0223],
                [92.1131, 91.0543, 90.9572],
                [57.9708, 57.7814, 58.1982],
                [34.1705, 92.453, 58.5107]],
            dates, tickers),
        'low': pd.DataFrame(
            [
                [15.6718, 75.1392, 34.0527],
                [27.1834, 12.3453, 95.9373],
                [28.2503, 24.2854, 23.2932],
                [86.3725, 32.223, 38.4107]],
            dates, tickers),
        'lookback_days': 2}
    fn_correct_outputs = OrderedDict([
        (
            'lookback_high',
            pd.DataFrame(
                [
                    [np.nan, np.nan, np.nan],
                    [np.nan, np.nan, np.nan],
                    [92.11310000, 91.05430000, 90.95720000],
                    [92.11310000, 91.05430000, 90.95720000]],
                dates, tickers)),
        (
            'lookback_low',
            pd.DataFrame(
                [
                    [np.nan, np.nan, np.nan],
                    [np.nan, np.nan, np.nan],
                    [15.67180000, 12.34530000, 34.05270000],
                    [27.18340000, 12.34530000, 23.29320000]],
                dates, tickers))
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_calculate_cumulative_returns(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        'returns': pd.DataFrame(
            [
                [np.nan, np.nan, np.nan],
                [1.59904743, 1.66397210, 1.67345829],
                [-0.37065629, -0.36541822, -0.36015840],
                [-0.41055669, 0.60004777, 0.00536958]],
            dates, tickers)}
    fn_correct_outputs = OrderedDict([
        (
            'cumulative_returns',
            pd.Series(
                [np.nan, 5.93647782, -0.57128454, -0.68260542],
                dates))])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_get_covariance_returns(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        'returns': pd.DataFrame(
            [
                [np.nan, np.nan, np.nan],
                [1.59904743, 1.66397210, 1.67345829],
                [-0.37065629, -0.36541822, -0.36015840],
                [-0.41055669, 0.60004777, 0.00536958]],
            dates, tickers)}
    fn_correct_outputs = OrderedDict([(
        'returns_covariance',
        np.array(
            [
                [0.89856076, 0.7205586, 0.8458721],
                [0.7205586, 0.78707297, 0.76450378],
                [0.8458721, 0.76450378, 0.83182775]]))])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_calculate_arithmetic_rate_of_return(fn):
    tickers = generate_random_tickers(5)
    dates = generate_random_dates(6)

    fn_inputs = {
        'close':
        pd.DataFrame(
            [[
                21.050810483942833, 17.013843810658827, 10.984503755486879,
                11.248093428369392, 12.961712733997235
            ],
             [
                 15.63570258751384, 14.69054309070934, 11.353027688995159,
                 475.74195118202061, 11.959640427803022
             ],
             [
                 482.34539247360806, 35.202580592515041, 3516.5416782257166,
                 66.405314327318209, 13.503960481087077
             ],
             [
                 10.918933017418304, 17.9086438675435, 24.801265417692324,
                 12.488954191854916, 10.52435923388642
             ],
             [
                 10.675971965144655, 12.749401436636365, 11.805257579935713,
                 21.539039489843024, 19.99766036804861
             ],
             [
                 11.545495378369814, 23.981468434099405, 24.974763062186504,
                 36.031962102997689, 14.304332320024963
             ]], dates, tickers)
    }
    fn_correct_outputs = OrderedDict([
        ('arithmetic_returns',
         pd.Series(
             [4.77892789, 0.22689225, 51.39616553, 6.83675173, 0.07443370],
             tickers))
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
Esempio n. 43
0
def test_filter_signals(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(10)

    fn_inputs = {
        'signal':
        pd.DataFrame(
            [[0, 0, 0], [-1, -1, -1], [1, 0, -1], [0, 0, 0], [1, 0, 0],
             [0, 1, 0], [0, 0, 1], [0, -1, 1], [-1, 0, 0], [0, 0, 0]], dates,
            tickers),
        'lookahead_days':
        3
    }
    fn_correct_outputs = OrderedDict([
        ('filtered_signal',
         pd.DataFrame(
             [[0, 0, 0], [-1, -1, -1], [1, 0, 0], [0, 0, 0], [0, 0, 0],
              [0, 1, 0], [0, 0, 1], [0, -1, 0], [-1, 0, 0], [0, 0, 0]], dates,
             tickers))
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
Esempio n. 44
0
def test_get_lookahead_prices(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(5)

    fn_inputs = {
        'close':
        pd.DataFrame([[25.6788, 35.1392, 34.0527], [25.1884, 14.3453, 39.9373],
                      [62.3457, 92.2524, 65.7893], [78.2803, 34.3854, 23.2932],
                      [88.8725, 52.223, 34.4107]], dates, tickers),
        'lookahead_days':
        2
    }
    fn_correct_outputs = OrderedDict([
        ('lookahead_prices',
         pd.DataFrame([[62.34570000, 92.25240000, 65.78930000],
                       [78.28030000, 34.38540000, 23.29320000],
                       [88.87250000, 52.22300000, 34.41070000],
                       [np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan]],
                      dates, tickers))
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_idiosyncratic_var_vector(fn):
    dates = generate_random_dates(4)
    assets = get_assets(3)

    fn_inputs = {
        'returns': pd.DataFrame(
            [
                [ 0.02769242,  1.34872387,  0.23460972],
                [-0.94728692,  0.68386883, -1.23987235],
                [ 1.93769376, -0.48275934,  0.34957348],
                [ 0.23985234,  0.35897345,  0.34598734]],
            dates, assets),
        'idiosyncratic_var_matrix': pd.DataFrame([
                [0.02272535,  0.0, 0.0],
                [0.0,  0.05190083, 0.0],
                [0.0, -0.49001128,  0.05431181]],
            assets, assets),}
    fn_correct_outputs = OrderedDict([
        (
            'idiosyncratic_var_vector', pd.DataFrame([0.02272535, 0.05190083, 0.05431181], assets))])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_get_long_short(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        'close': pd.DataFrame(
            [
                [25.6788, 35.1392, 34.0527],
                [25.1884, 14.3453, 39.9373],
                [78.2803, 34.3854, 23.2932],
                [88.8725, 52.223, 34.4107]],
            dates, tickers),
        'lookback_high': pd.DataFrame(
            [
                [np.nan, np.nan, np.nan],
                [92.11310000, 91.05430000, 90.95720000],
                [35.4411, 34.1799, 34.0223],
                [92.11310000, 91.05430000, 90.95720000]],
            dates, tickers),
        'lookback_low': pd.DataFrame(
            [
                [np.nan, np.nan, np.nan],
                [34.1705, 92.453, 58.5107],
                [15.67180000, 12.34530000, 34.05270000],
                [27.18340000, 12.34530000, 23.29320000]],
            dates, tickers)}
    fn_correct_outputs = OrderedDict([
        (
            'long_short',
            pd.DataFrame(
                [
                    [0, 0, 0],
                    [-1, -1, -1],
                    [1, 1, -1],
                    [0, 0, 0]],
                dates, tickers))])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_factor_returns(fn):
    n_components = 3
    dates = generate_random_dates(4)
    assets = get_assets(3)

    pca = PCA(n_components)
    pca.fit(pd.DataFrame(
        [
            [0.21487253,  0.12342312, -0.13245215],
            [0.23423439, -0.23434532,  1.67834324],
            [0.23432445, -0.23563226,  0.23423523],
            [0.24824535, -0.23523435,  0.36235236]],
        dates, assets))

    fn_inputs = {
        'pca': pca,
        'returns': pd.DataFrame(
            [
                [0.02769242,  1.34872387,  0.23460972],
                [-0.94728692, 0.68386883, -1.23987235],
                [1.93769376, -0.48275934,  0.34957348],
                [0.23985234,  0.35897345,  0.34598734]],
            dates, assets),
        'factor_return_indices': np.array(dates),
        'factor_return_columns': np.arange(n_components)}
    fn_correct_outputs = OrderedDict([
        (
            'factor_returns', pd.DataFrame(
                [
                    [-0.49503261,  1.45332369, -0.08980631],
                    [-1.87563271,  0.67894147, -1.11984992],
                    [-0.13027172, -0.49001128,  1.67259298],
                    [-0.25392567,  0.47320133,  0.04528734]],
                fn_inputs['factor_return_indices'],
                fn_inputs['factor_return_columns']))])

    assert_output(fn, fn_inputs, fn_correct_outputs, check_parameter_changes=False)
def test_filter_signals(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(10)

    fn_inputs = {
        'signal': pd.DataFrame(
            [
                [0, 0, 0],
                [-1, -1, -1],
                [1, 0, -1],
                [0, 0, 0],
                [1, 0, 0],
                [0, 1, 0],
                [0, 0, 1],
                [0, -1, 1],
                [-1, 0, 0],
                [0, 0, 0]],
            dates, tickers),
        'lookahead_days': 3}
    fn_correct_outputs = OrderedDict([
        (
            'filtered_signal',
            pd.DataFrame(
                [
                    [0, 0, 0],
                    [-1, -1, -1],
                    [1, 0, 0],
                    [0, 0, 0],
                    [0, 0, 0],
                    [0, 1, 0],
                    [0, 0, 1],
                    [0, -1, 0],
                    [-1, 0, 0],
                    [0, 0, 0]],
                dates, tickers))])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_calculate_dividend_weights(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        'dividends': pd.DataFrame(
            [
                [0.0, 0.0, 0.0],
                [0.0, 0.0, 0.1],
                [0.0, 1.0, 0.3],
                [0.0, 0.2, 0.0]],
            dates, tickers)}
    fn_correct_outputs = OrderedDict([
        (
            'dividend_weights',
            pd.DataFrame(
                [
                    [np.nan, np.nan, np.nan],
                    [0.00000000, 0.00000000, 1.00000000],
                    [0.00000000, 0.71428571, 0.28571429],
                    [0.00000000, 0.75000000, 0.25000000]],
                dates, tickers))])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_generate_returns(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        'prices': pd.DataFrame(
            [
                [35.4411, 34.1799, 34.0223],
                [92.1131, 91.0543, 90.9572],
                [57.9708, 57.7814, 58.1982],
                [34.1705, 92.453, 58.5107]],
            dates, tickers)}
    fn_correct_outputs = OrderedDict([
        (
            'returns',
            pd.DataFrame(
                [
                    [np.nan, np.nan, np.nan],
                    [1.59904743, 1.66397210, 1.67345829],
                    [-0.37065629, -0.36541822, -0.36015840],
                    [-0.41055669, 0.60004777, 0.00536958]],
                dates, tickers))])

    assert_output(fn, fn_inputs, fn_correct_outputs)