Skip to main content

# Causal Impact [![Build Status](https://travis-ci.com/dafiti/causalimpact.svg?branch=master)](https://travis-ci.com/dafiti/causalimpact) [![Coverage Status](https://coveralls.io/repos/github/dafiti/causalimpact/badge.svg?branch=master)](https://coveralls.io/github/dafiti/causalimpact?branch=master) [![PyPI version](https://badge.fury.io/py/pycausalimpact.svg)](https://badge.fury.io/py/pycausalimpact) [![Pyversions](https://img.shields.io/pypi/pyversions/pycausalimpact.svg)](https://pypi.python.org/pypi/pycausalimpact) [![GitHub license](https://img.shields.io/github/license/dafiti/causalimpact.svg)](https://github.com/dafiti/causalimpact/blob/master/LICENSE)

This repository is a Python version of [Google’s Causal Impact](https://github.com/google/CausalImpact) model with all functionalities fully ported and tested.

## How it works The main goal of the algorithm is to infer the expected effect a given intervention (or any action) had on some response variable by analyzing differences between expected and observed time series data.

Data is divided in two parts: the first one is what is known as the “pre-intervention” period and the concept of [Bayesian Structural Time Series](https://en.wikipedia.org/wiki/Bayesian_structural_time_series) is used to fit a model that best explains what has been observed. The fitted model is used in the second part of data (“post-intervention” period) to forecast what the response would look like had the intervention not taken place. The inferences are based on the differences between observed response to the predicted one which yields the absolute and relative expected effect the intervention caused on data.

The model makes as assumption (which is recommended to be confirmed in your data) that the response variable can be precisely modeled by a linear regression with what is known as “covariates” (or X) that must not be affected by the intervention that took place (for instance, if a company wants to infer what impact a given marketing campaign will have on its “revenue”, then its daily “visits” cannot be used as a covariate as probably the total visits might be affected by the campaign.

The model is more commonly used to infer the impact that marketing interventions have on businesses such as the expected revenue associated to a given campaign or even to assert more precisely the revenue a given channel brings in by completely turning it off (also known as “hold-out” tests). It’s important to note though that the model can be extensively used in different areas and subjects; any intervention on time series data can potentially be modeled and inferences be made upon observed and predicted data.

Please refer to <a href=http://nbviewer.jupyter.org/github/dafiti/causalimpact/blob/master/examples/getting_started.ipynb>getting started</a> in the examples folder for more information.

## Instalation

pip install pycausalimpact

or (recommended):

pipenv install pycausalimpact

## Requirements

  • python3

  • numpy

  • scipy

  • statsmodels 0.9.0

  • matplotlib

  • jinja2

## Getting Started We recommend this [presentation](https://www.youtube.com/watch?v=GTgZfCltMm8) by Kay Brodersen (one of the creators of the causal impact implementation in R).

We also created this introductory [ipython notebook](http://nbviewer.jupyter.org/github/dafiti/causalimpact/blob/master/examples/getting_started.ipynb) with examples of how to use this package.

### Simple Example Here’s a simple example (which can also be found in the original Google’s R implementation) running in Python:

`python import numpy as np import pandas as pd from statsmodels.tsa.arima_process import ArmaProcess from causalimpact import CausalImpact np.random.seed(12345) ar = np.r_[1, 0.9] ma = np.array([1]) arma_process = ArmaProcess(ar, ma) X = 100 + arma_process.generate_sample(nsample=100) y = 1.2 * X + np.random.normal(size=100) y[70:] += 5 data = pd.DataFrame({'y': y, 'X': X}, columns=['y', 'X']) pre_period = [0, 69] post_period = [70, 99] ci = CausalImpact(data, pre_period, post_period) print(ci.summary()) ci.plot() `

![alt text](https://raw.githubusercontent.com/dafiti/causalimpact/master/examples/ci_plot.png)

## Contributing, Bugs, Questions Contributions are more than welcome! If you want to propose new changes, fix bugs or improve something feel free to fork the repository and send us a Pull Request. You can also open new Issues for reporting bugs and general problems.

Release files for pycausalimpact 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pycausalimpact 0.0.6
File Size Uploaded
pycausalimpact-0.0.6.tar.gz 42.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pycausalimpact 0.0.6
File Interpreter ABI Platform
pycausalimpact-0.0.6-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 73.0 kB

Release files / pycausalimpact-0.0.6.tar.gz

Download URL pycausalimpact-0.0.6.tar.gz
Size 42.9 kB
Tags Source
SHA-256 checksum
How to use checksums
af64d3f8bc518f29f22eb665860147a06304691f77dec1fea041536371318cb4
BLAKE2b-256 checksum
How to use checksums
5b18f022b985166dd5ce96308ed5064dbd5f397c731a2b5e5a5f46c9bf81c2e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.9.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.2

Release files / pycausalimpact-0.0.6-py2.py3-none-any.whl

Download URL pycausalimpact-0.0.6-py2.py3-none-any.whl
Size 30.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
8881f505008eebe63098257d53ba9722118cd388982db79f38299375c2f57ba5
BLAKE2b-256 checksum
How to use checksums
d7b440ae8a701b48bdec464833e6f86e6dfa76be464adf3e0d2094f007db6278
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.9.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.2

Release history Release notifications | RSS feed

0.1.1

2 release files

0.1.0

2 release files

0.0.16

2 release files

0.0.15

2 release files

0.0.14

2 release files

0.0.13

2 release files

0.0.12

2 release files

0.0.10

2 release files

0.0.8

2 release files

0.0.7

5 release files

This release

0.0.6 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page