How to implement a walk forward

I would like to implement a walking analysis. Like on a picture.

enter image description here

enter image description here

I wrote this code.

@staticmethod
def build_rolling_calendar(start_date, end_date, out_of_sample_size, runs):
    days = (end_date - start_date).days

    in_sample_size = (100 - out_of_sample_size) / 100
    out_of_sample_size = out_of_sample_size / 100

    total_days_per_run = round(days / (runs * out_of_sample_size + in_sample_size))
    in_sample_days_per_run = round(total_days_per_run * in_sample_size)
    out_of_sample_days_per_run = round(total_days_per_run * out_of_sample_size)

    calendar = pd.DataFrame()

    calendar['InSampleStarts'] = [start_date + timedelta(days=(out_of_sample_days_per_run * x))
                                  for x in range(runs)]
    calendar['InSampleEnds'] = [x + timedelta(days=in_sample_days_per_run)
                                for x in calendar['InSampleStarts']]

    calendar['OutSampleStarts'] = [start_date + timedelta(days=in_sample_days_per_run) +
                                   timedelta(days=(out_of_sample_days_per_run * x))
                                   for x in range(runs)]
    calendar['OutSampleEnds'] = [x + timedelta(days=out_of_sample_days_per_run)
                                 for x in calendar['OutSampleStarts']]

    return calendar

      

Unfortunately, this code returns values ​​that are outside of end_date. I would like to know how can I fix this error?

PS My test call

calendar = build_rolling_calendar(datetime(2016, 1, 1), datetime(2017, 5, 31), 25, 10)

      

enter image description here

+3


source to share





All Articles