Analyze the effect of an intervention on a time series using covariates with Google's CausalImpact (TensorFlow port) — all through a simple Streamlit UI.
# 1) Install deps (ideally in a fresh venv)
pip install -r requirements.txt
# 2) Launch the app
streamlit run app.pyThen open the local URL printed in your terminal (usually http://localhost:8501).
- Lets you upload a CSV or Excel file containing a date column, a response variable, and optional control variables.
- Helps you pick an intervention date and pre/post periods.
- Runs CausalImpact to estimate the impact and shows:
- A short summary
- A more detailed report
- A visual plot of actual vs. counterfactual
Under the hood it uses
tfcausalimpactviafrom causalimpact import CausalImpact, with Streamlit and Matplotlib for the interface and charts.
- CSV (
.csv) - Excel (
.xlsx)
- At least one column with "date" in its header (case-insensitive), e.g.
Date,DATE,event_date, etc. - A response (dependent) variable column (e.g.
y,sales,visitors). - Optional control variables (explanatory covariates).
- Best supplied as numeric
YYYYMMDD(e.g.20240131).
The app will convert these to real dates for you.
- If your Excel file has top matter (titles, notes, logos), that's fine — the app skips early rows and drops all-empty columns automatically.
- Make sure the first row after any top matter contains actual headers for each column.
date,y,x1,x2
20240101,100,23,0.7
20240102,98,20,0.8
20240103,110,21,0.6
...- Start the app:
streamlit run app.py - Upload your CSV/Excel file.
- Pick the date column when prompted.
- Choose:
- the response column (what you’re measuring the impact on),
- any control columns (covariates).
- Set the intervention date (the date the change/treatment happened).
- Confirm pre/post periods (defaults are pre = start → day before intervention; post = from intervention → last date).
- Click Run Causal Impact Analysis.
- Read the summary, detailed report, and inspect the plot.
Tip: Pre/post periods must exist in your data. Controls should be available across the full pre & post windows.
- Pointwise & cumulative effects: How the response diverged from the counterfactual.
- Posterior intervals: Uncertainty bands for effects.
- Summary/report: Text interpretation from the model.
If your controls don’t explain the response well in the pre-period, the counterfactual may be weak. Try better covariates or a longer pre-period.
- Python 3.9+ recommended
pip
python -m venv .venv # optional but recommended
# Windows: .venv\Scripts\activate
# macOS/Linux: source .venv/bin/activate
pip install -r requirements.txt
streamlit run app.pyThe app will print a local URL (typically http://localhost:8501).
Build the image:
docker build -t causal-impact-app .Run the container:
docker run -p 8501:8501 causal-impact-appThen open http://localhost:8501 in your browser.
If deploying to platforms that expect port 8080 (e.g., Cloud Run), ensure the container is configured to serve on
0.0.0.0:8080.
Make sure you’re authenticated (gcloud auth login), have a project selected, and Artifact Registry & Cloud Run are enabled. Then run:
gcloud builds submit --tag europe-west2-docker.pkg.dev/PROJECT_ID/streamlit-repo/streamlit-app && gcloud run deploy streamlit-app --image europe-west2-docker.pkg.dev/PROJECT_ID/streamlit-repo/streamlit-app --platform managed --region europe-west2 --allow-unauthenticated --memory=2Gi --max-instances=1Replace PROJECT_ID with your actual Google Cloud project ID.
Notes
- The single-line command above works in Windows CMD, PowerShell, and macOS/Linux shells.
- First run may take several minutes while the image builds and pushes to Artifact Registry.
-
“No column with 'date' found”
Ensure at least one header contains the word “date” (any case). -
Dates aren’t recognized
Provide numericYYYYMMDDvalues (or clean your date column before upload). -
Empty or malformed data
Check that your first actual row after any prelude rows contains real headers and that columns aren’t entirely empty. -
Plot doesn’t render
Try a smaller date range, ensure data types are numeric where expected, and that pre/post windows exist in your index. -
Weird results
Try improving control variables, lengthening the pre-period, or removing noisy controls.
- Streamlit UI
- tfcausalimpact for Bayesian structural time-series
- pandas / numpy for data handling
- matplotlib for charts
Uploaded files are processed in-memory by the app for the duration of your session; the app doesn’t write your data to disk unless you modify the code to do so.
Add your preferred license here (e.g., MIT).
- Google’s original CausalImpact methodology
- The community
tfcausalimpactimplementation