/
When-Prediction-Fails.html
1992 lines (1855 loc) · 112 KB
/
When-Prediction-Fails.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>When Prediction Fails — Causal Inference for the Brave and True</title>
<!-- Loaded before other Sphinx assets -->
<link href="_static/styles/theme.css?digest=1999514e3f237ded88cf" rel="stylesheet">
<link href="_static/styles/pydata-sphinx-theme.css?digest=1999514e3f237ded88cf" rel="stylesheet">
<link rel="stylesheet"
href="_static/vendor/fontawesome/5.13.0/css/all.min.css">
<link rel="preload" as="font" type="font/woff2" crossorigin
href="_static/vendor/fontawesome/5.13.0/webfonts/fa-solid-900.woff2">
<link rel="preload" as="font" type="font/woff2" crossorigin
href="_static/vendor/fontawesome/5.13.0/webfonts/fa-brands-400.woff2">
<link rel="stylesheet" type="text/css" href="_static/pygments.css" />
<link rel="stylesheet" href="_static/styles/sphinx-book-theme.css?digest=5115cc725059bd94278eecd172e13a965bf8f5a9" type="text/css" />
<link rel="stylesheet" type="text/css" href="_static/togglebutton.css" />
<link rel="stylesheet" type="text/css" href="_static/copybutton.css" />
<link rel="stylesheet" type="text/css" href="_static/mystnb.css" />
<link rel="stylesheet" type="text/css" href="_static/sphinx-thebe.css" />
<link rel="stylesheet" type="text/css" href="_static/design-style.b7bb847fb20b106c3d81b95245e65545.min.css" />
<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="_static/scripts/pydata-sphinx-theme.js?digest=1999514e3f237ded88cf">
<script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
<script src="_static/jquery.js"></script>
<script src="_static/underscore.js"></script>
<script src="_static/doctools.js"></script>
<script src="_static/clipboard.min.js"></script>
<script src="_static/copybutton.js"></script>
<script src="_static/scripts/sphinx-book-theme.js?digest=9c920249402e914e316237a7dbc6769907cce411"></script>
<script>let toggleHintShow = 'Click to show';</script>
<script>let toggleHintHide = 'Click to hide';</script>
<script>let toggleOpenOnPrint = 'true';</script>
<script src="_static/togglebutton.js"></script>
<script>var togglebuttonSelector = '.toggle, .admonition.dropdown, .tag_hide_input div.cell_input, .tag_hide-input div.cell_input, .tag_hide_output div.cell_output, .tag_hide-output div.cell_output, .tag_hide_cell.cell, .tag_hide-cell.cell';</script>
<script src="_static/design-tabs.js"></script>
<script>const THEBE_JS_URL = "https://unpkg.com/thebe@0.8.2/lib/index.js"
const thebe_selector = ".thebe,.cell"
const thebe_selector_input = "pre"
const thebe_selector_output = ".output, .cell_output"
</script>
<script async="async" src="_static/sphinx-thebe.js"></script>
<script>window.MathJax = {"options": {"processHtmlClass": "tex2jax_process|mathjax_process|math|output_area"}}</script>
<script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Why Prediction Metrics are Dangerous For Causal Models" href="Prediction-Metrics-For-Causal-Models.html" />
<link rel="prev" title="Debiasing with Propensity Score" href="Debiasing-with-Propensity-Score.html" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="docsearch:language" content="en">
<!-- Google Analytics -->
<script async="" src="https://www.google-analytics.com/analytics.js"></script>
<script>
window.ga = window.ga || function () {
(ga.q = ga.q || []).push(arguments) };
ga.l = +new Date;
ga('create', 'UA-97848161-1', 'auto');
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
</script>
</head>
<body data-spy="scroll" data-target="#bd-toc-nav" data-offset="60">
<!-- Checkboxes to toggle the left sidebar -->
<input type="checkbox" class="sidebar-toggle" name="__navigation" id="__navigation" aria-label="Toggle navigation sidebar">
<label class="overlay overlay-navbar" for="__navigation">
<div class="visually-hidden">Toggle navigation sidebar</div>
</label>
<!-- Checkboxes to toggle the in-page toc -->
<input type="checkbox" class="sidebar-toggle" name="__page-toc" id="__page-toc" aria-label="Toggle in-page Table of Contents">
<label class="overlay overlay-pagetoc" for="__page-toc">
<div class="visually-hidden">Toggle in-page Table of Contents</div>
</label>
<!-- Headers at the top -->
<div class="announcement header-item noprint"></div>
<div class="header header-item noprint"></div>
<div class="container-fluid" id="banner"></div>
<div class="container-xl">
<div class="row">
<!-- Sidebar -->
<div class="bd-sidebar noprint" id="site-navigation">
<div class="bd-sidebar__content">
<div class="bd-sidebar__top"><div class="navbar-brand-box">
<a class="navbar-brand text-wrap" href="index.html">
<h1 class="site-logo" id="site-title">Causal Inference for the Brave and True</h1>
</a>
</div><form class="bd-search d-flex align-items-center" action="search.html" method="get">
<i class="icon fas fa-search"></i>
<input type="search" class="form-control" name="q" id="search-input" placeholder="Search this book..." aria-label="Search this book..." autocomplete="off" >
</form><nav class="bd-links" id="bd-docs-nav" aria-label="Main">
<div class="bd-toc-item active">
<ul class="nav bd-sidenav bd-sidenav__home-link">
<li class="toctree-l1">
<a class="reference internal" href="landing-page.html">
Causal Inference for The Brave and True
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Part I - The Yang
</span>
</p>
<ul class="nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="01-Introduction-To-Causality.html">
01 - Introduction To Causality
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="02-Randomised-Experiments.html">
02 - Randomised Experiments
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="03-Stats-Review-The-Most-Dangerous-Equation.html">
03 - Stats Review: The Most Dangerous Equation
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="04-Graphical-Causal-Models.html">
04 - Graphical Causal Models
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="05-The-Unreasonable-Effectiveness-of-Linear-Regression.html">
05 - The Unreasonable Effectiveness of Linear Regression
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="06-Grouped-and-Dummy-Regression.html">
06 - Grouped and Dummy Regression
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="07-Beyond-Confounders.html">
07 - Beyond Confounders
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="08-Instrumental-Variables.html">
08 - Instrumental Variables
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="09-Non-Compliance-and-LATE.html">
09 - Non Compliance and LATE
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="10-Matching.html">
10 - Matching
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="11-Propensity-Score.html">
11 - Propensity Score
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="12-Doubly-Robust-Estimation.html">
12 - Doubly Robust Estimation
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="13-Difference-in-Differences.html">
13 - Difference-in-Differences
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="14-Panel-Data-and-Fixed-Effects.html">
14 - Panel Data and Fixed Effects
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="15-Synthetic-Control.html">
15 - Synthetic Control
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="16-Regression-Discontinuity-Design.html">
16 - Regression Discontinuity Design
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Part II - The Yin
</span>
</p>
<ul class="nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="17-Predictive-Models-101.html">
17 - Predictive Models 101
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="18-Heterogeneous-Treatment-Effects-and-Personalization.html">
18 - Heterogeneous Treatment Effects and Personalization
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="19-Evaluating-Causal-Models.html">
19 - Evaluating Causal Models
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="20-Plug-and-Play-Estimators.html">
20 - Plug-and-Play Estimators
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="21-Meta-Learners.html">
21 - Meta Learners
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="22-Debiased-Orthogonal-Machine-Learning.html">
22 - Debiased/Orthogonal Machine Learning
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="23-Challenges-with-Effect-Heterogeneity-and-Nonlinearity.html">
23 - Challenges with Effect Heterogeneity and Nonlinearity
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="24-The-Diff-in-Diff-Saga.html">
24 - The Difference-in-Differences Saga
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="25-Synthetic-Diff-in-Diff.html">
25 - Synthetic Difference-in-Differences
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Appendix
</span>
</p>
<ul class="current nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="Debiasing-with-Orthogonalization.html">
Debiasing with Orthogonalization
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="Debiasing-with-Propensity-Score.html">
Debiasing with Propensity Score
</a>
</li>
<li class="toctree-l1 current active">
<a class="current reference internal" href="#">
When Prediction Fails
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="Prediction-Metrics-For-Causal-Models.html">
Why Prediction Metrics are Dangerous For Causal Models
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="Conformal-Inference-for-Synthetic-Control.html">
Conformal Inference for Synthetic Controls
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Contribute
</span>
</p>
<ul class="nav bd-sidenav">
<li class="toctree-l1">
<a class="reference external" href="https://www.patreon.com/causal_inference_for_the_brave_and_true">
Patreon
</a>
</li>
</ul>
</div>
</nav></div>
<div class="bd-sidebar__bottom">
<!-- To handle the deprecated key -->
<div class="navbar_extra_footer">
Powered by <a href="https://jupyterbook.org">Jupyter Book</a>
</div>
</div>
</div>
<div id="rtd-footer-container"></div>
</div>
<!-- A tiny helper pixel to detect if we've scrolled -->
<div class="sbt-scroll-pixel-helper"></div>
<!-- Main content -->
<div class="col py-0 content-container">
<div class="header-article row sticky-top noprint">
<div class="col py-1 d-flex header-article-main">
<div class="header-article__left">
<label for="__navigation"
class="headerbtn"
data-toggle="tooltip"
data-placement="right"
title="Toggle navigation"
>
<span class="headerbtn__icon-container">
<i class="fas fa-bars"></i>
</span>
</label>
</div>
<div class="header-article__right">
<div class="menu-dropdown menu-dropdown-launch-buttons">
<button class="headerbtn menu-dropdown__trigger"
aria-label="Launch interactive content">
<i class="fas fa-rocket"></i>
</button>
<div class="menu-dropdown__content">
<ul>
<li>
<a href="https://mybinder.org/v2/gh/matheusfacure/python-causality-handbook/master?urlpath=tree/When-Prediction-Fails.ipynb"
class="headerbtn"
data-toggle="tooltip"
data-placement="left"
title="Launch on Binder"
>
<span class="headerbtn__icon-container">
<img src="_static/images/logo_binder.svg">
</span>
<span class="headerbtn__text-container">Binder</span>
</a>
</li>
</ul>
</div>
</div>
<button onclick="toggleFullScreen()"
class="headerbtn"
data-toggle="tooltip"
data-placement="bottom"
title="Fullscreen mode"
>
<span class="headerbtn__icon-container">
<i class="fas fa-expand"></i>
</span>
</button>
<a href="https://github.com/matheusfacure/python-causality-handbook/issues/new?title=Issue%20on%20page%20%2FWhen-Prediction-Fails.html&body=Your%20issue%20content%20here."
class="headerbtn"
data-toggle="tooltip"
data-placement="bottom"
title="Open an issue"
>
<span class="headerbtn__icon-container">
<i class="fas fa-lightbulb"></i>
</span>
</a>
<div class="menu-dropdown menu-dropdown-download-buttons">
<button class="headerbtn menu-dropdown__trigger"
aria-label="Download this page">
<i class="fas fa-download"></i>
</button>
<div class="menu-dropdown__content">
<ul>
<li>
<a href="_sources/When-Prediction-Fails.ipynb"
class="headerbtn"
data-toggle="tooltip"
data-placement="left"
title="Download source file"
>
<span class="headerbtn__icon-container">
<i class="fas fa-file"></i>
</span>
<span class="headerbtn__text-container">.ipynb</span>
</a>
</li>
<li>
<button onclick="printPdf(this)"
class="headerbtn"
data-toggle="tooltip"
data-placement="left"
title="Print to PDF"
>
<span class="headerbtn__icon-container">
<i class="fas fa-file-pdf"></i>
</span>
<span class="headerbtn__text-container">.pdf</span>
</button>
</li>
</ul>
</div>
</div>
<label for="__page-toc"
class="headerbtn headerbtn-page-toc"
>
<span class="headerbtn__icon-container">
<i class="fas fa-list"></i>
</span>
</label>
</div>
</div>
<!-- Table of contents -->
<div class="col-md-3 bd-toc show noprint">
<div class="tocsection onthispage pt-5 pb-3">
<i class="fas fa-list"></i> Contents
</div>
<nav id="bd-toc-nav" aria-label="Page">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#when-all-you-have-is-a-hammer">
When all you have is a Hammer…
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#who-wants-a-coupon">
Who Wants a Coupon?
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#simple-policy">
Simple Policy
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#policy-with-model">
Policy With Model
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#hammering-your-thumb-with-predictions">
Hammering Your Thumb with Predictions
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#when-might-prediction-helps">
When Might Prediction Helps
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#a-graphical-explanation">
A Graphical Explanation
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#key-ideas">
Key Ideas
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#references">
References
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#contribute">
Contribute
</a>
</li>
</ul>
</nav>
</div>
</div>
<div class="article row">
<div class="col pl-md-3 pl-lg-5 content-container">
<!-- Table of contents that is only displayed when printing the page -->
<div id="jb-print-docs-body" class="onlyprint">
<h1>When Prediction Fails</h1>
<!-- Table of contents -->
<div id="print-main-content">
<div id="jb-print-toc">
<div>
<h2> Contents </h2>
</div>
<nav aria-label="Page">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#when-all-you-have-is-a-hammer">
When all you have is a Hammer…
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#who-wants-a-coupon">
Who Wants a Coupon?
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#simple-policy">
Simple Policy
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#policy-with-model">
Policy With Model
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#hammering-your-thumb-with-predictions">
Hammering Your Thumb with Predictions
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#when-might-prediction-helps">
When Might Prediction Helps
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#a-graphical-explanation">
A Graphical Explanation
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#key-ideas">
Key Ideas
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#references">
References
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#contribute">
Contribute
</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
<main id="main-content" role="main">
<div>
<section class="tex2jax_ignore mathjax_ignore" id="when-prediction-fails">
<h1>When Prediction Fails<a class="headerlink" href="#when-prediction-fails" title="Permalink to this headline">#</a></h1>
<section id="when-all-you-have-is-a-hammer">
<h2>When all you have is a Hammer…<a class="headerlink" href="#when-all-you-have-is-a-hammer" title="Permalink to this headline">#</a></h2>
<p>Between 2015 and 2020, Machine Learning went through a massive surge. Its proven usefulness in the fields of computer vision and natural language understanding, coupled with an initial lack of professionals in the area, provided the perfect opportunity for a machine learning teaching industry. Figures like Andrew Ng and Sebastian Thrun managed to teach machine learning to the world at rock bottom prices. At the same time, on the software side, it became increasingly easier to fit a complex machine learning model (as you’ve already seen by the very few lines of code it took us to write an ML in the previous chapter). Tutorials about how to make intelligent systems sprung all over the internet. The cost of entry in ML plummeted.</p>
<p><img alt="img" src="_images/ml-in-5.png" /></p>
<p>Building ML became so simple that you didn’t even need to know how to code very well (and I’m living evidence of that), nor the math behind the algorithms. In fact, you could build wonders with the following 5 lines of Python.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">X_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">,</span> <span class="n">X_test</span><span class="p">,</span> <span class="n">y_test</span> <span class="o">=</span> <span class="n">train_test_split</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="c1">## instantiate the machine learning model</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">MachineLearningModel</span><span class="p">()</span>
<span class="c1">## Fit the ML model</span>
<span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">)</span>
<span class="c1"># Make predictions on unseen data</span>
<span class="n">y_pred</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X_test</span><span class="p">)</span>
<span class="c1"># Evaluate the quality of predictions</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Performance"</span><span class="p">,</span> <span class="n">metric</span><span class="p">(</span><span class="n">y_test</span><span class="p">,</span> <span class="n">y_pred</span><span class="p">))</span>
</pre></div>
</div>
<p>For the most part, this is an amazing thing! I’m all in for taking valuable content and making it available. However, there is also a dark side to all of this. This new wave of data scientists were trained mostly in predictive modeling, since that is what ML primarily focuses on solving. As a result, whenever those data scientists encountered a business problem, they tried to tackle it with, not surprisingly, predictive models. When they were indeed prediction problems, like the one we saw in the previous chapter, the data scientist usually succeeded and everyone got happy. However, there is an entire class of problems that are simply not solvable with prediction techniques. And when those appeared, the data scientists usually failed miserably. These are problems that are framed like “how much can I increase Y by changing X”.</p>
<p>From my experience, this other type of problem is what management usually cares the most about. They often want to know how to increase sales, decrease cost or bring in more customers. Needless to say, they are not very happy when a data scientist comes up with an answer to how to predict sales instead of how to increase it. Sadly, when everything the data scientist knows is predictive models, this tends to happen a lot. As a boss of mine once told me: “when all you have is a hammer, everything starts to look like a thumb”.</p>
<p>Like I’ve said, I’m all in for lowering the cost of knowledge, but the current Data Scientist curriculum has a huge gap. I think that my job here is to fill in that gap. Is to equip you with tools to solve this other class of problems, which are causal in nature.</p>
<p>What you are trying to do is estimate how something you can control (advertisement, price, customer service) affects or causes something you want to change, but can’t control directly (sales, number of customers, PNL). But ,before showing you how to solve these problems, I want to show you what happens when you treat them like prediction tasks and try to solve them with the traditional ML toolkit. The reason for it is that data scientists often come to me and say “OK, but although tackling causal problems with prediction tools is not the best idea, it surely helps something, no? Imean, it couldn’t hurt…”. Well, as it turns out, it can. And you better understand this before you go on hammering your own thumb.</p>
<p><img alt="img" src="_images/horse-meme.png" /></p>
<div class="cell tag_hide-input docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">sklearn</span> <span class="kn">import</span> <span class="n">ensemble</span>
<span class="kn">from</span> <span class="nn">sklearn.model_selection</span> <span class="kn">import</span> <span class="n">train_test_split</span><span class="p">,</span> <span class="n">cross_val_predict</span>
<span class="kn">from</span> <span class="nn">sklearn.ensemble</span> <span class="kn">import</span> <span class="n">gradient_boosting</span>
<span class="kn">from</span> <span class="nn">sklearn.metrics</span> <span class="kn">import</span> <span class="n">r2_score</span>
<span class="kn">import</span> <span class="nn">seaborn</span> <span class="k">as</span> <span class="nn">sns</span>
<span class="kn">from</span> <span class="nn">matplotlib</span> <span class="kn">import</span> <span class="n">pyplot</span> <span class="k">as</span> <span class="n">plt</span>
<span class="kn">from</span> <span class="nn">matplotlib</span> <span class="kn">import</span> <span class="n">style</span>
<span class="n">style</span><span class="o">.</span><span class="n">use</span><span class="p">(</span><span class="s2">"ggplot"</span><span class="p">)</span>
<span class="c1"># helper functions for this notebook</span>
<span class="kn">from</span> <span class="nn">nb18</span> <span class="kn">import</span> <span class="n">ltv_with_coupons</span>
</pre></div>
</div>
</div>
</div>
</section>
<section id="who-wants-a-coupon">
<h2>Who Wants a Coupon?<a class="headerlink" href="#who-wants-a-coupon" title="Permalink to this headline">#</a></h2>
<p>To make matters more relatable, let’s continue with the example we used in the previous chapter, but with a little twist to it. Before, we were trying to distinguish the profitable from the non profitable customers. We framed that as a prediction problem: predicting customer profitability. We could then build a machine learning model for this task and use it to choose who we would do business with: only the customers we predicted to be profitable. In other words, our goal was to separate the profitable from the non-profitable, which we could do with a predictive model.</p>
<p>Now, you have a new task. You suspect that giving coupons to new customers increases their engagement with your business and makes them more profitable in the long run. That is, they spend more and for a longer period. Your new assignment is to figure out how much the coupon value should be (zero included). Notice that, with coupons, you are essentially giving away money for people to spend on your business. For this reason, they enter as a cost in your book account. Notice that if the coupon value is too high, you will probably lose money, since customers will buy all they need using only the coupons. That’s another way of saying that they will get your product for free. On the flip side, if coupon value is too low (or zero), you are not even giving coupons. This could be a valid answer, but it could also be that some discounts upfront, in the form of coupons, will be more profitable in the long run.</p>
<p>For reasons you will see later, we will use a data generating function instead of loading a static dataset. The function <code class="docutils literal notranslate"><span class="pre">ltv_with_coupons</span></code> generates transaction data for us. As you can see, they have the same format as the one we saw previously, with one row per customer, a column for the cost of acquisition and columns for the transactions between day 1 and 30.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">transactions</span><span class="p">,</span> <span class="n">customer_features</span> <span class="o">=</span> <span class="n">ltv_with_coupons</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="n">transactions</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="n">transactions</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>(10000, 32)
</pre></div>
</div>
<div class="output text_html"><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>customer_id</th>
<th>cacq</th>
<th>day_0</th>
<th>day_1</th>
<th>day_2</th>
<th>day_3</th>
<th>day_4</th>
<th>day_5</th>
<th>day_6</th>
<th>day_7</th>
<th>...</th>
<th>day_20</th>
<th>day_21</th>
<th>day_22</th>
<th>day_23</th>
<th>day_24</th>
<th>day_25</th>
<th>day_26</th>
<th>day_27</th>
<th>day_28</th>
<th>day_29</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td>-110</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>5</td>
<td>0</td>
<td>2</td>
<td>2</td>
<td>...</td>
<td>0</td>
<td>3</td>
<td>0</td>
<td>4</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<th>1</th>
<td>1</td>
<td>-61</td>
<td>2</td>
<td>0</td>
<td>5</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>1</td>
<td>0</td>
<td>...</td>
<td>5</td>
<td>0</td>
<td>1</td>
<td>35</td>
<td>11</td>
<td>0</td>
<td>5</td>
<td>2</td>
<td>4</td>
<td>0</td>
</tr>
<tr>
<th>2</th>
<td>2</td>
<td>-8</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>...</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<th>3</th>
<td>3</td>
<td>-30</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>...</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<th>4</th>
<td>4</td>
<td>-41</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>2</td>
<td>0</td>
<td>4</td>
<td>0</td>
<td>0</td>
<td>...</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</tbody>
</table>
<p>5 rows × 32 columns</p>
</div></div></div>
</div>
<p>As for the other parts of the data, again, we have a customer identifier, the region the customer lives, the customer income and the customer age. In addition, we now have a variable that is <code class="docutils literal notranslate"><span class="pre">coupons</span></code>, which tells us how much we’ve given in coupons for that customer.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">customer_features</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="n">customer_features</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>(10000, 5)
</pre></div>
</div>
<div class="output text_html"><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>customer_id</th>
<th>region</th>
<th>income</th>
<th>coupons</th>
<th>age</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td>18</td>
<td>1025</td>
<td>5</td>
<td>24</td>
</tr>
<tr>
<th>1</th>
<td>1</td>
<td>40</td>
<td>1649</td>
<td>5</td>
<td>26</td>
</tr>
<tr>
<th>2</th>
<td>2</td>
<td>35</td>
<td>2034</td>
<td>15</td>
<td>33</td>
</tr>
<tr>
<th>3</th>
<td>3</td>
<td>29</td>
<td>1859</td>
<td>15</td>
<td>35</td>
</tr>
<tr>
<th>4</th>
<td>4</td>
<td>11</td>
<td>1243</td>
<td>5</td>
<td>26</td>
</tr>
</tbody>
</table>
</div></div></div>
</div>
<p>To process this data to a single dataframe, we will sum all the columns in the first table (that is, summing <code class="docutils literal notranslate"><span class="pre">CACQ</span></code> with the transactions).This will give us the <code class="docutils literal notranslate"><span class="pre">net_value</span></code> as it was computed in the previous chapter. After that, we will join in the features data and update the <code class="docutils literal notranslate"><span class="pre">net_value</span></code> to include the coupon cost.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">process_data</span><span class="p">(</span><span class="n">transactions</span><span class="p">,</span> <span class="n">customer_data</span><span class="p">):</span>
<span class="n">profitable</span> <span class="o">=</span> <span class="p">(</span><span class="n">transactions</span><span class="p">[[</span><span class="s2">"customer_id"</span><span class="p">]]</span>
<span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">net_value</span> <span class="o">=</span> <span class="n">transactions</span>
<span class="o">.</span><span class="n">drop</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="s2">"customer_id"</span><span class="p">)</span>
<span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)))</span>
<span class="k">return</span> <span class="p">(</span><span class="n">customer_data</span>
<span class="c1"># join net_value and features</span>
<span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="n">profitable</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="s2">"customer_id"</span><span class="p">)</span>
<span class="c1"># include the coupons cost</span>
<span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">net_value</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">d</span><span class="p">:</span> <span class="n">d</span><span class="p">[</span><span class="s2">"net_value"</span><span class="p">]</span> <span class="o">-</span> <span class="n">d</span><span class="p">[</span><span class="s2">"coupons"</span><span class="p">]))</span>
<span class="n">customer_features</span> <span class="o">=</span> <span class="n">process_data</span><span class="p">(</span><span class="n">transactions</span><span class="p">,</span> <span class="n">customer_features</span><span class="p">)</span>
<span class="n">customer_features</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_html"><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>customer_id</th>
<th>region</th>
<th>income</th>
<th>coupons</th>
<th>age</th>
<th>net_value</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td>18</td>
<td>1025</td>
<td>5</td>
<td>24</td>
<td>-44</td>
</tr>
<tr>
<th>1</th>
<td>1</td>
<td>40</td>
<td>1649</td>
<td>5</td>
<td>26</td>
<td>74</td>
</tr>
<tr>
<th>2</th>
<td>2</td>
<td>35</td>
<td>2034</td>
<td>15</td>
<td>33</td>
<td>-23</td>
</tr>
<tr>
<th>3</th>
<td>3</td>