Joe Lee
[Math]

Fourier Decomposition
of Retail Sales

The Signal

Every business has a heartbeat. A weekly rhythm from weekend traffic. A seasonal swell before the holidays. A longer cycle tied to the economy. These patterns overlap and obscure each other in the raw data but they were always there, separable, measurable.

The dataset is 30 years of US retail and food service sales from the Census Bureau's Monthly Retail Trade Survey unadjusted. The unadjusted series is essential. Seasonal adjustment would remove exactly the patterns we're trying to surface.

What Fourier Found

The Fourier transform takes a signal a sequence of numbers and decomposes it into a sum of sine waves at different frequencies, amplitudes, and phases. Any signal. Any shape. The algorithm finds the exact combination of waves that, added together, reproduce the original data.

Applied to monthly retail sales, the power spectrum shows clear spikes at predictable periods: annual (12-month cycle), semi-annual, and quarterly. These aren't approximations they're the actual oscillatory structure buried in three decades of aggregate consumer behavior.

Reading the Spectrum

The visualization has three panels. The first shows the original detrended signal with a reconstruction overlay the sum of however many frequency components you've selected. The second is the power spectrum: a bar chart of amplitude by frequency, where clicking a bar toggles that component. The third shows the individual sine waves for selected components, stacked and color-coded.

Drag the "Top N" slider from 1 to 20 and watch the reconstruction converge toward the original signal. At N=1, you get only the annual cycle a clean sine wave that captures the shape of the year. At N=5, the quarterly beat appears. By N=10, the reconstruction is nearly indistinguishable from the raw data. The residual is noise.

THE KEY INSIGHT

Signal vs. noise

Your Q4 spike wasn't luck. It was math. The annual cycle accounts for roughly 60% of the variance in the unadjusted series. The remaining 40% is either smaller periodic structure or true noise.

Separating them is the prerequisite for forecasting

The Math

Given N equally-spaced samples, the Discrete Fourier Transform produces N complex coefficients each encoding the amplitude and phase of a sinusoidal component at a specific frequency. The inverse transform reconstructs the original signal exactly from all N coefficients.

The implementation uses the Cooley-Tukey FFT algorithm O(N log N) vs. the naive O(N²) written directly in TypeScript. For 360 months of data, N is trivially small. No library dependency. The FFT runs in the browser on every slider interaction, typically under 1ms.

One preprocessing step matters: detrending. Without removing the linear trend first, the FFT spends most of its energy on the DC offset and slope, and the seasonal components appear as sidebands around the zero frequency rather than as clean spikes. Subtract the least-squares fit line first. Then transform.