-
Notifications
You must be signed in to change notification settings - Fork 0
/
bayes_simulation.html
1812 lines (1772 loc) · 138 KB
/
bayes_simulation.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 xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.6.1">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>31 Bayesian Analysis by Simulation – Resampling statistics</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { display: inline-block; text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
/* CSS for citations */
div.csl-bib-body { }
div.csl-entry {
clear: both;
margin-bottom: 0em;
}
.hanging-indent div.csl-entry {
margin-left:2em;
text-indent:-2em;
}
div.csl-left-margin {
min-width:2em;
float:left;
}
div.csl-right-inline {
margin-left:2em;
padding-left:1em;
}
div.csl-indent {
margin-left: 2em;
}</style>
<script src="site_libs/quarto-nav/quarto-nav.js"></script>
<script src="site_libs/quarto-nav/headroom.min.js"></script>
<script src="site_libs/clipboard/clipboard.min.js"></script>
<script src="site_libs/quarto-search/autocomplete.umd.js"></script>
<script src="site_libs/quarto-search/fuse.min.js"></script>
<script src="site_libs/quarto-search/quarto-search.js"></script>
<meta name="quarto:offset" content="./">
<link href="./references.html" rel="next">
<link href="./how_big_sample.html" rel="prev">
<script src="site_libs/quarto-html/quarto.js"></script>
<script src="site_libs/quarto-html/popper.min.js"></script>
<script src="site_libs/quarto-html/tippy.umd.min.js"></script>
<script src="site_libs/quarto-html/anchor.min.js"></script>
<link href="site_libs/quarto-html/tippy.css" rel="stylesheet">
<link href="site_libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="site_libs/bootstrap/bootstrap.min.js"></script>
<link href="site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="site_libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script id="quarto-search-options" type="application/json">{
"location": "sidebar",
"copy-button": false,
"collapse-after": 3,
"panel-placement": "start",
"type": "textbox",
"limit": 50,
"keyboard-shortcut": [
"f",
"/",
"s"
],
"show-item-context": false,
"language": {
"search-no-results-text": "No results",
"search-matching-documents-text": "matching documents",
"search-copy-link-title": "Copy link to search",
"search-hide-matches-text": "Hide additional matches",
"search-more-match-text": "more match in this document",
"search-more-matches-text": "more matches in this document",
"search-clear-button-title": "Clear",
"search-text-placeholder": "",
"search-detached-cancel-button-title": "Cancel",
"search-submit-button-title": "Submit",
"search-label": "Search"
}
}</script>
<script type="text/javascript">
$(document).ready(function() {
$("table").addClass('lightable-paper lightable-striped lightable-hover')
});
</script>
<script src="https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?features=es6"></script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script>
<script type="text/javascript">
const typesetMath = (el) => {
if (window.MathJax) {
// MathJax Typeset
window.MathJax.typeset([el]);
} else if (window.katex) {
// KaTeX Render
var mathElements = el.getElementsByClassName("math");
var macros = [];
for (var i = 0; i < mathElements.length; i++) {
var texText = mathElements[i].firstChild;
if (mathElements[i].tagName == "SPAN") {
window.katex.render(texText.data, mathElements[i], {
displayMode: mathElements[i].classList.contains('display'),
throwOnError: false,
macros: macros,
fleqn: false
});
}
}
}
}
window.Quarto = {
typesetMath
};
</script>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="font-awesome.min.css">
</head>
<body class="nav-sidebar floating">
<div id="quarto-search-results"></div>
<header id="quarto-header" class="headroom fixed-top">
<nav class="quarto-secondary-nav">
<div class="container-fluid d-flex">
<button type="button" class="quarto-btn-toggle btn" data-bs-toggle="collapse" role="button" data-bs-target=".quarto-sidebar-collapse-item" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
<i class="bi bi-layout-text-sidebar-reverse"></i>
</button>
<nav class="quarto-page-breadcrumbs" aria-label="breadcrumb"><ol class="breadcrumb"><li class="breadcrumb-item"><a href="./bayes_simulation.html"><span class="chapter-number">31</span> <span class="chapter-title">Bayesian Analysis by Simulation</span></a></li></ol></nav>
<a class="flex-grow-1" role="navigation" data-bs-toggle="collapse" data-bs-target=".quarto-sidebar-collapse-item" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
</a>
<button type="button" class="btn quarto-search-button" aria-label="Search" onclick="window.quartoOpenSearch();">
<i class="bi bi-search"></i>
</button>
</div>
</nav>
</header>
<!-- content -->
<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article">
<!-- sidebar -->
<nav id="quarto-sidebar" class="sidebar collapse collapse-horizontal quarto-sidebar-collapse-item sidebar-navigation floating overflow-auto">
<div class="pt-lg-2 mt-2 text-left sidebar-header">
<div class="sidebar-title mb-0 py-0">
<a href="./">Resampling statistics</a>
</div>
</div>
<div class="mt-2 flex-shrink-0 align-items-center">
<div class="sidebar-search">
<div id="quarto-search" class="" title="Search"></div>
</div>
</div>
<div class="sidebar-menu-container">
<ul class="list-unstyled mt-1">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./index.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">R version</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./preface_third.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Preface to the third edition</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./preface_second.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Preface to the second edition</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./intro.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">1</span> <span class="chapter-title">Introduction</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./resampling_method.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">2</span> <span class="chapter-title">The resampling method</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./what_is_probability.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">3</span> <span class="chapter-title">What is probability?</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./about_technology.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">4</span> <span class="chapter-title">Introducing R and the Jupyter notebook</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./resampling_with_code.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">5</span> <span class="chapter-title">Resampling with code</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./resampling_with_code2.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">6</span> <span class="chapter-title">More resampling with code</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./sampling_tools.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">7</span> <span class="chapter-title">Tools for samples and sampling</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./probability_theory_1a.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">8</span> <span class="chapter-title">Probability Theory, Part 1</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./probability_theory_1b.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">9</span> <span class="chapter-title">Probability Theory Part I (continued)</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./more_sampling_tools.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">10</span> <span class="chapter-title">Two puzzles and more tools</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./probability_theory_2_compound.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">11</span> <span class="chapter-title">Probability Theory, Part 2: Compound Probability</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./probability_theory_3.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">12</span> <span class="chapter-title">Probability Theory, Part 3</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./probability_theory_4_finite.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">13</span> <span class="chapter-title">Probability Theory, Part 4: Estimating Probabilities from Finite Universes</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./sampling_variability.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">14</span> <span class="chapter-title">On Variability in Sampling</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./monte_carlo.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">15</span> <span class="chapter-title">The Procedures of Monte Carlo Simulation (and Resampling)</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./standard_scores.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">16</span> <span class="chapter-title">Ranks, Quantiles and Standard Scores</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./inference_ideas.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">17</span> <span class="chapter-title">The Basic Ideas in Statistical Inference</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./inference_intro.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">18</span> <span class="chapter-title">Introduction to Statistical Inference</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./point_estimation.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">19</span> <span class="chapter-title">Point Estimation</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./framing_questions.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">20</span> <span class="chapter-title">Framing Statistical Questions</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./testing_counts_1.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">21</span> <span class="chapter-title">Hypothesis-Testing with Counted Data, Part 1</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./significance.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">22</span> <span class="chapter-title">The Concept of Statistical Significance in Testing Hypotheses</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./testing_counts_2.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">23</span> <span class="chapter-title">The Statistics of Hypothesis-Testing with Counted Data, Part 2</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./testing_measured.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">24</span> <span class="chapter-title">The Statistics of Hypothesis-Testing With Measured Data</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./testing_procedures.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">25</span> <span class="chapter-title">General Procedures for Testing Hypotheses</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./confidence_1.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">26</span> <span class="chapter-title">Confidence Intervals, Part 1: Assessing the Accuracy of Samples</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./confidence_2.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">27</span> <span class="chapter-title">Confidence Intervals, Part 2: The Two Approaches to Estimating Confidence Intervals</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./reliability_average.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">28</span> <span class="chapter-title">Some Last Words About the Reliability of Sample Averages</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./correlation_causation.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">29</span> <span class="chapter-title">Correlation and Causation</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./how_big_sample.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">30</span> <span class="chapter-title">How Large a Sample?</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./bayes_simulation.html" class="sidebar-item-text sidebar-link active">
<span class="menu-text"><span class="chapter-number">31</span> <span class="chapter-title">Bayesian Analysis by Simulation</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./references.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">References</span></a>
</div>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-1" role="navigation" aria-expanded="true">
<span class="menu-text">Appendices</span></a>
<a class="sidebar-item-toggle text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-1" role="navigation" aria-expanded="true" aria-label="Toggle section">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-1" class="collapse list-unstyled sidebar-section depth1 show">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./exercise_solutions.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">A</span> <span class="chapter-title">Exercise Solutions</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./technical_note.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">B</span> <span class="chapter-title">Technical Note to the Professional Reader</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./acknowlegements.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">C</span> <span class="chapter-title">Acknowledgements</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./code_topics.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">D</span> <span class="chapter-title">Code topics</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./errors_suggestions.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">E</span> <span class="chapter-title">Errors and suggestions</span></span></a>
</div>
</li>
</ul>
</li>
</ul>
</div>
</nav>
<div id="quarto-sidebar-glass" class="quarto-sidebar-collapse-item" data-bs-toggle="collapse" data-bs-target=".quarto-sidebar-collapse-item"></div>
<!-- margin-sidebar -->
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#simple-decision-problems" id="toc-simple-decision-problems" class="nav-link active" data-scroll-target="#simple-decision-problems"><span class="header-section-number">31.1</span> Simple decision problems</a>
<ul class="collapse">
<li><a href="#assessing-the-likelihood-that-a-used-car-will-be-sound" id="toc-assessing-the-likelihood-that-a-used-car-will-be-sound" class="nav-link" data-scroll-target="#assessing-the-likelihood-that-a-used-car-will-be-sound"><span class="header-section-number">31.1.1</span> Assessing the Likelihood That a Used Car Will Be Sound</a></li>
<li><a href="#calculation-without-simulation" id="toc-calculation-without-simulation" class="nav-link" data-scroll-target="#calculation-without-simulation"><span class="header-section-number">31.1.2</span> Calculation without simulation</a></li>
</ul></li>
<li><a href="#probability-interpretation" id="toc-probability-interpretation" class="nav-link" data-scroll-target="#probability-interpretation"><span class="header-section-number">31.2</span> Probability interpretation</a>
<ul class="collapse">
<li><a href="#probability-from-proportion" id="toc-probability-from-proportion" class="nav-link" data-scroll-target="#probability-from-proportion"><span class="header-section-number">31.2.1</span> Probability from proportion</a></li>
<li><a href="#probability-relationships-conditional-probability" id="toc-probability-relationships-conditional-probability" class="nav-link" data-scroll-target="#probability-relationships-conditional-probability"><span class="header-section-number">31.2.2</span> Probability relationships: conditional probability</a></li>
<li><a href="#example-conditional-probability" id="toc-example-conditional-probability" class="nav-link" data-scroll-target="#example-conditional-probability"><span class="header-section-number">31.2.3</span> Example: conditional probability</a></li>
<li><a href="#estimating-driving-risk-for-insurance-purposes" id="toc-estimating-driving-risk-for-insurance-purposes" class="nav-link" data-scroll-target="#estimating-driving-risk-for-insurance-purposes"><span class="header-section-number">31.2.4</span> Estimating Driving Risk for Insurance Purposes</a></li>
<li><a href="#screening-for-disease" id="toc-screening-for-disease" class="nav-link" data-scroll-target="#screening-for-disease"><span class="header-section-number">31.2.5</span> Screening for Disease</a></li>
</ul></li>
<li><a href="#fundamental-problems-in-statistical-practice" id="toc-fundamental-problems-in-statistical-practice" class="nav-link" data-scroll-target="#fundamental-problems-in-statistical-practice"><span class="header-section-number">31.3</span> Fundamental problems in statistical practice</a></li>
<li><a href="#problems-based-on-normal-and-other-distributions" id="toc-problems-based-on-normal-and-other-distributions" class="nav-link" data-scroll-target="#problems-based-on-normal-and-other-distributions"><span class="header-section-number">31.4</span> Problems based on normal and other distributions</a>
<ul class="collapse">
<li><a href="#an-intermediate-problem-in-conditional-probability" id="toc-an-intermediate-problem-in-conditional-probability" class="nav-link" data-scroll-target="#an-intermediate-problem-in-conditional-probability"><span class="header-section-number">31.4.1</span> An Intermediate Problem in Conditional Probability</a></li>
<li><a href="#estimating-the-posterior-distribution" id="toc-estimating-the-posterior-distribution" class="nav-link" data-scroll-target="#estimating-the-posterior-distribution"><span class="header-section-number">31.4.2</span> Estimating the Posterior Distribution</a></li>
</ul></li>
<li><a href="#conclusion" id="toc-conclusion" class="nav-link" data-scroll-target="#conclusion"><span class="header-section-number">31.5</span> Conclusion</a></li>
</ul>
</nav>
</div>
<!-- main -->
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title"><span id="sec-bayes-simulation" class="quarto-section-identifier"><span class="chapter-number">31</span> <span class="chapter-title">Bayesian Analysis by Simulation</span></span></h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<blockquote class="blockquote">
<p>This branch of mathematics [probability] is the only one, I believe, in which good writers frequently get results entirely erroneous. <span class="citation" data-cites="peirce1923chance">(<a href="references.html#ref-peirce1923chance" role="doc-biblioref">Peirce 1923</a>, Doctrine of Chances, II)</span></p>
</blockquote>
<p>Bayesian analysis is a way of thinking about problems in probability and statistics that can help one reach otherwise-difficult decisions. It also can sometimes be used in science. The range of its recommended uses is controversial, but this chapter deals only with those uses of Bayesian analysis that are uncontroversial.</p>
<p>Better than defining Bayesian analysis in formal terms is to demonstrate its use. We shall start with the simplest sort of problem, and proceed gradually from there.</p>
<section id="simple-decision-problems" class="level2" data-number="31.1">
<h2 data-number="31.1" class="anchored" data-anchor-id="simple-decision-problems"><span class="header-section-number">31.1</span> Simple decision problems</h2>
<section id="assessing-the-likelihood-that-a-used-car-will-be-sound" class="level3" data-number="31.1.1">
<h3 data-number="31.1.1" class="anchored" data-anchor-id="assessing-the-likelihood-that-a-used-car-will-be-sound"><span class="header-section-number">31.1.1</span> Assessing the Likelihood That a Used Car Will Be Sound</h3>
<p>Consider a problem in estimating the soundness of a used car one considers purchasing (after <span class="citation" data-cites="wonnacott1990introductory">(<a href="references.html#ref-wonnacott1990introductory" role="doc-biblioref">Wonnacott and Wonnacott 1990, 93–94</a>)</span>). Seventy percent of the cars are known to be OK on average, and 30 percent are faulty. Of the cars that <em>are</em> really OK, a mechanic correctly identifies 80 percent as “OK” but says that 20 percent are “faulty”; of those that are faulty, the mechanic correctly identifies 90 percent as faulty and says (incorrectly) that 10 percent are OK.</p>
<p>We wish to know the probability that if the mechanic <em>says</em> a car is “OK,” it <em>really</em> is faulty. Phrased differently, what is the probability of a car being faulty if the mechanic said it was OK?</p>
<p>We can get the desired probabilities directly by simulation without knowing Bayes’ rule, as we shall see. But one must be able to model the physical problem correctly in order to proceed with the simulation; this requirement of a clearly visualized model is a strong point in favor of simulation.</p>
<ol type="1">
<li>Note that we are only interested in outcomes where the mechanic approved a car.</li>
<li>For each car, generate a label of either “faulty” or “working” with probabilities of 0.3 and 0.7, respectively.</li>
<li>For each <em>faulty car</em>, we generate one of two labels, “approved” or “not approved” with probabilities 0.1 and 0.9, respectively.</li>
<li>For each <em>working car</em>, we generate one of two labels, “approved” or “not approved” with probabilities 0.7 and 0.3, respectively.</li>
<li>Out of all cars “approved”, count how many are “faulty”. The ratio between these numbers is our answer.</li>
</ol>
<p>Here is the whole simulation of the car / mechanic problem:</p>
<div id="nte-bayes_cars" class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note 31.1: Notebook: Bayesian analysis of cars and mechanics
</div>
</div>
<div class="callout-body-container callout-body">
<div class="nb-links">
<p><a class="notebook-link" href="notebooks/bayes_cars.Rmd">Download notebook</a> <a class="interact-button" href="./interact/lab/index.html?path=bayes_cars.ipynb">Interact</a></p>
</div>
</div>
</div>
<div class="nb-start" name="bayes_cars" title="Bayesian analysis of cars and mechanics">
</div>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>n_trials <span class="ot"><-</span> <span class="dv">10000</span> <span class="co"># number of cars</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Counters for number of approved, number of approved and faulty</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a>approved <span class="ot"><-</span> <span class="dv">0</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>approved_and_faulty <span class="ot"><-</span> <span class="dv">0</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>n_trials) {</span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># Decide whether the car is faulty or working, with a probability of</span></span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a> <span class="co"># 0.3 and 0.7 respectively</span></span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a> car <span class="ot"><-</span> <span class="fu">sample</span>(<span class="fu">c</span>(<span class="st">'faulty'</span>, <span class="st">'working'</span>), <span class="at">size=</span><span class="dv">1</span>, <span class="at">prob=</span><span class="fu">c</span>(<span class="fl">0.3</span>, <span class="fl">0.7</span>))</span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (car <span class="sc">==</span> <span class="st">'faulty'</span>) {</span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a> <span class="co"># What the mechanic says of a faulty car</span></span>
<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a> mechanic_says <span class="ot"><-</span> <span class="fu">sample</span>(<span class="fu">c</span>(<span class="st">'approved'</span>, <span class="st">'not approved'</span>),</span>
<span id="cb1-16"><a href="#cb1-16" aria-hidden="true" tabindex="-1"></a> <span class="at">size=</span><span class="dv">1</span>,</span>
<span id="cb1-17"><a href="#cb1-17" aria-hidden="true" tabindex="-1"></a> <span class="at">prob=</span><span class="fu">c</span>(<span class="fl">0.1</span>, <span class="fl">0.9</span>))</span>
<span id="cb1-18"><a href="#cb1-18" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
<span id="cb1-19"><a href="#cb1-19" aria-hidden="true" tabindex="-1"></a> <span class="co"># What the mechanic says of a working car</span></span>
<span id="cb1-20"><a href="#cb1-20" aria-hidden="true" tabindex="-1"></a> mechanic_says <span class="ot"><-</span> <span class="fu">sample</span>(<span class="fu">c</span>(<span class="st">'approved'</span>, <span class="st">'not approved'</span>),</span>
<span id="cb1-21"><a href="#cb1-21" aria-hidden="true" tabindex="-1"></a> <span class="at">size=</span><span class="dv">1</span>,</span>
<span id="cb1-22"><a href="#cb1-22" aria-hidden="true" tabindex="-1"></a> <span class="at">prob=</span><span class="fu">c</span>(<span class="fl">0.7</span>, <span class="fl">0.3</span>))</span>
<span id="cb1-23"><a href="#cb1-23" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb1-24"><a href="#cb1-24" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-25"><a href="#cb1-25" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (mechanic_says <span class="sc">==</span> <span class="st">'approved'</span>) {</span>
<span id="cb1-26"><a href="#cb1-26" aria-hidden="true" tabindex="-1"></a> approved <span class="ot"><-</span> approved <span class="sc">+</span> <span class="dv">1</span></span>
<span id="cb1-27"><a href="#cb1-27" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (car <span class="sc">==</span> <span class="st">'faulty'</span>) {</span>
<span id="cb1-28"><a href="#cb1-28" aria-hidden="true" tabindex="-1"></a> approved_and_faulty <span class="ot"><-</span> approved_and_faulty <span class="sc">+</span> <span class="dv">1</span></span>
<span id="cb1-29"><a href="#cb1-29" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb1-30"><a href="#cb1-30" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb1-31"><a href="#cb1-31" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb1-32"><a href="#cb1-32" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-33"><a href="#cb1-33" aria-hidden="true" tabindex="-1"></a>k <span class="ot"><-</span> approved_and_faulty <span class="sc">/</span> approved</span>
<span id="cb1-34"><a href="#cb1-34" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-35"><a href="#cb1-35" aria-hidden="true" tabindex="-1"></a><span class="fu">message</span>(<span class="st">'Proportion of faulty cars of cars approved: '</span>, <span class="fu">round</span>(k, <span class="dv">2</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Proportion of faulty cars of cars approved: 0.06</code></pre>
</div>
</div>
<p>The answer looks to be somewhere between 5 and 6%. The code clearly follows the description step by step, but it is also quite slow. If we can improve the code, we may be able to do our simulation with more cars, and get a more accurate answer.</p>
<p>Let’s use arrays to store the states of all cars in the lot simultaneously:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Number of cars; we made this number larger by a factor of 100</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>n_trials <span class="ot"><-</span> <span class="dv">1000000</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Generate an array with as many entries as there are cars, each</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="co"># being either 'working' or 'faulty'.</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a><span class="co"># We are taking a sample _with_ replacement.</span></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a>cars <span class="ot"><-</span> <span class="fu">sample</span>(<span class="fu">c</span>(<span class="st">'working'</span>, <span class="st">'faulty'</span>),</span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a> <span class="at">size=</span>n_trials,</span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a> <span class="at">replace=</span><span class="cn">TRUE</span>,</span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a> <span class="at">prob=</span><span class="fu">c</span>(<span class="fl">0.7</span>, <span class="fl">0.3</span>))</span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a><span class="co"># Count how many cars are working</span></span>
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a>n_working <span class="ot"><-</span> <span class="fu">sum</span>(cars <span class="sc">==</span> <span class="st">'working'</span>)</span>
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-15"><a href="#cb3-15" aria-hidden="true" tabindex="-1"></a><span class="co"># All the rest are faulty</span></span>
<span id="cb3-16"><a href="#cb3-16" aria-hidden="true" tabindex="-1"></a>n_faulty <span class="ot"><-</span> n_trials <span class="sc">-</span> n_working</span>
<span id="cb3-17"><a href="#cb3-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-18"><a href="#cb3-18" aria-hidden="true" tabindex="-1"></a><span class="co"># Create a new vector in which to store what a mechanic says</span></span>
<span id="cb3-19"><a href="#cb3-19" aria-hidden="true" tabindex="-1"></a><span class="co"># about the car: 'approved' or 'not approved'. We use</span></span>
<span id="cb3-20"><a href="#cb3-20" aria-hidden="true" tabindex="-1"></a><span class="co"># "character" to tell R these are strings.</span></span>
<span id="cb3-21"><a href="#cb3-21" aria-hidden="true" tabindex="-1"></a>mechanic_says <span class="ot"><-</span> <span class="fu">character</span>(n_trials)</span>
<span id="cb3-22"><a href="#cb3-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-23"><a href="#cb3-23" aria-hidden="true" tabindex="-1"></a><span class="co"># We start with the working cars; what does the mechanic say about them?</span></span>
<span id="cb3-24"><a href="#cb3-24" aria-hidden="true" tabindex="-1"></a><span class="co"># Generate 'approved' or 'not approved' labels with the given probabilities.</span></span>
<span id="cb3-25"><a href="#cb3-25" aria-hidden="true" tabindex="-1"></a>mechanic_says[cars <span class="sc">==</span> <span class="st">'working'</span>] <span class="ot"><-</span> <span class="fu">sample</span>(</span>
<span id="cb3-26"><a href="#cb3-26" aria-hidden="true" tabindex="-1"></a> <span class="fu">c</span>(<span class="st">'approved'</span>, <span class="st">'not approved'</span>),</span>
<span id="cb3-27"><a href="#cb3-27" aria-hidden="true" tabindex="-1"></a> <span class="at">size=</span>n_working,</span>
<span id="cb3-28"><a href="#cb3-28" aria-hidden="true" tabindex="-1"></a> <span class="at">replace=</span><span class="cn">TRUE</span>,</span>
<span id="cb3-29"><a href="#cb3-29" aria-hidden="true" tabindex="-1"></a> <span class="at">prob=</span><span class="fu">c</span>(<span class="fl">0.8</span>, <span class="fl">0.2</span>)</span>
<span id="cb3-30"><a href="#cb3-30" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb3-31"><a href="#cb3-31" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-32"><a href="#cb3-32" aria-hidden="true" tabindex="-1"></a><span class="co"># Similarly, for each faulty car, generate 'approved'/'not approved'</span></span>
<span id="cb3-33"><a href="#cb3-33" aria-hidden="true" tabindex="-1"></a><span class="co"># labels with the given probabilities.</span></span>
<span id="cb3-34"><a href="#cb3-34" aria-hidden="true" tabindex="-1"></a>mechanic_says[cars <span class="sc">==</span> <span class="st">'faulty'</span>] <span class="ot"><-</span> <span class="fu">sample</span>(</span>
<span id="cb3-35"><a href="#cb3-35" aria-hidden="true" tabindex="-1"></a> <span class="fu">c</span>(<span class="st">'approved'</span>, <span class="st">'not approved'</span>),</span>
<span id="cb3-36"><a href="#cb3-36" aria-hidden="true" tabindex="-1"></a> <span class="at">size=</span>n_faulty,</span>
<span id="cb3-37"><a href="#cb3-37" aria-hidden="true" tabindex="-1"></a> <span class="at">replace=</span><span class="cn">TRUE</span>,</span>
<span id="cb3-38"><a href="#cb3-38" aria-hidden="true" tabindex="-1"></a> <span class="at">prob=</span><span class="fu">c</span>(<span class="fl">0.1</span>, <span class="fl">0.9</span>)</span>
<span id="cb3-39"><a href="#cb3-39" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb3-40"><a href="#cb3-40" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-41"><a href="#cb3-41" aria-hidden="true" tabindex="-1"></a><span class="co"># Identify all cars that were approved</span></span>
<span id="cb3-42"><a href="#cb3-42" aria-hidden="true" tabindex="-1"></a><span class="co"># This produces a binary mask, an array that looks like:</span></span>
<span id="cb3-43"><a href="#cb3-43" aria-hidden="true" tabindex="-1"></a><span class="co"># [True, False, False, True, ... ]</span></span>
<span id="cb3-44"><a href="#cb3-44" aria-hidden="true" tabindex="-1"></a>approved <span class="ot"><-</span> (mechanic_says <span class="sc">==</span> <span class="st">'approved'</span>)</span>
<span id="cb3-45"><a href="#cb3-45" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-46"><a href="#cb3-46" aria-hidden="true" tabindex="-1"></a><span class="co"># Identify cars that are faulty AND were approved</span></span>
<span id="cb3-47"><a href="#cb3-47" aria-hidden="true" tabindex="-1"></a>faulty_but_approved <span class="ot"><-</span> (cars <span class="sc">==</span> <span class="st">'faulty'</span>) <span class="sc">&</span> approved</span>
<span id="cb3-48"><a href="#cb3-48" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-49"><a href="#cb3-49" aria-hidden="true" tabindex="-1"></a><span class="co"># Count the number of cars that are faulty but approved, as well as</span></span>
<span id="cb3-50"><a href="#cb3-50" aria-hidden="true" tabindex="-1"></a><span class="co"># the total number of cars that were approved</span></span>
<span id="cb3-51"><a href="#cb3-51" aria-hidden="true" tabindex="-1"></a>n_faulty_but_approved <span class="ot"><-</span> <span class="fu">sum</span>(faulty_but_approved)</span>
<span id="cb3-52"><a href="#cb3-52" aria-hidden="true" tabindex="-1"></a>n_approved <span class="ot"><-</span> <span class="fu">sum</span>(approved)</span>
<span id="cb3-53"><a href="#cb3-53" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-54"><a href="#cb3-54" aria-hidden="true" tabindex="-1"></a><span class="co"># Calculate the ratio, which is the answer we seek</span></span>
<span id="cb3-55"><a href="#cb3-55" aria-hidden="true" tabindex="-1"></a>k <span class="ot"><-</span> n_faulty_but_approved <span class="sc">/</span> n_approved</span>
<span id="cb3-56"><a href="#cb3-56" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-57"><a href="#cb3-57" aria-hidden="true" tabindex="-1"></a><span class="fu">message</span>(<span class="st">'Proportion of faulty cars of cars approved: '</span>, <span class="fu">round</span>(k, <span class="dv">2</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Proportion of faulty cars of cars approved: 0.05</code></pre>
</div>
</div>
<p>The code now runs much faster, and with a larger number of cars we see that the answer is closer to a 5% chance of a car being broken after it has been approved by a mechanic.</p>
<div class="nb-end">
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
End of notebook: Bayesian analysis of cars and mechanics
</div>
</div>
<div class="callout-body-container callout-body">
<p><code>bayes_cars</code> starts at <a href="#nte-bayes_cars" class="quarto-xref">Note <span>31.1</span></a>.</p>
</div>
</div>
<!---
End of notebook
-->
</section>
<section id="calculation-without-simulation" class="level3" data-number="31.1.2">
<h3 data-number="31.1.2" class="anchored" data-anchor-id="calculation-without-simulation"><span class="header-section-number">31.1.2</span> Calculation without simulation</h3>
<p>Simulation forces us to model our problem clearly and concretely in code. Such code is most often easier to reason about than opaque statistical methods. Running the simulation gives a good sense of what the correct answer should be. Thereafter, we can still look into different — sometimes more elegant or accurate — ways of modeling and solving the problem.</p>
<p>Let’s examine the following diagram of our car selection:</p>
<p><img src="diagrams/car-tree.png" class="img-fluid"></p>
<p>We see that there are two paths, highlighted, that results in a car being approved by a mechanic. Either a car can be working, and correctly identified as such by a mechanic; or the car can be broken, while the mechanic mistakenly determines it to be working. Our question only pertains to these two paths, so we do not need to study the rest of the tree.</p>
<p>In the long run, in our simulation, about 70% of the cars will end with the label “working”, and about 30% will end up with the label “faulty”. We just took 10000 sample cars above but, in fact, the larger the number of cars we take, the closer we will get to 70% “working” and 30% “faulty”. So, with many samples, we can think of 70% of these samples flowing down the “working” path, and 30% flowing along the “faulty” path.</p>
<p>Now, we want to know, of all the cars approved by a mechanic, how many are faulty:</p>
<p><span class="math display">\[ \frac{\mathrm{cars_{\mathrm{faulty}}}}{\mathrm{cars}_{\mathrm{approved}}} \]</span></p>
<p>We follow the two highlighted paths in the tree:</p>
<ol type="1">
<li>Of a large sample of cars, 30% are faulty. Of these, 10% are approved by a mechanic. That is, 30% * 10% = 3% of all cars.</li>
<li>Of all cars, 70% work. Of these, 80% are approved by a mechanic. That is, 70% * 80% = 56% of all cars.</li>
</ol>
<p>The percentage of faulty cars, out of approved cars, becomes:</p>
<p><span class="math display">\[
3\% / (56\% + 3\%) = 5.08\%
\]</span></p>
<p>Notation-wise, it is a bit easier to calculate these sums using proportions rather than percentages:</p>
<ol type="1">
<li>Faulty cars approved by a mechanic: 0.3 * 0.1 = 0.03</li>
<li>Working cars approved by a mechanic: 0.7 * 0.8 = 0.56</li>
</ol>
<p>Fraction of faulty cars out of approved cars: 0.03 / (0.03 + 0.56) = 0.0508</p>
<p>We see that every time the tree branches, it filters the cars: some go to one branch, the rest to another. In our code, we used the AND (<code>&</code>) operator to find the intersection between faulty AND approved cars, i.e., to filter out from all faulty cars only the cars that were ALSO approved.</p>
</section>
</section>
<section id="probability-interpretation" class="level2" data-number="31.2">
<h2 data-number="31.2" class="anchored" data-anchor-id="probability-interpretation"><span class="header-section-number">31.2</span> Probability interpretation</h2>
<section id="probability-from-proportion" class="level3" data-number="31.2.1">
<h3 data-number="31.2.1" class="anchored" data-anchor-id="probability-from-proportion"><span class="header-section-number">31.2.1</span> Probability from proportion</h3>
<p>In these examples, we often calculate proportions. In the given simulation:</p>
<ul>
<li>How many cars are approved by a mechanic? 59/100.</li>
<li>How many of those 59 were faulty? 3/59.</li>
</ul>
<p>We often also count how commonly events occur: “it rained 4 out of the 10 days”.</p>
<p>An extension of this idea is to <em>predict</em> the probability of an event occurring, based on what we had seen in the past. We can say “out of 100 days, there was some rain on 20 of them; we therefore estimate that the probability of rain occurring is 20/100”. Of course, this is not a complex or very accurate weather model; for that, we’d need to take other factors—such as season—into consideration. Overall, the more observations we have, the better our probability estimates become. We discussed this idea previously in “The Law of Large Numbers”.</p>
<!---
** TODO: REFERENCE SECTION ON LARGE NUMBERS **
-->
<section id="ratios-of-proportions" class="level4" data-number="31.2.1.1">
<h4 data-number="31.2.1.1" class="anchored" data-anchor-id="ratios-of-proportions"><span class="header-section-number">31.2.1.1</span> Ratios of proportions</h4>
<p>At our mechanic’s yard, we can ask “how many red cars here are faulty”? To calculate that, we’d first count the number of red cars, then the number of those red cars that are also broken, then calculate the ratio: <code>red_cars_faulty / red_cars</code>.</p>
<p>We could just as well have worked in percentages: <code>percentage_of_red_cars_broken / percentage_of_cars_that_are_red</code>, since that is <code>(red_cars_broken / 100) / (red_cars / 100)</code>—the same ratio calculated before.</p>
<p>Our point is that the denominator doesn’t matter when calculating ratios, so we could just as well have written:</p>
<p>(red_cars_broken / all_cars) / (red_cars / all_cars)</p>
<p>or</p>
<p><span class="math display">\[
P(\text{cars that are red and that are broken}) / P(\text{red cars})
\]</span></p>
<!---
** TODO: THE ABOVE MAY BE A SUBTLE POINT THAT NEEDS TO EXPANDED, BUT THE TEXT IS GETTING LONG AS-IS **
-->
</section>
</section>
<section id="probability-relationships-conditional-probability" class="level3" data-number="31.2.2">
<h3 data-number="31.2.2" class="anchored" data-anchor-id="probability-relationships-conditional-probability"><span class="header-section-number">31.2.2</span> Probability relationships: conditional probability</h3>
<p>Here’s one way of writing the probability that a car is broken:</p>
<p><span class="math display">\[
P(\text{car is broken})
\]</span></p>
<p>We can shorten “car is broken” to B, and write the same thing as:</p>
<p><span class="math display">\[
P(B)
\]</span></p>
<p>Similarly, we could write the probability that a car is red as:</p>
<p><span class="math display">\[
P(R)
\]</span></p>
<p>We might also want to express the <em>conditional probability</em>, as in the probability that the car is broken, <em>given that</em> we already know that the car is red:</p>
<p><span class="math display">\[
P(\text{car is broken GIVEN THAT car is red})
\]</span></p>
<p>That is getting getting pretty verbose, so we will shorten this as we did above:</p>
<p><span class="math display">\[
P(B \text{ GIVEN THAT } R)
\]</span></p>
<p>To make things even more compact, we write “GIVEN THAT” as a vertical bar <code>|</code> — so the whole thing becomes:</p>
<p><span class="math display">\[
P(B | R)
\]</span></p>
<p>We read this as “the probability that the car is broken given that the car is red”. Such a probability is known as a <em>conditional probability</em>. We discuss these in more detail in <a href="probability_theory_1a.html#sec-cond-uncond" class="quarto-xref"><span>Section 8.13</span></a>.</p>
<p>In our original problem, we ask what the chance is of a car being broken given that a mechanic approved it. As discussed under “Ratios of proportions”, it can be calculated with:</p>
<p><span class="math display">\[\begin{align*}
P(\text{car broken | mechanic approved}) = \\
P(\text{car broken and mechanic approved}) / P(\text{mechanic approved})
\end{align*}\]</span></p>
<p>We have already used <span class="math inline">\(B\)</span> to mean “broken” (above), so let us use <span class="math inline">\(A\)</span> to mean “mechanic approved”. Then we can write the statement above in a more compact way:</p>
<p><span class="math display">\[
P(B | A) = P(B \text{ and } A) / P(A)
\]</span></p>
<p>To put this generally, conditional probabilities for two events <span class="math inline">\(X\)</span> and <span class="math inline">\(Y\)</span> can be written as:</p>
<p><span class="math inline">\(P(X | Y) = P(X \text{ and } Y) / P(Y)\)</span></p>
<p>Where (again) <span class="math inline">\(\text{ and }\)</span> means that <em>both</em> events occur.</p>
</section>
<section id="example-conditional-probability" class="level3" data-number="31.2.3">
<h3 data-number="31.2.3" class="anchored" data-anchor-id="example-conditional-probability"><span class="header-section-number">31.2.3</span> Example: conditional probability</h3>
<p>Let’s discuss a very relevant example. You get a Covid test, and the test is negative. Now, you would like to know what the chance is of you having Covid.</p>
<p>We have the following information:</p>
<ul>
<li>1.5% of people in your area have Covid</li>
<li>The false positive rate of the tests (i.e., that they detect Covid when it is absent) is very low at 0.5%</li>
<li>The false negative rate (i.e., that they fail to detect Covid when it is present) is quite high at 40%</li>
</ul>
<p><img src="diagrams/covid-tree.png" class="img-fluid"></p>
<p>Again, we start with our simulation.</p>
<div id="nte-bayes_covid" class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note 31.2: Notebook: Bayesian analysis of Covid test result
</div>
</div>
<div class="callout-body-container callout-body">
<div class="nb-links">
<p><a class="notebook-link" href="notebooks/bayes_covid.Rmd">Download notebook</a> <a class="interact-button" href="./interact/lab/index.html?path=bayes_covid.ipynb">Interact</a></p>
</div>
</div>
</div>
<div class="nb-start" name="bayes_covid" title="Bayesian analysis of Covid test result">
</div>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="co"># The number of people.</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>n_trials <span class="ot"><-</span> <span class="dv">1000000</span></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a><span class="co"># For each person, generate a True or False label,</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a><span class="co"># indicating that they have / don't have Covid.</span></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a>person_has_covid <span class="ot"><-</span> <span class="fu">sample</span>(</span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">FALSE</span>),</span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> <span class="at">size=</span>n_trials,</span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="at">replace=</span><span class="cn">TRUE</span>,</span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="at">prob=</span><span class="fu">c</span>(<span class="fl">0.015</span>, <span class="fl">0.985</span>)</span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a><span class="co"># Calculate the numbers of people with and without Covid.</span></span>
<span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a>n_with_covid <span class="ot"><-</span> <span class="fu">sum</span>(person_has_covid)</span>
<span id="cb5-15"><a href="#cb5-15" aria-hidden="true" tabindex="-1"></a>n_without_covid <span class="ot"><-</span> n_trials <span class="sc">-</span> n_with_covid</span>
<span id="cb5-16"><a href="#cb5-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-17"><a href="#cb5-17" aria-hidden="true" tabindex="-1"></a><span class="co"># In this array, we will store, for each person, whether they</span></span>
<span id="cb5-18"><a href="#cb5-18" aria-hidden="true" tabindex="-1"></a><span class="co"># had a positive or a negative test.</span></span>
<span id="cb5-19"><a href="#cb5-19" aria-hidden="true" tabindex="-1"></a>test_result <span class="ot"><-</span> <span class="fu">logical</span>(n_trials)</span>
<span id="cb5-20"><a href="#cb5-20" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-21"><a href="#cb5-21" aria-hidden="true" tabindex="-1"></a><span class="co"># Draw test results for people with Covid.</span></span>
<span id="cb5-22"><a href="#cb5-22" aria-hidden="true" tabindex="-1"></a>test_result[person_has_covid] <span class="ot"><-</span> <span class="fu">sample</span>(</span>
<span id="cb5-23"><a href="#cb5-23" aria-hidden="true" tabindex="-1"></a> <span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">FALSE</span>),</span>
<span id="cb5-24"><a href="#cb5-24" aria-hidden="true" tabindex="-1"></a> <span class="at">size=</span>n_with_covid,</span>
<span id="cb5-25"><a href="#cb5-25" aria-hidden="true" tabindex="-1"></a> <span class="at">replace=</span><span class="cn">TRUE</span>,</span>
<span id="cb5-26"><a href="#cb5-26" aria-hidden="true" tabindex="-1"></a> <span class="at">prob=</span><span class="fu">c</span>(<span class="fl">0.6</span>, <span class="fl">0.4</span>)</span>
<span id="cb5-27"><a href="#cb5-27" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb5-28"><a href="#cb5-28" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-29"><a href="#cb5-29" aria-hidden="true" tabindex="-1"></a><span class="co"># Draw test results for people without Covid.</span></span>
<span id="cb5-30"><a href="#cb5-30" aria-hidden="true" tabindex="-1"></a><span class="co"># !person_has_covid` flips all Boolean values from FALSE to TRUE</span></span>
<span id="cb5-31"><a href="#cb5-31" aria-hidden="true" tabindex="-1"></a><span class="co"># and from TRUE to FALSE.</span></span>
<span id="cb5-32"><a href="#cb5-32" aria-hidden="true" tabindex="-1"></a>test_result[<span class="sc">!</span>person_has_covid] <span class="ot"><-</span> <span class="fu">sample</span>(</span>
<span id="cb5-33"><a href="#cb5-33" aria-hidden="true" tabindex="-1"></a> <span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">FALSE</span>),</span>
<span id="cb5-34"><a href="#cb5-34" aria-hidden="true" tabindex="-1"></a> <span class="at">size=</span>n_without_covid,</span>
<span id="cb5-35"><a href="#cb5-35" aria-hidden="true" tabindex="-1"></a> <span class="at">replace=</span><span class="cn">TRUE</span>,</span>
<span id="cb5-36"><a href="#cb5-36" aria-hidden="true" tabindex="-1"></a> <span class="at">prob=</span><span class="fu">c</span>(<span class="fl">0.005</span>, <span class="fl">0.995</span>)</span>
<span id="cb5-37"><a href="#cb5-37" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb5-38"><a href="#cb5-38" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-39"><a href="#cb5-39" aria-hidden="true" tabindex="-1"></a><span class="co"># Get the Covid statuses of all those with negative tests</span></span>
<span id="cb5-40"><a href="#cb5-40" aria-hidden="true" tabindex="-1"></a><span class="co"># (`test_result` is a Boolean mask, like `[TRUE, FALSE, FALSE, TRUE, ...]`,</span></span>
<span id="cb5-41"><a href="#cb5-41" aria-hidden="true" tabindex="-1"></a><span class="co"># and `!test_result` flips all Boolean values to `[FALSE, TRUE, TRUE, FALSE, ...]`.</span></span>
<span id="cb5-42"><a href="#cb5-42" aria-hidden="true" tabindex="-1"></a>covid_status_negative_test <span class="ot"><-</span> person_has_covid[<span class="sc">!</span>test_result]</span>
<span id="cb5-43"><a href="#cb5-43" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-44"><a href="#cb5-44" aria-hidden="true" tabindex="-1"></a><span class="co"># Now, count how many with Covid had a negative test results.</span></span>
<span id="cb5-45"><a href="#cb5-45" aria-hidden="true" tabindex="-1"></a>n_with_covid_and_negative_test <span class="ot"><-</span> <span class="fu">sum</span>(covid_status_negative_test)</span>
<span id="cb5-46"><a href="#cb5-46" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-47"><a href="#cb5-47" aria-hidden="true" tabindex="-1"></a><span class="co"># And how many people, overall, had negative test results.</span></span>
<span id="cb5-48"><a href="#cb5-48" aria-hidden="true" tabindex="-1"></a>n_with_negative_test <span class="ot"><-</span> <span class="fu">length</span>(covid_status_negative_test)</span>
<span id="cb5-49"><a href="#cb5-49" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-50"><a href="#cb5-50" aria-hidden="true" tabindex="-1"></a>k <span class="ot"><-</span> n_with_covid_and_negative_test <span class="sc">/</span> n_with_negative_test</span>
<span id="cb5-51"><a href="#cb5-51" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-52"><a href="#cb5-52" aria-hidden="true" tabindex="-1"></a><span class="fu">message</span>(<span class="st">'Proportion with Covid of those with negative test: '</span>, <span class="fu">round</span>(k, <span class="dv">4</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Proportion with Covid of those with negative test: 0.0061</code></pre>
</div>
</div>
<p>This gives around 0.006 or 0.6%.</p>
<div class="nb-end">
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
End of notebook: Bayesian analysis of Covid test result
</div>
</div>
<div class="callout-body-container callout-body">
<p><code>bayes_covid</code> starts at <a href="#nte-bayes_covid" class="quarto-xref">Note <span>31.2</span></a>.</p>
</div>
</div>
<!---
End of notebook.
-->
<p>Now that we have a rough indication of what the answer should be, let’s try and calculate it directly, based on the tree of information shown earlier.</p>
<p>We will use these abbreviations:</p>
<ul>
<li><span class="math inline">\(C^+\)</span> means Covid positive (you do actually have Covid).</li>
<li><span class="math inline">\(C^-\)</span> means Covid negative (you do <em>not</em> actually have Covid).</li>
<li><span class="math inline">\(T^+\)</span> means the Covid <em>test</em> was positive.</li>
<li><span class="math inline">\(T^-\)</span> means the Covid <em>test</em> was negative.</li>
</ul>
<p>For example <span class="math inline">\(P(C^+ | T^-)\)</span> is the probability (<span class="math inline">\(P\)</span>) that you do actually have Covid (<span class="math inline">\(C^+\)</span>) <em>given that</em> (<span class="math inline">\(|\)</span>) the test was negative (<span class="math inline">\(T^-\)</span>).</p>
<p>We would like to know the probability of having Covid <em>given that</em> your test was negative (<span class="math inline">\(P(C^+ | T^-)\)</span>). Using the conditional probability relationship from above, we can write:</p>
<p><span class="math display">\[
P(C^+ | T^-) = P(C^+ \text{ and } T^-) / P(T^-)
\]</span></p>
<p>We see from the tree diagram that <span class="math inline">\(P(C^+ \text{ and } T^-) = P(T^- | C^+) * P(C^+) = .4 * .015 = 0.006\)</span>.</p>
<!---
**TODO: ADD REFERENCE TO SUMMATION OF MUTUALLY EXCLUSIVE PROBABILITIES**
-->
<p>We observe that <span class="math inline">\(P(T^-) = P(T^- \text{ and } C^-) + P(T^- \text{ and } C^+)\)</span>, i.e. that we can obtain a negative test result through two paths, having Covid or not having Covid. We expand these further as conditional probabilities:</p>
<p><span class="math inline">\(P(T^- \text{ and } C^-) = P(T^- | C^-) * P(C^-)\)</span></p>
<p>and</p>
<p><span class="math inline">\(P(T^- \text{ and } C^+) = P(T^- | C^+) * P(C^+)\)</span>.</p>
<p>We can now calculate</p>
<p><span class="math display">\[
P(T^-) = P(T^- | C^-) * P(C^-) + P(T^- | C^+) * P(C^+)
\]</span></p>
<p><span class="math display">\[
= .995 * .985 + .4 * .015 = 0.986
\]</span></p>
<p>The answer, then, is:</p>
<p><span class="math inline">\(P(C^+ | T^-) = 0.006 / 0.986 = 0.0061\)</span> or 0.61%.</p>
<p>This matches very closely our simulation result, so we have some confidence that we have done the calculation correctly.</p>
</section>
<section id="estimating-driving-risk-for-insurance-purposes" class="level3" data-number="31.2.4">
<h3 data-number="31.2.4" class="anchored" data-anchor-id="estimating-driving-risk-for-insurance-purposes"><span class="header-section-number">31.2.4</span> Estimating Driving Risk for Insurance Purposes</h3>
<p>Another sort of introductory problem, following after <span class="citation" data-cites="feller1968introduction">(<a href="references.html#ref-feller1968introduction" role="doc-biblioref">Feller 1968</a>, p 122)</span>:</p>
<div id="nte-bayes_accidents" class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note 31.3: Notebook: Bayesian analysis for insurance premium
</div>
</div>
<div class="callout-body-container callout-body">
<div class="nb-links">
<p><a class="notebook-link" href="notebooks/bayes_accidents.Rmd">Download notebook</a> <a class="interact-button" href="./interact/lab/index.html?path=bayes_accidents.ipynb">Interact</a></p>
</div>
</div>
</div>
<div class="nb-start" name="bayes_accidents" title="Bayesian analysis for insurance premium">
</div>
<p>A mutual insurance company charges its members according to the risk of having an car accident. It is known that there are two classes of people — 80 percent of the population with good driving judgment and with a probability of .06 of having an accident each year, and 20 percent with poor judgment and a probability of .6 of having an accident each year. The company’s policy is to charge $100 for each percent of risk, i. e., a driver with a probability of .6 should pay 60*$100 = $6000.</p>
<p>If nothing is known of a driver except that they had an accident last year, what fee should they pay?</p>
<p>Another way to phrase this question is: given that a driver had an accident last year, what is the probability of them having an accident overall?</p>
<p>We will proceed as follows:</p>
<ol type="1">
<li>Generate a population of N people. Label each as <code>good driver</code> or <code>poor driver</code>.</li>
<li>Simulate the last year for each person: did they have an accident or not?</li>
<li>Select only the ones that had an accident last year.</li>
<li>Among those, calculate what their average risk is of making an accident. This will indicate the appropriate insurance premium.</li>
</ol>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>n_trials <span class="ot"><-</span> <span class="dv">100000</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>cost_per_percent <span class="ot"><-</span> <span class="dv">100</span></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a>people <span class="ot"><-</span> <span class="fu">sample</span>(</span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">c</span>(<span class="st">'good driver'</span>, <span class="st">'poor driver'</span>),</span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a> <span class="at">size=</span>n_trials,</span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a> <span class="at">replace=</span><span class="cn">TRUE</span>,</span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a> <span class="at">prob=</span><span class="fu">c</span>(<span class="fl">0.8</span>, <span class="fl">0.2</span>)</span>
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a>good_driver <span class="ot"><-</span> (people <span class="sc">==</span> <span class="st">'good driver'</span>)</span>
<span id="cb7-12"><a href="#cb7-12" aria-hidden="true" tabindex="-1"></a>poor_driver <span class="ot"><-</span> <span class="sc">!</span>good_driver</span>
<span id="cb7-13"><a href="#cb7-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-14"><a href="#cb7-14" aria-hidden="true" tabindex="-1"></a><span class="co"># Did they have an accident last year?</span></span>
<span id="cb7-15"><a href="#cb7-15" aria-hidden="true" tabindex="-1"></a>had_accident <span class="ot"><-</span> <span class="fu">logical</span>(n_trials)</span>
<span id="cb7-16"><a href="#cb7-16" aria-hidden="true" tabindex="-1"></a>had_accident[good_driver] <span class="ot"><-</span> <span class="fu">sample</span>(</span>
<span id="cb7-17"><a href="#cb7-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">FALSE</span>),</span>
<span id="cb7-18"><a href="#cb7-18" aria-hidden="true" tabindex="-1"></a> <span class="at">size=</span><span class="fu">sum</span>(good_driver),</span>
<span id="cb7-19"><a href="#cb7-19" aria-hidden="true" tabindex="-1"></a> <span class="at">replace=</span><span class="cn">TRUE</span>,</span>
<span id="cb7-20"><a href="#cb7-20" aria-hidden="true" tabindex="-1"></a> <span class="at">prob=</span><span class="fu">c</span>(<span class="fl">0.06</span>, <span class="fl">0.94</span>)</span>
<span id="cb7-21"><a href="#cb7-21" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb7-22"><a href="#cb7-22" aria-hidden="true" tabindex="-1"></a>had_accident[poor_driver] <span class="ot"><-</span> <span class="fu">sample</span>(</span>
<span id="cb7-23"><a href="#cb7-23" aria-hidden="true" tabindex="-1"></a> <span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">FALSE</span>),</span>
<span id="cb7-24"><a href="#cb7-24" aria-hidden="true" tabindex="-1"></a> <span class="at">size=</span><span class="fu">sum</span>(poor_driver),</span>
<span id="cb7-25"><a href="#cb7-25" aria-hidden="true" tabindex="-1"></a> <span class="at">replace=</span><span class="cn">TRUE</span>,</span>
<span id="cb7-26"><a href="#cb7-26" aria-hidden="true" tabindex="-1"></a> <span class="at">prob=</span><span class="fu">c</span>(<span class="fl">0.6</span>, <span class="fl">0.4</span>)</span>
<span id="cb7-27"><a href="#cb7-27" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb7-28"><a href="#cb7-28" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-29"><a href="#cb7-29" aria-hidden="true" tabindex="-1"></a>ppl_with_accidents <span class="ot"><-</span> people[had_accident]</span>
<span id="cb7-30"><a href="#cb7-30" aria-hidden="true" tabindex="-1"></a>n_good_driver_accidents <span class="ot"><-</span> <span class="fu">sum</span>(ppl_with_accidents <span class="sc">==</span> <span class="st">'good driver'</span>)</span>
<span id="cb7-31"><a href="#cb7-31" aria-hidden="true" tabindex="-1"></a>n_poor_driver_accidents <span class="ot"><-</span> <span class="fu">sum</span>(ppl_with_accidents <span class="sc">==</span> <span class="st">'poor driver'</span>)</span>
<span id="cb7-32"><a href="#cb7-32" aria-hidden="true" tabindex="-1"></a>n_all_with_accidents <span class="ot"><-</span> n_good_driver_accidents <span class="sc">+</span> n_poor_driver_accidents</span>
<span id="cb7-33"><a href="#cb7-33" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-34"><a href="#cb7-34" aria-hidden="true" tabindex="-1"></a>avg_risk_percent <span class="ot"><-</span> (n_good_driver_accidents <span class="sc">*</span> <span class="fl">0.06</span> <span class="sc">+</span></span>
<span id="cb7-35"><a href="#cb7-35" aria-hidden="true" tabindex="-1"></a> n_poor_driver_accidents <span class="sc">*</span> <span class="fl">0.6</span>) <span class="sc">/</span></span>
<span id="cb7-36"><a href="#cb7-36" aria-hidden="true" tabindex="-1"></a> n_all_with_accidents <span class="sc">*</span> <span class="dv">100</span></span>
<span id="cb7-37"><a href="#cb7-37" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-38"><a href="#cb7-38" aria-hidden="true" tabindex="-1"></a>premium <span class="ot"><-</span> avg_risk_percent <span class="sc">*</span> cost_per_percent</span>
<span id="cb7-39"><a href="#cb7-39" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-40"><a href="#cb7-40" aria-hidden="true" tabindex="-1"></a><span class="fu">message</span>(<span class="st">'Premium is: '</span>, <span class="fu">round</span>(premium))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Premium is: 4418</code></pre>
</div>
</div>
<p>The answer should be around 4450 USD.</p>
<div class="nb-end">
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
End of notebook: Bayesian analysis for insurance premium
</div>
</div>
<div class="callout-body-container callout-body">
<p><code>bayes_accidents</code> starts at <a href="#nte-bayes_accidents" class="quarto-xref">Note <span>31.3</span></a>.</p>
</div>
</div>
<!---
End of notebook.
-->
</section>
<section id="screening-for-disease" class="level3" data-number="31.2.5">
<h3 data-number="31.2.5" class="anchored" data-anchor-id="screening-for-disease"><span class="header-section-number">31.2.5</span> Screening for Disease</h3>