You are on page 1of 21

Mathematics, Probability, and Computers

Brooks Davis
April 25, 2014

Introduction
Probability questions are often solved using complicated theoretical arguments. Suppose

we use probability theory to determine that the probability of an event A occurring is 0.25
(this is denoted by P r(A) = 0.25). One interpretation of P r(A) = 0.25 is that if we conduct
the experiment 1000 times then we would expect to see the event A occur 0.25 1000 = 250
times. We now suppose that we do not know P r(A), but have the ability to conduct the
experiment a large number of times. We could then treat the proportion of times that we
observe A occurring as an approximation to P r(A).
We illustrate the thought process in the previous paragraph on the toy example of rolling
one die. Let A be the event that you roll a six. Probability theory tells us that P r(A) = 1/6,
because there are six faces on the die and we assume that each face has an equal probability of
being the top face when the die is rolled. If we wanted to empirically verify that P r(A) = 1/6
we could do the following experiment. We could roll a die n times (n should be large) and
if the die landed on six i times, then we would expect i/n to be approximately 1/6.
Even in our toy example one would not want to actually conduct the experiment a
large number of times. However computers are well suited to performing large numbers of
computations quickly, so provided that one can translate the experiment into a computer
algorithm, one could have a computer repeatedly conduct the experiment a large number of
times. In the absence of a theoretically determined probability one could use the empirically
determined probability as a substitute for the theoretically determined probability. If one
knew the theoretically determine probability beforehand, then one could use the empirically
determined probability to verify the theoretically determined probability.
In this paper we have selected four problems from probability and checked the theoretically determined probabilities against the empirical probabilities determined by simulation.

Gamblers Ruin

2.1

Problem

Suppose we have two gamblers A and B. Suppose that gambler A starts with i dollars
and gambler B starts with k dollars. They play a game in a series of rounds. On any given
round the probability that gambler A wins the round is 0 < p < 1. If gambler A wins the
round he wins a dollar from gambler B, otherwise gambler B wins a dollar from gambler A.
The game is over when one of the gamblers is out of money. Given i, k, and p, what is the
probability that gambler A succeeds in winning all of the money from gambler B?

2.2

Formula

We will derive the theoretical solution to the Gamblers Ruin problem. This solution is
adopted from [MD]. For each j {0, 1, . . . , i + k}, let aj be the probability that player A
eventually wins Gamblers Ruin given that gambler A currently has j dollars and gambler
B has i + k j dollars. We note that a0 = 0 and ai+k = 1. Let W be the event where player
A wins Gamblers Ruin. Let U be the event that gambler A wins the next round and U be
the event that gambler A loses the next round. Then using the Law of Total Probabilities
we have
P (W ) = P (W |U )P (U ) + P (W |U )P (U )

(2.1)

Starting from a point in the game where gambler A has j dollars (1 j i+k 1), equation
(2.1) becomes
aj = aj+1 p + aj1 (1 p)

(2.2)

Theorem 2.1. For every j {0, 1, 2, . . . , i + k 1},


(
aj+1 aj =

1p
p

)j
a1

(2.3)

Proof. We proceed by induction. When j = 0 equation (2.3) holds since

a1 a0 = a1
((
))0
1p
a1
a1 =
p
Suppose for a fixed j {0, 1, 2, . . . , i + k 2},
(
aj+1 aj =

1p
p

)j
a1

Thus
aj+1 (1 p)aj
aj+1 p

p
p
aj+1 (1 p) (1 p)aj
=
p
(1 p)(aj+1 aj )
=
p
(
)j
1p 1p
=
a1
p
p
(
)j+1
1p
=
a1
p

aj+2 aj+1 =

Let r =

1p
.
p

We consider two cases. First we will work out the case where p = 1/2 and

therefore r = 1.
Proposition 2.1. If p = 1/2 then a1 =

1r
1ri+k

Proof. Summing on both sides of equation (2.3) yields


i+k1

(aj+1 aj ) =

j=0

i+k1

r j a1

j=0

ai+k a0 =

i+k1

r j a1

j=0

1 ri+k
1 a0 = a1
1r
1r
a1 =
1 ri+k

Theorem 2.2. For every j {0, 1, . . . , i + k}, aj =

1rj
1ri+k

Proof. We proceed by induction. When j = 0 we have a0 = 0 =


j {0, 1, . . . , i + k 1} we have aj =

1rj
.
1ri+k

aj+1 = rj a1 + aj
(
)
1r
1 rj
j
=r
+
1 ri+k
1 ri+k
rj rj+1 + 1 rj
=
1 ri+k
1 rj+1
=
1 ri+k

1
i+k

Suppose for some

Then using the result of proposition 2.1 and

equation (2.3) we have

Proposition 2.2. If p = 1/2 then a1 =

1r0
.
1ri+k

Proof. Summing both sides of equation (2.3) with r = 1 yields


i+k1

(aj+1 aj ) =

j=0

i+k1

a1

j=0

1 a0 = a1 (i + k)
a1 =

Theorem 2.3. For all j {0, 1, . . . , i + k}, aj =

1
i+k

j
i+k

Proof. Again we prove this by induction. Fix j {0, 1, . . . , i + k}

aj =

j
i+k
1
i+k

aj+1 = aj +

j
1
+
i+k i+k
j+1
=
i+k
=

Thus we have proved the following theorem.


Theorem 2.4. If gambler A starts with i dollars, gambler B starts with k dollars, and p
is the probability that gambler A wins any given round, then the probability that gambler A
wins is

1
( 1p
p )
1p i+k
( p ) 1

if

p = 1/2

i
i+k

if

p = 1/2

2.3

Computer Code

round[p , i0 , k0 ] := Module[{x, i = i0, k = k0},


x = Random[ ];
If[
x < p,
i=i+1;
k = k - 1,
k = k + 1;
i=i-1
] (*If*);
{i, k}
] (*Module*)

game[p , i0 , k0 ] := Module[{i = i0, k = k0, out},


While[ i > 0 && k > 0,
{i, k} = round[p, i, k]
] (*While*);
If[
i == 0,
out = 0,
out = 1
](*If*);
out
] (*Module*)

ruin[p , i0 , k0 , lim ] := Module[{count = 0, n, i},


For[
n = 1, n <= lim, n++,
i = game[p, i0, k0];
count = count + i
](*For*);
N[count/lim, 6]
](*Module*)

2.4

Explanation of Computer Code

We first wrote a routine Round. The routine Round took three inputs: i, k, and p.
Gambler A begins the round with i dollars. Gambler B begins the round with k dollars and
6

p is the probability that gambler A wins the round.


Round selected a random number, x, from a uniform distribution on the interval [0, 1].
If x < p, Round increased i by 1 and decreased k by 1. If x p, Round increased k by 1
and decreased i by 1. Round returned the updated values of i and k.
Next we wrote a routine Game. Game took inputs i, k, and p. Game utilized a while
loop. The body of the while loop consisted of Game calling Round to increment i and k.
The while loop was exited when either i = 0 or k = 0, thus simulating one complete game
of Gamblers Ruin.
The final routine Ruin ran Game n times and kept a tally of the games won by gambler
A. The number of games won by A was divided by n to determine empirically the probability
that gambler A would win Gamblers Ruin with the given initial conditions.

2.5

Results

We ran Ruin with parameters n = 10, 000, i = 20, k = 5, and p = 0.4. With these parameters the theoretical probability as determined by Theorem 2.4 is approximately 0.1317.
Table 1 shows the empirically probability computed by Ruin in ten runs.
Table 1: Empirical Probabilities of Gamblers Ruin
Run Number Probability Run Number Probability
1
0.1313
6
0.1318
2

0.1307

0.1292

0.1352

0.1340

0.1308

0.1341

0.1333

10

0.1362

3
3.1

Full House
Problem

A standard deck of 52 cards consists of four dierent suits (Spades, Hearts, Clubs, and
Diamonds). Each suit is then made up of 13 ranks (Ace, 2, 3, . . . , 10, Jack, Queen, and
King). A hand in poker consists of five cards. The hand known as a full house consists of
three of one rank and a pair of another. We now consider the probability that a randomly
dealt hand of five cards is a full house.

3.2

Formula

We will now count the number of full houses there are. Suppose you are holding a full house.
There are 13 dierent ranks for the three of a kind. For each of those ranks, there are four
()
dierent cards to choose three from, i.e. there are 43 three of a kinds from each rank. There
()
are 12 remaining ranks to have the pair in and for each of those 12, there are 42 possible
( )
pairs. The total number of unordered hands is given by 52
. Thus the probability of being
5
randomly dealt a full house is
13

(4)

(4)

12
(52)

3.3

6
0.001441
4165

Computer Code

deal := Module[{card1, card2, card3, card4, card5},


card1 = {Random[Integer, {1, 4}], Random[Integer, {1, 13}]};
card2 = card1;
While[card2 == card1,
card2 = {Random[Integer, {1, 4}], Random[Integer, {1, 13}]}
](*While*);
card3 = card1;
While[card3 == card1 card3 == card2,
card3 = {Random[Integer, {1, 4}], Random[Integer, {1, 13}]}
8

](*While*);
card4 = card1;
While[card4 == card1 card4 == card2 card4 == card3,
card4 = {Random[Integer, {1, 4}], Random[Integer, {1, 13}]}
](*While*);
card5 = card1;
While[card5 == card1 card5 == card2 card5 == card3
card5 == card4,
card5 = {Random[Integer, {1, 4}], Random[Integer, {1, 13}]}
](*While*);
{card1, card2, card3, card4, card5}
](*Module*)

hand[cards ] := Module[{count, i},


Do[count[i] = 0, {i, 1, 13}
](*Do*);
Do[count[cards[[i]][[2]]]++, {i, 1, 5}
](*Do*);
Table[count[i], {i, 1, 13}]
](*Module*)

fullhouse[vec ] := Module[{ind = 0},


Do[
Do[
If[vec[[i]] == 2 && vec[[j]] == 3, ind = 1
](*If*),
{j, 1, 13}](*Do*),
{i, 1, 13}](*Do*);
ind
](*Module*)

winning[n ] := Module[{count = 0, d},


Do[
d = deal;
count = count + fullhouse[hand[d]],
{i, 1, n}](*D0*);
N[count/n, 6]
](*Module*)

3.4

Explanation of Computer Code

We encoded each card as an ordered pair. The first entry of the ordered pair was an integer
from 1 to 4 representing the suit of the card. The second entry was an integer from 1 to 13
representing the rank of the card.
We first wrote a routine Deal. Deal randomly dealt a first card as described in the
paragraph above. We used a while loop to keep drawing until the second card was distinct
from the first card. In a similar manner the third, fourth, and fifth cards were repeatedly
drawn until each card did not match any of the previously drawn cards.
Next we had to determine a way for the computer to tell whether or not a hand contained
a full house. Typically if a human is holding a fullhouse they will arrange the matching ranks
to be adjacent. However, the hand the computer is holding is not arranged in any particular
order. For instance the first, third, and fifth card may all be the same rank and the second
and fourth may be the same rank; or the first and fifth card are the same rank and the
second, third, and fourth card are the same rank. To get around this diculty we wrote a
routine Hand. The input to Hand was a list of five cards and each card was a list of two
integers. Hand returned a list of 13 integers where the ith number on the list is the number
of cards of rank i in the hand.
For example the hand consisting of a 5 of diamonds, a 6 of clubs, a 5 of hearts, an Ace
of hearts, and a 10 of spades would be coded as

{{4, 5}, {3, 6}, {2, 5}, {2, 1}, {1, 10}}

(3.4)

When Hand receives the input (3.4) it would return the 13-vector

{1, 0, 0, 0, 2, 1, 0, 0, 0, 1, 0, 0, 0}

Next we observe that a hand is a full house if and only if the 13-vector returned by Hand

10

was all zero except for having exactly one 3 and one 2.
Thus we wrote a routine Fullhouse. Fullhouse received the output of Hand and returned
a 1 if and only if the 13-vector met the requirement to represent a full house as stated in the
previous paragraph.
The final routine Winning had a single input n. Winning was a loop which ran n times. It
would deal a hand with Deal, pass the hand to Hand and then pass this output to Fullhouse.
Finally, Winning would increment a counter by the output of Fullhouse. Then Winning
returned an empirically determined probability that a randomly dealt hand of five cards
would be a full house.

3.5

Results

Table 2 shows the output of Winning with n = 100, 000. The theoretical probability is
0.001441.
Table 2: Empirical Probability of being Dealt a Full House
Run Number Probability Run Number Probability
1
0.00148
6
0.00138

4
4.1

0.00134

0.00161

0.00154

0.00134

0.00144

0.00137

0.00159

10

0.00146

Craps
Problem

Craps is a game where one player rolls two fair dice. The two dice are then added together and
their sum is observed. The player immediately wins if the sum is a 7 or 11. He immediately
11

loses if the sum is a 2, 3, or 12. If the sum of the roll is a 4, 5, 6, 8, 9, or 10, the player must
continue to roll until either a 7 or the first sum is rolled again . If the player matches the
sum of his first roll before rolling a 7 he wins. If the player rolls a 7 before he rolls his first
sum he loses. What is the probability that a player will win a game of craps?

4.2

Formula

Let W be the event that a player wins a game of craps. Since the way the game is played
is heavily dependent on the first roll, we will condition on the outcome of the first roll. For
2 i 12, let Fi be the event that you roll a sum of i on the first roll. Then by the Law of
Total Probabilities we have
P (W ) =

12

P (W |Fi )P (Fi )

(4.5)

i=2

We will carefully examine the case where the first roll is a sum of 4 and then the pattern
will be clear. Let R4 be the event of rolling a sum of 4 on a single roll. The probability of
eventually rolling a 4 before rolling a 7 given that the first roll is a 4 is given by
and therefore we have

P (W |F4 ) =

P (R4 )
3/36
3
=
=
P (R4 ) + P (R7 )
3/36 + 6/36
9

Computing in a similar manner, we have


P (W |R2 ) = 0

P (W |R3 ) = 0

P (W |R4 ) =

3
9

P (W |R5 ) =

4
10

P (W |R6 ) =

5
11

P (W |R7 ) = 1

P (W |R8 ) =

5
11

P (W |R9 ) =

4
10

P (W |R10 ) =

3
9

P (W |R11 ) = 1 P (W |R12 ) = 0

12

P (R4 )
,
P (R4 )+P (R7 )

Now we compute each term in equation (4.5)


P (W |R2 )P (R2 ) = 0
P (W |R4 )P (R4 ) =

1
36

=0

P (W |R3 )P (R3 ) = 0

3
9

3
36

1
36

P (W |R6 )P (R6 ) =

5
11

5
36

25
396

P (W |R8 )P (R8 ) =

5
11

5
36

25
396

P (W |R10 )P (R10 ) =

3
36

P (W |R12 )P (R12 ) = 0

1
36

=0

3
9

2
36

=0

4
36

2
45

P (W |R7 )P (R7 ) = 1

6
36

1
6

4
36

2
45

P (W |R11 )P (R11 ) = 1

2
36

1
18

P (W |R5 )P (R5 ) =

1
36

P (W |R9 )P (R9 ) =

4
10

4
10

Thus the probability of winning craps is given by summing the previous table.

P (W ) =

12

P (W |Fi )P (Fi ) =

i=2

4.3

Computer Code

roll := Module[{x, y},


x = Random[Integer, {1, 6}];
y = Random[Integer, {1, 6}];
x+y
](*Module*)

game := Module[{firstroll, currentroll},


firstroll = roll;
Switch[
firstroll,
7, out = 1;
Goto[end],
11, out = 1;
Goto[end],
2, out = 0;
Goto[end],
3, out = 0;
Goto[end],
12, out = 0;

13

244
= 0.4929
495

Goto[end],
,
currentroll = -1;
While[
currentroll != 7 && currentroll != firstroll,
currentroll = roll;
](*While*);
](*Switch*);
If[
currentroll == firstroll,
out = 1, out = 0
](*If*);
Label[end];
out
](*Module*)

craps[n ] := Module[{count = 0},


Do[
count = count + game,
{n}](*Do*);
N[count/n, 6]
](*Module*)

4.4

Explanation of Computer Code

The first routine we wrote was Roll. The output for Roll was an integer between 2 and 12,
inclusively. We could not have Mathematica randomly pick an integer between 2 and 12
because that would not represent the probability of rolling any given sum with two dice. So
we uniformly randomly chose two integers between 1 and 6, inclusively and summed them
to mimic the rolling of the dice.
The next routine we wrote was Game. Game provided a simulation of one complete
game of craps. Thus Game required no inputs and returned a 1 if you won or a 0 if you lost.
We first initialized a variable Firstroll with Roll. We placed a Label named end at the very
end of Game. In order to decide the subsequent steps following the first roll we implemented
a Switch in Mathematica. Basically a Switch is a multi-ary If, then, else statement.
14

The Switch was implemented on Firstroll. If the value of Firstroll was a 7 or 11 we set out,
the output of Game, equal to one and used a Goto statement to jump to the Label end
and thus terminate Game. If the value for Firstroll was a 2, 3, or 12 we set out equal to zero
and jumped to end. Mathematica executes the lines after the underscore if Firstroll was none
of the explicitly listed values for the Switch. For all other values of Firstroll we continued
the game with a While loop. We initialized Currentroll to 1 to make sure the While
loop was run at least once. The While loop continued to update Currentroll by Roll until
Currentroll was either a 7 or equal to Firstroll. Finally we set out equal to 1 if Currentroll was
equal to Firstroll or set out equal to 0 otherwise.
The last routine we wrote was Craps. Craps takes an input n and runs Game n times.
We incremented a counter by the output of Game. The final output is the counter divided
by the input n, which gives us an empirically determined probability for winning craps.

4.5

Results

The exact probability of winning a game of craps is 0.4929. Table 3 shows the empirical
probabilities of winning a game of craps.
n = 1, 000, 000
Table 3: Empirical Probability of Winning Craps
Run Number Probability Run Number Probability
1
0.493118
6
0.493918
2

0.493154

0.493146

0.493428

0.492831

0.492829

0.493869

0.492872

10

0.492891

15

Birthday Problem

5.1

Problem

Suppose you meet a random father of two kids. You find out that one of his kids is a boy
born on Tuesday. What is the probability that his other child is a boy?
A typical reaction to this problem is that one assumes that the probability is 50% based
on the assumption of independence of the genders of the children. However, we will see that
the probability in question is not 50%.

5.2

Formula

Assume that the probability that a child is a boy is 50%. Assume that the probability
that a child is born any given day of the week is 1/7. The sample space for the experiment
consists of pairs of children. We can represent each child as an ordered pair with the first
entry being a 0 or a 1, and the second entry being a integer from 1 to 7. For instance, we
represent a boy born on Tuesday by (0, 3). We represent a pair of kids by a pair of such
ordered pairs. I.e. a pair of children is represented by a tuple of the form

((x1 , y1 ), (x2 , y2 ))

(5.6)

where xi {0, 1} and yi {1, 2, . . . , 7}. For example if the children in a family consist of a
boy born on Tuesday and a girl born on Saturday, we represent this as ((0, 3), (1, 7)). Let
S be the set of all such ordered pairs of ordered pairs. We assume that there is a first child
and a second child in each such family. Let A be the event that at least one of the children
is a boy born on Tuesday and the other child is a boy. Let B be the event that at least one
of the children is a boy born on Tuesday. The probability we seek is

P r(A|B) =

P r(A)
|A|/|S|
|A|
P (A B)
=
=
=
P (B)
P r(B)
|B|/|S|
|B|
16

The ordered pairs in B consist of all the ordered pairs beginning with (0, 3) of which
there are 14. Also B contains all the ordered pairs ending in (0, 3) which there are also 14
of. However, we must subtract o one for the pair ((0, 3), (0, 3)) otherwise we would have
counted this ordered pair twice. Thus |B| = 14 + 14 1 = 27. The ordered pairs in A consist
of all ordered pairs beginning with (0, 3) and starting with a 0 in the first slot of the second
ordered pair of which there are 7. Also A consists of all ordered pairs ending with a (0, 3)
and starting with a 0 in the first slot of the first ordered pair of which there are 7. Again
we have to subtract 1 to prevent the ordered pair ((0, 3), (0, 3)) from being counted twice.
Thus |A| = 7 + 7 1 = 13. So

P r(A|B) =

5.3

|A|
13
=
= 0.481
|B|
27

Computer Code

sample := {{Random[Integer, {0, 1}],


Random[Integer, {1, 7}]}, {Random[Integer, {0, 1}],
Random[Integer, {1, 7}]}}

family := Module[{out, kids = sample, temp},


While[
kids[[1]] != {0, 3} && kids[[2]] != {0, 3},
kids = sample;
](*While*);
If[
kids[[1]] != {0, 3},
temp = kids[[1]];
kids[[1]] = kids[[2]];
kids[[2]] = temp
](*If*);
If[
kids[[2]][[1]] == 0,
out = 1,
out = 0
](*If*);
17

out
](*Module*)

boytuesday[n ] := Module[{count = 0},


Do[
count = count + family,
{n}
](*Do*);
N[count/n, 6]
](*Module*)

5.4

Explanation of Computer Code

The first routine we wrote was Sample. Sample produces a random tuple of the form (5.6).
The next routine we wrote was Family. Family used a variable Kids of the form (5.6). Using
a While loop, Family repeatedly called Sample until at least one of the pairs was a boy
born on Tuesday. Then if the first pair in Kids was not a boy born on Tuesday, we swapped
the two pairs such that the first pair of Kids was always a boy born on Tuesday. Then Family
looked at the first entry of the second ordered pair and returned a 1 if it was a boy or a 0
otherwise.
The final routine BoyTuesday had one input parameter n. BoyTuesday called Family
n times and incremented a counter Count by the output of Family. BoyTuesday returned
Count/n. The value that BoyTuesday returns is the empirically determined probability that
the other child is a boy given that one of the children is a boy born on Tuesday.

5.5

Results

Table 4 shows the empirical probabilities of the birthday problem. Notice all the probabilities
are less than 1/2, and therefore this empirical data alone suggests that the probability in
question is less than 1/2. Moreover, all the entries in Table 4 are close to the theoretical

18

probability 0.481 which serves as strong evidence that the theoretical probability is correct.

Table 4: Empirical Probability for Birthday Problem


Run Number Probability Run Number Probability
1
0.481815
6
0.481592

0.482101

0.481592

0.481531

0.481984

0.480895

0.481437

0.481583

10

0.481407

Conclusion
Throughout this project we both empirically determined and theoretically determined

probabilities for certain problems. In the end the results from our empirically determined
probabilities were really close to the theoretical ones. This demonstrates that there are
diverse ways to solve probability problems. It all depends on who is trying to solve them.
Some people may not be as fluent with programming, so they may have to solve problems
by hand, but also there may be someone that can not solve some problems by hand. We
also realized that some problems may be dicult to solve using the theory, but may be
relatively easy to solve via simulations and vice versa. A problem such as Gamblers Ruin
was extremely dicult to solve by hand, but it was simple to program given that you have
that ability. Fullhouse was very simple to solve in theory, but proved dicult to write a
program for. There are a couple of things that I can do further with this project in the
future. One is that I can look deeper into the empirically determined probabilities and
study the variance and error among the results. This project used my mathematical skills
as well as my computer science skills to combine the two and show how technology can help
with math.
19

References
[MD]

Degroot, Morris H. Schervish, Mark J. Probability and Statistics. Boston, MA:


Pearson Education Inc, 2012

20

You might also like