<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Python on Analysis Paralysis</title><link>https://blog.recommend.games/tags/python/</link><description>Recent content in Python on Analysis Paralysis</description><generator>Hugo -- gohugo.io</generator><language>en</language><managingEditor>contact@recommend.games (Recommend.Games)</managingEditor><webMaster>contact@recommend.games (Recommend.Games)</webMaster><lastBuildDate>Wed, 23 Oct 2024 12:00:00 +0300</lastBuildDate><atom:link href="https://blog.recommend.games/tags/python/index.xml" rel="self" type="application/rss+xml"/><item><title>Beyond games: Visualising the Riemann Hypothesis</title><link>https://blog.recommend.games/posts/visualising-riemann-hypothesis/</link><pubDate>Wed, 23 Oct 2024 12:00:00 +0300</pubDate><author>contact@recommend.games (Recommend.Games)</author><guid>https://blog.recommend.games/posts/visualising-riemann-hypothesis/</guid><description>&lt;p&gt;This subject has nothing to do with games nor data science, but chances are that amongst the readers of this blogs are many who appreciate mathematics – and its beauty. Long before Recommend.Games, I&amp;rsquo;ve had a &lt;a href="https://primes.recommend.games/"&gt;blog on the Riemann Hypothesis&lt;/a&gt; – the famous conjecture which is usually formulated in terms of the zeros of the complex Riemann \(\zeta\)-function, but is really about the distribution of prime numbers. If you&amp;rsquo;re curious to learn more, check out the blog. 🤓&lt;/p&gt;
&lt;p&gt;The reason I bring this up now is that I just re-created and updated an old animation I&amp;rsquo;ve uploaded to YouTube &lt;a href="https://youtu.be/NAMuls4q2f4"&gt;almost ten years ago&lt;/a&gt;:&lt;/p&gt;
&lt;div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;"&gt;
 &lt;iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen" loading="eager" referrerpolicy="strict-origin-when-cross-origin" src="https://www.youtube.com/embed/CkwlwL8hv2Q?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" title="YouTube video"&gt;&lt;/iframe&gt;
 &lt;/div&gt;

&lt;p&gt;The line you&amp;rsquo;re watching going round and round in circles is a plot of the values of said \(\zeta\)-function on what&amp;rsquo;s called the &lt;em&gt;critical line&lt;/em&gt; \(s=\frac12+ti\). Every time the line hits the origin, we&amp;rsquo;ve just witnessed another one of those enigmatic \(\zeta\)-zeros. We know that there are infinitely many of those zeros on the critical line; the Riemann Hypothesis states that there are none elsewhere (except for the trivial zeros at negative even integers).&lt;/p&gt;
&lt;p&gt;If this all sounds really confusing and you have no idea how on earth this could possibly be connected to prime numbers, I&amp;rsquo;d again refer you to my &lt;a href="https://primes.recommend.games/"&gt;other blog&lt;/a&gt; where I&amp;rsquo;ve tried to explain those questions with very little mathematical prerequisites.&lt;/p&gt;
&lt;p&gt;But the real reason I wanted to draw your attention to the video: I just find it so pretty. 😇 Even if you don&amp;rsquo;t care for analytic number theory, I hope you can still appreciate the beauty of this spiral making seemingly unpredictable narrow and wide turns. If you really want to unwind and get hypnotised by the \(\zeta\)-function, I&amp;rsquo;ve also create an hour-long version:&lt;/p&gt;
&lt;div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;"&gt;
 &lt;iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen" loading="eager" referrerpolicy="strict-origin-when-cross-origin" src="https://www.youtube.com/embed/TzeZGQfVlsw?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" title="YouTube video"&gt;&lt;/iframe&gt;
 &lt;/div&gt;

&lt;p&gt;Enjoy! I promise, the next post will be about board games again… 😎&lt;/p&gt;</description></item><item><title>How long is a game of High Society?</title><link>https://blog.recommend.games/posts/high-society/</link><pubDate>Tue, 27 Feb 2024 22:37:01 +0200</pubDate><author>contact@recommend.games (Recommend.Games)</author><guid>https://blog.recommend.games/posts/high-society/</guid><description>&lt;p&gt;&lt;a href="https://recommend.games/#/game/220" style="font-variant: small-caps;"&gt;High Society&lt;/a&gt; is a classic bidding game by classic designer &lt;a href="https://recommend.games/#/?designer=2"&gt;Dr Reiner Knizia&lt;/a&gt;, most recently released by &lt;a href="https://www.ospreypublishing.com/uk/osprey-games/"&gt;Osprey Games&lt;/a&gt; with a wonderfully classic look:&lt;/p&gt;

&lt;img
	src="https://blog.recommend.games/posts/high-society/high_society_cover_hu_b48947f1060cc7a1.jpg"
	alt="Cover of High Society"
	 /&gt;

&lt;p&gt;The general concept is quite simple: the players are members of said &amp;ldquo;high society&amp;rdquo; and are trying to outdo each other in showing off their wealth. The game is played over a series of rounds, and in each round, a card is revealed from a deck of 16 cards. The players then take turns bidding on the card, and the player who wins the bid gets the card. The cards are worth points, and the player with the most points at the end of the game wins.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s the twist though: The player with the least money at the end of the game is disqualified (that poor bastard can no longer be accepted as a member of this exclusive club!), so the players are trying to get the most points while also keeping enough money to stay in the game. This provides agonising decisions every turn – as well as a biting satire of the upper class.&lt;/p&gt;
&lt;p&gt;One interesting detail is the way the game ends. There&amp;rsquo;s a total of 16 cards to be autioned off in the deck; 4 of these cards have a dark background. As soon as the 4th dark card is revealed, the game ends immediately. (That last card isn&amp;rsquo;t sold anymore.) This looming end adds extra tension to the game as players need to take gambles towards the end of the game and try to guess how many more chances they&amp;rsquo;ll get to bid.&lt;/p&gt;
&lt;p&gt;This is where the maths comes in. While the strategy is way too deep to be easily analysed (and involves some interesting meta in pricing the cards), modelling the game length is pretty straightforward and we&amp;rsquo;ll do so in a couple of different ways. We&amp;rsquo;ll start with a simple simulation to understand what&amp;rsquo;s going on. Then we&amp;rsquo;ll develop exact formulae, which will culminate in meeting your new best friend: the negative hypergeometric distribution.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s settle one important convention first though: In everything that follows, I&amp;rsquo;ll only count the cards that are actually auctioned off. The last card is never auctioned off, so it doesn&amp;rsquo;t count towards the game length. This means that the shortest possible game is 3 rounds long: this happens if all 4 dark cards are on top of the deck (very unlikely). The longest possible game on the other hand is 15 rounds long: this happens if the card at the bottom of the deck is dark and hence the 15 cards above it will be autioned off (actually pretty likely, as we shall see).&lt;/p&gt;
&lt;h2 id="simulation"&gt;Simulation&lt;/h2&gt;
&lt;p&gt;It&amp;rsquo;s really simple to simulate this situation. Just create a bunch of (virtual) decks of 16 cards each, colour 4 of them dark, then shuffle them. Now we just need to count where in each of the decks the 4th dark card is. If our sample was big enough, this gives us a really good approximation of the distribution of game lengths.&lt;/p&gt;
&lt;p&gt;If you know a little bit of Python and the ubiquitous &lt;code&gt;numpy&lt;/code&gt; library, you can do this in just a few lines of code, so I thought I&amp;rsquo;d walk you through it. If you don&amp;rsquo;t care about coding, just skip ahead to the results. (Be warned that formulae are coming up next though, so the code might be the relaxing bit.)&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;import&lt;/span&gt; numpy &lt;span style="color:#66d9ef"&gt;as&lt;/span&gt; np
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;# Set up the parameters&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;num_cards &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;16&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;num_dark &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;4&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;num_games &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;1_000_000&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;rng &lt;span style="color:#f92672"&gt;=&lt;/span&gt; np&lt;span style="color:#f92672"&gt;.&lt;/span&gt;random&lt;span style="color:#f92672"&gt;.&lt;/span&gt;default_rng(seed&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;13&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;# Create the decks: num_games rows, num_cards columns&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;games &lt;span style="color:#f92672"&gt;=&lt;/span&gt; np&lt;span style="color:#f92672"&gt;.&lt;/span&gt;zeros((num_games, num_cards), dtype&lt;span style="color:#f92672"&gt;=&lt;/span&gt;bool)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;# Colour the last four columns in each row dark&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;games[:, :num_dark] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;True&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;# Shuffle each row individually&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;games &lt;span style="color:#f92672"&gt;=&lt;/span&gt; rng&lt;span style="color:#f92672"&gt;.&lt;/span&gt;permuted(games, axis&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;, out&lt;span style="color:#f92672"&gt;=&lt;/span&gt;games)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;# Count the game lengths: in each row,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;# sum the number of dark cards revealed so far.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;# Every column where this cumulative sum is&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;# less than the number of dark cards is a round of the game.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;game_lengths &lt;span style="color:#f92672"&gt;=&lt;/span&gt; np&lt;span style="color:#f92672"&gt;.&lt;/span&gt;sum(np&lt;span style="color:#f92672"&gt;.&lt;/span&gt;cumsum(games, axis&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;) &lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt; num_dark, axis&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;# Mean and standard deviation of the game lengths&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;print(game_lengths&lt;span style="color:#f92672"&gt;.&lt;/span&gt;mean(), game_lengths&lt;span style="color:#f92672"&gt;.&lt;/span&gt;std())
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;# Histogram the game lengths&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;unique_lengths, length_counts &lt;span style="color:#f92672"&gt;=&lt;/span&gt; np&lt;span style="color:#f92672"&gt;.&lt;/span&gt;unique(game_lengths, return_counts&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;True&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;print(dict(zip(unique_lengths, length_counts &lt;span style="color:#f92672"&gt;/&lt;/span&gt; num_games)))
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Don&amp;rsquo;t worry if you can&amp;rsquo;t follow the code immediately, I&amp;rsquo;ll admit to playing a bit of code golf there. The important part are the results anyways, which should look something like this: the mean game length is &lt;strong&gt;12.6 rounds&lt;/strong&gt; with a &lt;strong&gt;standard deviation of 2.3&lt;/strong&gt;. This is the full distribution of game lengths:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: right"&gt;Length&lt;/th&gt;
 &lt;th style="text-align: right"&gt;Probability&lt;/th&gt;
 &lt;th&gt;&lt;/th&gt;
 &lt;th style="text-align: right"&gt;Length&lt;/th&gt;
 &lt;th style="text-align: right"&gt;Probability&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: right"&gt;3&lt;/td&gt;
 &lt;td style="text-align: right"&gt;0.1%&lt;/td&gt;
 &lt;td&gt;&lt;/td&gt;
 &lt;td style="text-align: right"&gt;10&lt;/td&gt;
 &lt;td style="text-align: right"&gt;6.6%&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: right"&gt;4&lt;/td&gt;
 &lt;td style="text-align: right"&gt;0.2%&lt;/td&gt;
 &lt;td&gt;&lt;/td&gt;
 &lt;td style="text-align: right"&gt;11&lt;/td&gt;
 &lt;td style="text-align: right"&gt;9.1%&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: right"&gt;5&lt;/td&gt;
 &lt;td style="text-align: right"&gt;0.5%&lt;/td&gt;
 &lt;td&gt;&lt;/td&gt;
 &lt;td style="text-align: right"&gt;12&lt;/td&gt;
 &lt;td style="text-align: right"&gt;12.1%&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: right"&gt;6&lt;/td&gt;
 &lt;td style="text-align: right"&gt;1.1%&lt;/td&gt;
 &lt;td&gt;&lt;/td&gt;
 &lt;td style="text-align: right"&gt;13&lt;/td&gt;
 &lt;td style="text-align: right"&gt;15.8%&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: right"&gt;7&lt;/td&gt;
 &lt;td style="text-align: right"&gt;1.9%&lt;/td&gt;
 &lt;td&gt;&lt;/td&gt;
 &lt;td style="text-align: right"&gt;14&lt;/td&gt;
 &lt;td style="text-align: right"&gt;20.0%&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: right"&gt;8&lt;/td&gt;
 &lt;td style="text-align: right"&gt;3.1%&lt;/td&gt;
 &lt;td&gt;&lt;/td&gt;
 &lt;td style="text-align: right"&gt;15&lt;/td&gt;
 &lt;td style="text-align: right"&gt;25.0%&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: right"&gt;9&lt;/td&gt;
 &lt;td style="text-align: right"&gt;4.6%&lt;/td&gt;
 &lt;td&gt;&lt;/td&gt;
 &lt;td style="text-align: right"&gt;&lt;/td&gt;
 &lt;td&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;If you prefer the visual representation, here&amp;rsquo;s a histogram of the game lengths:&lt;/p&gt;

&lt;img
	src="https://blog.recommend.games/posts/high-society/game_lengths.svg"
	alt="Histogram of game lengths"
	 /&gt;

&lt;p&gt;As promised, very short games are extremely rare, whilst long games are in fact the most common. This isn&amp;rsquo;t necessarily what I would have expected when starting to think about this problem. I think there&amp;rsquo;s a pretty good intuition for why this is the case, but it&amp;rsquo;s instructive to examine the distribution from a more theoretic point of view first.&lt;/p&gt;
&lt;h2 id="hypergeometric-distribution"&gt;Hypergeometric distribution&lt;/h2&gt;
&lt;p&gt;If you&amp;rsquo;ve done your statistics 101, you&amp;rsquo;ve indubitably come across questions about poker hands. E.g., how likely is it to get a flush when drawing 5 cards out of a standard deck of 52? The answer to this question can be calculated using the hypergeometric distribution. If you find the name intimidating, wait till you see the formula:&lt;/p&gt;
&lt;p&gt;\[ p_\text{HG}(N, K, n; k) = \frac{{K \choose k} {N - K \choose n - k}}{{N \choose n}}. \]&lt;/p&gt;
&lt;p&gt;So let&amp;rsquo;s take this step by step. Applied to our situation, we have a total of \(N = 16\) cards, \(K = 4\) of which are &amp;ldquo;successes&amp;rdquo;, i.e., the dark ones. In the standard application of the hypergeometric distribution, we&amp;rsquo;d draw a fixed number of \(n\) cards and want to know the probability that exactly \(k\) of them are dark. Our problem however is a little different: we need to fix \(k = 4\) dark cards to be drawn and want to know the probability that this happens within the first \(n\) draws. This is &lt;em&gt;almost&lt;/em&gt; what we want – it describes the probability that the game lasts &lt;em&gt;less&lt;/em&gt; than \(n\) rounds. Let \(X\) be the random variable that describes the game length. Then what we just said can be expressed as&lt;/p&gt;
&lt;p&gt;\[ P(X \lt n) = p_\text{HG}(16, 4, n; 4) = \frac{{4 \choose 4} {16 - 4 \choose n - 4}}{{16 \choose n}} = \frac{{12 \choose n - 4}}{{16 \choose n}}. \]&lt;/p&gt;
&lt;p&gt;Now, it&amp;rsquo;s easy to recover the probability is that the game lasts &lt;em&gt;exactly&lt;/em&gt; \(n\) rounds:&lt;/p&gt;
&lt;p&gt;\[ P(X = n) = P(X \lt n + 1) - P(X \lt n) = \frac{{12 \choose n - 3}}{{16 \choose n + 1}} - \frac{{12 \choose n - 4}}{{16 \choose n}}, \]&lt;/p&gt;
&lt;p&gt;where \(n \ge 4\) to make sure the binomial coefficients are defined (I&amp;rsquo;ll leave \(P(X = 3)\) as an exercise to the reader). If you&amp;rsquo;re bored, you can flex your algebraic muscles and simplify this expression:&lt;/p&gt;
&lt;p&gt;\[ P(X = n) = \frac{12! \cdot n! \cdot 4}{(n - 3)! \cdot 16!}. \]&lt;/p&gt;
&lt;p&gt;This is a nice closed form, but it&amp;rsquo;s &lt;em&gt;a)&lt;/em&gt; tedious to calculate as those factorials really do blow up and &lt;em&gt;b)&lt;/em&gt; not particularly insightful. Can we do better?&lt;/p&gt;
&lt;h2 id="pedestrian-but-insightful-formula"&gt;Pedestrian, but insightful formula&lt;/h2&gt;
&lt;p&gt;Did you notice above how \(P(X = 15) = 25\%\) is such a nice round number? Maybe it&amp;rsquo;s worth calculating this value in yet another way. So, what does it mean for the game to go over the full length? It means that the very last card has to be a dark one. What&amp;rsquo;s the probability of this happening? Well, 4 out of 16 cards are dark, so there&amp;rsquo;s a 1 in 4 chance of the last card being dark.&lt;/p&gt;
&lt;p&gt;Similarly, we can check why \(P(X = 14) = 20\%\). This means the penultimate card will have to be the 4th dark card, i.e., the last card has to be light. In other words,&lt;/p&gt;
&lt;p&gt;\[ P(X = 14) = \frac{12}{16} \cdot \frac{4}{15} = \frac{1}{5} = 20\%. \]&lt;/p&gt;
&lt;p&gt;Once I started to examine this pattern, a lightbulb went on in my head. We need to flip the deck over! 💡 Now, we&amp;rsquo;re no longer asking where the 4th dark card is in the pile, but rather how many light cards are in a row at the bottom of it. This is pretty straightforward to write down, e.g., for the next probability in line:&lt;/p&gt;
&lt;p&gt;\[ P(X = 13) = \frac{12}{16} \cdot \frac{11}{15} \cdot \frac{4}{14} = \frac{11}{70} \approx 15.7\%. \]&lt;/p&gt;
&lt;p&gt;And in general:&lt;/p&gt;
&lt;p&gt;\[ P(X = n) = \frac{4}{n+1} \cdot \prod_{k=n-2}^{12} \frac{k}{k+4}. \]&lt;/p&gt;
&lt;p&gt;Again, if you feel bored, you can express this product in terms of the same factorials as above. But the big advantage of this formulation is that it&amp;rsquo;s pretty transparent what&amp;rsquo;s going on.&lt;/p&gt;
&lt;!-- TODO: Let's carry out those algebraic steps. --&gt;
&lt;p&gt;So, are we finally done? Not quite, there&amp;rsquo;s still another way of arriving at the same result, which is a bit more elegant and general.&lt;/p&gt;
&lt;h2 id="negative-hypergeometric-distribution"&gt;Negative hypergeometric distribution&lt;/h2&gt;
&lt;p&gt;When playing with the hypergeometric distribution above, you might&amp;rsquo;ve stumbled across an important question we&amp;rsquo;ve ignored: is this even a proper distribution, i.e., do all those values sum up to 1? Those \(p_\text{HG}(N, K, n; k)\) assume you fix some parameters \(N\) (the number of cards), \(K\) (the number of dark cards), and \(n\) (the number of draws) and then run through all permissible values of \(k\) (the number of dark cards drawn). Summing \(p_\text{HG}(N, K, n; k)\) over all \(k\) for fixed parameters is guaranteed to give 1, but we&amp;rsquo;ve instead kept \(k\) fixed at 4 (i.e., draw all dark cards) and ranged \(n\) over all possible number of draws. Hasn&amp;rsquo;t someone already figured out the details of this distribution?&lt;/p&gt;
&lt;p&gt;Of course they have – this is called the &lt;a href="https://en.wikipedia.org/wiki/Negative_hypergeometric_distribution"&gt;negative hypergeometric distribution&lt;/a&gt;. Whilst the hypergeometric distribution counts the number of successes in a fixed number of draws, the negative hypergeometric distribution counts the number of successes until a fixed number of failures occur. Translated to our problem, it&amp;rsquo;ll count how many light cards are drawn before the 4th dark card is drawn. Again, this is &lt;em&gt;almost&lt;/em&gt; what we want – the game length will be the number of light cards drawn plus 3 for the first dark cards, which will be auctioned off as well.&lt;/p&gt;
&lt;p&gt;The probability mass function of the negative hypergeometric distribution is given by&lt;/p&gt;
&lt;p&gt;\[ p_\text{NHG}(N, K, r; k) = \frac{{k + r - 1 \choose k} {N - r - k \choose K - k}}{N \choose K}, \]&lt;/p&gt;
&lt;p&gt;where \(N = 16\) is the total number of cards, \(K = 12\) is the number of &lt;em&gt;light&lt;/em&gt; cards amongst them, \(r = 4\) is the number of dark cards (which will end the experiment / game), and \(k\) is the number of light cards drawn. Again, let \(X\) be the random variable that describes the game length. Then the probability that the game lasts \(n\) rounds is&lt;/p&gt;
&lt;p&gt;\[ P(X = n) = p_\text{NHG}(16, 12, 4; n - 3) = \frac{{n - 3 + 4 - 1 \choose n - 3} {16 - 4 - n + 3 \choose 12 - n + 3}}{16 \choose 12} = \frac{{n \choose n - 3}}{16 \choose 12}, \]&lt;/p&gt;
&lt;p&gt;which we can further simplify to&lt;/p&gt;
&lt;p&gt;\[ P(X = n) = \frac{n!}{(n-3)! \cdot 3!} \cdot \frac{12! \cdot 4!}{16!} = \frac{12! \cdot n! \cdot 4}{(n - 3)! \cdot 16!}, \]&lt;/p&gt;
&lt;p&gt;so thankfully, this checks out. 😌&lt;/p&gt;
&lt;p&gt;Now, you might think it&amp;rsquo;s quite redundant to derive the same result over and over, but I find it immensely satisfying to obtain the same formula in 3 different ways – and have them verified by simulation. Probability theory can be very tricky and plausible calculations can go wrong in unexpected ways. This article could&amp;rsquo;ve just been a single paragraph if we had immediately given away the answer with the negative hypergeometric distribution, but I honestly wouldn&amp;rsquo;t have trusted the results just like that.&lt;/p&gt;
&lt;!-- TODO: What's the intuition behind long games? --&gt;
&lt;p&gt;One final question remains: Did Reiner Knizia crunch the numbers when he designed the game? I can&amp;rsquo;t tell for sure, but he does hold a PhD in mathematics, so he would certainly have carefully considered the impact of the game length on the balance and gaming experience. When it comes to the &amp;ldquo;fun&amp;rdquo; in games, theory doesn&amp;rsquo;t matter and play testing is king, but calculations like these will provide a shortcut in the design process. Simulations are usually the fastest and most robust way to model a game, but calculations can lead you down really fun rabbit holes – I speak from experience… 🐇&lt;/p&gt;
&lt;p&gt;&lt;em&gt;PS&lt;/em&gt;: You can find the code for the simulation etc &lt;a href="https://gitlab.com/recommend.games/blog/-/tree/master/experiments/high_society"&gt;here&lt;/a&gt;.&lt;/p&gt;</description></item></channel></rss>