Showing posts with label Hypergeometric. Show all posts
Showing posts with label Hypergeometric. Show all posts

Saturday, January 20, 2024

New math formula for multiple hand combinations

The latest version of Deck-u-lator shows new math formulas for advanced calculations with two or three hands.  Previously multi-hand calculations simply confirmed how many alternate hand combinations you were calculating.  This is still the case for combinations with four or more hands.

To calculate your chances to draw alternate hands, Deck-u-lator performs the multivariate hypergeometric calculation for each hand and adds those probabilities together.  What about draws that contain all the cards needed for two different hands?  These get double counted by just summing probabilities for both hands.  So we calculate the probabilities for all combinations of two hands and subtract those.  This process continues, alternately adding probabilities for combinations of three hands, subtracting probabilities for combinations of four hands and so on.

The new formula gives a glimpse into this process.  Enjoy!

$$ \frac{ H_{0} + H_{1} - H_{0} \cap H_{1} }{ \binom{ 52 }{ 2 } } = 95.0\% $$

Friday, May 17, 2019

How to use the Deck-u-lator API

If you want to add a multivariate hypergeometric distribution calculator to your program, consider using the Deck-u-lator API.  This has been available since 2010 when it was introduced on this blog.

You can see an example of calling this using JavaScript over at Observable.

Saturday, April 13, 2019

Summation formulas for multivariate hypergeometric

Frank Karsten taught me several things with his recent article on Magic applications of the multivariate hypergeometric distribution.  Deck-u-lator has always been clear that this is the math formula we are calculating.  Frank includes summations over variables to account for the difference between the cards specified in the combination and how many cards are being drawn.
\sum_{ C_0=1 }^{4} \sum_{ C_1=1 }^{4} \sum_{ C_2=1 }^{\min(4,10-C_0-C_1)}\frac{{\binom{4}{C_0} \binom{4}{C_1} \binom{4}{C_2} \binom{48}{10-C_0-C_1-C_2}}}{{\binom{60}{10}}} = 12.6\%
Summations over variables in multivariate hypergeometric distribution
When implementing Deck-u-lator, I thought through the allocation of these extra cards using recursive functions.  Frank's formula examples were the first time I noticed summations with hypergeometric distribution.  As he says, once you know the formula, you can implement it a variety of ways using a spreadsheet or a programming language.

I ran all Frank's examples through Deck-u-lator as test cases and compared the calculated results to formula results.  Here is one of the test case formulas in Python.

sum(sum(sum(choose(4,m)*choose(4,p)*choose(4,t)*choose(48,10-m-p-t)
            / choose(60,10)
            for t in span(1,max(4,10-m-p))
        for p in span(1,4))
    for m in span(1,4))

Since I found the formulas so helpful, I thought I would start generating them in Deck-u-lator and maybe others will also find them helpful.
$$ \sum_{ C_0=1 }^{4} \sum_{ C_1=1 }^{4} \sum_{ C_2=1 }^{\min(4,10-C_0-C_1)}\frac{{\binom{4}{C_0} \binom{4}{C_1} \binom{4}{C_2} \binom{48}{10-C_0-C_1-C_2}}}{{\binom{60}{10}}} = 12.6\% $$

Tuesday, January 22, 2019

How to calculate with maximums

Advanced calculator entering maximum values
Enter maximum values using advanced calculator
For a Modern Burn deck, what is the chance of drawing at most 2 creatures, either 2 or 3 lands, and at least 5 burn spells in your first 10 cards?  Our deck in this case will have 12 creatures, 20 lands, and 28 burn spells.

Use the advanced calculator
Calculating this in Deck-u-lator requires using the Maximum card count in the advanced calculator.
First describe the deck and enter the minimum values for each card as usual.
Then click the minimum button to toggle to maximum and enter the maximum values for each card.
Click to calculate and find that there is a 29.1% chance to draw this combination.  The hypergeometric distribution formula shows how this is being calculated.
\[ \sum_{ C_0=0 }^{2} \sum_{ C_1=2 }^{3}\frac{{\binom{12}{C_0} \binom{20}{C_1} \binom{28}{10-C_0-C_1}}}{{\binom{60}{10}}} = 29.1\% \]
graph showing 0.291 chance to draw combination in 10 cards
Best chance to draw this combination is at 10 cards
Try out the advanced calculator at https://deckulator.appspot.com/static/advanced.html

Monday, January 21, 2019

Updates to Deck-u-lator

Deck-u-lator has just been updated with several changes.  I hope you enjoy the new functionality.

Better mobile experience
Deck-u-lator on a phone screen
Small screen view

Deck-u-lator is now a responsive web app with a display that adapts to smaller screens like phones and tablets.

Add a hand (advanced) 
Basic calculations are exactly as they were before.  Deck-u-lator has added an advanced calculator that lets you add alternate hands in your combination.  Thanks to Carson McFarlane who wrote to ask for "an 'or' function between cards".

Maximum (advanced) 
In the advanced calculator you can also enter an upper limit for the maximum number of a card to draw for your combination.  Thanks to Frank Karsten who wrote about calculations that are "not easy to do via Deck-u-lator".  I took that as a challenge to make it easier!  Maximum cards also work with alternate hands.

Better graphs 
Graphs are now updated to use the latest Google charts.  This interactive graph allows you to move the crosshairs.

Math formulas (multivariate hypergeometric) 
Thanks again to Frank Karsten for sharing formulas for more complex combinations.  I found them useful, so I added MathJax to display formulas for Deck-u-lator calculations.

Thursday, October 23, 2014

Of Math and Magic / Limiting Chance

Chris Mascioli has a nice article in his series Of Math and Magic where he shows how to calculate multivariate hypergeometric probability mass function using a spreadsheet.  He calculates the odds of drawing a pair of the same spell, something not easily done in Deck-u-lator.

He wrote this series as a response to Dan Nelson's series Limiting Chance.  Dan focuses his attention on the 40 card Limited format and addressed topics including play or draw, mulligans, cantrips, and the mana curve.

I appreciate what both these authors have done in helping to educate gamers about how to use math to improve their play.  Dan's approach is a bit more "scruffy", but he does carefully explain his methods.  Chris provides the "neat" contrast by rigorously explaining results using probability theory

Tuesday, December 21, 2010

Visualizing hypergeometric distribution

What are the chances to draw exactly one card of four in a sixty card deck? This is exactly the question the hypergeometric distribution answers. Here are two graphs showing this distribution. First, the height of the bars shows the percentage of hands that have exactly the specified number of cards. The red bars show chances for drawing exactly one card of four. As you can see the maximum value is about 44% when 15 cards are drawn. This also happens to be when the average or mean value is exactly one.


Here is the same data with the bars stacked to show that the percentages always add to 100. This view clearly shows that your chance to draw at least one of the four cards after drawing fifteen is about 69%.

Saturday, December 4, 2010

How does it work?

Deck-u-lator computes the ratio between the number of different hands that include specific cards and the number of possible hands that can be drawn from the deck you describe. If half these possible hands have the card combination you want, there should be a 50% or 0.5 chance of drawing your combination.

Calculating this chance uses a branch of mathematics called combinatorics. The number of different ways to choose \(H\) elements from a set of \(D\) elements is called the binomial coefficient is calculated using factorials (!).

$$ \binom{D}{H} = \frac{D!}{H!\,(D-H)!} $$

The number of different hands of size \(H\) drawn from a deck of \(D\) cards is \(\binom{D}{H}\). A similar formula is used to calculate the number of ways to satisfy each part of your combination. If your deck has \(D_X\) cards of type \(X\) and you need \(H_X\) for your combination, then there are \(\binom{D_X}{H_X}\)  different ways to choose the type \(X\) cards you need. The number of hands with the cards you need is the product of all the different ways to choose each card. This is called a multivariate hypergeometric distribution (without replacement).

The Mathematics of Magic The Gathering: Probability, Statistics, Game Theory, and Strategy by Jon Prywes has an excellent discussion of mathematics for games if you are looking for even more information.


Why are some calculations slow?

Deck-u-lator calculates card combo probabilities using multivariate hypergeometric distribution.  This calculation is relatively fast if the number of cards you draw is the same as the size of your combo.  However, things slow down when you try calculating results for big combinations when you draw about half your deck.  Why is that?

The problem is what to do with the extra cards.  If you want to know the chances of drawing a four card combo in the first twenty cards, there are sixteen extra cards.  Deck-u-lator tries all the different ways to choose those extra cards.  For each way to choose those extra cards from your deck, add them to your combination and then calculate the multivariate hypergeometric distribution.

How many different ways are there to choose those extra cards?  Our Black Lotus Mana Vault combination uses a deck with five types of cards.  The combination has five cards from four of those types.  If we want to calculate results for drawing thirty cards, we have twenty-five extra cards to work with.  How many ways are there to choose twenty-five cards from five different types?  In mathematical terms these twenty-five cards are a multiset and an upper bound on the number of different ways to choose them can be calculated as the number of combinations with repetition.  For this example there are 23,751 different multisets of twenty-five cards from five types.
$$ \binom{25+5-1}{25} = \binom{29}{25} = 23,751 $$
Deck-u-lator is fast enough that generating these multisets and then calculating the hypergeometric distributions only takes about half a second.  But if we try something similar with fifteen different types of cards, the number of multisets is much larger.  At the same speed this will take about four hours.
$$ \binom{15+15-1}{15} = \binom{29}{15} = 77,558,760 $$
Decks with eight types of cards are about the limit.  Calculations for drawing thirty cards from a deck like this will require 1,560,780 multisets and can be done in under ten minutes
$$ \binom{22+8-1}{22} = \binom{29}{22} = 1,560,780 $$