示例#1
0
def get_trades_based_amihud_lambda(log_ret: list,
                                   dollar_volume: list) -> List[float]:
    """
    Get Amihud lambda from trades data, p.288-289.

    :param log_ret: (list) of log returns
    :param dollar_volume: (list) of dollar volumes (price * size)
    :return: (float) Amihud lambda for a bar
    """
    X = np.array(dollar_volume).reshape(-1, 1)
    y = np.abs(np.array(log_ret))
    coef, std = _get_betas(X, y)
    t_value = coef[0] / std[0]
    return [coef[0], t_value[0]]
示例#2
0
def get_trades_based_hasbrouck_lambda(log_ret: list, dollar_volume: list,
                                      aggressor_flags: list) -> List[float]:
    """
    Get Hasbrouck lambda from trades data, p.289-290.

    :param log_ret: (list) of log returns
    :param dollar_volume: (list) of dollar volumes (price * size)
    :param aggressor_flags: (list) of trade directions [-1, 1]  (tick rule or aggressor side can be used to define)
    :return: (list) Hasbrouck lambda for a bar and t value
    """
    X = (np.sqrt(np.array(dollar_volume)) * np.array(aggressor_flags)).reshape(
        -1, 1)
    y = np.abs(np.array(log_ret))
    coef, std = _get_betas(X, y)
    t_value = coef[0] / std[0]
    return [coef[0], t_value[0]]
示例#3
0
def get_trades_based_kyle_lambda(price_diff: list, volume: list,
                                 aggressor_flags: list) -> List[float]:
    """
    Get Kyle lambda from trades data, p.286-288.

    :param price_diff: (list) of price diffs
    :param volume: (list) of trades sizes
    :param aggressor_flags: (list) of trade directions [-1, 1]  (tick rule or aggressor side can be used to define)
    :return: (list) Kyle lambda for a bar and t-value
    """
    signed_volume = np.array(volume) * np.array(aggressor_flags)
    X = np.array(signed_volume).reshape(-1, 1)
    y = np.array(price_diff)
    coef, std = _get_betas(X, y)
    t_value = coef[0] / std[0]
    return [coef[0], t_value[0]]
示例#4
0
def _get_dfc_for_t(series: pd.Series, molecule: list) -> pd.Series:
    """
    Get Chow-Type Dickey-Fuller Test statistics for each index in molecule

    :param series: (pd.Series) series to test
    :param molecule: (list) of dates to test
    :return: (pd.Series) fo statistics for each index from molecule
    """

    dfc_series = pd.Series(index=molecule)

    for index in molecule:
        series_diff = series.diff().dropna()
        series_lag = series.shift(1).dropna()
        series_lag[:index] = 0  # D_t* indicator: before t* D_t* = 0

        y = series_diff.loc[series_lag.index].values
        x = series_lag.values
        coefs, coef_vars = _get_betas(x.reshape(-1, 1), y)
        b_estimate, b_var = coefs[0], coef_vars[0][0]
        dfc_series[index] = b_estimate / (b_var**0.5)
    return dfc_series
    def test_sadf_test(self):
        """
        Test get_sadf function
        """

        log_prices = np.log(self.data.close)
        lags_int = 5
        lags_array = [1, 2, 5, 7]
        min_length = 20

        sm_power_sadf = get_sadf(log_prices,
                                 model='sm_power',
                                 add_const=True,
                                 min_length=min_length,
                                 lags=lags_int)

        linear_sadf = get_sadf(log_prices,
                               model='linear',
                               add_const=True,
                               min_length=min_length,
                               lags=lags_int)
        linear_sadf_no_const_lags_arr = get_sadf(log_prices,
                                                 model='linear',
                                                 add_const=False,
                                                 min_length=min_length,
                                                 lags=lags_array)

        quandratic_sadf = get_sadf(log_prices,
                                   model='quadratic',
                                   add_const=True,
                                   min_length=min_length,
                                   lags=lags_int)
        sm_poly_1_sadf = get_sadf(log_prices,
                                  model='sm_poly_1',
                                  add_const=True,
                                  min_length=min_length,
                                  lags=lags_int)
        sm_poly_2_sadf = get_sadf(log_prices,
                                  model='sm_poly_2',
                                  add_const=True,
                                  min_length=min_length,
                                  lags=lags_int)
        sm_exp_sadf = get_sadf(log_prices,
                               model='sm_exp',
                               add_const=True,
                               min_length=min_length,
                               lags=lags_int)

        self.assertEqual(log_prices.shape[0] - min_length - lags_int - 1,
                         sm_power_sadf.shape[0])  # -1 for series_diff
        self.assertEqual(log_prices.shape[0] - min_length - lags_int - 1,
                         linear_sadf.shape[0])
        self.assertEqual(log_prices.shape[0] - min_length - lags_int - 1,
                         quandratic_sadf.shape[0])
        self.assertEqual(log_prices.shape[0] - min_length - lags_int - 1,
                         sm_poly_1_sadf.shape[0])
        self.assertEqual(log_prices.shape[0] - min_length - lags_int - 1,
                         sm_poly_2_sadf.shape[0])
        self.assertEqual(log_prices.shape[0] - min_length - lags_int - 1,
                         sm_exp_sadf.shape[0])

        self.assertAlmostEqual(sm_power_sadf.mean(), 17.814, delta=1e-3)
        self.assertAlmostEqual(sm_power_sadf.iloc[29], -4.281, delta=1e-3)

        self.assertAlmostEqual(linear_sadf.mean(), -0.669, delta=1e-3)
        self.assertAlmostEqual(linear_sadf[29], -0.717, delta=1e-3)

        self.assertAlmostEqual(linear_sadf_no_const_lags_arr.mean(),
                               1.899,
                               delta=1e-3)
        self.assertAlmostEqual(linear_sadf_no_const_lags_arr[29],
                               1.252,
                               delta=1e-3)

        self.assertAlmostEqual(quandratic_sadf.mean(), -0.651, delta=1e-3)
        self.assertAlmostEqual(quandratic_sadf[29], -1.065, delta=1e-3)

        self.assertAlmostEqual(sm_poly_1_sadf.mean(), 21.020, delta=1e-3)
        self.assertAlmostEqual(sm_poly_1_sadf[29], 0.8268, delta=1e-3)

        self.assertAlmostEqual(sm_poly_2_sadf.mean(), 21.01, delta=1e-3)
        self.assertAlmostEqual(sm_poly_2_sadf[29], 0.822, delta=1e-3)

        self.assertAlmostEqual(sm_exp_sadf.mean(), 17.632, delta=1e-3)
        self.assertAlmostEqual(sm_exp_sadf[29], -5.821, delta=1e-3)

        # Trivial series case.
        ones_series = pd.Series(index=log_prices.index,
                                data=np.ones(shape=log_prices.shape[0]))
        trivial_sadf = get_sadf(ones_series,
                                model='sm_power',
                                add_const=True,
                                min_length=min_length,
                                lags=lags_int)
        self.assertTrue((trivial_sadf.unique() == [
            -np.inf
        ]).all())  # All values should be -np.inf

        # Test rubbish model argument.
        self.assertRaises(ValueError,
                          get_sadf,
                          series=log_prices,
                          model='rubbish_string',
                          add_const=True,
                          min_length=min_length,
                          lags=lags_int)

        # Assert that nans are parsed if singular matrix
        singular_matrix = np.array([[1, 0, 0], [-1, 3, 3], [1, 2, 2]])
        b_mean, b_var = _get_betas(singular_matrix, singular_matrix)
        self.assertTrue(b_mean, [np.nan])
        self.assertTrue(b_var, [[np.nan, np.nan]])