Saturday, 4 October 2008

Not More Yahtzee Maths

It has been a while since I lasted blogged. In the intervening time I have made a few more discoveries about Mathematics of Yahtzee. So for completeness I thought I'd put the electronic pen to internet paper and note down my conclusions. By advertising it in this way I can virtually guarantee that my discoveries will be kept as secret as the true identity of JFK's assassin, the formula for everlasting youth etc.

The route to my conclusion was far more tortuous that I will outline here. I used about 100 sheets of paper proving what could be written down in only a few lines. So I will now present the shortcut solution... But first the question..... Oh Guru Tim please can you provide me with a formula to give me the probability of getting a specific 5-of-a-kind, 4-of-a-kind, 3-of-a-kind and so on when playing Yahtzee.


Throwing One Dice Three Times
To answer the question we need to realize that you need three attempts (see www.yahtzee.org.uk for the rules). So lets start there, and only consider throwing one dice... regular dice p=1/6 (successful throw) q=5/6 (unsuccessful throw)
1st Attempt, success p, failure q
2nd Attempt, success qp, failure q2
3rd Attempt, success q2p, failure q3

So adding this up p + pq + pq2 + q3 ==> p(1+q+q2) + q3 which is == 1
And
Extending this further for n throws 1 = p(1+q+q2+ .. +q(n-1)) + qn

The first thing to note is that is not normally how one would write it. Normally one would expect a binomial expansion e.g.
(p+q)3 = p3+3pq2+3p2q+q3 == 1

However, the way in which we progress is different. The binomial expansion means that you would throw the dice 3 times no matter what you got, hence there is an option for throwing 3 correct dice in a row. So what the new expansion does for us is to allow us to stop throwing when the target is reached. The expansion is no less valid than the binomial, but it simply describes a different set of mechanics.

Throwing Five Dice Three times


OK Now we have a simple expression for throwing 1 dice three times, lets use five dice, and have 3 attempts. We are now armed with a new set of formula which is expressed like so

Probability of getting target number P = p(1+q+q2)
Probability of failing to get Target number Q = q3

Now remember that P+Q = 1 (and also p+q=1, p=1/6, q=5/6), Throwing the 5 dice with 3 attempts will yield a binomial expansion of (P+Q)5 = 1

So
Throwing 5-of-a-kind is P5 or p5(1+q+q2)5 or 1.33%
Throwing 4-of-a-kind is 5P4Q or 5p4(1+q+q2)4q3 or 9.12%
Throwing 3-of-a-kind is 10P3Q2 or 10p3(1+q+q2)3q6 or 25.04%
Throwing 2-of-a-kind is 10P2Q3 or 10p2(1+q+q2)2q9 or 34.40%
Throwing 1-of-a-Kind is 5PQ4 or 5p(1+q+q2)q12 or 23.63%
Throwing 0-of-a-kind is Q5 or q15 or 6.49%

The Long Way 'round

So taking a look at the above, one might say .. hey this is way too simple, I don't believe it. Well the as I said at the start I used up a small forest working the above through the long way round. If you wish to do it feel free. The long way round is to simply look at the binomial expansion for all the permutations (you can get a flavor from my previous posts), but remember that when you get the target number you stop. So look at the chances of getting 5 in one go (5M), then 5 in two goes, (4M:5M 3M:5M; 2M;5M; 1M:5M; 0M;5M), then in three goes (4M:4M;5M 3M:4M:5M 3M:3M:5M etc.... ) .. This will give you 5-of-a-kind, then repeat whole process for 4-of-a-Kind
etc... after a few forests and a couple of pencils you should end up with the above set of simple formulas.....

Time to go now, kids are suspiciously quiet...

Saturday, 26 January 2008

Double Maths (again!)

Probability of throwing Any Yahtzee
last time we left off with the result of getting a specific Yhatzee was
p5 [1+q(1+q)]5 . Which I thought was a very neat (p=1/6, q=5/6). The next, and probably more realistic question is "what are the chances of throwing ANY Yahtzee". My first attempts were very much based on the previous attempts, and indeed I came up with a result of p4 [1+q(1+q)]4 . This However was wrong, this evaluates to 3.15%, when I did my test runs on it I came up with a value closer to 4.5%. My first thoughts were, the Java random algorithm was poor, but it did match the previous formulae for a specific Yahtzee, so I was wondering if my algorithm was wrong or I had a bug!!


After some further thought I realised that a potential for getting more Yahtzees was obvious.
  • When you are going for a specific yahtzee and you have 0 1 or 2 matching numbers, you forge on...
  • BUT if you need ANY old Yahtzee, then of you have 2 matching numbers, you are not tied to these. So lets say roll 1 gave you 2,2, 3,4,6 You hold the twos, roll again ... now there is a chance that you then roll a full house say 2,2,5,5,5. At this point you abandon yahtzees in 2's and go for the more fruitful 5's. And this is what my Yahtzee algorithm will do. Hence the extra Yahtzees.
My problem now was converting this into neat formulae as per my previous result. This was not so easy. So turning to google ... I found this ...http://www.yahtzee.org.uk/theory.html which was a different approach to me, but none the less had a very interesting set of results. The Yahtzee.ork.uk paper says getting any Yahtzee is about 3.7% (a bit low), and in general it dealt with the maths quite nicely, BUT, when dealing with 2-of-a-kind it did not consider abandoning it when a full house was thrown. When you start to look at the numbers throwing any two-of-a-kind (a pair and 3 other numbers or two pair and a single number) it is nearly 70% of the time... We are on the right track (thanks Yahtzee.org.uk)

So I perseveered. The eventual formula was derrived is a very similar way to previous with inspiration from the above paper. I'll not do all the maths but, here is a summary


5M (Yahtzee) --> 6/7776 == 6.p5
4M (4-of-a-kind) --> 150/7776 == 6.5.p4q
3M (3-of-a-kind) --> 1500/7776 == 6.10.10p3q2
2M(a pair) --> 5400/7776 == 6.Q.p2
0M(nothing) --> 720/7776 == 6.R.p

so p = 1/6, q=5/6, Q is a fudge and is 5400/1296 R is also a fudge and is 720/7776

1st Attempt
This is simple, roll a Yahtzee probability is
6.p5
  • 5M --> 6.p5
or 0.077%


2nd Attempt
A few more combinations here
  • 4M:5M --> 6.5.p4q . p
  • 3M:5M --> 6.10.10p3q2 . p2
  • 2M:5M --> 6.Qp2 .p3
  • 0M:5M --> 6.p5
adding it up, after the 2nd attempt (so adding in the 1st attempt) we get

6p5(1 + 5q + 10q2 + Q + R), or 1.26%

3rd attempt

4 of a kind, 4 of a kind, then yahtzee ..
  • 4M:4M:5M --> 6.5p4q . q . p
3 of a kind to 3 & 4 of a kind to Yahtzee
  • 3M:4M:5M --> 10.6.p3q2 . 2pq . p
  • 3M:3M:5M --> 10.6.p3q2 . q2 . p2

2 of a kind to 2,3,4 of a kind and then Yahtzee PLUS, 2 of one kind to 3 of another, to yahtzee

  • 2M:4M:5M --> 6Qp2 . 3p2q . p
  • 2M:3M:5M --> 6Qp2 . 3pq2 . p2
  • 2M:2M:5M --> 6Qp2 . q3 . p3
  • 2M:2N:5N --> 6Qp2 . 5p3 . p2

Nothing to something better to yahtzee (basically 6RP * 2nd attempt)

  • 0M:0-4M:5M --> 6Rp . 6P5(5q + 10q2 + Q + R)
The whole lot

6p5{1+ (5q + 10q2 + Q + R)(1+6Rp) + 5q2 + 10q2[(1+q)2 – 1] + q[(1+q)3 -1] + 5Qp2}

Not so nice but is 4.610%, which is 3.47 times larger than a specific Yahtzee.

If anyone has suggestions for substitutions for P & Q (preferably to fit in with the binomial expansions) that make the above simpler feel free to comment..

Tally ho, I'm going to be running 10,000,000 games tonight.

Wednesday, 23 January 2008

Some Mathematics and Yahtzee

Probability of getting a Yahtzee...

OK, quick recap of what a Yahtzee is. You roll 5 regular cubic dice, and if all the dice score are the same it is a Yahtzee (e.g. 5 dice each showing 1). That said, you don't have to do it on the first go. You can have up to 3 attempts, and you can roll as many dice you choose. So if you wanted a Yahtzee in 1s you might roll 1,2,1,4,5.. You can keep the two 1s and roll the remaining 3 dice.

So now look at some of the mathematics. Let

p = probability of rolling a specific number on one dice (1/6)

q = the probability of not throwing the specific number (1-p) or (5/6)

I'm using p & q as it is easier than writing 1/6 and 5/6 each time. Additionally, there are some arithmetic simplifications that are easier to deal with using algebra, rather than hard numbers.

Some fundamental probability combinations. I will use these latter. Rolling 5,4,3,2 or 1 dice gives the following binomial permutations

..............0M ......1M.....2M........3M......4M.......5M

(p+q)5 = q5 + 5pq4 + 10p2q3 + 10p3q2 + 5p4q+ p5 ... rolling all 5 dice

(p+q)4 = q4 + 4pq3 + 6p2q2 + 4p3q+ p4­­­ ... rolling 4 dice

(p+q)3 = q3 + 3pq2 + 3p2q+ p3 ... rolling 3 dice

(p+q)2 = q2 + 2pq + q2 ... rolling 2 dice

(p+q)1 = q + p ... rolling just 1 dice

Note that each row adds to 1 (as p+q=1, and 1x = 1), this fact will be used (lots) later on.

The top row (5M, 4M etc) shows the number of matching dice rolled (so Yahtze is 5M, 4 of the same number is 4M, and so on). Each row corresponds to throwing 5 dice, 4 dice etc. These permutations will be used latter on, and it is assumed that the reader is familiar with the concept of binomial permutations... if not .. sorry no tutorial here

Also below I will use some short hand to denote the way the dice were rolled. So 1M:3M:5M represents 3 attempts, 1st attempt rolled 1 target number, the next attempt the remaining 4 dice were rolled and 3 target numbers (in total) were matched (or two new target numbers), and the last attempt gave a Yahtzee with 5 matching target numbers.

The last thing is that if you roll a target number, it is assumed you hold it, thus the following combination would not occur 4M:3M:5M, if you rolled 4 target numbers you would hold all four, so it would be impossible (stupid) to have less than 4 target numbers after the second roll.

1st Attempt

Only 1 permutation to get Yahtzee, roll 5 dice & get it, the probability is

5M is p5

2nd Attempt

There a few permutations here, you could roll 4,3,2,1 or 0 matching dice, then throw a Yahtzee from the remaining, the combinations are listed below along with the probabilities.

  • 4M:5M is 5p4q . p
  • 3M:5M is 10p3q2 . p2
  • 2M:5M is 10p2q3 . p3
  • 1M:5M is 5pq4 . p4
  • 0M:5M is q5 . p5

Adding these up

p5 {5q + 10q2 + 10q3 + 5q4 + q5}

The expression within the { } brackets is (1+q)5 – 1, so this simplifies further to

p5 {(1+q)5 – 1}

3rd Attempt

There are lots of permutations here (as one might expect)

4M:4M:5M is 5p4q . q . p

==> p5{ 5q2 } ==>

p5 { 5q (( 1+q) -1)} (this complication will become apparent as the other terms are calculated)

  • 3M:4M:5M is 10p3q2 . 2pq . p
  • 3M:3M:5M is 10p3q2 . q2 . p2

So 3M:3-4M:5M is the sum of the above

==> p5{ 10q2 ( 2q + q2 ) } == > (note that 2q + q2 == (1+q)2 -1, hence the above complication!)

p5 { 10q2 ((1+q)2 -1) }

  • 2M:4M:5M is 10p2q3 . 3p2q . p
  • 2M:3M:5M is 10p2q3 . 3pq2 . p2
  • 2M:2M:5M is 10p2q3 . q3 . p3

So 2M:2-4M:5M is the sum of the above

==> p5{ 10q3( 3q + 3q2 + q3 ) } == > (again note 3q+3q2+q3=(1+q)3-1)

p5 { 10q3((1+q)3 -1) }

  • 1M:4M:5M is 5pq4 . 4p3q . p
  • 1M:3M:5M is 5pq4 . 6p2q2 . p2
  • 1M:2M:5M is 5pq4 . 4pq3 . p3
  • 1M:1M:5M is 5pq4 . q4 . p4

So 1M:1-4M:5M is the sum of the above

==> p5{ 5q4( 4q + 6q2 + 4q3 + q4) } == > (you should be getting the idea now..)

p5 { 5q4((1+q)4 -1) }

  • 0M:4M:5M is q5 . 5p4q . p
  • 0M:3M:5M is q5 . 10p3q2 . p2
  • 0M:2M:5M is q5 . 10p2q3 . p3
  • 0M:1M:5M is q5 . 5pq4 . p4
  • 0M:0M:5M is q5 . q5 . p5

So 0M:0-4M:5M is the sum of the above

==> p5{ q5( 5q + 10q2 + 10q3 + 5q4 + q5) } == >

p5 { q5((1+q)5 -1) }

The 3rd attempt can now be summed up and reduced (if that is the word!!) to

p5 { 5q(1+q) + 10q2(1+q)2 + 10q3(1+q)3 + 5q4(1+q)4 + q5(1+q)5 – [(1+q)5 -1]}

Adding it All Up

Adding together the 3 terms for 1st attempt, 2nd attempt and 3rd attempt

  • 5M is p5
  • 1-4M:5M is p5 {(1+q)5 – 1}
  • 1-4M:1-4M:5M is p5 { 5q(1+q) + 10q2(1+q)2 + 10q3(1+q)3 + 5q4(1+q)4 + q5(1+q)5 – [(1+q)5 -1]}

This simplifies as the bit in [...] cancels with 2nd attempt expression, to

p5 { 1 + 5q(1+q) + 10q2(1+q)2 + 10q3(1+q)3 + 5q4(1+q)4 + q5(1+q)5}

Replacing q(1+q) with x gives

p5 {1 + 5x + 10x2 + 10x3 + 5x4 + x5}

The bit in the { } is a simple expansion of (1+x)5 becomes

p5 (1 + x)5

Now as the above is a very simple expression (considering where we started), and back substituting x=q(1+q) gives the final result

Of probability of getting a specific Yahtzee in 3 attempts is

p5 [1+q(1+q)]5

Putting in some numbers the result is 915/615 or 0.01327. So to get any Yahtzee is 6 times this or

915/614, or 0.0796, or just under 8%. This is much larger than I had originally thought.

As each game has 13 sets, then one would expect to see 0.66 Yahtzees per game.

Max Score Yahtzee
The maximum score yopu can get in Yahtzee is 1575, and means not only you get 13 Yahtzees in a row, but you some are specific Yhatzees. Doing some more maths the first two yahtzees can be any type of yahtzee, then you have 8 specific Yahtzees and 3 any old yahtzees, this. So denoting p(ay) for any Yahtzee, and p(y) as a specific Yahtzee

p(ay).p(ay).[any order of 8 p(y), 3 p(ay)]

The way to arrange 11 things with 8 and 3 is 11*10*9/3*2 ==> 165
also p(ay) = 3.47*p(y) (see next blog, Double Maths (again!) )

so

(3.47)2 p(y)2 . 165 . p(y)8 . (3.47)3p(y)3

or

p(y)13.(3.47)5. 165

as above p(y) = 0.01327, doing some maths, if my computer can run 130 games per second, to have a relatively good chance of getting this I need to run the simulation for 7.4 billion years!