skip to content

Rolling and Expanding Computations

A window moving over an ordering: how long it is, whether it can see ahead, and how many observations it needs before it answers. Seeing ahead is how a feature table learns the future.

on this pageshow

questions

5

Over an ordered sequence, how does a moving window of fixed length differ from a window anchored at the start?

level: juniorimportance: must knowfreq 68%

answer

  1. one answer per position
  2. which edge is allowed to move
  3. fixed span against never forgetting
  4. anchored keeps the first record forever

basics

~20 s

A moving window of fixed length keeps its edges a fixed distance apart, so old records drop out as new ones enter. A window anchored at the start never moves its left edge, so each answer covers everything so far.

solid answer

~50 s

Both shapes walk along an ordering and emit one answer per position; they differ only in where the left edge sits. A **moving window of fixed length** holds its two edges a fixed distance apart — a distance declared either as a count of records or as a span of time — so a record that was inside it a hundred positions ago has since fallen out and affects nothing. A **window anchored at the start** pins its left edge to the first record and lets only the right edge advance, so each answer covers everything from the beginning up to that position and nothing is ever discarded. The practical difference is memory. The first gives a local statistic that tracks recent behaviour and forgets an old spike; the second gives a running statistic that an early spike still perturbs, and that grows steadily less sensitive to any one new record as the sequence lengthens.

go deeper

for a junior

Be able to say which edge moves. A fixed-length window forgets old records; a window anchored at the start does not, and each of its answers covers one more record than the last.

for a middle

Explain what that does to sensitivity. The anchored statistic becomes progressively harder to move as the sequence grows, while the fixed-length one responds to the recent stretch only and discards everything behind it.

for a senior

Say which you would pick for a live signal and why, and be ready to explain what a long anchored statistic hides: a level shift that happened recently is barely visible in it, and the output looks perfectly healthy.

for a principal

The angle is commitment. A window length is a parameter every downstream consumer inherits, so argue for deriving it once from how fast the process actually changes, rather than letting each dashboard pick its own round number.

## One answer per position A window computation walks along an ordering — most often a time ordering, but any stable ordering will serve — and emits **one answer per position**, computed from whatever records the window covers while it is sitting at that position. That is the whole shape of the operation. Everything that distinguishes one window computation from another is a rule about where the window's two edges sit and how they move as the position advances. Two rules dominate, and a third sits between them. The market's words for the first two are a *rolling* window — one that walks along with its length held fixed — and an *expanding* window — one whose left edge is pinned to the first record and never moves. ## A moving window of fixed length Both edges advance together, a fixed distance apart. That distance, the window's length, is declared either as a count of records or as a span of time, and which one you declared changes which records the window covers whenever the observations are not evenly spaced. Its defining property is that this window **forgets**. A record enters at the right edge, contributes to a bounded number of answers, then leaves at the left edge and never affects another one. So: - the answer at each position is a *local* statistic — it describes the recent stretch, not the history; - an extreme reading perturbs a bounded number of answers and then vanishes completely; - the answer follows a genuine change of level within roughly one window length; - early positions have fewer records behind them than the length asks for, so the leading edge of the output needs a rule of its own. ## A window anchored at the start The left edge is pinned to the first record and never moves; only the right edge advances. Each answer therefore covers **everything from the beginning up to and including that position**, and each one covers one more record than the last. Its defining property is that this window **never forgets**: - an early extreme reading is folded into every later answer, its influence shrinking only as the record count grows; - the answer becomes progressively harder to move — by the ten-thousandth position, one new record shifts a running mean by roughly one ten-thousandth of its distance from that mean; - a real change of level late in the sequence is nearly invisible, because the accumulated history outweighs it; - there is no length to choose, which is often why people reach for it, and is also why it answers a different question. ## Side by side | | moving window of fixed length | window anchored at the start | |---|---|---| | left edge | advances with the position | pinned to the first record | | records per answer | fixed by count, or variable by span | grows by one each position | | an old record's influence | drops out entirely | never drops out | | response to a level shift | within about one window length | heavily damped, more so over time | | parameter to choose | the length | none | | question it answers | what is happening lately | what has happened so far | ## A third shape, with no hard left edge Between them sits an **exponentially weighted moving statistic**: a moving statistic in which no record is ever dropped, but each earlier observation contributes with a geometrically smaller weight than the one after it. There is no length, only a decay rate. It behaves like a fixed-length window in that recent records dominate, and like an anchored window in that nothing is discarded outright. It is useful when you want recency without the cliff a hard left edge creates, where one record's departure visibly moves the answer. ## What each one actually costs It is tempting to say a window computation is one pass with a cheap incremental update, so its cost is independent of the length. That holds for **additive statistics** — a sum, a count, a mean — where a running accumulator can add the entering record and subtract the leaving one. It does not hold for **order statistics** such as a median or a quantile, which have no cheap removal step, and it does not hold when the per-position computation is a function you wrote yourself: whether that is invoked once per position over the small set of covered records, or recognised and dispatched to a compiled routine, differs by design and by what you passed. The honest answer is that the cost depends on the statistic and on the path the tool took, not that windows are free. ## Choosing 1. Ask what the number is for. An alert threshold, a control limit or a display of current behaviour wants the fixed-length form; a cumulative total, a to-date average or a best estimate of a level assumed stable wants the anchored form. 2. If you take the fixed length, choose it from how fast the underlying process actually changes rather than from a round number, and write the reason down — every consumer of that column inherits it. 3. If you take the anchored form, understand that you have chosen to give recent records almost no weight once the sequence is long, and confirm that this is what you meant.

  • What happens to a window anchored at the start when the very first record is a bad reading?
    It contaminates every subsequent answer. Its weight is roughly one over the number of records seen so far, so the effect shrinks as the sequence grows but never reaches zero, and it is largest exactly where the output is usually inspected — the first few dozen positions. A fixed-length window has the opposite behaviour: the bad reading distorts a bounded number of answers and then leaves.
  • Is there a shape between the two, with no hard left edge?
    Yes — an exponentially weighted moving statistic. Every earlier observation still contributes, but with a weight that decays geometrically with distance, so recent records dominate without anything being dropped outright. It takes a decay rate rather than a length, and it avoids the visible step a fixed-length window produces when one influential record falls out of the left edge.
  • Does the cost per position grow with the length of a moving window?
    It depends on the statistic and the implementation path. A sum, a count or a mean can be maintained incrementally — add the entering record, subtract the leaving one — so cost is roughly independent of length. A median or a quantile has no cheap removal step. A function you wrote yourself may be invoked once per position, or may be recognised and compiled, and which happens differs by design.

A moving window of fixed length is the rear-view mirror: it shows a fixed stretch of road behind you, and the stretch you passed an hour ago has slid out of it entirely. A window anchored at the start is the odometer: it never forgets a metre you drove, so a slow crawl at the beginning of the trip is still dragging on your average speed hours later.

saying these in an interview costs you the question

  • Says a window anchored at the start is just a very long moving window.
  • Thinks a moving window's earliest positions are computed over the full length.
  • Assumes the two shapes converge to the same answer once enough records exist.
  • Believes an old outlier stops affecting a running statistic on its own.
  • Treats window length as free because the computation is one pass.
open as a page

A feature column was smoothed with a window centred on each row; why could its value at that row's stamp not have been computed then?

level: seniorimportance: must knowfreq 57%

basics

~20 s

A centred window is stamped in the middle of the records it covers, so roughly half of them come from after that stamp. The value therefore encodes observations that had not happened at the moment the row describes.

open as a page

For a moving window of thirty records, what decides whether the first twenty-nine positions produce a value at all?

level: middleimportance: should knowfreq 48%

basics

~20 s

A minimum observation count — how many real observations a window must cover before it answers. At the full length the leading positions hold no value; below it they answer early, from fewer observations than the length implies.

open as a page

On an irregularly-sampled feed, why does a moving window of seven cover different records when seven means days rather than records?

level: middleimportance: should knowfreq 60%

basics

~20 s

A count-defined window always covers exactly seven records and therefore a variable span of time. A span-defined window always covers exactly seven days and therefore a variable number of records. On irregular data the two select different sets.

open as a page

A moving average over one table holding many devices' readings looks wrong at each device's first rows; what happened?

level: seniorimportance: should knowfreq 45%

basics

~20 s

The window walked straight across the boundary between devices, so each device's first answers mix in the tail of the device before it. The window has to be restricted so it never reaches past an entity boundary.

open as a page