When you ship something to everyone at once, there is no control group, and the obvious analysis is to compare the average before to the average after. If the metric was already trending or has seasonality, that comparison is wrong. An interrupted time series (ITS) fixes it by fitting the pre-launch trend, projecting it forward as the counterfactual, and measuring the gap. In Python this is an ordinary smf.ols regression with a step term and a slope-change term, using cov_type="HAC" for autocorrelated errors. The code targets statsmodels 0.14.
Quick Example
A product ships a new onboarding flow at week 60. Weekly signups were already growing, with a yearly seasonal cycle. The true effect is a jump of +25 signups per week that fades by 0.2 per week:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import statsmodels.formula.api as smf
rng = np.random.default_rng(54)
weeks = np.arange(104)
launch = 60
season = 14 * np.sin(2 * np.pi * weeks / 52)
noise = np.zeros(len(weeks))
for i in range(1, len(weeks)): # AR(1) noise
noise[i] = 0.5 * noise[i - 1] + rng.normal(0, 5)
post = (weeks >= launch).astype(int)
since = np.where(post == 1, weeks - launch, 0)
signups = 300 + 1.0 * weeks + season + 25 * post - 0.2 * since + noise
df = pd.DataFrame({"week": weeks, "signups": signups, "post": post, "since": since,
"sin": np.sin(2 * np.pi * weeks / 52), "cos": np.cos(2 * np.pi * weeks / 52)})
naive = df.signups[df.post == 1].mean() - df.signups[df.post == 0].mean()
its = smf.ols("signups ~ week + post + since + sin + cos", df).fit(
cov_type="HAC", cov_kwds={"maxlags": 4})
ci = its.conf_int()
print(f"naive before/after: {naive:.1f}")
print(f"level change (post): {its.params['post']:.1f} 95% CI {ci.loc['post'].round(1).tolist()}")
print(f"slope change (since): {its.params['since']:.2f} 95% CI {ci.loc['since'].round(2).tolist()}")
The naive comparison says the launch added 71.4 signups per week. The ITS model says the immediate jump was 26.8 (95% CI 20.1 to 33.6), close to the true 25. The naive number is more than 2.5 times too big because signups were rising by about one per week before the launch, so any later period looks higher than any earlier one.
Why Does a Simple Before/After Comparison Mislead?
A before/after comparison attributes every difference between the two periods to the launch, including:
- A pre-existing trend. If the metric was already climbing, the "after" average is higher regardless of the launch. This is the bulk of the error here.
- Seasonality. Launching in a high season makes the "after" period look better.
- Other changes at the same time. Pricing, marketing, or a competitor's move. ITS can't remove these, but it can show whether the jump lines up with the launch date.
- Regression to the mean. Launching in response to a bad week and then seeing the metric recover.
ITS handles the first two by modeling them. It cannot fix the third and fourth, so it is weaker evidence than a randomized test, but it is much better than comparing averages.
What Is a Segmented Regression Model?
The model is signups ~ week + post + since + sin + cos:
weekis the underlying trend (about +0.96 per week here).postis a 0/1 step that turns on at launch. Its coefficient is the level change: the immediate jump.sincecounts weeks since launch (0 before it). Its coefficient is the slope change: how the trend bends afterward.sinandcoscapture a yearly cycle with one pair of Fourier terms. Without them the seasonal swing leaks into the other coefficients.
The slope change here is -0.18 per week with a confidence interval of -0.44 to 0.08, which includes zero. With 44 post-launch weeks the data can't tell whether the effect fades or holds steady. That is a useful thing for the model to say.
Plot the Counterfactual
To see the effect, predict twice: once with the real post/since values, and once with them set to zero (what the model expects had the launch never happened):
cf = df.copy()
cf["post"], cf["since"] = 0, 0
df["fitted"] = its.predict(df)
df["counterfactual"] = its.predict(cf)
fig, ax = plt.subplots(figsize=(8, 4.8))
ax.plot(df.week, df.signups, color="#94a3b8", lw=1.2, marker="o", ms=3, label="Weekly signups")
ax.plot(df.week[df.post == 0], df.fitted[df.post == 0], color="#0ea5e9", lw=2.5, label="Fitted, before launch")
ax.plot(df.week[df.post == 1], df.fitted[df.post == 1], color="#16a34a", lw=2.5, label="Fitted, after launch")
ax.plot(df.week[df.post == 1], df.counterfactual[df.post == 1], color="#f97316", lw=2.5, ls="--",
label="Counterfactual (no launch)")
ax.axvline(launch - 0.5, color="#0f172a", lw=1, ls=":")
ax.set(xlabel="Week", ylabel="Signups per week",
title="Interrupted time series: weekly signups and the launch")
ax.legend(loc="upper left")

The dashed orange line is the model's forecast of what signups would have been, continuing the pre-launch trend and seasonal pattern. The vertical gap between it and the green fitted line is the estimated effect at each week. It starts at 26.8 at launch and narrows slightly to 19.1 by week 103, following the (statistically uncertain) negative slope change.
Which Effect Should I Report?
The effect is a path, not one number, so say which summary you mean:
gap = df.fitted - df.counterfactual
print("at launch: ", round(gap[df.week == launch].iloc[0], 1))
print("average after launch:", round(gap[df.post == 1].mean(), 1))
print("last week: ", round(gap[df.week == 103].iloc[0], 1))

| Summary | Signups per week |
|---|---|
| Naive before/after | 71.4 |
| ITS, effect at launch (level change) | 26.8 |
| ITS, average effect over the 44 post-launch weeks | 23.0 |
| ITS, effect in the last week | 19.1 |
Report the level change for the immediate effect, and the average gap (or the cumulative gap) for the total benefit. A question like "how many extra signups did we get?" is the average gap times 44 weeks, about 1,010.
Why Use HAC (Newey-West) Standard Errors?
Time series errors are usually autocorrelated: a high week tends to be followed by another high week. Ordinary OLS standard errors assume independence and come out too small, which makes weak effects look significant. cov_type="HAC", cov_kwds={"maxlags": 4} uses Newey-West standard errors, which allow for autocorrelation up to the stated lag. The coefficients are the same as plain OLS and only the standard errors change.
Choose maxlags from the data's rhythm. A few lags is typical for weekly data. For monthly data with 12-month seasonality, use more, or add seasonal terms as above and check the residual autocorrelation. For short series (under about 24 points) these corrections are less reliable.
How Much Data Do I Need?
There is no fixed minimum. A common recommendation is at least 8 to 12 points before and after the interruption, and more if there is seasonality: you want at least one full seasonal cycle before the launch so the model can learn the pattern. The pre-launch period is what builds the counterfactual, so a long, stable pre-period matters more than a long post-period.
Which Method Fits My Situation?
| Situation | Method |
|---|---|
| Launch to everyone, one time series, no comparison group | Interrupted time series (this post) |
| Rollout to some users or regions, others untouched | Difference-in-differences |
| Rollout by a score or threshold | Regression discontinuity |
| Users chose the treatment themselves | Propensity scores |
| You can randomize | An A/B test |
Practical Tips
- Fix the launch date before looking at the data. Searching for the date where a jump looks biggest finds noise.
- Plot first. If the jump isn't visible in the raw series, be skeptical of a significant coefficient.
- Allow for a ramp-up. If the effect builds over a few weeks, drop a short transition window after launch, or model the ramp.
- Add a comparison series when you have one. A similar metric that the launch shouldn't affect (a "control series") protects against coincident events and turns the analysis into a difference-in-differences.
- Don't extrapolate far. The counterfactual gets less reliable the further it runs from the last pre-launch data.