2022-02-17 16:36:35 +01:00
|
|
|
from ..types import EventLabeller, EventsDataFrame, ReturnSeries, ForwardReturnSeries
|
2022-01-26 23:22:43 +01:00
|
|
|
import pandas as pd
|
2022-02-17 16:36:35 +01:00
|
|
|
from .utils import create_forward_returns
|
2022-01-26 23:22:43 +01:00
|
|
|
|
|
|
|
|
class FixedTimeHorionTwoClassEventLabeller(EventLabeller):
|
|
|
|
|
|
|
|
|
|
time_horizon: int
|
|
|
|
|
|
2022-02-17 16:36:35 +01:00
|
|
|
def __init__(self, time_horizon: int):
|
2022-01-26 23:22:43 +01:00
|
|
|
self.time_horizon = time_horizon
|
|
|
|
|
|
2022-02-17 16:36:35 +01:00
|
|
|
def label_events(self, event_start_times: pd.DatetimeIndex, returns: ReturnSeries) -> tuple[EventsDataFrame, ForwardReturnSeries]:
|
2022-01-26 23:22:43 +01:00
|
|
|
|
2022-02-17 16:36:35 +01:00
|
|
|
forward_returns = create_forward_returns(returns, self.time_horizon)
|
|
|
|
|
cutoff_point = returns.index[-self.time_horizon]
|
|
|
|
|
event_start_times[event_start_times < cutoff_point]
|
2022-01-26 23:22:43 +01:00
|
|
|
event_candidates = forward_returns[event_start_times]
|
|
|
|
|
|
|
|
|
|
def get_class_binary(x: float) -> int:
|
|
|
|
|
return -1 if x <= 0.0 else 1
|
|
|
|
|
labels = event_candidates.map(get_class_binary)
|
|
|
|
|
|
2022-02-17 16:36:35 +01:00
|
|
|
return (pd.DataFrame({
|
2022-01-26 23:22:43 +01:00
|
|
|
'start': event_start_times,
|
|
|
|
|
'end': event_start_times + pd.Timedelta(days=self.time_horizon),
|
|
|
|
|
'label': labels,
|
|
|
|
|
'returns': forward_returns[event_start_times]
|
2022-02-17 16:36:35 +01:00
|
|
|
}), forward_returns[event_start_times])
|
2022-01-26 23:22:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|