antoinecarme / pyaf

PyAF is an Open Source Python library for Automatic Time Series Forecasting built on top of popular pydata modules.
BSD 3-Clause "New" or "Revised" License
458 stars 73 forks source link

Analyze Business Seasonals (WeekOfMonth and derivatives) #137

Closed antoinecarme closed 4 years ago

antoinecarme commented 4 years ago

For business daily data the signal may be better analyzed by crossing dayOfWeek and week index in month.

This can be made by dividing the work month in weeks and analyzing WeekOfMonth.

WeekOfMonth = dayOfMonth // 7

(This is only an approximation, not to be confused with WeekOfYear, which is already analyzed by PyAF)

An important derivative is DayOfNthWeekOfMonth, a crossing of DayOfWeek and WeekOfMonth

DayOfNthWeekOfMonth = WeekOfMonth * 7 + DayOfWeek

This pattern captures concepts like 'first Monday of month' , 'last Tuesday' , 'third Friday' etc.

antoinecarme commented 4 years ago

Check if this can capture religious holidays (Easter , Holy Week etc)

antoinecarme commented 4 years ago

Avoid WeekOfMonth approximation, more intuitive and takes care of first day of month being Tuesday etc (increasing ordinal values are also guaranteed).

    def get_week_of_month(self, series):
        lFirstDayOfMonth = series - pd.to_timedelta(series.dt.day - 1, unit='D')
        return series.dt.weekofyear - lFirstDayOfMonth.dt.weekofyear + 1
antoinecarme commented 4 years ago

Closing