Path: blob/master/deprecated/notebooks/causal_impact_tfp.ipynb
1192 views
Kernel: Python 3
Causal Impact as implemented on top of TFP SSM library
https://github.com/WillianFuks/tfcausalimpact/blob/master/notebooks/getting_started.ipynb
Setup
In [1]:
Out[1]:
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
In [2]:
Data
In [22]:
Out[22]:
In [36]:
Out[36]:
Fit default model
In [23]:
Out[23]:
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.
In [33]:
Out[33]:
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]
In [39]:
Out[39]:
In [ ]:
In [35]:
Out[35]:
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'))