def test_purgedkfold_02_exception(self): """ Test exception is raised when passing in a dataset with a different length than the samples_info_sets used in the constructor. """ info_sets = pd.Series( index=pd.date_range(start='2019-01-01 00:00:00', periods=10, freq='T'), data=pd.date_range(start='2019-01-01 00:02:00', periods=10, freq='T'), ) self.log(f"info_sets=\n{info_sets}") dataset = pd.DataFrame( index=pd.date_range(start='2019-01-01 00:00:00', periods=11, freq='T'), data={'feat': np.arange(0, 11)}, # One entry more than info_sets ) self.log(f"dataset=\n{dataset}") pkf = PurgedKFold(n_splits=3, samples_info_sets=info_sets, pct_embargo=0.) try: for _, _ in pkf.split(dataset): pass except ValueError: pass else: self.fail("ValueError not raised")
def test_purgedkfold_04_embargo(self): """ Test PurgedKFold class using the 'embargo' parameter set to pct_points_test which means pct_points_test percent which also means pct_points_test entries from a total of 100 in total in the dataset. """ info_sets = pd.Series( index=pd.date_range(start='2019-01-01 00:00:00', periods=100, freq='T'), data=pd.date_range(start='2019-01-01 00:02:00', periods=100, freq='T'), ) dataset = pd.DataFrame( index=info_sets.index, data={'feat': np.arange(0, 100)}, ) pct_points_test: int = 2 self.log(f"pct_points_test= {pct_points_test}") pkf = PurgedKFold(n_splits=3, samples_info_sets=info_sets, pct_embargo=0.01 * pct_points_test) # Also test that X can be an np.ndarray by passing in the .values of the pd.DataFrame for train_indices, test_indices in pkf.split(dataset): train_times_ret = info_sets.iloc[train_indices] self.log(f"train_times_ret=\n{train_times_ret}") test_times_ret = info_sets.iloc[test_indices] self.log(f"test_times_ret=\n{test_times_ret}") test_times_gtt = pd.Series( index=[info_sets[test_indices[0]]], data=[info_sets[test_indices[-1]]], ) self.log(f"test_times_gtt=\n{test_times_gtt}") train_times_gtt = ml_get_train_times(info_sets, test_times_gtt) # if test set is at the beginning, drop pct_points_test records from beginning of train dataset if test_times_ret.index[0] == dataset.index[0]: train_times_gtt = train_times_gtt.iloc[pct_points_test:] # if test set is in the middle drop pct_points_test records from the end of test set index elif test_times_ret.index[-1] != dataset.index[-1]: last_test_ix = test_times_ret.index.get_loc( test_times_ret.index[-1]) to_drop: pd.DatetimeIndex = train_times_gtt.iloc[ last_test_ix + 2:last_test_ix + 2 + pct_points_test].index train_times_gtt.drop(to_drop.to_list(), inplace=True) self.log(f"train_times_gtt=\n{train_times_gtt}") self.log("-" * 100) self.assertTrue(train_times_ret.equals(train_times_gtt), "dataset don't match")
def test_purgedkfold_03_simple(self): """ Test PurgedKFold class using the ml_get_train_times method. Get the test range from PurgedKFold and then make sure the train range is exactly the same using the two methods. This is the test with no embargo. """ info_sets = pd.Series( index=pd.date_range(start='2019-01-01 00:00:00', periods=20, freq='T'), data=pd.date_range(start='2019-01-01 00:02:00', periods=20, freq='T'), ) self.log(f"info_sets=\n{info_sets}") dataset = pd.DataFrame( index=info_sets.index, data={'feat': np.arange(0, 20)}, ) self.log(f"dataset=\n{dataset}") pkf = PurgedKFold(n_splits=3, samples_info_sets=info_sets, pct_embargo=0.) for train_indices, test_indices in pkf.split(dataset): self.log(f"test_times_ret=\n{info_sets[test_indices]}") train_times_ret = info_sets.iloc[train_indices] self.log(f"train_times_ret=\n{train_times_ret}") test_times_gtt = pd.Series( index=[info_sets[test_indices[0]]], data=[info_sets[test_indices[-1]]], ) self.log(f"test_times_gtt=\n{test_times_gtt}") train_times_gtt = ml_get_train_times(info_sets, test_times_gtt) self.log(f"train_times_gtt=\n{train_times_gtt}") self.log("-" * 100) self.assertTrue(train_times_ret.equals(train_times_gtt), "dataset don't match")