Professional Documents
Culture Documents
52
Advanced dice
53
[ http://gmdice.com/ ]
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).
54
Same as
randi([0,1],1,N);
55
Dice Simulator
http://www.dicesimulator.com/
Support up to 6 dice and also has some background
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.
59
Two Dice
A pair of dice
60
Double six
[ 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
2
36
1
P[sum of the two dice = 12]
36
0.2
0.15
0.1
0.05
100
200
300
400
500
600
700
800
900
1000
65
[Gorroochurn, 2012]
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
Horizontal concatenation:
number of rows.
Vertical concatenation:
number of columns.
67
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
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
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
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
for k = 1:5
x = k
end
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.
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);
Number_11 = sum(S==11)
Number_12 = sum(S==12)
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
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
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
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
Exercise
The following script
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
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
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
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
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]
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
if expression
statements
elseif expression
statements
else
statements
end
The statements are executed if the real part of the expression has
true
grade = pass
88
score 50
false
grade = fail
true
grade = A
score 80
true
grade = B
false
score 70
true
grade = C
89
false
score 60
true
grade = D
false
score 50
false
grade = F
Exercise
Write a MATLAB script to evaluate the relative frequencies
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
0.9
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