SlideShare a Scribd company logo
1
Causal Inference in R
Ana Daglis, Farfetch
2
Farfetch
Customer
Customer
Customer
Customer
Customer
Boutique
Boutique
Boutique
Boutique
Boutique
3
One of the most common questions we face in
marketing is measuring the incremental effects
● How much incremental revenue did the new pricing
strategy drive?
● What impact did the new feature on the website have?
● How many incremental conversions were achieved by
increasing the commission rate for our affiliates?
● …
4
The main gold standard method for estimating causal
effects is a randomised experiment
10%
Conversion
15%
Conversion
50% of visitors see Version B
Version B
Version A
50% of visitors see Version A
5
However, often A/B tests are either too expensive to run
or cannot be run, e.g. due to legal reasons
15%
Conversion
100% of visitors see Version B
Version B
Version A
6
Example: financial performance of a company A
80
120
160
200
2011 2012 2013 2014 2015 2016 2017
Date
Adjusted
Closing
Price
Scandal broke
Actual share
price
7
Approach: estimate the share price had the scandal not
happened
100
150
200
2011 2012 2013 2014 2015 2016 2017
Date
Adjusted
Closing
Price
Scandal broke
Actual share
price
Predicted
share price
8
By comparing the actual and predicted share price, we
can estimate the drop in stock value due to the scandal
100
150
200
2011 2012 2013 2014 2015 2016 2017
Date
Adjusted
Closing
Price
Scandal broke
Drop in stock
value due to
scandal
Actual share
price
Predicted
share price
9
Thanks to a fully Bayesian approach, we can quantify
the confidence level of our predictions
100
150
200
250
2011 2012 2013 2014 2015 2016 2017
Day
Clicks
Scandal broke
Actual share
price
Predicted
share price
95% credible
interval
10
How do we construct the counterfactual estimate?
50
100
150
200
250
2011 2012 2013 2014 2015 2016 2017
Date
Adjusted
Closing
Price
Actual share
price
Predicted
share price
95% credible
interval
Company B
share price
Company C
share price
Training Prediction
Scandal broke
11
Causal Impact methodology is based on a Bayesian
structural time series model
𝑦𝑡 = 𝑍𝑡
𝑇
𝛼𝑡 + 𝜀𝑡
𝛼𝑡+1 = 𝑇𝑡
𝑇
𝛼𝑡 + 𝑅𝑡𝜂𝑡
Causal Impact model
Most general form of the model
Observation equation
State equation
𝑦𝑡 = 𝜇𝑡 + 𝜏𝑡 + 𝑥𝑡
𝑇
𝛽 + 𝜀𝑡
𝜇𝑡+1 = 𝜇𝑡 + 𝛿𝑡 + 𝜂𝜇,𝑡
𝛿𝑡+1 = 𝛿𝑡 + 𝜂𝛿,𝑡
𝜏𝑡+1 = −
𝑖=0
𝑆−2
𝜏𝑡−𝑖 + 𝜂𝜏,𝑡
12
The model has 5 main parameters: 4 variance terms
𝝈𝜺
𝟐
, 𝝈𝝁
𝟐
, 𝝈𝜹
𝟐
, 𝝈𝝉
𝟐
and regression coefficients 𝜷
𝑦𝑡 = 𝜇𝑡 + 𝜏𝑡 + 𝑥𝑡
𝑇
𝛽 + 𝜀𝑡
𝜇𝑡+1 = 𝜇𝑡 + 𝛿𝑡 + 𝜂𝜇,𝑡
𝛿𝑡+1 = 𝛿𝑡 + 𝜂𝛿,𝑡
𝜏𝑡+1 = −
𝑖=0
𝑆−2
𝜏𝑡−𝑖 + 𝜂𝜏,𝑡
~𝒩 0, 𝜎𝜀
2
~𝒩 0, 𝜎𝜇
2
~𝒩 0, 𝜎𝛿
2
~𝒩 0, 𝜎𝜏
2
13
We impose an inv-gamma prior on 𝝈𝜺
𝟐
, with parameters 𝒔𝜺
and 𝒗𝜺 selected based on the expected goodness-of-fit
𝑦𝑡 = 𝜇𝑡 + 𝜏𝑡 + 𝑥𝑡
𝑇
𝛽 + 𝜀𝑡
𝜇𝑡+1 = 𝜇𝑡 + 𝛿𝑡 + 𝜂𝜇,𝑡
𝛿𝑡+1 = 𝛿𝑡 + 𝜂𝛿,𝑡
𝜏𝑡+1 = −
𝑖=0
𝑆−2
𝜏𝑡−𝑖 + 𝜂𝜏,𝑡
~𝒩 0, 𝜎𝜀
2
~𝒩 0, 𝜎𝜇
2
~𝒩 0, 𝜎𝛿
2
~𝒩 0, 𝜎𝜏
2
Priors
𝜎𝜀
2
~ 𝐼𝑛𝑣−𝐺𝑎𝑚𝑚𝑎 𝑠𝜀, 𝑣𝜀
0
1
2
3
4
0 1 2 3
x
Probability
Density
a = 1, b = 1
a = 2, b = 1
a = 3, b = 1
a = 3, b = 0.5
Inv−Gamma(a,b) density for varying values of a and b
14
We impose weak priors on 𝝈𝝁
𝟐, 𝝈𝜹
𝟐
and 𝝈𝝉
𝟐 reflecting the
assumption that errors are small in the state process
𝑦𝑡 = 𝜇𝑡 + 𝜏𝑡 + 𝑥𝑡
𝑇
𝛽 + 𝜀𝑡
𝜇𝑡+1 = 𝜇𝑡 + 𝛿𝑡 + 𝜂𝜇,𝑡
𝛿𝑡+1 = 𝛿𝑡 + 𝜂𝛿,𝑡
𝜏𝑡+1 = −
𝑖=0
𝑆−2
𝜏𝑡−𝑖 + 𝜂𝜏,𝑡
~𝒩 0, 𝜎𝜀
2
~𝒩 0, 𝜎𝜇
2
~𝒩 0, 𝜎𝛿
2
~𝒩 0, 𝜎𝜏
2
Priors
𝜎𝜇
2
, 𝜎𝛿
2
, 𝜎𝜏
2
~ 𝐼𝑛𝑣−𝐺𝑎𝑚𝑚𝑎 1, 0.01 × 𝑉𝑎𝑟(𝑦)
0
1
2
3
4
0 1 2 3
x
Probability
Density
a = 1, b = 1
a = 2, b = 1
a = 3, b = 1
a = 3, b = 0.5
Inv−Gamma(a,b) density for varying values of a and b
15
We let the model choose an appropriate set of controls
by placing a spike and slab prior over coefficients 𝜷
𝑦𝑡 = 𝜇𝑡 + 𝜏𝑡 + 𝑥𝑡
𝑇
𝛽 + 𝜀𝑡
𝜇𝑡+1 = 𝜇𝑡 + 𝛿𝑡 + 𝜂𝜇,𝑡
𝛿𝑡+1 = 𝛿𝑡 + 𝜂𝛿,𝑡
𝜏𝑡+1 = −
𝑖=0
𝑆−2
𝜏𝑡−𝑖 + 𝜂𝜏,𝑡
~𝒩 0, 𝜎𝜀
2
~𝒩 0, 𝜎𝜇
2
~𝒩 0, 𝜎𝛿
2
~𝒩 0, 𝜎𝜏
2
Priors
𝛽𝛾|𝜎𝜀
2
~ 𝒩(0, 𝑛𝜎𝜀
2
𝑋𝑇
𝑋 −1
)
𝑝 𝜚 ~
𝑗=1
𝐽
𝜋𝑗
𝜚𝑗
(1 − 𝜋𝑗)
𝜚𝑗
0
1
2
3
4
−2 −1 0 1 2
x
Probability
Density
Spike
Slab
Density functions of spike and slab priors
16
The inference can be performed in R with just 6 lines of
code
1 library(CausalImpact)
2 pre.period <- as.Date(c("2011-01-03", "2015-09-14"))
3 post.period <- as.Date(c("2015-09-21", "2017-03-19"))
4 impact <- CausalImpact(data, pre.period, post.period)
5 plot(impact)
6 summary(impact)
17
Results can be plotted and summarised in a table
original
pointwise
cumulative
2011 2012 2013 2014 2015 2016 2017
100
150
200
250
−80
−40
0
−4000
−3000
−2000
−1000
0
Date
Adjusted
Closing
Price
Cumulative panel only makes sense when the metric is
additive, such as clicks or the number of orders, but not
in the case when it is a share price
18
The package can even write a report for you!
19
Additional considerations
● It is important that covariates included in the model are not
themselves affected by the event. For each covariate included,
it is critical to reason why this is the case.
● The model can be validated by running the Causal Impact
analysis on an ‘imaginary event’ before the actual event. We
should not be seeing any significant effect, and actual and
predicted lines should match reasonably closely before the actual
event.
20
References
● K.H. Brodersen, F. Gallusser, J. Koehler, N. Remy, S. L. Scott,
(2015). Inferring Causal Impact Using Bayesian Structural Time-
Series Models.
https://research.google.com/pubs/pub41854.html.
● S. L. Scott, H. Varian, (2013). Predicting the Present with
Bayesian Structural Time Series.
https://people.ischool.berkeley.edu/~hal/Papers/2013/pred-
present-with-bsts.pdf.
21
Thank you!

More Related Content

Similar to Causal Inference in R

A PRACTICAL POWERFUL ROBUST AND INTERPRETABLE FAMILY OF CORRELATION COEFFICIE...
A PRACTICAL POWERFUL ROBUST AND INTERPRETABLE FAMILY OF CORRELATION COEFFICIE...A PRACTICAL POWERFUL ROBUST AND INTERPRETABLE FAMILY OF CORRELATION COEFFICIE...
A PRACTICAL POWERFUL ROBUST AND INTERPRETABLE FAMILY OF CORRELATION COEFFICIE...
Savas Papadopoulos, Ph.D
 
Lecture Notes: EEEC4340318 Instrumentation and Control Systems - Fundamental...
Lecture Notes:  EEEC4340318 Instrumentation and Control Systems - Fundamental...Lecture Notes:  EEEC4340318 Instrumentation and Control Systems - Fundamental...
Lecture Notes: EEEC4340318 Instrumentation and Control Systems - Fundamental...
AIMST University
 
Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...
Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...
Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...
height
 
Confirmatory Bayesian Online Change Point Detection in the Covariance Structu...
Confirmatory Bayesian Online Change Point Detection in the Covariance Structu...Confirmatory Bayesian Online Change Point Detection in the Covariance Structu...
Confirmatory Bayesian Online Change Point Detection in the Covariance Structu...
JeeyeonHan
 
Statistics Assignment Help
Statistics Assignment HelpStatistics Assignment Help
Statistics Assignment Help
Statistics Assignment Help
 
Pricing optimization poster version 2 (1)
Pricing optimization poster version 2 (1)Pricing optimization poster version 2 (1)
Pricing optimization poster version 2 (1)Alex Potocki
 
Doe introductionh
Doe introductionhDoe introductionh
Doe introductionh
sumandaspersonal
 
Statistical Process Control - SPC
Statistical Process Control - SPCStatistical Process Control - SPC
Statistical Process Control - SPC
Prasenjit Puri
 
Federated Tensor Factorization for Computational Phenotyping
Federated Tensor Factorization for Computational PhenotypingFederated Tensor Factorization for Computational Phenotyping
Federated Tensor Factorization for Computational Phenotyping
Yejin Kim
 
Py data19 final
Py data19   finalPy data19   final
Py data19 final
Maria Navarro Jiménez
 
Regression Analysis
Regression AnalysisRegression Analysis
Regression Analysis
Shiela Vinarao
 
Regression Analysis
Regression AnalysisRegression Analysis
Regression Analysis
Shiela Vinarao
 
Intro to Quantitative Investment (Lecture 4 of 6)
Intro to Quantitative Investment (Lecture 4 of 6)Intro to Quantitative Investment (Lecture 4 of 6)
Intro to Quantitative Investment (Lecture 4 of 6)
Adrian Aley
 
presentation_python_11_1569171345_375360.pptx
presentation_python_11_1569171345_375360.pptxpresentation_python_11_1569171345_375360.pptx
presentation_python_11_1569171345_375360.pptx
GAURAVRATHORE86
 
Curvefitting
CurvefittingCurvefitting
Curvefitting
Philberto Saroni
 
newmicrosoftofficepowerpointpresentation-150826055944-lva1-app6891.pdf
newmicrosoftofficepowerpointpresentation-150826055944-lva1-app6891.pdfnewmicrosoftofficepowerpointpresentation-150826055944-lva1-app6891.pdf
newmicrosoftofficepowerpointpresentation-150826055944-lva1-app6891.pdf
BiswajitPalei2
 
IE-002 Control Chart For Variables
IE-002 Control Chart For VariablesIE-002 Control Chart For Variables
IE-002 Control Chart For Variableshandbook
 
A Short Study of Galois Field
A Short Study of Galois FieldA Short Study of Galois Field
A Short Study of Galois Field
Hazratali Naim
 
Interpreting test score ~ Language Testing
Interpreting test score ~ Language Testing Interpreting test score ~ Language Testing
Interpreting test score ~ Language Testing
AndiSyafitriSyam
 
cat-quant-cheat-sheet
 cat-quant-cheat-sheet cat-quant-cheat-sheet
cat-quant-cheat-sheettechonomics1
 

Similar to Causal Inference in R (20)

A PRACTICAL POWERFUL ROBUST AND INTERPRETABLE FAMILY OF CORRELATION COEFFICIE...
A PRACTICAL POWERFUL ROBUST AND INTERPRETABLE FAMILY OF CORRELATION COEFFICIE...A PRACTICAL POWERFUL ROBUST AND INTERPRETABLE FAMILY OF CORRELATION COEFFICIE...
A PRACTICAL POWERFUL ROBUST AND INTERPRETABLE FAMILY OF CORRELATION COEFFICIE...
 
Lecture Notes: EEEC4340318 Instrumentation and Control Systems - Fundamental...
Lecture Notes:  EEEC4340318 Instrumentation and Control Systems - Fundamental...Lecture Notes:  EEEC4340318 Instrumentation and Control Systems - Fundamental...
Lecture Notes: EEEC4340318 Instrumentation and Control Systems - Fundamental...
 
Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...
Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...
Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...
 
Confirmatory Bayesian Online Change Point Detection in the Covariance Structu...
Confirmatory Bayesian Online Change Point Detection in the Covariance Structu...Confirmatory Bayesian Online Change Point Detection in the Covariance Structu...
Confirmatory Bayesian Online Change Point Detection in the Covariance Structu...
 
Statistics Assignment Help
Statistics Assignment HelpStatistics Assignment Help
Statistics Assignment Help
 
Pricing optimization poster version 2 (1)
Pricing optimization poster version 2 (1)Pricing optimization poster version 2 (1)
Pricing optimization poster version 2 (1)
 
Doe introductionh
Doe introductionhDoe introductionh
Doe introductionh
 
Statistical Process Control - SPC
Statistical Process Control - SPCStatistical Process Control - SPC
Statistical Process Control - SPC
 
Federated Tensor Factorization for Computational Phenotyping
Federated Tensor Factorization for Computational PhenotypingFederated Tensor Factorization for Computational Phenotyping
Federated Tensor Factorization for Computational Phenotyping
 
Py data19 final
Py data19   finalPy data19   final
Py data19 final
 
Regression Analysis
Regression AnalysisRegression Analysis
Regression Analysis
 
Regression Analysis
Regression AnalysisRegression Analysis
Regression Analysis
 
Intro to Quantitative Investment (Lecture 4 of 6)
Intro to Quantitative Investment (Lecture 4 of 6)Intro to Quantitative Investment (Lecture 4 of 6)
Intro to Quantitative Investment (Lecture 4 of 6)
 
presentation_python_11_1569171345_375360.pptx
presentation_python_11_1569171345_375360.pptxpresentation_python_11_1569171345_375360.pptx
presentation_python_11_1569171345_375360.pptx
 
Curvefitting
CurvefittingCurvefitting
Curvefitting
 
newmicrosoftofficepowerpointpresentation-150826055944-lva1-app6891.pdf
newmicrosoftofficepowerpointpresentation-150826055944-lva1-app6891.pdfnewmicrosoftofficepowerpointpresentation-150826055944-lva1-app6891.pdf
newmicrosoftofficepowerpointpresentation-150826055944-lva1-app6891.pdf
 
IE-002 Control Chart For Variables
IE-002 Control Chart For VariablesIE-002 Control Chart For Variables
IE-002 Control Chart For Variables
 
A Short Study of Galois Field
A Short Study of Galois FieldA Short Study of Galois Field
A Short Study of Galois Field
 
Interpreting test score ~ Language Testing
Interpreting test score ~ Language Testing Interpreting test score ~ Language Testing
Interpreting test score ~ Language Testing
 
cat-quant-cheat-sheet
 cat-quant-cheat-sheet cat-quant-cheat-sheet
cat-quant-cheat-sheet
 

Recently uploaded

一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
ewymefz
 
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
pchutichetpong
 
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTESAdjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
Subhajit Sahu
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
ewymefz
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
ahzuo
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Subhajit Sahu
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
John Andrews
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
axoqas
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
Subhajit Sahu
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
u86oixdj
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
haila53
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
dwreak4tg
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
u86oixdj
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
balafet
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Subhajit Sahu
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
Roger Valdez
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
Timothy Spann
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
axoqas
 

Recently uploaded (20)

一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
 
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTESAdjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
 

Causal Inference in R

  • 1. 1 Causal Inference in R Ana Daglis, Farfetch
  • 3. 3 One of the most common questions we face in marketing is measuring the incremental effects ● How much incremental revenue did the new pricing strategy drive? ● What impact did the new feature on the website have? ● How many incremental conversions were achieved by increasing the commission rate for our affiliates? ● …
  • 4. 4 The main gold standard method for estimating causal effects is a randomised experiment 10% Conversion 15% Conversion 50% of visitors see Version B Version B Version A 50% of visitors see Version A
  • 5. 5 However, often A/B tests are either too expensive to run or cannot be run, e.g. due to legal reasons 15% Conversion 100% of visitors see Version B Version B Version A
  • 6. 6 Example: financial performance of a company A 80 120 160 200 2011 2012 2013 2014 2015 2016 2017 Date Adjusted Closing Price Scandal broke Actual share price
  • 7. 7 Approach: estimate the share price had the scandal not happened 100 150 200 2011 2012 2013 2014 2015 2016 2017 Date Adjusted Closing Price Scandal broke Actual share price Predicted share price
  • 8. 8 By comparing the actual and predicted share price, we can estimate the drop in stock value due to the scandal 100 150 200 2011 2012 2013 2014 2015 2016 2017 Date Adjusted Closing Price Scandal broke Drop in stock value due to scandal Actual share price Predicted share price
  • 9. 9 Thanks to a fully Bayesian approach, we can quantify the confidence level of our predictions 100 150 200 250 2011 2012 2013 2014 2015 2016 2017 Day Clicks Scandal broke Actual share price Predicted share price 95% credible interval
  • 10. 10 How do we construct the counterfactual estimate? 50 100 150 200 250 2011 2012 2013 2014 2015 2016 2017 Date Adjusted Closing Price Actual share price Predicted share price 95% credible interval Company B share price Company C share price Training Prediction Scandal broke
  • 11. 11 Causal Impact methodology is based on a Bayesian structural time series model 𝑦𝑡 = 𝑍𝑡 𝑇 𝛼𝑡 + 𝜀𝑡 𝛼𝑡+1 = 𝑇𝑡 𝑇 𝛼𝑡 + 𝑅𝑡𝜂𝑡 Causal Impact model Most general form of the model Observation equation State equation 𝑦𝑡 = 𝜇𝑡 + 𝜏𝑡 + 𝑥𝑡 𝑇 𝛽 + 𝜀𝑡 𝜇𝑡+1 = 𝜇𝑡 + 𝛿𝑡 + 𝜂𝜇,𝑡 𝛿𝑡+1 = 𝛿𝑡 + 𝜂𝛿,𝑡 𝜏𝑡+1 = − 𝑖=0 𝑆−2 𝜏𝑡−𝑖 + 𝜂𝜏,𝑡
  • 12. 12 The model has 5 main parameters: 4 variance terms 𝝈𝜺 𝟐 , 𝝈𝝁 𝟐 , 𝝈𝜹 𝟐 , 𝝈𝝉 𝟐 and regression coefficients 𝜷 𝑦𝑡 = 𝜇𝑡 + 𝜏𝑡 + 𝑥𝑡 𝑇 𝛽 + 𝜀𝑡 𝜇𝑡+1 = 𝜇𝑡 + 𝛿𝑡 + 𝜂𝜇,𝑡 𝛿𝑡+1 = 𝛿𝑡 + 𝜂𝛿,𝑡 𝜏𝑡+1 = − 𝑖=0 𝑆−2 𝜏𝑡−𝑖 + 𝜂𝜏,𝑡 ~𝒩 0, 𝜎𝜀 2 ~𝒩 0, 𝜎𝜇 2 ~𝒩 0, 𝜎𝛿 2 ~𝒩 0, 𝜎𝜏 2
  • 13. 13 We impose an inv-gamma prior on 𝝈𝜺 𝟐 , with parameters 𝒔𝜺 and 𝒗𝜺 selected based on the expected goodness-of-fit 𝑦𝑡 = 𝜇𝑡 + 𝜏𝑡 + 𝑥𝑡 𝑇 𝛽 + 𝜀𝑡 𝜇𝑡+1 = 𝜇𝑡 + 𝛿𝑡 + 𝜂𝜇,𝑡 𝛿𝑡+1 = 𝛿𝑡 + 𝜂𝛿,𝑡 𝜏𝑡+1 = − 𝑖=0 𝑆−2 𝜏𝑡−𝑖 + 𝜂𝜏,𝑡 ~𝒩 0, 𝜎𝜀 2 ~𝒩 0, 𝜎𝜇 2 ~𝒩 0, 𝜎𝛿 2 ~𝒩 0, 𝜎𝜏 2 Priors 𝜎𝜀 2 ~ 𝐼𝑛𝑣−𝐺𝑎𝑚𝑚𝑎 𝑠𝜀, 𝑣𝜀 0 1 2 3 4 0 1 2 3 x Probability Density a = 1, b = 1 a = 2, b = 1 a = 3, b = 1 a = 3, b = 0.5 Inv−Gamma(a,b) density for varying values of a and b
  • 14. 14 We impose weak priors on 𝝈𝝁 𝟐, 𝝈𝜹 𝟐 and 𝝈𝝉 𝟐 reflecting the assumption that errors are small in the state process 𝑦𝑡 = 𝜇𝑡 + 𝜏𝑡 + 𝑥𝑡 𝑇 𝛽 + 𝜀𝑡 𝜇𝑡+1 = 𝜇𝑡 + 𝛿𝑡 + 𝜂𝜇,𝑡 𝛿𝑡+1 = 𝛿𝑡 + 𝜂𝛿,𝑡 𝜏𝑡+1 = − 𝑖=0 𝑆−2 𝜏𝑡−𝑖 + 𝜂𝜏,𝑡 ~𝒩 0, 𝜎𝜀 2 ~𝒩 0, 𝜎𝜇 2 ~𝒩 0, 𝜎𝛿 2 ~𝒩 0, 𝜎𝜏 2 Priors 𝜎𝜇 2 , 𝜎𝛿 2 , 𝜎𝜏 2 ~ 𝐼𝑛𝑣−𝐺𝑎𝑚𝑚𝑎 1, 0.01 × 𝑉𝑎𝑟(𝑦) 0 1 2 3 4 0 1 2 3 x Probability Density a = 1, b = 1 a = 2, b = 1 a = 3, b = 1 a = 3, b = 0.5 Inv−Gamma(a,b) density for varying values of a and b
  • 15. 15 We let the model choose an appropriate set of controls by placing a spike and slab prior over coefficients 𝜷 𝑦𝑡 = 𝜇𝑡 + 𝜏𝑡 + 𝑥𝑡 𝑇 𝛽 + 𝜀𝑡 𝜇𝑡+1 = 𝜇𝑡 + 𝛿𝑡 + 𝜂𝜇,𝑡 𝛿𝑡+1 = 𝛿𝑡 + 𝜂𝛿,𝑡 𝜏𝑡+1 = − 𝑖=0 𝑆−2 𝜏𝑡−𝑖 + 𝜂𝜏,𝑡 ~𝒩 0, 𝜎𝜀 2 ~𝒩 0, 𝜎𝜇 2 ~𝒩 0, 𝜎𝛿 2 ~𝒩 0, 𝜎𝜏 2 Priors 𝛽𝛾|𝜎𝜀 2 ~ 𝒩(0, 𝑛𝜎𝜀 2 𝑋𝑇 𝑋 −1 ) 𝑝 𝜚 ~ 𝑗=1 𝐽 𝜋𝑗 𝜚𝑗 (1 − 𝜋𝑗) 𝜚𝑗 0 1 2 3 4 −2 −1 0 1 2 x Probability Density Spike Slab Density functions of spike and slab priors
  • 16. 16 The inference can be performed in R with just 6 lines of code 1 library(CausalImpact) 2 pre.period <- as.Date(c("2011-01-03", "2015-09-14")) 3 post.period <- as.Date(c("2015-09-21", "2017-03-19")) 4 impact <- CausalImpact(data, pre.period, post.period) 5 plot(impact) 6 summary(impact)
  • 17. 17 Results can be plotted and summarised in a table original pointwise cumulative 2011 2012 2013 2014 2015 2016 2017 100 150 200 250 −80 −40 0 −4000 −3000 −2000 −1000 0 Date Adjusted Closing Price Cumulative panel only makes sense when the metric is additive, such as clicks or the number of orders, but not in the case when it is a share price
  • 18. 18 The package can even write a report for you!
  • 19. 19 Additional considerations ● It is important that covariates included in the model are not themselves affected by the event. For each covariate included, it is critical to reason why this is the case. ● The model can be validated by running the Causal Impact analysis on an ‘imaginary event’ before the actual event. We should not be seeing any significant effect, and actual and predicted lines should match reasonably closely before the actual event.
  • 20. 20 References ● K.H. Brodersen, F. Gallusser, J. Koehler, N. Remy, S. L. Scott, (2015). Inferring Causal Impact Using Bayesian Structural Time- Series Models. https://research.google.com/pubs/pub41854.html. ● S. L. Scott, H. Varian, (2013). Predicting the Present with Bayesian Structural Time Series. https://people.ischool.berkeley.edu/~hal/Papers/2013/pred- present-with-bsts.pdf.