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

Saturday, March 16, 2019

Deck-u-lator history

Deck-u-lator is over eighteen years old.  Here is a look back to how it has evolved.

2001 Implair Perl CGI web application

Deck-u-lator launched in December 2001 when I was part of The Implair Group with Barry Reich.  The original calculator was implemented in Perl as a CGI script on the implair.com server.  Many of us at The Implair Group were Magic play testers.  I loved designing intricate combinations and became curious about how often I could actually draw them.

2010 Django on Google App Engine

After The Implair Group servers shut down, I moved the calculator to Google App Engine in November 2010.  This was a port to the Python 2.5 language and used Django web framework.  Since this time, the source code has been managed in the Git version control system.  Deck-u-lator changed to a single-page app generating graphs from Google charts.  This blog was started to provide documentation.  A REST API was described on the blog when this version launched.

2012 Upgrades

Deck-u-lator upgraded to Python 2.7 for the new Google High Replication Datastore and a new template engine, Jinja2.

2019 Advanced calculator for mobile

This winter I finally set aside time to add more advanced features to the calculator to cover alternate hands and maximum limits for cards in hands.  The user interface moved to a Bootstrap template for responsive display on smaller screens.  Formulas were added and the graph moved to the latest Google charts.

Thank you

Deck-u-lator is a passion project for me and I enjoy hearing from you about what you are doing with this calculator.  Who knows, your article or email may inspire changes for the next version.




Saturday, February 16, 2019

Chance of drawing either of two hands

Advanced calculator with button to add a hand
Add a hand for alternate ways to draw your combination
Suppose the combination of cards I want to draw is more complex than some subset of each of the cards in the deck.  I started talking about this kind of situation in an earlier post How to calculate the probability when either one of two different cards will do

It may be easier to describe your combination as being either one of two different hands, call them hand A and hand B.  If the chance of drawing hand A is written as P(A), what is the chance of drawing P(A or B)?
P(A or B) = P(A) + P(B) - P(A and B)
The last term P(A and B) is needed so we do not double count hands that match both A and B.

The advanced calculator has a new button to [Add a hand].  This copies the right-most hand column so that you can describe another way to draw your combination.

How to draw 1GG mana on turn 3

If you want to draw lands from your Magic: The Gathering deck to cast a 1GG spell on turn 3, two of them need to be a Forest, but the other can be any land.  Start by describing your deck with the different kinds of land.  You can draw 2 Forests and 1 Swamp.

Click [Add a hand] to create another option for your combination.  Change this hand to draw 3 Forests.

Deck-u-lator calculates the chance of each of these hands individually, and then automatically adjusts the chance that your hand descriptions overlap.  In this case, an overlap would be drawing 3 Forests and 1 other land.  That hand matches both of your descriptions, but we can only count it once.

More alternate hands

You can add as many alternate hands as you need.  More alternate hands take longer to calculate.  Because each alternate hand generates more calculations, graphs and formulas are not presented.

Try the advanced calculator at https://deckulator-hrd.appspot.com/static/advanced.html

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, April 24, 2012

Infrastructure Upgrades

The technology underlying the Deck-u-lator has been upgraded to the latest versions. This application now takes advantage of the High Replication Datastore and takes advantage of multithreading. There are also performance improvements due to faster page generation using a new template engine, using native Python language features for calculations and JSON processing.

Any change like this introduces the risk of changed functionality. If you see any problems, please contact me.