Exemplo n.º 1
0
    def test_ta_opening_gap(self):
        df = pd.DataFrame({"Open": [0, 2, 2, 2],
                           "Close":   [1, 2, 3, 0]})

        label = ta_opening_gap(df)

        self.assertListEqual([2., 0., 1.], label.dropna().tolist())
    def test_one_hot_encoder_vec(self):
        df = pd.DataFrame({"a": [1, 2, 3, 4, 4, 5]})

        encoded = ta_one_hot_encode_discrete(df["a"])

        print(repr(encoded))
        self.assertListEqual(df.index.tolist(), encoded.index.tolist())
        np.testing.assert_array_equal(
            np.array([[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0],
                      [0, 0, 0, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]]),
            encoded._.values)
Exemplo n.º 3
0
    def test_normalize_row_multi_column(self):
        df = ta_rnn(pd.DataFrame({"a": np.arange(10), "b": np.arange(10)}), 5)

        mm01 = ta_normalize_row(df, 'minmax01', level=1)
        mm11 = ta_normalize_row(df, 'minmax-11', level=1)
        std = ta_normalize_row(df, 'standard', level=1)
        uni = ta_normalize_row(df, 'uniform', level=1)

        np.testing.assert_array_equal(mm01.xs("a", level=1, axis=1), mm01.xs("b", level=1, axis=1))
        np.testing.assert_array_equal(mm11.xs("a", level=1, axis=1), mm11.xs("b", level=1, axis=1))
        np.testing.assert_array_almost_equal(std.xs("a", level=1, axis=1), std.xs("b", level=1, axis=1))
        np.testing.assert_array_equal(uni.xs("a", level=1, axis=1), uni.xs("b", level=1, axis=1))
Exemplo n.º 4
0
    def test_rescale(self):
        df = pd.DataFrame({"a": [1, 2, 3, 4, 4, 5], "b": [5, 4, 3, 2, 2, 2]})

        series = ta_rescale(df["a"])
        all = ta_rescale(df)
        columns = ta_rescale(df, axis=0)
        rows = ta_rescale(df, axis=1)

        self.assertEqual(1, series.values[-1])
        self.assertListEqual([1, -0.5], all[-1:].values[-1].tolist())
        self.assertListEqual([1, -1], columns[-1:].values[-1].tolist())
        self.assertListEqual([1, -1], rows[-1:].values[-1].tolist())
Exemplo n.º 5
0
    def __test__LinearRegressionLayer(self):
        """given"""
        df = DF_TEST.copy()
        model = Sequential()
        model.add(LinearRegressionLayer())
        model.compile(loss='mse', optimizer='nadam')

        x = df["Close"].values.reshape(1, -1)

        """when"""
        model.fit(x, x, epochs=500, verbose=0)

        "then"
        res = pd.DataFrame({"close": df["Close"], "reg": model.predict_on_batch(x)}, index=df.index)
        print(res.head())
Exemplo n.º 6
0
    def test_lag_smoothing_nan(self):
        """given"""
        df = pd.DataFrame({"featureA": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]})
        #                               1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  # original
        #                                  1, 2, 3, 4, 5, 6, 7, 8, 9]   # lag 1
        #                                        1, 2, 3, 4, 5, 6, 7]   # lag 1 + shift 2
        #                                        ^                      # this is where the df starts
        """when lag smoothing is enabled using shift (which is introducing nan into the data frame)"""
        rnn, min_needed_data = ta_rnn(
            df[["featureA"]],
            feature_lags=[0, 1],
            lag_smoothing={1: lambda df: df["featureA"].shift(2)},
            return_min_required_samples=True)

        len_features = 10 - 1 - 2
        """then"""
        self.assertEqual(len(rnn), len_features)
        self.assertAlmostEqual(rnn[0, "featureA"].iloc[0], 4)
        self.assertAlmostEqual(rnn[1, "featureA"].iloc[0], 1.0)
        self.assertAlmostEqual(rnn[0, "featureA"].iloc[-1], 10)
        self.assertAlmostEqual(rnn[1, "featureA"].iloc[-1], 7.0)
Exemplo n.º 7
0
    def test__LPPLLayer(self):
        """given"""
        df = DF_TEST.copy()
        model = Sequential([LPPLLayer()])
        model.compile(loss='mse', optimizer=SGD(0.2, 0.01))
        #model.compile(loss='mse', optimizer='adam')

        x = np.log(df["Close"].values)
        x = ReScaler((x.min(), x.max()), (1, 2))(x)
        x = x.reshape(1, -1)

        x2 = np.vstack([x, x])

        """when"""
        model.fit(x2, x2, epochs=5000, verbose=0, callbacks=[EarlyStopping('loss')])

        """then"""
        print(model.predict_on_batch(x))
        res = pd.DataFrame({"close": x[0], "lppl": model.predict_on_batch(x)}, index=df.index)
        res.to_csv('/tmp/lppl.csv')
        print(res.head())
        print(model.layers[0].get_weights())
Exemplo n.º 8
0
DF_TEST_MULTI = pd.read_pickle(
    os.path.join(os.path.dirname(os.path.abspath(__file__)), ".data",
                 "spy_gld.pickle"))
DF_TEST = pd.read_csv(os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                   ".data", "SPY.csv"),
                      index_col='Date',
                      parse_dates=True)
DF_TEST2 = pd.read_csv(os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                    ".data", "GLD.csv"),
                       index_col='Date',
                       parse_dates=True)
DF_TEST_MULTI_ROW = pd.concat([
    DF_TEST.add_multi_index("SPY", axis=0),
    DF_TEST2.add_multi_index("GLD", axis=0)
],
                              axis=0)
DF_TEST_MULTI_ROW_MULTI_COLUMN = pd.concat([
    DF_TEST_MULTI.add_multi_index("A", axis=0),
    DF_TEST_MULTI.add_multi_index("B", axis=0)
],
                                           axis=0)
DF_DEBUG = pd.DataFrame({"Close": np.random.random(10)})

DF_INVERSE_GAF = pd.read_pickle(
    os.path.join(os.path.dirname(os.path.abspath(__file__)), ".data",
                 "inverse_gaf.df"))
DF_TEST_MULTI_CLASS = pd.read_pickle(
    os.path.join(os.path.dirname(os.path.abspath(__file__)), ".data",
                 "one_hot_classified_df.pickle"))