40
The word “dice” 52 Historically, dice is the plural of die. In modern standard English, dice is used as both the singular and the plural. Example of 19th Century bone dice

The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

The word “dice”

52

Historically, dice is the plural of die.

In modern standard English, dice is used as both the

singular and the plural.

Example of 19th Century bone dice

Page 2: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

“Advanced” dice

53

[ http://gmdice.com/ ]

Page 3: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

randi function

54

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].

We have already seen the rand and randn functions.

Page 4: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

randi function: Example

55

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

LLN_cointoss.m

Same as

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

Page 5: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Dice Simulator

56

http://www.dicesimulator.com/

Support up to 6 dice and also has some background

information on dice and random numbers.

Page 6: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Two Dice

57

Page 7: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Two-Dice Statistics

58

Page 8: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Classical Probability

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 fraction in which the bottomrepresents the number of possible outcomes, while the number on top represents the number of outcomes in which the event of interest occurs.

Page 9: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Two Dice

60

A pair of dice

Double six

Page 10: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Two dice: Simulation

61

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

Page 11: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Two dice

62

Assume that the two dice are fair and independent.

P[sum of the two dice = 5] = 4/36

Page 12: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Two dice

63

Assume that the two dice are fair and independent.

Page 13: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Two-Dice Statistics

64

Page 14: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Leibniz’s Error (1768)

65

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...

Leibniz is a German philosopher, mathematician,and statesman who developed differential and integral calculus independently of Isaac Newton.

2[sum of the two dice = 11]

36

1[sum of the two dice = 12]

36

P

P

[Gorroochurn, 2012]

0 100 200 300 400 500 600 700 800 900 10000

0.05

0.1

0.15

0.2

0.25

Page 15: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Two dice: MATLAB Simulation

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 100 200 300 400 500 600 700 800 900 1000

0

0.01

0.02

0.03

0.04

0.05

0.06

0.07

0.08

[SumofTwoDice.m]

Page 16: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Concatenation

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.

Page 17: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

More on plot: Line specification

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 - solid

m magenta o circle : dotted

c cyan x x-mark -. dashdot

r red + plus -- dashed

g green * star

b blue s square

w white d diamond

k black v triangle (down)

^ triangle (up)

< triangle (left)

> triangle (right)

p pentagram

h hexagram

Page 18: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Character Strings

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.

Page 19: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

More on plot : Examples

70

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

ts a cyan dotted line with a

plus at each data point

0 1 2 3 4 5 6 7 8 9 10-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-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

PLOT(X,Y,'bd')plots

blue diamond at each data point

but does not draw any line

Page 20: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

More on plot : Axes and Title

71

0 1 2 3 4 5 6 7 8 9 10-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1sin and cos functions

x

y

y = sin(x)

y = cos(x)

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

Page 21: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Two dice: MATLAB Simulation

72

[SumofTwoDice.m]

0 100 200 300 400 500 600 700 800 900 10000

0.05

0.1

0.15

0.2

0.25

0.3

0.35

0.4

0.45

0.5

Number of Rolls

Rela

tive F

requency

Sum = 11

Sum = 12

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);

2 3 4 5 6 7 8 9 10 11 120

20

40

60

80

100

120

140

160

180

Page 22: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Loops

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.

Page 23: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

for loop: a preview

74

Execute a block of statements a specified number of times.

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 endstatement are known as the body of the loop. They are executed repeatedly during each pass of the for loop.

for = expr k

body end

for k = 1:5 x = k end

for_ex1.m

Page 24: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Two dice: Probability Calculation

75

Generate all the 36 possibilities.

Find the corresponding sum.

Count how many, among the

36, have a particular value.

Nested loops

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

Concatenation

Page 25: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Vectorization and Preallocation

76

LLN_cointoss.m

LLN_cointoss_for_preallocated.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

The script will still run without this line.

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

Page 26: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Preallocating Vectors (or Arrays)

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 foundthat 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.

Page 27: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

zeros, ones, eye

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.

Page 28: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Exercise

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

Page 29: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Galileo and the Duke of Tuscany

(1620)

80

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:

[Gorroochurn, 2012]

Page 30: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Exercise

81

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.

2 4 6 8 10 12 14 16 180

0.02

0.04

0.06

0.08

0.1

0.12

0.14

sum of three dice

probability

relative frequency

0 100 200 300 400 500 600 700 800 900 10000

0.1

0.2

0.3

0.4

0.5

0.6

0.7

Number of Rolls

Rela

tive F

requency

Sum = 11

Sum = 12

Page 31: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Three-Dimensional Arrays

82

Arrays in MATLAB are not limited to two dimensions.

Page 32: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Three-Dimensional Arrays

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.

Page 33: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Empty Array

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.

Page 34: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Scandal of Arithmetic

85

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)?

44 4

4

2424 24

24

6 5 5( ) 1 .518

6 6

36 35 35( ) 1 .491

36 36

P A

P B

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

Page 35: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

“Origin” of Probability Theory

86

Probability theory was originally inspired by gamblingproblems.

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

Page 36: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Branching Statements: if statement

87

General form:

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 ~=.

if expression

statements

elseif expression

statements

else

statements

end

The ELSE and ELSEIF parts

are optional.

Page 37: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Example: Assigning Grades

88

Pass vs. Fail

% Generate a random number

score = randi(100, 1)

if score >= 50

grade = 'pass'

else

grade = 'fail'

end

score ≥ 50true false

grade = ‘pass’ grade = ‘fail’

if_ex_1.m

Page 38: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Example: Assigning Letter Grades

89

score ≥ 80

score ≥ 70

score ≥ 60

score ≥ 50

true false

true false

true false

true false

grade = ‘A’

grade = ‘B’

grade = ‘C’

grade = ‘D’ grade = ‘F’

% 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

Page 39: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Exercise

90

Write a MATLAB script to evaluate the relative frequencies

involved in the scandal of arithmetic.

0 100 200 300 400 500 600 700 800 900 10000

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

number of rolls

rela

tive f

requency

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

Page 40: The word “dice” - Thammasat University - 1_2 - Basic... · 2019-11-06 · Character Strings 69 A character string is a sequence of any number of characters enclosed in single

Exercise

91

Write a MATLAB script to evaluate the relative frequencies

involved in the Monty Hall game.

0 100 200 300 400 500 600 700 800 900 10000

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Number of Trials

Rela

tive F

requency o

f W

innin

g

Not Switch

Swicth