Showing posts with label How it works. Show all posts
Showing posts with label How it works. Show all posts

Friday, February 6, 2026

Quickly calculate probabilities

A common question asked about Deck-u-lator is why does it take so long to calculate my combination. 

Time to calculate binomial coefficient

The fundamental calculation performed is the binomial coefficient.  Python can perform four million binomials coefficients per second using math.comb() function.

Deck-u-lator uses Python functools.cache to do these even faster, only calculating the result for each set of inputs once and remembering that result.

Time to calculte multivariate hypergeometric

When you draw exactly as many cards as are in your combination,  Deck-u-lator calculates the multivariate hypergeomatric once, with one binomial coefficient for each card in the combination (C) and one for the deck.  For a combination with 4 cards, binomial coefficient is calculated 5 times.  

Time to draw extra cards

When you draw more cards than your combination, Deck-u-lator calculates the multivariate hypergeometric for each different way to draw those extra cards.  The number of different ways to draw the extra cards is roughly the product of the number of cards C of each type in your deck.  In a 60 card deck, a three card combination where there are four of each card in your deck will calculate multivariate hypergeometric 64 times (\(4^C\) or \(4^3\))

Time to draw alternate hands

When your combination includes alternate hand columns, Deck-u-lator must calculate every combination of hand columns.  For H hand columns, there are \(2^H -1\) combinations to calculate.

Deck-u-lator also uses functools.cache to combine hand columns, making this a bit faster.

Total time to calculate your combination

To estimate the time it will take to complete your calculation, substitute C for the card rows in your combination and H for the hand columns in this formula:

$$ \frac {(2^H - 1) \times (4^C) \times (C + 1) }{4,000,000 } \text{ seconds} $$

A sample calculation with C=5 card rows and H=3 card columns

$$ (2^3 - 1) \times (4^5) \times (5 + 1) / 4,000,000 \text{ seconds} $$ 

$$ (7) \times (1,024) \times 6 /4,000,000 \text{ seconds} $$ 

$$ 0.01 \text{ seconds}$$ 


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 $$