How do Fourier terms with period m=365 encode yearly seasonality as model columns?
answer
- encode the calendar, do not count days
- sine and cosine, always in pairs
- the pair sets amplitude and phase
- K harmonics give 2K columns
- smooth curves cannot spike on holidays
basics
~10 sFourier terms are sine and cosine columns built from the date index: sin(2pikt/365) and cos(2pikt/365) for k = 1..K. Those 2K columns approximate a smooth yearly cycle using far fewer parameters than day-of-year indicators.
solid answer
~40 sNumber the days `t = 1, 2, 3, ...` and for each harmonic `k` add two columns, `sin(2*pi*k*t/365)` and `cos(2*pi*k*t/365)`. `k=1` completes one cycle per year, `k=2` two cycles, and so on, so `K` harmonics give `2K` columns whose weighted sum can trace any reasonably smooth annual shape. The pair matters: sine and cosine together let the model place the peak anywhere in the year, where a lone sine would fix the phase. `K` is the smoothness dial — small `K` gives a broad summer-versus-winter shape, large `K` chases narrow spikes and eventually overfits, so choose it by validation. The win over 365 day-of-year indicators is parameter count and the fact that neighbouring days get similar values instead of being learned independently. Sharp, date-moving events such as holidays stay as their own flag columns.
go deeper
Recognise that seasonality can be encoded as sine and cosine columns of the date rather than as one indicator per day, and that holidays are handled by separate flags.
Write the terms correctly, explain why sine and cosine come in pairs, and describe K as a smoothness setting traded against parameter count and history length.
Show judgment about which structure gets which encoding: harmonics for smooth cycles, flags for moving holidays and paydays, and a defensible way of choosing K out of sample.
Own the seasonality contract across many models: which periods are standard, how holiday calendars are sourced and maintained per market, and who is accountable when a calendar is wrong.
## The problem with long seasonal periods Seasonality means a pattern that repeats with a fixed period `m`: 7 for a weekly cycle in daily data, 24 for a daily cycle in hourly data, 365 for a yearly cycle in daily data. Short periods are easy to encode directly — with `m = 7` you add six indicator columns for the day of week and the model learns one offset per weekday from thousands of examples each. That approach collapses at `m = 365`. Day-of-year indicators mean 364 columns, and with three years of history each column is fit from three observations. The estimates are pure noise, they are unregularised against their neighbours (14 March learns nothing from 13 March), and leap years break the alignment. You need an encoding that is smooth in the date and cheap in parameters. ## The construction Give each timestamp an integer index `t` that counts forward through the series. For a chosen number of harmonics `K`, add the columns `sin(2*pi*k*t/m)` and `cos(2*pi*k*t/m)` for `k = 1, 2, ..., K` with `m = 365` for a yearly cycle in daily data. That is `2K` columns in total. The model then learns coefficients `a_k` and `b_k`, and the fitted seasonal contribution at time `t` is the sum over `k` of `a_k*sin(2*pi*k*t/m) + b_k*cos(2*pi*k*t/m)`. Two details explain why this works. First, sine and cosine of the same frequency always appear as a pair. A weighted sum `a*sin(x) + b*cos(x)` equals a single sine wave of amplitude `sqrt(a^2 + b^2)` shifted by a phase determined by the ratio of `a` to `b`. So the pair buys the model freedom to put the annual peak in June, in November, or anywhere else; a single sine column would pin the peak to a fixed date and force the model to fight the encoding. Second, the harmonics stack. `k = 1` is one full cycle per year — a single broad hump, the summer-versus-winter shape. `k = 2` adds a second cycle, allowing two peaks. Higher `k` adds progressively finer wiggles. Truncating at `K` is exactly a decision to represent the seasonal shape with a limited amount of detail. ## Choosing K `K` is a smoothness hyperparameter and behaves like one. Too small and the fitted seasonality is a blunt sinusoid that misses a genuinely double-peaked year. Too large and the curve bends to accommodate individual noisy stretches, and each extra harmonic costs two more parameters. Practical daily-data values often sit in the low single digits for yearly seasonality, but the honest answer in an interview is that you pick it by out-of-sample error or an information criterion rather than by rule of thumb, and that a series with only two or three years of history cannot support many harmonics. Nothing forces one period. A daily series with both a weekly and a yearly rhythm can carry Fourier terms for `m = 7` and for `m = 365` in the same table, each with its own `K`. Fractional periods are also fine — `m = 365.25` handles leap years without special-casing, which is one of the underrated advantages over date-indexed dummies. ## Why these terms are always available Fourier columns are deterministic functions of the timestamp. They are computable for any future date with no data at all, which makes them structurally different from lags and rolling windows. Lags run out at long horizons; calendar and Fourier terms never do. That is exactly why a long-horizon model leans heavily on them: the autoregressive columns lose availability while the deterministic ones remain, so a model forecasting a year ahead is mostly a calendar model with a level anchor. ## What Fourier terms cannot do They are smooth and periodic by construction, so anything sharp and irregular needs its own column. Three families come up constantly. **Holidays.** Some are fixed (25 December) and some move (Easter, Thanksgiving, Lunar New Year, any observance tied to a lunar calendar). A smooth annual curve cannot represent a one-day spike that lands on a different date each year, so add an `is_holiday` flag, and usually flags for the days immediately around it — demand often shifts into the days before and slumps after. **Day of week.** Six indicators are cheap and interpretable at `m = 7`, and there is little reason to prefer harmonics unless the model is short on capacity. **Month-position effects.** Payday clustering, month-end reporting cycles and billing dates depend on the day of month, not on the day of year, so `is_payday` or `day_of_month` flags capture what a yearly harmonic never will. A typical daily demand feature row therefore mixes three kinds of columns: autoregressive lags and rolling aggregates for the level, Fourier terms for the smooth annual shape, and explicit flags for holidays, weekdays and paydays. Interviewers ask about Fourier terms specifically to see whether a candidate understands that seasonality can be encoded rather than only estimated, and that the encoding choice is about parameter count and smoothness.
- Why is each harmonic added as a sine and cosine pair rather than a sine alone?Because a weighted sum `a*sin(x) + b*cos(x)` is itself a sine wave whose amplitude is `sqrt(a^2 + b^2)` and whose phase shift is set by the ratio of the coefficients. Supplying both lets the model place the annual peak on any date. A lone sine column fixes the phase at whatever the index origin implies, so the model can scale the wave but never slide it.
- How do you handle a holiday like Easter that moves each year?With an explicit flag, not with seasonality terms. Fourier columns are smooth periodic functions of the date index, so they cannot produce a spike that lands on a different date each year. Add an `is_holiday` indicator, and usually separate indicators for the days immediately before and after, since demand typically shifts across that window rather than only rising on the day.
- What makes Fourier and calendar columns especially valuable at long horizons?They are deterministic functions of the timestamp, so they can be computed for any future date without observing anything. Lags and rolling windows lose availability as the horizon grows past their width, but calendar terms never do. A year-ahead model is therefore mostly calendar structure plus a level anchor, which is a very different feature mix from a next-day model.
Describing the year with a handful of harmonics is like describing a coastline with a few smooth curves: cheap and close enough, but you still mark the harbour separately because a curve cannot represent it.
saying these in an interview costs you the question
- Adds a sine column without its matching cosine
- Uses 364 day-of-year indicators on three years of data
- Treats K as fixed rather than tuned
- Expects smooth harmonics to fit a one-day holiday spike
- Rebuilds the time index per split so phases shift
- Assumes yearly harmonics also cover the weekly cycle