A simple moving average treats every value in its window equally and forgets everything outside it. An exponential moving average instead keeps a single running number, mixing each new value into it:
So the newest value gets weight α, the one before it α(1-α), the one before that α(1-α)², and so on — every past value still contributes, with influence fading geometrically.
Task: write ema(series, alpha) returning one smoothed value per input value, each rounded to 4 decimal places.
series[0]. There's no earlier estimate to mix with, and starting from zero instead would drag the whole front of the series toward the origin.alpha is between 0 and 1. At alpha = 1 the EMA has no memory and just copies the input; small values make it slow and smooth.The appeal over a windowed average is that it needs no window. One number holds the entire history, so it costs the same memory whether you're smoothing ten points or ten million — which is why it's the default in streaming systems, and why the same recursion turns up inside Adam and momentum.