Skip to content
Tutorials Center
My learning

Topics

Lesson 40 of 40 ยท Projects and Practice

Time Series Practice Set

Eight worked practice problems for time series analysis: resampling, stationarity, decomposition, forecasting baselines, ARIMA choice, leakage-free features, backtesting and interval coverage.

Reading about forecasting is not the same as doing it. This practice set has eight problems that cover the whole course. Each one gives a task, then an editable solution you can run and change. Try to solve the task yourself first in the editor (clear the code, or write your own below it), and only then compare. Every problem uses generated data with a fixed seed, so your results match the comments.

Problem 1: resample and fill

Hourly readings have a gap of a few missing hours. Compute daily means and fill the missing hourly values by linear interpolation before averaging. Which approach changes the daily mean more, dropping the gaps or interpolating?

Python Resample with gaps

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The trend rises through the day, and the missing hours sit in the middle of day 2, so both give close results here. If the gap sat at the end of a rising day, skipping would bias the mean low. See missing data and resampling.

Problem 2: is it stationary?

Test whether a random walk and its first difference are stationary with the ADF test. State which has a unit root.

Python ADF on a random walk

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Problem 3: decompose and deseasonalize

Build a monthly series with trend and a yearly pattern, decompose it with STL and report the seasonal strength (1 minus the variance of the remainder over the variance of remainder plus seasonal).

Python STL and seasonal strength

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Problem 4: beat the baseline

For the series below, compute the MAE of naive, seasonal naive and a simple drift forecast over the last 12 months. Which is best, and why?

Python Three baselines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Drift wins here (about 6.7 against 10.9 for naive and 13.6 for seasonal naive). The trend is strong, 1.2 per month or about 14 over a year, so repeating last year's values is far too low, even though they carry the right seasonal shape. The lesson: no baseline is best everywhere, so always compute several. A seasonal naive forecast plus a drift term handles both trend and seasonality and is a tougher benchmark that a model must pass. See forecasting workflow and baselines.

Problem 5: identify the ARIMA order

Generate an AR(2) process, inspect the PACF to see where it cuts off, and confirm with AIC across a small grid.

Python PACF and AIC for order selection

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The PACF has two clear spikes and then drops into the noise band, which indicates p = 2; AIC agrees. See choosing ARIMA orders.

Problem 6: spot the leak

The code below builds features to predict tomorrow's value. One feature leaks the future. Find it, fix it and compare the (fake) improvement with the honest score.

Python Find the leaking feature

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

center=True averages values that come after time t, including y[t] itself, so the first model looks far better than it can ever be in production. Whenever a rolling statistic feeds a prediction, shift it so it only sees the past (see lag features).

Problem 7: backtest two models

Compare a seasonal naive forecast with a Ridge regression on lags over five rolling origins with a 7-day horizon.

Python Rolling-origin comparison

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Problem 8: interval coverage

Fit a model on one block, compute 80% residual-quantile intervals from a second block and measure the coverage on a third block.

Python Does the 80% interval cover 80%?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

If coverage differs a lot from 0.80, ask why: a different horizon, changed noise or too small a calibration block are the usual reasons. See prediction intervals.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A series' ADF test p-value is 0.8. What is the usual next step?

    A large p-value cannot reject a unit root, so the series is treated as non-stationary and is differenced.
  2. 2. Which rolling feature is leak-free for predicting y at time t?

    Only a statistic computed from values before t is available at forecast time.
  3. 3. The PACF of an AR(2) process typically:

    The partial autocorrelation of an AR(p) process is zero after lag p.
  4. 4. Why use several backtest origins?

    A single test window can be lucky or unlucky; many origins give a reliable estimate.

Summary

  • The same checklist applies to every problem: clean and resample, check stationarity, decompose, set baselines, model, backtest, quantify uncertainty.
  • Leakage hides in rolling and centered statistics, scalers and global features; shift or compute them inside each fold.
  • Judge every model against seasonal naive over several rolling origins.

Next: back to the track overview