{"id":3363,"date":"2024-02-25T20:04:02","date_gmt":"2024-02-25T20:04:02","guid":{"rendered":"http:\/\/optimumsportsperformance.com\/blog\/?p=3363"},"modified":"2024-02-27T15:28:13","modified_gmt":"2024-02-27T15:28:13","slug":"bayes-theorem-pab-pa-pba-pb-but-why","status":"publish","type":"post","link":"https:\/\/optimumsportsperformance.com\/blog\/bayes-theorem-pab-pa-pba-pb-but-why\/","title":{"rendered":"Bayes Theorem: p(A|B) = p(A) * p(B|A) \/ p(B) &#8212; But why?"},"content":{"rendered":"<p>A student recently asked me if I could show them why Bayes Theorem looks the way it does. If we are trying to determine the probability A given B, how did we arrive at the theorem in the title?<\/p>\n<p>Let&#8217;s create some data and see if we can sort it out. We will simulate a 2&#215;2 table, similar to one we might find in sports medicine journals when looking at some type of test (positive or negative) and some type of outcome (disease or no disease).<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\ndat_tbl &lt;- data.frame(\r\n  test = c(&quot;positive&quot;, &quot;negative&quot;, &quot;total&quot;),\r\n  disease = c(25, 5, 30),\r\n  no_disease = c(3, 40, 43),\r\n  total = c(28, 45, 73)\r\n)\r\n\r\ndat_tbl\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.26.57\u202fAM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3364\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.26.57\u202fAM.png\" alt=\"\" width=\"489\" height=\"176\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.26.57\u202fAM.png 584w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.26.57\u202fAM-300x108.png 300w\" sizes=\"auto, (max-width: 489px) 100vw, 489px\" \/><\/a><\/p>\n<p>A logical question we&#8217;d like to answer here is, <strong><em>&#8220;What is the probability of disease given a positive test.&#8221; <\/em><\/strong>Written in probability notation we are asking, <em><strong>p(Disease | Positive)<\/strong><\/em>.<\/p>\n<p>Using the data in the table, we can quickly compute this as 25 people who were positive and had the disease divided by 28 total positive tests. <em><strong>25\/28 = 89.3%<\/strong><\/em><\/p>\n<p>Of course, we could also compute this using Bayes Theorem:<\/p>\n<p style=\"text-align: center;\"><em><strong>p(Disease | Positive) = p(Disease) * p(Positive | Disease) \/ p(Positive)<\/strong><\/em><\/p>\n<p>We store the necessary values in R objects and then compute the result<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n### What we want to know: p(Disease | Positive)\r\n# p(Disease | Positive) = p(Disease) * p(Positive | Disease) \/ p(Positive)\r\n# p(A | B) = p(A) * p(B|A) \/ p(B)\r\n\r\np_disease &lt;- 30\/73\r\np_positive_given_disease &lt;- 25\/30\r\np_positive &lt;- 28\/73\r\n\r\np_no_disease &lt;- 43\/73\r\np_positive_given_no_disease &lt;- 3\/43\r\n\r\np_disease_given_positive &lt;- (p_disease * p_positive_given_disease) \/ (p_disease * p_positive_given_disease + p_no_disease * p_positive_given_no_disease) \r\np_disease_given_positive\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.32.13\u202fAM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3365\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.32.13\u202fAM-1024x320.png\" alt=\"\" width=\"625\" height=\"195\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.32.13\u202fAM-1024x320.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.32.13\u202fAM-300x94.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.32.13\u202fAM-768x240.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.32.13\u202fAM-624x195.png 624w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.32.13\u202fAM.png 1832w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p>This checks out. We get the exact same result as when we did 25\/28.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Okay, how did we get here? Why does it work out like that?<\/strong><\/span><\/p>\n<p>The math works out because we start with two different joint probabilities, p(A n B) and p(B n A). Or, in our case, p(Disease n Positive) and p(Positive n Disease). Formally, we can write these as follows:<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.39.00\u202fAM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3366\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.39.00\u202fAM.png\" alt=\"\" width=\"409\" height=\"293\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.39.00\u202fAM.png 598w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.39.00\u202fAM-300x215.png 300w\" sizes=\"auto, (max-width: 409px) 100vw, 409px\" \/><\/a><\/p>\n<p>We&#8217;ve already stored the necessary probabilities in specific elements, above. But here it what they both look like using our 2&#215;2. First I&#8217;ll calculate it with the counts directly from the table and then calculate it with the R elements that we stored. You&#8217;ll see the answers are the same.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n## Joint Probability 1: p(Positive n Disease) = p(Positive | Disease) * p(Disease)\r\n\r\n25\/30 * 30\/73\r\n\r\np_positive_given_disease * p_disease\r\n\r\n## Joint Probability 2: p(Disease n Positive) = p(Disease | Positive) * p(Positive)\r\n\r\n25\/28 * 28\/73\r\n\r\np_disease_given_positive * p_positive\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.39.46\u202fAM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3367\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.39.46\u202fAM-1024x420.png\" alt=\"\" width=\"625\" height=\"256\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.39.46\u202fAM-1024x420.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.39.46\u202fAM-300x123.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.39.46\u202fAM-768x315.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.39.46\u202fAM-624x256.png 624w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.39.46\u202fAM.png 1424w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p><em><strong>Well, would you look at<\/strong> <strong>that! <\/strong><\/em>The two joint probabilities are equal to each other. We can formally test that they are equal to each other by setting up a logical equation in R.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n## These two joint probabilities are equal!\r\n#  p(Positive n Disease) = p(Disease n Positive)\r\n\r\n(p_positive_given_disease * p_disease) == (p_disease_given_positive * p_positive)\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.48.29\u202fAM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3368\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.48.29\u202fAM-1024x149.png\" alt=\"\" width=\"625\" height=\"91\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.48.29\u202fAM-1024x149.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.48.29\u202fAM-300x44.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.48.29\u202fAM-768x112.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.48.29\u202fAM-624x91.png 624w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.48.29\u202fAM.png 1336w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p>So, if they are equal, what we are saying is this:<\/p>\n<p style=\"text-align: center;\"><em><strong> p(Disease | Positive) * p(Positive) = p(Positive | Disease) * p(Disease)<\/strong><\/em><\/p>\n<p>Now, with some algebra, we can divide the right side of the equation by <em><strong>p(Positive)<\/strong><\/em> and we are left with Bayes Theorem for our problem:<\/p>\n<p style=\"text-align: center;\"><em><strong>p(Disease | Positive) = p(Disease) * p(Positive | Disease) \/ p(Positive)<\/strong><\/em><\/p>\n<p>Putting it altogether, it looks like this:<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.59.39\u202fAM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3369\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.59.39\u202fAM.png\" alt=\"\" width=\"524\" height=\"203\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.59.39\u202fAM.png 728w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.59.39\u202fAM-300x116.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-25-at-11.59.39\u202fAM-624x242.png 624w\" sizes=\"auto, (max-width: 524px) 100vw, 524px\" \/><\/a><\/p>\n<p>So, all we&#8217;ve done is taken two joint probabilities and used some algebra to arrange the terms in order to get us to the conditional probability we were interested in, <em><strong>p(Disease | Positive)<\/strong><\/em> and we end up with Bayes Theorem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A student recently asked me if I could show them why Bayes Theorem looks the way it does. If we are trying to determine the probability A given B, how did we arrive at the theorem in the title? Let&#8217;s create some data and see if we can sort it out. We will simulate a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-3363","post","type-post","status-publish","format-standard","hentry","category-bayesian-model-building"],"_links":{"self":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/3363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/comments?post=3363"}],"version-history":[{"count":2,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/3363\/revisions"}],"predecessor-version":[{"id":3371,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/3363\/revisions\/3371"}],"wp:attachment":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/media?parent=3363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/categories?post=3363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/tags?post=3363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}