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
53
[ http://gmdice.com/ ]
54
We have already seen the rand and randn functions.
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 -byn 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 ].
55
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);
56
http://www.dicesimulator.com/
Support up to 6 dice and also has some background information on dice and random numbers.
57
58
59
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
in which the
represents the number of possible outcomes, while the number on
represents the number of outcomes in which the event of interest occurs.
60
A pair of dice
Double six
61
[ http://www2.whidbey.net/ohmsmath/webwork/javascript/dice2rol.htm
]
62
Assume that the two dice are fair and independent.
P[sum of the two dice = 5] = 4/36
63
Assume that the two dice are fair and independent.
64
0.2
0.15
0.1
0.05
0.25
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...
P [sum of the two dice = 11]
2
36
P [sum of the two dice = 12]
1
36
0
0 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]
66
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
RF12 = cumsum(S==12)./(1:n); plot(1:n,RF12, 'r' )
0.08
0.07
0.06
0.05
0.04
0.03
0.02
0.01
0
0 100 200 300 400 500 600 700 800 900 1000
[SumofTwoDice.m]
67
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.
68
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 yellow . point m magenta o circle c cyan r red x x-mark
+ plus g green * star
- solid
: dotted
-. dashdot
-- dashed b blue s square w white d diamond k black v triangle (down)
^ triangle (up)
< triangle (left)
> triangle (right) p pentagram h hexagram
69
A character string is a 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 .
70
PLOT(X,Y,'c+:') plo ts a cyan dotted line with a plus at each data point
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0
1
0.8
0.6
0.4
1 2 3 4 5 6 7 8 9 10
PLOT(X,Y,'bd') plots blue diamond at each data point but does not draw any line
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7 8 9 10
71
sin and cos functions 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
0.8
0.6
0.4
0.2
-1
0 1 2 y = sin(x) y = cos(x)
3 4 5 x
6 7 8 9 10
72
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
RF12 = cumsum(S==12)./(1:n); plot(1:n,RF12, 'r' ) 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)
N_S_Sim = hist(S,S_Support);
[SumofTwoDice.m]
120
100
80
60
40
20
0
180
160
140
0.3
0.25
0.2
0.15
0.1
0.05
0
0
0.5
0.45
0.4
0.35
Sum = 11
Sum = 12
100 200 300 400 500 600 700
Number of Rolls
800 900 1000
2 3 4 5 6 7 8 9 10 11 12
73
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.
74
Execute a block of statements a specified number of times. for_ex1.m
for = 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 statement are known as the body of the loop. They are executed repeatedly during each pass of the for loop.
75
Generate all the 36 possibilities.
Find the corresponding sum.
Count how many, among the
36, have a particular value.
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
Nested loops
Concatenation
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
76
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 s(1) = randi([0,1]); for k = 2:N
The script will still run without this line.
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
77
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.
78
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.
79
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 6 × 6. The value of its ( k1 , k2 ) element should be k1 + k2 .
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
80
[Gorroochurn, 2012]
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:
81
0.5
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.4
0.3
0.2
0.1
0.14
0.12
0.1
0.08
0.06
0.04
0.02
0
0
0.7
0.6
Sum = 11
Sum = 12
100 200 300 400 500 600
Number of Rolls
700 800 900 1000 probability relative frequency
0
2 4 6 8 10 sum of three dice
12 14 16 18
82
Arrays in MATLAB are not limited to two dimensions.
83
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 4×3×2 matrix of all 0s.
84
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 removed from a matrix.
85
A
B
6
4
5
4
6
4
36
24
35
24
36
24
5
4
.518
35
24
36
.491
[http://www.youtube.com/watch?v=MrVD4q1m1Vo]
86
Probability theory was originally inspired by gambling problems.
In 1654, Chevalier de Méré 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
87
if
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 if ’s.
The expression usually contains ==, <, >, <=, >=, or ~=.
88
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’ score ≥ 50 false grade = ‘fail’
true grade = ‘A’
score ≥ 80 true false score ≥ 70
% 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 grade = ‘B’ true score ≥ 60 false grade = ‘C’ true false score ≥ 50
89 grade = ‘D’ grade = ‘F’
90
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
0.6
0.5
0.4
0.3
0.2
0.1
0
0 100 200 300 400 500 600 700 800 900 1000 number of rolls
91
Write a MATLAB script to evaluate the relative frequencies involved in the Monty Hall game.
1
0.9
Not Switch
Swicth
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0 100 200 300 400 500 600 700 800 900 1000
Number of Trials