Time Series ForecastingFeature Engineering (Lag Features, Rolling Stats, Seasonality)Medium⏱️ ~2 min

Seasonality Encoding: Calendar Features and Fourier Terms

Calendar Features

Extract temporal indicators from timestamps: day_of_week (0-6), month (1-12), day_of_month (1-31), week_of_year (1-52), is_weekend, is_holiday. These capture known cyclical patterns. Retail sales spike on weekends; restaurant demand dips on Mondays. Calendar features are known future covariates—available for any forecast horizon.

Encoding Strategy: Use cyclical encoding for ordinal calendar features. Day of week as sin/cos preserves circular relationship: Sunday (day 0) is adjacent to Saturday (day 6). Linear encoding treats them as distant, which is incorrect.

Cyclical Encoding Formula

For feature with period P: sin_feature = sin(2π × value / P), cos_feature = cos(2π × value / P). Day of week (P=7): sin(2π × day / 7), cos(2π × day / 7). Hour of day (P=24): sin(2π × hour / 24), cos(2π × hour / 24). Both sin and cos are needed to uniquely identify position in cycle.

Fourier Terms for Complex Seasonality

Multiple seasonal patterns (weekly + yearly) require multiple harmonics. Fourier terms decompose seasonality into sine-cosine pairs at different frequencies. For yearly seasonality: include sin(2πt/365), cos(2πt/365), sin(4πt/365), cos(4πt/365), etc. More terms capture sharper seasonal patterns but risk overfitting.

Practical Guidance: Start with 2-4 Fourier terms per seasonal period. More terms needed for sharp patterns (holiday spikes), fewer for smooth patterns (gradual summer increase). Cross-validate to select optimal number.

Holiday Features

Create binary indicators for holidays. Different holidays have different effects—Christmas differs from Labor Day. Include lead/lag effects: is_day_before_holiday, is_day_after_holiday. Holiday calendars vary by region; use location-specific holiday lists for multi-region forecasting.

💡 Key Takeaways
Cyclical encoding preserves circular relationships: Sunday is adjacent to Saturday, not distant
Both sin and cos needed for unique position: sin(2π × value / P), cos(2π × value / P)
Start with 2-4 Fourier terms per seasonal period; more for sharp patterns, fewer for smooth
📌 Interview Tips
1Day of week encoding: sin(2π × day / 7), cos(2π × day / 7) where P=7
2Include holiday lead/lag effects: is_day_before_holiday, is_day_after_holiday for spillover
← Back to Feature Engineering (Lag Features, Rolling Stats, Seasonality) Overview
Seasonality Encoding: Calendar Features and Fourier Terms | Feature Engineering (Lag Features, Rolling Stats, Seasonality) - System Overflow