Forecasting and Time-Series Analysis Questions
Analyzing and projecting data that moves over time. Covers trend and seasonality decomposition, forecasting approaches, demand modeling, and anomaly detection on time series. Emphasizes reasoning about baselines, drivers, and forecast reliability.
Implement single exponential smoothing in Python. Input: a list or numpy array of historical numeric values and smoothing factor alpha (0 < alpha <= 1). Output: the smoothed series and the next-period point forecast. Explain initialization choices and how alpha controls responsiveness.
Sample Answer
Direct answer
Single exponential smoothing maintains one smoothed level, updated each period as a weighted blend of the new observation and the previous smoothed value (St=αyt+(1−α)St−1); the forecast for ALL future periods is simply the final smoothed level Sn (SES has no trend or seasonal component to extrapolate further), and alpha controls how much weight recent observations get versus the smoothed history.
Structured elaboration and worked example (executed, cross-checked against statsmodels)
import numpy as np
def simple_exp_smoothing(values, alpha):
smoothed = np.empty_like(values, dtype=float)
smoothed[0] = values[0] # initialize at the first observation
for t in range(1, len(values)):
smoothed[t] = alpha * values[t] + (1 - alpha) * smoothed[t - 1]
return smoothed, smoothed[-1] # forecast = last smoothed level
Run on [112, 118, 132, 129, 121, 135, 148, 142, 130, 145] with alpha=0.3:
from-scratch smoothed: [112. 113.8 119.26 122.182 121.827 125.779 132.445 135.312 133.718 137.103]
from-scratch next forecast: 137.103
Cross-checked against statsmodels.tsa.holtwinters.SimpleExpSmoothing with the same alpha (optimized=False): the fitted values matched exactly once accounting for statsmodels' indexing convention (its fittedvalues[t] is the one-step-ahead forecast made BEFORE seeing yt, i.e. it equals our smoothed[t-1], not our post-update smoothed[t]) - after aligning on that offset, max abs diff = 0.0 and both implementations agree on the final forecast, 137.103.
This cross-check caught a genuine implementation bug on the first attempt: an initial version computed "next forecast" as alpha * values[-1] + (1-alpha) * smoothed[-1], which double-applies the last observation (since smoothed[-1] already incorporated it in the recursion), producing 139.47 instead of the correct 137.10. The correct SES forecast is simply the final smoothed level itself, y^n+h=Sn for every horizon h≥1 - SES has no mechanism to extrapolate differently at different horizons, which is itself an important limitation to flag (see below).
- Initialization choices: initializing S1=y1 (used above) is the simplest and most common convention; an alternative is to initialize with the average of the first few observations, which reduces sensitivity to a noisy first data point at the cost of a slightly more complex setup. The choice mostly matters for a short series; its influence fades geometrically as more observations are smoothed in.
- How alpha controls responsiveness: alpha close to 1 makes the smoothed level track new observations almost immediately (responsive, but passes through more noise); alpha close to 0 makes it change very slowly (smooth, but slow to react to a genuine shift). Alpha is typically chosen by minimizing in-sample or backtested one-step-ahead error rather than picked by hand.
Trade-offs & pitfalls
SES has no trend and no seasonality component - its forecast is FLAT (the same value) at every horizon, which makes it a poor choice for any series with a visible trend or seasonal pattern (that's exactly what Holt's linear method and Holt-Winters extend it to handle). The implementation trap surfaced above generalizes: any time you're tempted to apply "one more update step" to produce a forecast from a recursive smoothing state, check the model's actual mathematical definition of its forecast function rather than assuming intuition about "the next step" is correct - for SES specifically, the forecast function is constant in the smoothed level, not a further-updated value.
You are asked to implement a monitoring metric that measures business value of forecasts and not just accuracy. Propose specific KPIs such as stockouts prevented, holding cost reduction, or revenue uplift, explain how to compute them from historical forecasts and actuals, and describe attribution challenges when multiple operational changes occurred simultaneously.
Sample Answer
Direct answer
Moving beyond raw accuracy, a forecast's business value can be measured through KPIs like stockouts prevented, holding-cost reduction, and revenue uplift - each computed by comparing what actually happened (with the forecast-driven decision in place) against a credible counterfactual of what would have happened under a naive or prior approach, with attribution challenges handled by isolating the forecast's specific contribution from other simultaneous operational changes.
Structured elaboration
- Stockouts prevented: compare the actual stockout rate under the current (forecast-informed) inventory policy against a counterfactual - either the stockout rate the OLD policy would have produced on the same demand realization (if you can simulate it), or a controlled holdout where a subset of SKUs/stores continued using the old approach as a genuine comparison group.
- Holding-cost reduction: compute the excess inventory carried under the old policy versus the new forecast-informed one, valued at the business's actual holding-cost rate (capital cost, storage, spoilage/obsolescence risk) - this requires the SAME kind of counterfactual comparison as stockouts, not just "inventory went down," since inventory could fall for unrelated reasons (a demand drop, say).
- Revenue uplift: the incremental revenue attributable to better availability/staffing/allocation decisions the improved forecast enabled, computed similarly by comparison against a credible counterfactual rather than simply period-over-period revenue growth (which conflates many causes).
- Computing these from historical forecasts and actuals: requires not just the forecast and the actual outcome, but a record of what DECISION the forecast drove (the resulting inventory order, staffing level, allocation) so you can quantify the downstream operational consequence, not just the raw forecast error - a forecast can be numerically accurate yet drive a poor decision if the decision rule built on top of it (e.g. the safety-stock formula) is miscalibrated.
- Attribution challenges when multiple changes happen simultaneously: if inventory policy, pricing, AND the forecasting model all changed in the same quarter, isolating the forecast's specific contribution to any observed improvement requires either staggering the rollouts (so each change's effect window is at least partially separable), a genuine holdout/control group that kept the old forecast while everything else changed identically, or, at minimum, an honest acknowledgment in the reporting that the estimate is a joint effect of several changes rather than the forecast's alone.
Worked example
A holding-cost-reduction claim of "$500K saved this quarter" is only credible if it's benchmarked against a stated counterfactual (e.g. "versus the safety-stock levels the prior policy would have required for the same realized demand") - reported without that counterfactual, the number is really just "inventory happened to be lower," which could equally reflect a demand slowdown having nothing to do with forecast quality.
Trade-offs & pitfalls
The recurring failure mode across all three KPIs is presenting a raw before/after comparison as if it were the forecast's isolated causal contribution, when in a real operating business, demand, pricing, promotions, and policy all move simultaneously - treat any business-value number without an explicit counterfactual or control group as a directional estimate, not a precise measurement, and say so plainly when reporting it.
Compare algorithms for online and offline change-point detection: CUSUM, Bayesian Online Change Point Detection (BOCPD), PELT, and algorithms available in the ruptures library. For a business metric that can exhibit both abrupt and gradual changes, which algorithms would you choose for online detection vs offline forensic analysis, how to tune sensitivity, and how to quantify detection delay and false alarm rate?
Sample Answer
Direct answer
CUSUM, Bayesian Online Change Point Detection (BOCPD), and PELT sit on a spectrum from lightweight-and-online to expensive-and-thorough: CUSUM is a simple, fast, purely-online statistic best suited to detecting a known, sustained shift size quickly; BOCPD is also online but gives a full probabilistic estimate of where a break is, at higher compute cost; PELT (and other ruptures-library algorithms) are OFFLINE, penalized-optimization methods best suited to forensic, after-the-fact analysis across a whole series at once.
Structured elaboration and worked example (executed)
Against a synthetic series (seed=42, n=200) with an ABRUPT level shift at index 80 (mean +3) and a separate GRADUAL drift starting at index 140 (linear ramp +0 to +4 through index 199):
import numpy as np
import ruptures as rpt
np.random.seed(42)
n = 200
series = np.random.normal(0, 1.0, n)
series[80:] += 3.0 # abrupt level shift at index 80 (persists)
ramp = np.linspace(0, 4.0, n - 140)
series[140:] += ramp # gradual drift starting at index 140
X = series.reshape(-1, 1)
pelt_breaks = rpt.Pelt(model="rbf", min_size=10).fit(X).predict(pen=8)
binseg_breaks = rpt.Binseg(model="rbf", min_size=10).fit(X).predict(n_bkps=2)
target = series[:80].mean() # CUSUM: reference = pre-break mean
k, h = 0.5, 5.0 # slack, threshold (units of noise std=1)
s_pos = s_neg = 0.0
alarms = []
for i, x in enumerate(series):
s_pos = max(0.0, s_pos + (x - target) - k)
s_neg = max(0.0, s_neg - (x - target) - k)
if s_pos > h or s_neg > h:
alarms.append(i)
Executed result (ruptures 1.1.9):
PELT (offline) detected breakpoints: [80, 155, 200]
BinSeg (offline, n_bkps=2) detected breakpoints: [80, 155, 200]
PELT detection error on the abrupt break (true=80): 0
PELT detection error on the gradual drift (true start=140): 15 (flagged at 155)
First CUSUM alarm: index 81 (1-period delay after the true abrupt break at 80)
CUSUM alarms from index 80 through 199: 119 of 120 points -- alarms continuously, not just once
Both offline methods located the ABRUPT break at 80 exactly, but detected the GRADUAL drift (which truly started at 140) only at 155, a 15-period delay - a real, informative miss, not a tuning failure: algorithms built around detecting a sharp mean/variance shift have no equally-strong signal to latch onto for a slow drift, since there's no single point of maximal separation the way there is for an abrupt jump.
The online CUSUM detector (s_pos/s_neg accumulating deviation from a fixed target, alarming when either exceeds a threshold) alarmed starting at index 81 (a 1-period detection delay after the true abrupt break at 80) - and then continued alarming on 119 of the 120 subsequent points, essentially every period for the rest of the series. This is also a real, informative behavior, not a bug: once the process has genuinely and permanently shifted away from the original target mean, a CUSUM comparing against that STALE, fixed target will keep re-triggering indefinitely; a production deployment needs an explicit re-baselining step after a confirmed break (update the target mean) rather than comparing against the original target forever.
- Online vs offline choice: for real-time alerting on a live metric, CUSUM (cheap, fast, purely sequential) or BOCPD (richer probabilistic output, still sequential, but costlier per update) are the appropriate online choices; for a forensic, after-the-fact investigation of a whole historical series (e.g. "when exactly did this trend break happen"), PELT or BinSeg, which optimize over the WHOLE series at once, are more appropriate and typically more accurate since they use future context a genuinely online method cannot.
- Tuning sensitivity: CUSUM's k (reference/slack) and h (threshold) trade off detection delay against false-alarm rate; PELT's penalty parameter trades off the number of detected breakpoints against over-segmentation (too low a penalty finds spurious breaks in pure noise, too high a penalty misses real ones).
- Quantifying detection delay and false-alarm rate: on labeled synthetic data with known true break locations (as done above), detection delay is simply the offset between the true break and the first alarm after it; false-alarm rate is the count of alarms NOT near any true break, both of which should be measured explicitly on a validation set with known ground truth before trusting a specific parameter choice in production.
Trade-offs & pitfalls
The CUSUM re-baselining trap demonstrated above (indefinite re-alarming against a stale target after a confirmed permanent shift) is a common, easy-to-miss production issue - always pair an online changepoint detector with an explicit "confirmed break -> update the reference target" step, or it will functionally become a permanent, uninformative alarm rather than a useful signal.
You inherit an ensemble of forecasting models but the ensemble's prediction intervals are overconfident and too narrow, which has caused inventory shortages. Describe a diagnostic approach to identify why intervals are too narrow, methods to recalibrate intervals (for example variance inflation, isotonic regression on quantile predictions, or re-training quantile regressors), and how to implement a permanent fix and monitoring to prevent recurrence.
Sample Answer
Direct answer
Overconfident (too-narrow) prediction intervals causing real inventory shortages need a systematic diagnostic (is the issue in the point-forecast model, the interval-construction method, or a genuine regime change the intervals haven't adapted to), a recalibration fix matched to the diagnosed cause, and a permanent monitoring layer so the same narrowness doesn't silently recur.
Structured elaboration
- Diagnostic approach: check EMPIRICAL coverage on a genuine holdout first - what fraction of actual outcomes actually fell within the stated interval, compared to the nominal rate (e.g. 90%)? A big gap (say, actual coverage of 60% against a stated 90%) confirms the intervals are indeed miscalibrated, not just unlucky. Then localize the cause: is under-coverage uniform across all series/segments (points to a systematic issue in how the interval-construction method itself works) or concentrated in specific segments (points to those segments having genuinely different, larger variance than the model assumes, e.g. through a Gaussian-residual assumption that's a poor fit for a heavy-tailed or heteroscedastic segment)?
- Recalibration methods: variance inflation (scale up the interval width by an empirically-fit multiplier until holdout coverage matches the nominal target - simple, model-agnostic, but a blunt fix that doesn't address WHY the original interval was wrong); isotonic regression on quantile predictions (a nonparametric recalibration that learns a monotonic correction mapping the model's stated quantiles to empirically-observed quantiles, capable of fixing more complex, non-uniform miscalibration than a single inflation factor); re-training quantile regressors directly against the empirically observed outcomes (the most thorough fix, appropriate when the underlying point-forecast/interval-construction approach itself needs to change, not just be recalibrated post-hoc).
- Implementing a permanent fix and monitoring: choose the recalibration method matched to what the diagnostic revealed (a uniform, modest under-coverage across the board is well-suited to simple variance inflation; segment-specific or complex miscalibration needs isotonic regression or retrained quantile models); critically, add ONGOING calibration monitoring (tracking rolling empirical coverage against the nominal target, the same discipline used for monitoring any forecasting model's drift) so a recurrence - from a genuine regime change, model staleness, or a new segment entering the portfolio - is caught before it again causes real inventory shortages, rather than only being caught the next time someone happens to investigate.
Worked example
If diagnosis shows the intervals were built assuming Gaussian residuals but a meaningful fraction of the portfolio has genuinely heavy-tailed demand (occasional large, rare spikes), a single global variance-inflation factor would either over-correct the well-behaved majority of the portfolio or under-correct the heavy-tailed segment - isotonic regression, or switching that specific segment to a bootstrap or quantile-regression-based interval method (which don't assume Gaussian residuals to begin with), is the more precisely-targeted fix, informed directly by the segment-level diagnostic rather than a uniform blanket correction.
Trade-offs & pitfalls
A recalibration applied once and never re-checked is itself a latent risk - the correction factor (or isotonic mapping) that fixes today's miscalibration can become stale if the underlying process shifts again, so the monitoring step isn't optional cleanup, it's the actual guarantee that this specific operational failure (inventory shortages from overconfident intervals) doesn't recur silently.
You are leading a cross-functional initiative to move forecasting models into production. Product, Ops, and Data Engineering disagree about model retrain frequency and prediction TTL. How would you evaluate trade-offs (accuracy vs compute cost vs operational stability), align stakeholders, and make and communicate a data-driven recommendation? Describe a process for revisiting this decision.
Sample Answer
Direct answer
Resolving a cross-functional disagreement about retrain frequency and prediction TTL means making the trade-off (accuracy vs compute cost vs operational stability) explicit and quantified rather than argued in the abstract, aligning stakeholders around a shared decision framework rather than a single number, and building in a defined process to revisit the decision as conditions change rather than treating it as permanently settled.
Structured elaboration
- Evaluating the trade-off: quantify how much forecast accuracy actually degrades as prediction TTL (time-to-live, how long a forecast is served before being refreshed) increases - this is directly measurable via backtesting (compare accuracy of a forecast used immediately vs one used N days stale); quantify the COMPUTE cost of more frequent retraining (a real, budgetable number); and separately name the OPERATIONAL STABILITY cost of frequent changes (a forecast that changes too often can itself be disruptive to downstream planning processes that expect some consistency, distinct from either accuracy or raw compute cost).
- Aligning stakeholders: bring Product, Ops, and Data Engineering into a SHARED view of the actual trade-off curve (accuracy degradation vs TTL, compute cost vs retrain frequency) rather than each side arguing from its own priority in isolation - a concrete, data-grounded trade-off curve turns an abstract disagreement ("we need fresher forecasts" vs "retraining is expensive") into a specific, negotiable point on a shared curve.
- Making and communicating a data-driven recommendation: propose a specific retrain cadence and TTL grounded in where the accuracy-degradation curve starts to bend meaningfully (the point past which staleness costs materially more accuracy than it saves in compute/stability), explicitly stating the trade-offs being accepted at that point, so the decision is legible and defensible to all three stakeholder groups rather than feeling arbitrary to whichever side "lost."
- A process for revisiting the decision: define explicit triggers for reconsidering the choice (a monitored accuracy-degradation signal crossing a threshold, a meaningful compute-cost change, a business requirement shift) rather than either revisiting it constantly (churn, instability) or never revisiting it (staleness accumulating silently) - this closes the loop the same way ongoing calibration/drift monitoring does for a model's outputs, applied here to an operational POLICY decision rather than the model itself.
Worked example
A concrete quantified trade-off might show that forecast accuracy degrades only mildly from a 1-day to a 3-day TTL, but meaningfully more from 3 to 7 days, while retrain compute cost scales roughly linearly with frequency - this evidence supports a recommendation of a 3-day TTL as the point where you're not leaving much accuracy on the table relative to daily retraining, while meaningfully reducing compute cost and operational churn relative to it, a specific, defensible number rather than either extreme position (daily retraining "because fresher is always better," or infrequent retraining "because compute is expensive") argued from principle alone.
Trade-offs & pitfalls
The most common failure in this kind of cross-functional disagreement is treating it as a one-time negotiation to "win," rather than establishing a durable, revisitable process - a decision made once and never revisited will eventually become wrong as the underlying trade-off curve itself shifts (compute costs change, the business's tolerance for staleness changes, the model's own accuracy characteristics change), and a stakeholder group that felt unheard in the original decision is far more likely to escalate the disagreement again later if there's no defined mechanism for it to be legitimately reconsidered.
Unlock Full Question Bank
Get access to all Forecasting and Time-Series Analysis interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.