AI Hints
Home›Data Science Tutorial›Before and After
Data Science · Lesson 61 of 101

Interrupted Time Series

When the only comparison is the past.

⏱ 2 min read▶ Video at 1:41:59🎓 Free
Watch this lesson in the Data Science courseWatch on YouTube ↗Full Data Science course

The whole chain raises prices on one day. Sales go up by a lot afterwards. A manager announces that the higher price boosted sales! But sales were going up already, and the change happened just before the summer. Simple before and after numbers are one of the most common errors in business.

Interrupted Time Series
From the video: Interrupted Time Series
  • Prices rose on one day
  • Sales went up
  • But they were rising already

Accounting for the trend

We simulate two years of weekly sales, with a rising trend, a season, and a true jump of sixty at week seventy. First, the naive comparison, before against after. Then a regression with the trend, the season, and a step for the change. Run it. Before and after says a hundred and ninety four. That's mostly just the trend. With the trend and the season taken out, the step is fifty six, close to the true sixty. Same data, very different story. Whenever someone quotes a before and after, ask about the trend.

interrupted.pypython
import numpy as np
import pandas as pd
import statsmodels.formula.api as smf

rng = np.random.default_rng(0)
t = np.arange(104)
after = (t >= 70).astype(int)
sales = 1000 + 3 * t + 60 * after + 40 * np.sin(t * 2 * np.pi / 52) + rng.normal(0, 15, 104)
df = pd.DataFrame({"t": t, "after": after, "sales": sales, "s": np.sin(t * 2 * np.pi / 52)})

print("Before vs after:", round(df[df.after == 1].sales.mean() - df[df.after == 0].sales.mean()))
m = smf.ols("sales ~ t + after + s", df).fit()
print("With trend and season:", round(m.params["after"]), "(true 60)")
Output
Before vs after: 194
With trend and season: 56 (true 60)

Summary

Separate the trend from the change. Next, what should you do with extreme values?

Key takeaways
  • Before-and-after misleads
  • Model trend and season
  • The step is the effect
Next up: Handling Outliers — delete, keep, or fix?