{"id":2939,"date":"2020-08-28T14:08:58","date_gmt":"2020-08-28T19:08:58","guid":{"rendered":"https:\/\/laurentlessard.com\/bookproofs\/?p=2939"},"modified":"2020-08-28T17:25:10","modified_gmt":"2020-08-28T22:25:10","slug":"flawless-war","status":"publish","type":"post","link":"https:\/\/laurentlessard.com\/bookproofs\/flawless-war\/","title":{"rendered":"Flawless war"},"content":{"rendered":"<p>his week&#8217;s <a href=\"https:\/\/fivethirtyeight.com\/features\/can-you-cover-the-globe\/\">Riddler Classic<\/a> has to do with the card game &#8220;War&#8221;. Here is the problem, paraphrased:<\/p>\n<blockquote><p>\nWar is a two-player game in which a standard deck of cards is first shuffled and then divided into two piles with 26 cards each; one pile for each player. In every turn of the game, both players flip over and reveal the top card of their deck. The player whose card has a higher rank wins the turn and places both cards on the bottom of their pile. Assuming a deck is randomly shuffled before every game, how many games of War would you expect to play until you had a game that lasted just 26 turns (with no ties; a flawless victory)?\n<\/p><\/blockquote>\n<p>Here is my solution:<br \/>\n<a href=\"javascript:Solution('soln_flawless_war','toggle_flawless_war')\" id=\"toggle_flawless_war\">[Show Solution]<\/a><\/p>\n<div id=\"soln_flawless_war\" style=\"display: none\">\n<p>Suppose our cards have values $\\{1,2,\\dots,n\\}$ and the deck contains $m_i$ cards with value $i$. The total number of cards in the deck is equal to $m = m_1 +\\dots+m_n$, and we assume $m$ is even so that the deck can be evenly split in half. In a standard deck of playing cards, we have $n=13$ and $m_1=m_2=\\dots=m_n = 4$.<\/p>\n<p>Rather than thinking about &#8220;how many games we would expect to play before I had a flawless game&#8221;, let&#8217;s instead work out the probability that a game of War will be flawless for me (I wins in $m\/2$ turns with no ties). This is a counting problem: how many ways are there of arranging the cards in the deck so that I win flawlessly?<\/p>\n<p>To this effect, define $f(m_1,\\dots,m_n)$ to be the number of ways I can win flawlessly if the cards remaining have distribution $(m_1,\\dots,m_n)$. We will develop a recursive formulation of $f$.<\/p>\n<p>If I pick &#8220;$i$&#8221; in the first round, I will win the round with no tie if my opponent picks &#8220;$j$&#8221; with $j \\lt i$. This can happen in $m_i m_j$ ways, and then I can win the remaining rounds flawlessly in $f(\\dots)$ ways, where the arguments $m_i$ and $m_j$ are each decremented by $1$. Therefore, we can write:<br \/>\n\\[<br \/>\nf(m_1,\\dots,m_n) = \\sum_{1 \\leq j \\lt i \\leq n} m_i m_j f(m_1,\\dots,m_j-1,\\dots,m_i-1,\\dots,m_n)<br \/>\n\\]We also have the terminal condition $f(0,\\dots,0)=1$, for if we ever achieve this condition, there are no cards left and we have won!<\/p>\n<p>We can make two critical observations that will greatly simplify computation of $f$:<\/p>\n<ul>\n<li> <strong>We can prune out any zero entries.<\/strong> For example, $f(0,2,0,4,0,0) = f(2,4)$. This follows because when a particular card number is not present in the deck, we may relabel the numbers on the cards and simply skip over the missing number.\n<li> <strong>We can rearrange arguments.<\/strong> For example, $f(6,4,1,7) = f(1,4,6,7)$. This is due to the fact that our recursive equation is symmetric in the arguments (all pairs of indices are included in the sum and contribute similarly to the count) and our base case $f(0,\\dots,0)$ is symmetric as well.\n<\/ul>\n<p>Equipped with these tools, we can write very efficient code to compute the solution for the case of a standard deck of cards. Here is an efficient recursive implementation in Python that computes the solution for a standard deck of cards.<\/p>\n<pre>\r\nfrom itertools import combinations\r\n\r\ndef memoize(f):\r\n    memo = {}\r\n    def helper(x):\r\n        # prune zeros and sort!\r\n        y = tuple( sorted([z for z in x if z > 0]) )\r\n        if y not in memo:\r\n            memo[y] = f(y)\r\n        return memo[y]\r\n    return helper\r\n\r\n@memoize\r\ndef winning_hands(counts):\r\n    N = len(counts)    \r\n    if N == 0:  # base case (no cards left in the deck)    \r\n        return 1\r\n    \r\n    out = 0\r\n    for i1,i2 in combinations(range(N),2):\r\n        tmp = list(counts)\r\n        tmp[i1] -= 1\r\n        tmp[i2] -= 1\r\n        out += counts[i1] * counts[i2] * winning_hands(tmp)\r\n    return out\r\n\r\nwinning_hands( 13 * [4] )\r\n<\/pre>\n<p>The code took about 100ms to compute the answer:<br \/>\n\\[<br \/>\n252656585660288881535364185959261220490470307542335488000000<br \/>\n\\]<\/p>\n<p>We can divide this by the total number of hands (which is $m!$) to obtain the probability of winning flawlessly.<\/p>\n<pre>\r\nfrom math import factorial\r\nfrom fractions import Fraction\r\n\r\nFraction( winning_hands(13 * [4]), factorial(52) )\r\n<\/pre>\n<p>And the result is:<br \/>\n\\begin{align}<br \/>\np &#038;= \\mathrm{Prob}(\\text{flawless win}) \\\\<br \/>\n&#038;= \\frac{93686147409122073121}{29908397871631390876014250000} \\\\<br \/>\n&#038;\\approx 3.132436\\times 10^{-9}<br \/>\n\\end{align}<\/p>\n<p>This is a very small number, but the more we play, the likelier we are to win. To quantify this, suppose we play $N$ games. The probability that we win flawlessly at least once is $1-(1-p)^N$. We can plot this as a function of $N$ to see how it grows:<\/p>\n<p><a href=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2020\/08\/war_flawless.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2020\/08\/war_flawless-1024x715.png\" alt=\"\" width=\"840\" height=\"587\" class=\"aligncenter size-large wp-image-2943\" srcset=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2020\/08\/war_flawless-1024x715.png 1024w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2020\/08\/war_flawless-300x209.png 300w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2020\/08\/war_flawless-768x536.png 768w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2020\/08\/war_flawless-1200x837.png 1200w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2020\/08\/war_flawless.png 1297w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><\/p>\n<p>So to have a $50\\%$ chance of winning flawlessly at least once, I would have to play around 220 million times. To have a $95\\%$ chance of winning flawlessly at least once, I would have to play around 1 billion times. Needless to say, that&#8217;s a lot of war.<\/p>\n<p><!--\n\n\n<h3>Approximate solution<\/h3>\n\n\n\nAlthough I had to resort to a computational approach to get the solution, there are nonetheless ways we can approximate the general solution analytically.\n\n<strong>Upper bound:<\/strong> If we assume all cards in the deck are different (numbered from $1$ to $52$, say) then there is no possibility for a tie. The problem is much easier in this case, because each round, you have precisely a $\\frac{1}{2}$ probability of winning the round. Therefore, if we let $m = m_1+\\dots+m_n$, we can write:\n\\[\nf(m_1,\\dots,m_n) \\leq \\frac{1}{2^m}\n\\]\n\n<strong>Lower bound:<\/strong>\n--><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>his week&#8217;s Riddler Classic has to do with the card game &#8220;War&#8221;. Here is the problem, paraphrased: War is a two-player game in which a standard deck of cards is first shuffled and then divided into two piles with 26 cards each; one pile for each player. In every turn of the game, both players &hellip; <a href=\"https:\/\/laurentlessard.com\/bookproofs\/flawless-war\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Flawless war&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2943,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[7],"tags":[6,8,15,2],"class_list":["post-2939","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-riddler","tag-counting","tag-probability","tag-recursion","tag-riddler"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts\/2939","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/comments?post=2939"}],"version-history":[{"count":7,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts\/2939\/revisions"}],"predecessor-version":[{"id":2949,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts\/2939\/revisions\/2949"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/media\/2943"}],"wp:attachment":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/media?parent=2939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/categories?post=2939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/tags?post=2939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}