You are on page 1of 40

The word dice

Historically, dice is the plural of die.


In modern standard English, dice is used as both the

singular and the plural.

52

Example of 19th Century bone dice

Advanced dice

53

[ http://gmdice.com/ ]

We have already seen the rand and randn functions.

randi function
Generate uniformly distributed pseudorandom integers
randi(imax) returns a scalar value between 1 and

imax.
randi(imax,m,n) and randi(imax,[m,n])
return an m-by-n matrix containing pseudorandom integer
values drawn from the discrete uniform distribution on the
interval [1,imax].
randi(imax) is the same as randi(imax,1).

randi([imin,imax],...) returns an array

containing integer values drawn from the discrete uniform


distribution on the interval [imin,imax].

54

randi function: Example


LLN_cointoss.m
close all; clear all;
N = 1e3; % Number of trials (number of times that the coin is tossed)
s = (rand(1,N) < 0.5); % Generate a sequence of N Coin Tosses.
% The results are saved in a row vector s.
NH = cumsum(s); % Count the number of heads
plot(NH./(1:N)) % Plot the relative frequencies

Same as
randi([0,1],1,N);

55

Dice Simulator
http://www.dicesimulator.com/
Support up to 6 dice and also has some background

information on dice and random numbers.

56

Two Dice

57

Two-Dice Statistics

58

Classical Probability
Assumptions
The number of possible outcomes is finite.
Equipossibility: The outcomes have equal probability of

occurrence.

Equi-possible; equi-probable; euqally likely; fair


The bases for identifying equipossibility were often

physical symmetry (e.g. a well-balanced die, made of homogeneous


material in a cubical shape)
a balance of information or knowledge concerning the various possible
outcomes.

Formula: A probability is a fraction in which the bottom

represents the number of possible outcomes, while the number on


top represents the number of outcomes in which the event of
interest occurs.

59

Two Dice
A pair of dice

60

Double six

Two dice: Simulation

[ http://www2.whidbey.net/ohmsmath/webwork/javascript/dice2rol.htm ]

61

Two dice
Assume that the two dice are fair and independent.
P[sum of the two dice = 5] = 4/36

62

Two dice
Assume that the two dice are fair and independent.

63

Two-Dice Statistics

64

Leibnizs Error (1768)


Though one of the finest minds of his age, Leibniz was not

immune to blunders: he thought it just as easy to throw 12


with a pair of dice as to throw 11.
The truth is...
0.25

2
36
1
P[sum of the two dice = 12]
36

P[sum of the two dice = 11]

0.2

0.15

0.1

0.05

100

200

300

400

500

600

700

800

900

1000

Leibniz is a German philosopher, mathematician,

and statesman who developed differential and


integral calculus independently of Isaac Newton.

65

[Gorroochurn, 2012]

Two dice: MATLAB Simulation


n = 1e3;
Number_Dice = 2;
D = randi([1,6],Number_Dice,n);
S = sum(D); % sum along each column
RF11 = cumsum(S==11)./(1:n);
plot(1:n,RF11)
hold on

0.08
0.07
0.06
0.05
0.04
0.03
0.02

RF12 = cumsum(S==12)./(1:n);
plot(1:n,RF12,'r')

[SumofTwoDice.m]

66

0.01
0

100

200

300

400

500

600

700

800

900

1000

Concatenation
Concatenation is the process of

joining arrays to make larger ones.


The pair of square brackets [] is

the concatenation operator.

Horizontal concatenation:

Concatenate arrays horizontally


using commas.
Each array must have the same

number of rows.

Vertical concatenation:

Concatenate arrays vertically using


semicolons.
The arrays must have the same

number of columns.

67

More on plot: Line specification


Various line types (styles), plot symbols (markers) and colors
may be obtained with plot(X,Y,S) where S is a character
string made from one element from any or all the following 3
columns:
y
m
c
r
g
b
w
k

68

yellow
magenta
cyan
red
green
blue
white
black

.
o
x
+
*
s
d
v
^
<
>
p
h

point
circle
x-mark
plus
star
square
diamond
triangle (down)
triangle (up)
triangle (left)
triangle (right)
pentagram
hexagram

:
-.
--

solid
dotted
dashdot
dashed

Character Strings
A character string is a

69

sequence of any number of


characters enclosed in single
quotes.
If the text includes a single
quote, use two single quotes
within the definition.
These sequences are arrays,
like all MATLAB variables.
Their class or data type is
char, which is short for
character.
You can concatenate strings
with square brackets, just as
you concatenate numeric
arrays.

To convert numeric values


to strings, use functions,
such as num2str or
int2str.

More on plot : Examples


PLOT(X,Y,'c+:')plo

ts a cyan dotted line with a


plus at each data point

blue diamond at each data point


but does not draw any line

0.8

0.8

0.6

0.6

0.4

0.4

0.2

0.2

-0.2

-0.2

-0.4

-0.4

-0.6

-0.6

-0.8

-0.8

-1

70

PLOT(X,Y,'bd')plots

10

-1

10

More on plot : Axes and Title


sin and cos functions
1
y = sin(x)
y = cos(x)

0.8
0.6
0.4
0.2

x = linspace(0,10,100);
y1 = sin(x);
y2 = cos(x);
plot(x,y1,'mo--',x,y2,'bs--')
title('sin and cos functions')
xlabel('x')
ylabel('y')
legend('y = sin(x)','y = cos(x)')
grid on

0
-0.2
-0.4
-0.6
-0.8
-1

71

5
x

10

Two dice: MATLAB Simulation


0.5

RF11 = cumsum(S==11)./(1:n);
plot(1:n,RF11)
hold on
RF12 = cumsum(S==12)./(1:n);
plot(1:n,RF12,'r')

0.4
0.35

Relative Frequency

n = 1e3;
Number_Dice = 2;
D = randi([1,6],Number_Dice,n);
S = sum(D); % sum along each column

0.3
0.25
0.2
0.15
0.1
0.05
0

xlabel('Number of Rolls')
ylabel('Relative Frequency')
legend('Sum = 11', 'Sum = 12')
grid on
figure
S_Support = (1*Number_Dice):(6*Number_Dice);
hist(S,S_Support)

Sum = 11
Sum = 12

0.45

100

200

300

400
500
600
Number of Rolls

700

180
160
140
120
100

N_S_Sim = hist(S,S_Support);

80
60

[SumofTwoDice.m]

40
20

72

10

11

12

800

900

1000

Loops
Loops are MATLAB constructs that permit us to execute a

sequence of statements more than once.


There are two basic forms of loop constructs:
while loops and
for loops.

The major difference between these two types of loops is in

how the repetition is controlled.

The code in a while loop is repeated an indefinite number of

times until some user-specified condition is satisfied.


By contrast, the code in a for loop is repeated a specified
number of times, and the number of repetitions is known
before the loops starts.
73

for loop: a preview


Execute a block of statements a specified number of times.
for_ex1.m
for k = expr
body
end

for k = 1:5
x = k
end

k is the loop variable (also known as the loop index or

iterator variable).
expr is the loop control expression, whose result
usually takes the form of a vector.
The elements in the vector are stored one at a time in the

variable k, and then the loop body is executed, so that the loop
is executed once for each element in the array produced by
expr.

The statements between the for statement and the end


74

statement are known as the body of the loop. They are


executed repeatedly during each pass of the for loop.

Two dice: Probability Calculation


Generate all the 36 possibilities.

75

Number_Dice = 2;
Dice_Support = 1:6;
S_Support = (1*Number_Dice):(6*Number_Dice);
S = [];
for k1 = Dice_Support
for k2 = Dice_Support
Nested loops
S = [S k1+k2];
end
Concatenation
end
Size_SampleSpace = length(S);

Find the corresponding sum.

Number_11 = sum(S==11)
Number_12 = sum(S==12)

Count how many, among the


36, have a particular value.

% Count all possible cases at once


N_S = hist(S,S_Support)
P = sym(N_S)/Size_SampleSpace

Vectorization and Preallocation


Revisiting an old script:
close all; clear all;
N = 1e3; % Number of trials (number of times that the coin is tossed)
s = randi([0,1],1,N); % Generate a sequence of N Coin Tosses.
% The results are saved in a row vector s.
NH = cumsum(s); % Count the number of heads
plot(NH./(1:N)) % Plot the relative frequencies

LLN_cointoss.m
close all; clear all;
N = 1e3; % Number of trials
s = zeros(1,N); % Preallocation

The script will still run without this line.

s(1) = randi([0,1]);
for k = 2:N
s(k) = randi([0,1]);
end
NH = cumsum(s); % Count the number of heads
plot(NH./(1:N)) % Plot relative frequencies

LLN_cointoss_for_preallocated.m
76

Preallocating Vectors (or Arrays)


Used when the content of a vector is computed/added/known while the loop is

executed.
One method is to start with an empty vector and extend the vector by adding
each number to it as the numbers are computed.
Inefficient.
Every time a vector is extended a new chunk of memory must be found
that is large enough for the new vector, and all of the values must be copied from
the original location in memory to the new one. This can take a long time.
A better method is to preallocate the vector to the correct size and then
change the value of each element to store the desired value.
This method involves referring to each index in the output vector, and placing
each number into the next element in the output vector.
This method is far superior, if it is known ahead of time how many elements the
vector will have.
One common method is to use the zeros function to preallocate the vector to
the correct length.

77

zeros, ones, eye


zeros

Create array of all zeros.


zeros returns the scalar 0.
zeros(n) returns an n-by-n matrix of zeros.
zeros(n,m) returns an n-by-m matrix of
zeros.

ones
Create array of all ones.

eye
Create identity matrix.
eye returns the scalar, 1.
eye(n) returns an n-by-n identity matrix with

ones on the main diagonal and zeros elsewhere.


eye(n,m) returns an n-by-m matrix with ones
on the main diagonal and zeros elsewhere.
78

Exercise
The following script

was used to calculate


the probabilities related
to the sum resulting
from a roll of two dice.
Modify the script here
so that the vector S is
preallocated.
Modify the script to
also create a matrix SS.
Its size is 66. The
value of its (k1,k2)
element should be
k1+k2.
79

Number_Dice = 2;
Dice_Support = 1:6;
S_Support = (1*Number_Dice):(6*Number_Dice);
S = [];
for k1 = Dice_Support
for k2 = Dice_Support
S = [S k1+k2];
end
end
Size_SampleSpace = length(S);
Number_11 = sum(S==11)
Number_12 = sum(S==12)
% Count all possible cases at once
N_S = hist(S,S_Support)
P = sym(N_S)/Size_SampleSpace

Galileo and the Duke of Tuscany


[Gorroochurn, 2012]
(1620)
When you toss three dice, the chance of the sum being 10 is

greater than the chance of the sum being 9.


The Grand Duke of Tuscany ordered Galileo to explain a paradox
arising in the experiment of tossing three dice:
Why, although there were an equal number of 6 partitions of the

numbers 9 and 10, did experience state that the chance of throwing a
total 9 with three fair dice was less than that of throwing a total of
10?
Partitions of sums 11, 12, 9 and 10 of the game of three fair dice:

80

Exercise

0.5

0.4

0.3

0.2

0.1

100

200

300

400
500
600
Number of Rolls

700

800

900

1000

0.14
probability
relative frequency

0.12

0.1

0.08

0.06

0.04

0.02

81

Sum = 11
Sum = 12

0.6

Relative Frequency

Write a MATLAB script to


Simulate N = 1000 repeated rolls of three dices.
Calculate the sum of the three dices from the rolls
above.
Plot the relative frequency for the event that the
sum is 9.
In the same figure, plot (in red) the relative
frequency for the event that the sum is 10.
In another figure, create a histogram for the sum
after N rolls of three dice.
Calculate the actual probability for each possible
value of the sum.
In another figure, compare the probability with the
relative frequency obtained after the N simulations.

0.7

8
10
12
sum of three dice

14

16

18

Three-Dimensional Arrays
Arrays in MATLAB are not limited to two dimensions.

82

Three-Dimensional Arrays
Three-dimensional arrays can be created directly using functions

such as the zeros, ones, rand, and randi functions by


specifying three dimensions to begin with.
For example, zeros(4,3,2) will create a 432 matrix of
all 0s.

83

Empty Array
An array that stores no value
Created using empty square

brackets: []
Values can then be added by
concatenating.
Can be used to delete elements
from vectors or matrices.
Individual elements cannot be

removed from matrices.


Matrices always have to have the same

number of elements in every row.


Entire rows or columns could be
84

removed from a matrix.

Scandal of Arithmetic
Which is more likely, obtaining at least one
six in 4 tosses of a fair dice (event A), or
obtaining at least one double six in 24 tosses
of a pair of dice (event B)?
P ( A)

6 5
5

.518
4
6
6
4

36 24 3524
35
P( B)

.491
24
36
36
24

85

[http://www.youtube.com/watch?v=MrVD4q1m1Vo]

Origin of Probability Theory


Probability theory was originally inspired by gambling

86

problems.
In 1654, Chevalier de Mr invented a gambling system
which bet even money on case B.
When he began losing money, he asked his mathematician
friend Blaise Pascal to analyze his gambling system.
Pascal discovered that the Chevalier's system would lose
about 51 percent of the time.
Pascal became so interested in probability and together with
another famous mathematician, Pierre de Fermat, they laid
the foundation of probability theory.
best known for Fermat's Last Theorem

Branching Statements: if statement


General form:

if expression
statements
elseif expression
statements
else
statements
end

The ELSE and ELSEIF parts


are optional.

The statements are executed if the real part of the expression has

all non-zero elements.


Zero or more ELSEIF parts can be used as well as nested ifs.
The expression usually contains ==, <, >, <=, >=, or ~=.
87

Example: Assigning Grades


Pass vs. Fail
if_ex_1.m
% Generate a random number
score = randi(100, 1)
if score >= 50
grade = 'pass'
else
grade = 'fail'
end

true
grade = pass
88

score 50

false
grade = fail

Example: Assigning Letter Grades

true
grade = A

score 80

true
grade = B

false

score 70

true
grade = C

89

% Generate a random number


score = randi(100, 1)
if score >= 80
grade = 'A'
elseif score >= 70
grade = 'B'
elseif score >= 60
grade = 'C'
elseif score >= 50
grade = 'D'
else
grade = 'F'
end

false

score 60

true
grade = D

false

score 50

false
grade = F

Exercise
Write a MATLAB script to evaluate the relative frequencies

involved in the scandal of arithmetic.


0.8
Event A: At least one six in 4 tosses of a fair dice
Event B: At least one double six in 24 tosses of a pair of dice

0.7

relative frequency

0.6
0.5
0.4
0.3
0.2
0.1
0

90

100

200

300

400
500
600
number of rolls

700

800

900

1000

Exercise
Write a MATLAB script to evaluate the relative frequencies

involved in the Monty Hall game.


1
Not Switch
Swicth

0.9

Relative Frequency of Winning

0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

91

100

200

300

400
500
600
Number of Trials

700

800

900

1000

You might also like