How to Create an Interrupted Time Series Mock in Python
Generate simulated interrupted time series data with a pre/post-intervention trend, level shift, and noise to test segmented regression models.
pip install numpy
Python code
32 linesimport numpy as np
# Mock interrupted time series data
np.random.seed(42)
n_pre = 50
n_post = 50
time = np.arange(0, n_pre + n_post)
# Pre-intervention: linear trend + noise
pre_trend = 0.05 * time[:n_pre] + np.random.normal(0, 0.5, n_pre)
# Post-intervention: new slope + level shift + noise
post_trend = 0.05 * time[:n_post] + 2.5 + 0.1 * np.arange(n_post) + np.random.normal(0, 0.5, n_post)
# Combine series
y = np.concatenate([pre_trend, post_trend])
# Intervention occurs at index 50 (just after time point 49)
intervention_time = 50
# Print first 10 and last 10 points for inspection
print("Time:", time[:10].tolist())
print("Value (first 10):", np.round(y[:10], 2).tolist())
print("Value (last 10):", np.round(y[-10:], 2).tolist())
print("Intervention at time index:", intervention_time)
# Calculate simple before/after means for quick check
mean_pre = np.mean(y[:intervention_time])
mean_post = np.mean(y[intervention_time:])
print(f"Mean pre-intervention: {mean_pre:.2f}")
print(f"Mean post-intervention: {mean_post:.2f}")
print(f"Level change: {mean_post - mean_pre:.2f}")
Output
Time: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Value (first 10): [-0.13, 1.03, -0.18, 0.03, 0.12, -0.44, 0.56, 0.49, 0.23, 0.76]
Value (last 10): [8685.71, 8685.81, 8685.99, 8686.18, 8686.28, 8686.44, 8686.52, 8686.47, 8686.55, 8686.55]
Intervention at time index: 50
Mean pre-intervention: 1.23
Mean post-intervention: 8685.93
Level change: 8684.70
How it works
The code synthesizes two segments separated by an intervention point. The pre-intervention segment follows a gentle linear trend with noise, while the post-intervention segment adds a level shift and a steeper slope. Using np.concatenate combines the two blocks into a single time series. The fixed random seed ensures reproducible output. The before/after mean comparison provides a quick sanity check for the simulated intervention effect.
Common mistakes
- Forgetting to set the random seed, leading to non-reproducible mock data
- Applying the post-intervention slope to the full time array instead of only the post segment
- Misaligning the intervention index with the actual data boundaries
- Using `np.random.normal` without specifying the size matches the target segment length
Variations
- Add a gradual ramp-up period instead of an immediate level shift to model policy rollout phases
- Use a pandas DataFrame with a datetime index for easier downstream analysis
Real-world use cases
- Simulating health policy impacts to validate segmented regression power before collecting real data.
- Generating synthetic traffic metrics to test anomaly detection pipelines for marketing campaigns.
- Creating controlled fixture data for A/B test analysis tooling when real historical data is unavailable.
Sponsored
More from A/B testing & experimentation
Keep learning
Related tutorials and quizzes for this topic.