def __init__( self, features_and_labels: FeaturesAndLabels, symbols: Union[str, List[str]], strategy: Strategy, pct_train_data: float = 0.8, max_steps: int = None, min_training_samples: float = np.inf, use_cache: Cache = NoCache(), ): super().__init__(strategy) self.max_steps = math.inf if max_steps is None else max_steps self.min_training_samples = min_training_samples self.features_and_labels = features_and_labels self.pct_train_data = pct_train_data # define spaces self.observation_space = None # define execution mode self.cache = use_cache self.mode = 'train' self.done = True # load symbols available to randomly draw from if isinstance(symbols, str): with open(symbols) as f: self.symbols = np.array(re.split("[\r\n]|[\n]|[\r]", f.read())) else: self.symbols = symbols # finally make a dummy initialisation self._init()
def test__ewma_markowitz(self): """given""" df = DF_TEST_MULTI """when""" portfolios = ta_markowitz(df, return_period=20) """then""" print(portfolios) np.testing.assert_array_almost_equal(np.array([0.683908, 3.160920e-01]), portfolios.iloc[-4].values, 0.00001)
def test__differentiable_argmax(self): """given""" args = 10 argmax = DifferentiableArgmax(args) """when""" res = np.array( [argmax(t.tensor(one_hot(i, args))).numpy() for i in range(args)]) """then""" print(res) np.testing.assert_array_almost_equal(res, np.arange(0, args))
def test_youngest_portion(self): """given""" df = pd.DataFrame({ "featureA": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "labelA": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] }) """when""" train_ix, test_ix = random_splitter(test_size=0.6, youngest_size=0.25)(df.index) "then" self.assertEqual(6, len(test_ix)) np.testing.assert_array_equal(test_ix[-2:], np.array([8, 9]))
def test__markowitz_strategy(self): """given""" df = DF_TEST_MULTI df_price = df._['Close'] df_expected_returns = df_price._["Close"].ta.macd()._["histogram"] df_trigger = ta_zscore(df_price['Close']).abs() > 2.0 df_data = inner_join(df_price, df_expected_returns) df_data = inner_join(df_data, df_trigger, prefix='trigger') """when""" portfolios = ta_markowitz(df_data, prices='Close', expected_returns='histogram', rebalance_trigger='trigger') """then""" print(portfolios) np.testing.assert_array_almost_equal(np.array([1.000000, 3.904113e-07]), portfolios.iloc[-1].values, 0.00001)
def test_classifier(self): """given some toy classification data""" df = pd.DataFrame({ "a": [ 1, 0, 1, 0, 1, 0, 1, 0, ], "b": [ 0, 0, 1, 1, 0, 0, 1, 1, ], "c": [ 1, 0, 0, 1, 1, 0, 0, 1, ] }) """and a model""" model = self.provide_classification_model( FeaturesAndLabels(features=["a", "b"], labels=["c"], label_type=int)) """when we fit the model""" fit = df.model.fit(model, NaiveSplitter(0.49), verbose=0, epochs=1500) print(fit.training_summary.df) prediction = df.model.predict(fit.model) binary_prediction = prediction.iloc[:, 0] >= 0.5 np.testing.assert_array_equal( binary_prediction, np.array([ True, False, False, True, True, False, False, True, ])) """and save and load the model""" temp = os.path.join(tempfile.gettempdir(), str(uuid.uuid4())) try: fit.model.save(temp) copy = Model.load(temp) pd.testing.assert_frame_equal(df.model.predict(fit.model), df.model.predict(copy), check_less_precise=True) finally: os.remove(temp)
def test_classifier(self): """given some toy classification data""" df = pd.DataFrame({ "a": [ 1, 0, 1, 0, 1, 0, 1, 0, ], "b": [ 0, 0, 1, 1, 0, 0, 1, 1, ], "c": [ 1, 0, 0, 1, 1, 0, 0, 1, ] }) """and a model""" model = self.provide_classification_model( FeaturesAndLabels(features=["a", "b"], labels=["c"], label_type=int)) temp = os.path.join(tempfile.gettempdir(), str(uuid.uuid4())) """when we fit the model""" batch_size, epochs = self.provide_batch_size_and_epoch() with df.model(temp) as m: fit = m.fit(model, FittingParameter(splitter=naive_splitter(0.49), batch_size=batch_size, epochs=epochs), verbose=0) print(fit.training_summary.df) # fit.training_summary.df.to_pickle('/tmp/classifier.df') # print(fit._repr_html_()) """then we get a html summary and can predict""" self.assertIn('<style>', fit.training_summary._repr_html_()) prediction = df.model.predict(fit.model) binary_prediction = prediction.iloc[:, 0] >= 0.5 np.testing.assert_array_equal( binary_prediction, np.array([ True, False, False, True, True, False, False, True, ])) """and load the model""" try: copy = Model.load(temp) pd.testing.assert_frame_equal(df.model.predict(fit.model), df.model.predict(copy), check_less_precise=True) # test using context manager and ForecastProvider pd.testing.assert_frame_equal( df.model(temp).predict(forecast_provider=Forecast).df, df.model.predict(copy), check_less_precise=True) finally: os.remove(temp)