Skip to content

Connective3Innovation/causal-impact-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📈 Causal Impact Analysis Dashboard (Streamlit)

Analyze the effect of an intervention on a time series using covariates with Google's CausalImpact (TensorFlow port) — all through a simple Streamlit UI.


TL;DR (Quickstart)

# 1) Install deps (ideally in a fresh venv)
pip install -r requirements.txt

# 2) Launch the app
streamlit run app.py

Then open the local URL printed in your terminal (usually http://localhost:8501).


What this app does

  • 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 tfcausalimpact via from causalimpact import CausalImpact, with Streamlit and Matplotlib for the interface and charts.


Data format & tips

Accepted files

  • CSV (.csv)
  • Excel (.xlsx)

Required columns

  • 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).

Date values

  • Best supplied as numeric YYYYMMDD (e.g. 20240131).
    The app will convert these to real dates for you.

Special notes for Excel

  • 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.

Minimal example (CSV)

date,y,x1,x2
20240101,100,23,0.7
20240102,98,20,0.8
20240103,110,21,0.6
...

How to use the app

  1. Start the app: streamlit run app.py
  2. Upload your CSV/Excel file.
  3. Pick the date column when prompted.
  4. Choose:
    • the response column (what you’re measuring the impact on),
    • any control columns (covariates).
  5. Set the intervention date (the date the change/treatment happened).
  6. Confirm pre/post periods (defaults are pre = start → day before intervention; post = from intervention → last date).
  7. Click Run Causal Impact Analysis.
  8. 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.


Interpreting results (at a glance)

  • 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.


Local development

Prereqs

  • Python 3.9+ recommended
  • pip

Setup

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.py

The app will print a local URL (typically http://localhost:8501).


Run with Docker (local)

Build the image:

docker build -t causal-impact-app .

Run the container:

docker run -p 8501:8501 causal-impact-app

Then 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.


Deploy to Google Cloud Run (one-liner)

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=1

Replace 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.

Troubleshooting

  • “No column with 'date' found”
    Ensure at least one header contains the word “date” (any case).

  • Dates aren’t recognized
    Provide numeric YYYYMMDD values (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.


Tech stack

  • Streamlit UI
  • tfcausalimpact for Bayesian structural time-series
  • pandas / numpy for data handling
  • matplotlib for charts

Privacy & data handling

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.


License

Add your preferred license here (e.g., MIT).


Credits

  • Google’s original CausalImpact methodology
  • The community tfcausalimpact implementation

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors