{"id":809,"date":"2021-01-27T08:28:29","date_gmt":"2021-01-27T13:28:29","guid":{"rendered":"https:\/\/www.clayford.net\/statistics\/?p=809"},"modified":"2021-01-28T08:56:27","modified_gmt":"2021-01-28T13:56:27","slug":"would-you-rather","status":"publish","type":"post","link":"https:\/\/www.clayford.net\/statistics\/would-you-rather\/","title":{"rendered":"Would you rather"},"content":{"rendered":"<p>Someone texted me the following image and asked for my response.<\/p>\n<p><a href=\"https:\/\/www.clayford.net\/statistics\/wp-content\/uploads\/2021\/01\/coins_v_dice.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.clayford.net\/statistics\/wp-content\/uploads\/2021\/01\/coins_v_dice-1024x768.jpg\" alt=\"\" width=\"625\" height=\"469\" class=\"alignnone size-large wp-image-810\" srcset=\"https:\/\/www.clayford.net\/statistics\/wp-content\/uploads\/2021\/01\/coins_v_dice-1024x768.jpg 1024w, https:\/\/www.clayford.net\/statistics\/wp-content\/uploads\/2021\/01\/coins_v_dice-300x225.jpg 300w, https:\/\/www.clayford.net\/statistics\/wp-content\/uploads\/2021\/01\/coins_v_dice-768x576.jpg 768w, https:\/\/www.clayford.net\/statistics\/wp-content\/uploads\/2021\/01\/coins_v_dice-624x468.jpg 624w, https:\/\/www.clayford.net\/statistics\/wp-content\/uploads\/2021\/01\/coins_v_dice.jpg 1280w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p>So what would I rather do?<\/p>\n<ul>\n<li>Flip 3 coins and win if all match<\/li>\n<li>Roll 3 dice and win if none match<\/li>\n<\/ul>\n<p>I would rather do the one with the highest probability of happening. So let\u2019s calculate that using R.<\/p>\n<p>There are two ways to solve problems like this. One is to use the appropriate probability distribution and calculate it mathematically. The other is to enumerate all possible outcomes and find the proportion of interest. Let\u2019s do the latter first.<\/p>\n<p>We\u2019ll use the <code>expand.grid<\/code> function to create a data frame of all possible outcomes of flipping 3 coins. All we have to do is give it 3 vectors representing coins. It\u2019s such a small sample space we can eyeball it and see the probability of getting all heads or all tails is 2\/8 or 0.25.<\/p>\n<pre class=\"r\"><code>s1 &lt;- expand.grid(c(&quot;H&quot;, &quot;T&quot;), c(&quot;H&quot;, &quot;T&quot;), c(&quot;H&quot;, &quot;T&quot;))\r\ns1<\/code><\/pre>\n<pre><code>##   Var1 Var2 Var3\r\n## 1    H    H    H\r\n## 2    T    H    H\r\n## 3    H    T    H\r\n## 4    T    T    H\r\n## 5    H    H    T\r\n## 6    T    H    T\r\n## 7    H    T    T\r\n## 8    T    T    T<\/code><\/pre>\n<p>If we wanted to use R to determine the proportion, we could use <code>apply<\/code> to apply a function to each row and return the number of unique values, and then find the proportion of <code>1<\/code>s. (<code>1<\/code> means there was only one unique value: all \u201cH\u201d or all \u201cT\u201d)<\/p>\n<pre class=\"r\"><code>count1 &lt;- apply(s1, 1, function(x)length(unique(x)))\r\nmean(count1 == 1)<\/code><\/pre>\n<pre><code>## [1] 0.25<\/code><\/pre>\n<p>We can do the same with rolling 3 dice. Give <code>expand.grid<\/code> 3 dice and have it generate all possible results. This will generate 216 possibilities, so there\u2019s no eyeballing this for an answer. As before, we\u2019ll apply a function to each row and determine if there are any duplicates. The <code>anyDuplicated<\/code> function returns the location of the first duplicate within a vector. If there are no duplicates, the result is a <code>0<\/code>, which means we just need to find the proportion of <code>0<\/code>s.<\/p>\n<pre class=\"r\"><code>s2 &lt;- expand.grid(1:6, 1:6, 1:6)\r\ncount2 &lt;- apply(s2, 1, anyDuplicated)\r\nmean(count2 == 0)<\/code><\/pre>\n<pre><code>## [1] 0.5555556<\/code><\/pre>\n<p>An easier way to do that is to use permutation calculations. There are \\(6^3 = 216\\) possible results from rolling 3 dice. Furthermore there are \\(6 \\cdot 5 \\cdot 4 = 120\\) possible non-matching results. There are 6 possibilities for the first die, only 5 for the second, and 4 for the third. We can then divide to find the probability: \\(120\/216 = 0.55\\).<\/p>\n<p>Clearly we\u2019d rather roll the dice (assuming fair coins and fair dice).<\/p>\n<p>We can also answer the question using the binomial probability distribution. The binomial distribution is appropriate when you have:<\/p>\n<ul>\n<li>two outcomes (Heads vs Tales, no match vs One or more match)<\/li>\n<li>independent events<\/li>\n<li>same probability for each event<\/li>\n<\/ul>\n<p>The <code>dbinom<\/code> function calculates probabilities of binomial outcomes. Below we use it to calculate the probability of 0 heads (3 tails) and 3 heads, and sum the total. The <code>x<\/code> argument is the sum of \u201csuccesses\u201d, for example 0 heads (or tails, whatever you call a \u201csuccess\u201d). The <code>size<\/code> argument is the number of trials, or coins in this case. The <code>prob<\/code> argument is the probability of success on each trial, or for each coin in this case.<\/p>\n<pre class=\"r\"><code>dbinom(x = 0, size = 3, prob = 0.5) + \r\n  dbinom(x = 3, size = 3, prob = 0.5)<\/code><\/pre>\n<pre><code>## [1] 0.25<\/code><\/pre>\n<p>We can also frame the dice rolling as a binomial outcome, but it\u2019s a little trickier. Think of rolling the dice one at a time:<\/p>\n<ul>\n<li>It doesn\u2019t matter what we roll the first time. We don\u2019t care if it\u2019s a 1 or 6 or whatever. We\u2019re certain to get something, so the probability is 1.<\/li>\n<li>The second role is a \u201csuccess\u201d if it <em>does not<\/em> match the first role. That\u2019s one dice roll (<code>size = 1<\/code>) where success (<code>x = 1<\/code>) happens with probability of 5\/6.<\/li>\n<li>The third and final roll is a \u201csuccess\u201d if it doesn\u2019t match either of the first two rolls. That\u2019s one dice roll (<code>size = 1<\/code>) where success (<code>x = 1<\/code>) happens with probability of 4\/6.<\/li>\n<\/ul>\n<p>Notice we don\u2019t add these probabilities but rather <em>multiply<\/em> them.<\/p>\n<pre class=\"r\"><code>1 * dbinom(x = 1, size = 1, prob = 5\/6) *\r\n  dbinom(x = 1, size = 1, prob = 4\/6)<\/code><\/pre>\n<pre><code>## [1] 0.5555556<\/code><\/pre>\n<p>We added the coin flipping probabilities because they each represented mutually exclusive events. They cannot both occur. There is one way to get 0 successes and one way to get 3 successes. Together they sum to the probability of all matching (ie, all heads or all tales).<\/p>\n<p>We multiplied the dice rolling probabilities because they each represented events that could occur at the same time, and they were conditional if we considered each die in turn. This requires the multiplication rule of probability.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Someone texted me the following image and asked for my response. So what would I rather do? Flip 3 coins&#8230; <a class=\"read-more\" href=\"https:\/\/www.clayford.net\/statistics\/would-you-rather\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,13],"tags":[],"class_list":["post-809","post","type-post","status-publish","format-standard","hentry","category-probability","category-using-r"],"_links":{"self":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/809","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/comments?post=809"}],"version-history":[{"count":4,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/809\/revisions"}],"predecessor-version":[{"id":814,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/809\/revisions\/814"}],"wp:attachment":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/media?parent=809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/categories?post=809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/tags?post=809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}