Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/deprecated/notebooks/causal_impact_tfp.ipynb
1192 views
Kernel: Python 3

Open In Colab

Setup

!pip install tfcausalimpact
Collecting tfcausalimpact Downloading tfcausalimpact-0.0.9.tar.gz (34 kB) Requirement already satisfied: jinja2 in /usr/local/lib/python3.7/dist-packages (from tfcausalimpact) (2.11.3) Requirement already satisfied: pandas in /usr/local/lib/python3.7/dist-packages (from tfcausalimpact) (1.1.5) Requirement already satisfied: tensorflow in /usr/local/lib/python3.7/dist-packages (from tfcausalimpact) (2.7.0) Requirement already satisfied: tensorflow-probability>=0.14.0 in /usr/local/lib/python3.7/dist-packages (from tfcausalimpact) (0.15.0) Requirement already satisfied: matplotlib in /usr/local/lib/python3.7/dist-packages (from tfcausalimpact) (3.2.2) Requirement already satisfied: cloudpickle>=1.3 in /usr/local/lib/python3.7/dist-packages (from tensorflow-probability>=0.14.0->tfcausalimpact) (1.3.0) Requirement already satisfied: numpy>=1.13.3 in /usr/local/lib/python3.7/dist-packages (from tensorflow-probability>=0.14.0->tfcausalimpact) (1.19.5) Requirement already satisfied: gast>=0.3.2 in /usr/local/lib/python3.7/dist-packages (from tensorflow-probability>=0.14.0->tfcausalimpact) (0.4.0) Requirement already satisfied: dm-tree in /usr/local/lib/python3.7/dist-packages (from tensorflow-probability>=0.14.0->tfcausalimpact) (0.1.6) Requirement already satisfied: six>=1.10.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow-probability>=0.14.0->tfcausalimpact) (1.15.0) Requirement already satisfied: decorator in /usr/local/lib/python3.7/dist-packages (from tensorflow-probability>=0.14.0->tfcausalimpact) (4.4.2) Requirement already satisfied: absl-py in /usr/local/lib/python3.7/dist-packages (from tensorflow-probability>=0.14.0->tfcausalimpact) (0.12.0) Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.7/dist-packages (from jinja2->tfcausalimpact) (2.0.1) Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->tfcausalimpact) (3.0.6) Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib->tfcausalimpact) (0.11.0) Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->tfcausalimpact) (1.3.2) Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->tfcausalimpact) (2.8.2) Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.7/dist-packages (from pandas->tfcausalimpact) (2018.9) Requirement already satisfied: typing-extensions>=3.6.6 in /usr/local/lib/python3.7/dist-packages (from tensorflow->tfcausalimpact) (3.10.0.2) Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.21.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow->tfcausalimpact) (0.23.1) Requirement already satisfied: keras-preprocessing>=1.1.1 in /usr/local/lib/python3.7/dist-packages (from tensorflow->tfcausalimpact) (1.1.2) Requirement already satisfied: wrapt>=1.11.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow->tfcausalimpact) (1.13.3) Requirement already satisfied: flatbuffers<3.0,>=1.12 in /usr/local/lib/python3.7/dist-packages (from tensorflow->tfcausalimpact) (2.0) Requirement already satisfied: keras<2.8,>=2.7.0rc0 in /usr/local/lib/python3.7/dist-packages (from tensorflow->tfcausalimpact) (2.7.0) Requirement already satisfied: tensorflow-estimator<2.8,~=2.7.0rc0 in /usr/local/lib/python3.7/dist-packages (from tensorflow->tfcausalimpact) (2.7.0) Requirement already satisfied: tensorboard~=2.6 in /usr/local/lib/python3.7/dist-packages (from tensorflow->tfcausalimpact) (2.7.0) Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow->tfcausalimpact) (1.1.0) Requirement already satisfied: wheel<1.0,>=0.32.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow->tfcausalimpact) (0.37.1) Requirement already satisfied: google-pasta>=0.1.1 in /usr/local/lib/python3.7/dist-packages (from tensorflow->tfcausalimpact) (0.2.0) Requirement already satisfied: astunparse>=1.6.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow->tfcausalimpact) (1.6.3) Requirement already satisfied: grpcio<2.0,>=1.24.3 in /usr/local/lib/python3.7/dist-packages (from tensorflow->tfcausalimpact) (1.43.0) Requirement already satisfied: protobuf>=3.9.2 in /usr/local/lib/python3.7/dist-packages (from tensorflow->tfcausalimpact) (3.17.3) Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.7/dist-packages (from tensorflow->tfcausalimpact) (3.3.0) Requirement already satisfied: libclang>=9.0.1 in /usr/local/lib/python3.7/dist-packages (from tensorflow->tfcausalimpact) (12.0.0) Requirement already satisfied: h5py>=2.9.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow->tfcausalimpact) (3.1.0) Requirement already satisfied: cached-property in /usr/local/lib/python3.7/dist-packages (from h5py>=2.9.0->tensorflow->tfcausalimpact) (1.5.2) Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3.7/dist-packages (from tensorboard~=2.6->tensorflow->tfcausalimpact) (0.4.6) Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.7/dist-packages (from tensorboard~=2.6->tensorflow->tfcausalimpact) (3.3.6) Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.7/dist-packages (from tensorboard~=2.6->tensorflow->tfcausalimpact) (1.0.1) Requirement already satisfied: setuptools>=41.0.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard~=2.6->tensorflow->tfcausalimpact) (57.4.0) Requirement already satisfied: google-auth<3,>=1.6.3 in /usr/local/lib/python3.7/dist-packages (from tensorboard~=2.6->tensorflow->tfcausalimpact) (1.35.0) Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard~=2.6->tensorflow->tfcausalimpact) (1.8.1) Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard~=2.6->tensorflow->tfcausalimpact) (2.23.0) Requirement already satisfied: tensorboard-data-server<0.7.0,>=0.6.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard~=2.6->tensorflow->tfcausalimpact) (0.6.1) Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.7/dist-packages (from google-auth<3,>=1.6.3->tensorboard~=2.6->tensorflow->tfcausalimpact) (0.2.8) Requirement already satisfied: cachetools<5.0,>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from google-auth<3,>=1.6.3->tensorboard~=2.6->tensorflow->tfcausalimpact) (4.2.4) Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.7/dist-packages (from google-auth<3,>=1.6.3->tensorboard~=2.6->tensorflow->tfcausalimpact) (4.8) Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.7/dist-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard~=2.6->tensorflow->tfcausalimpact) (1.3.0) Requirement already satisfied: importlib-metadata>=4.4 in /usr/local/lib/python3.7/dist-packages (from markdown>=2.6.8->tensorboard~=2.6->tensorflow->tfcausalimpact) (4.10.0) Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.7/dist-packages (from importlib-metadata>=4.4->markdown>=2.6.8->tensorboard~=2.6->tensorflow->tfcausalimpact) (3.7.0) Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /usr/local/lib/python3.7/dist-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tensorboard~=2.6->tensorflow->tfcausalimpact) (0.4.8) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard~=2.6->tensorflow->tfcausalimpact) (2021.10.8) Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard~=2.6->tensorflow->tfcausalimpact) (3.0.4) Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard~=2.6->tensorflow->tfcausalimpact) (2.10) Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard~=2.6->tensorflow->tfcausalimpact) (1.24.3) Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.7/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard~=2.6->tensorflow->tfcausalimpact) (3.1.1) Building wheels for collected packages: tfcausalimpact Building wheel for tfcausalimpact (setup.py) ... done Created wheel for tfcausalimpact: filename=tfcausalimpact-0.0.9-py3-none-any.whl size=38366 sha256=7bb737a3311e9d06410e3de060d5b466e0b81334021768675c4664e528a55f16 Stored in directory: /root/.cache/pip/wheels/b2/df/96/8a0832fdb37824c5b6998d6aa32dab501110ab47dfc8df9d8e Successfully built tfcausalimpact Installing collected packages: tfcausalimpact Successfully installed tfcausalimpact-0.0.9
%matplotlib inline import sys import os # sys.path.append(os.path.abspath('../')) import numpy as np import pandas as pd import tensorflow as tf import tensorflow_probability as tfp import matplotlib.pyplot as plt from causalimpact import CausalImpact tfd = tfp.distributions plt.rcParams["figure.figsize"] = [15, 10]

Data

# This is modified from an example presented in Google's R code. # https://google.github.io/CausalImpact/CausalImpact.html#creating-an-example-dataset # The true generative process is y = 1.2 * x0 + 0.8 * x1 + 0 * x2 + random_walk # where x2 is an irrelevant input variable. # We simulate the impact of an intervention at I=70 by adding an offset of 10 (decreasing to 0) to the outcome y. T = 100 I = 70 observed_stddev, observed_initial = ( tf.convert_to_tensor(value=0.1, dtype=tf.float32), tf.convert_to_tensor(value=0.0, dtype=tf.float32), ) level_scale_prior = tfd.LogNormal(loc=tf.math.log(0.0001 * observed_stddev), scale=1, name="level_scale_prior") # level_scale_prior = tfd.LogNormal(loc=tf.math.log(0.05 * observed_stddev), scale=1, name='level_scale_prior') initial_state_prior = tfd.MultivariateNormalDiag( loc=observed_initial[..., tf.newaxis], scale_diag=(tf.abs(observed_initial) + observed_stddev)[..., tf.newaxis], name="initial_level_prior", ) ll_ssm = tfp.sts.LocalLevelStateSpaceModel( T, initial_state_prior=initial_state_prior, level_scale=level_scale_prior.sample() ) ll_ssm_sample = np.squeeze(ll_ssm.sample().numpy()) np.random.seed(0) x0 = 1 * np.random.rand(T) x1 = 2 * np.random.rand(T) x2 = 3 * np.random.rand(T) y = 2 * x0 + 3 * x1 + ll_ssm_sample # intervention offset = 5 # y[I:] += offset duration = 10 y[I : (I + duration)] += np.linspace(offset, 0, num=duration) # decreasing effect data = pd.DataFrame({"x0": x0, "x1": x1, "x2": x2, "y": y}, columns=["y", "x0", "x1", "x2"])
Image in a Jupyter notebook
data.plot() plt.axvline(I - 1, linestyle="--", color="k") plt.legend() plt.savefig("causal-impact-data.pdf")
Image in a Jupyter notebook

Fit default model

pre_period = [0, I - 1] post_period = [I, T - 1] ci = CausalImpact(data, pre_period, post_period)
WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass. WARNING:tensorflow:@custom_gradient grad_fn has 'variables' in signature, but no ResourceVariables were used on the forward pass.
print(ci.model_samples.keys()) # We sample 100 times from the variational posterior of each parameter # https://github.com/WillianFuks/tfcausalimpact/blob/master/causalimpact/model.py#L378 for name, values in ci.model_samples.items(): print(f"{name}: {values.numpy().mean(axis=0)}")
odict_keys(['observation_noise_scale', 'LocalLevel/_level_scale', 'SparseLinearRegression/_global_scale_variance', 'SparseLinearRegression/_global_scale_noncentered', 'SparseLinearRegression/_local_scale_variances', 'SparseLinearRegression/_local_scales_noncentered', 'SparseLinearRegression/_weights_noncentered']) observation_noise_scale: 0.34998977184295654 LocalLevel/_level_scale: 0.010700251907110214 SparseLinearRegression/_global_scale_variance: 1.5136687755584717 SparseLinearRegression/_global_scale_noncentered: 1.5066444873809814 SparseLinearRegression/_local_scale_variances: [1.0874447 2.730983 0.8268008] SparseLinearRegression/_local_scales_noncentered: [1.2025315 1.7309521 0.44093436] SparseLinearRegression/_weights_noncentered: [ 1.4927632 1.945029 -0.10319362]
ci.plot(show=False) fig = plt.gcf() fig.savefig("causal-impact-inferences.pdf")
Image in a Jupyter notebook
print(ci.summary())
Posterior Inference {Causal Impact} Average Cumulative Actual 4.21 126.28 Prediction (s.d.) 3.34 (0.18) 100.14 (5.26) 95% CI [3.0, 3.69] [90.03, 110.63] Absolute effect (s.d.) 0.87 (0.18) 26.14 (5.26) 95% CI [0.52, 1.21] [15.65, 36.25] Relative effect (s.d.) 26.1% (5.25%) 26.1% (5.25%) 95% CI [15.63%, 36.2%] [15.63%, 36.2%] Posterior tail-area probability p: 0.0 Posterior prob. of a causal effect: 100.0% For more details run the command: print(impact.summary('report'))