0% found this document useful (0 votes)
33 views53 pages

Matlab Lec - 1 - Simulation of Basic Random Experiments

The document discusses simulating random experiments in MATLAB. It begins by explaining how MATLAB uses a pseudorandom number generator to produce random numbers between 0 and 1 that can be used to simulate experiments with random outcomes. Several examples are provided to demonstrate how to simulate common experiments like coin flips, die rolls, and testing processes with random outcomes. The document compares simulation results to theoretical probabilities to validate the simulations.

Uploaded by

misbahrajpoot875
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views53 pages

Matlab Lec - 1 - Simulation of Basic Random Experiments

The document discusses simulating random experiments in MATLAB. It begins by explaining how MATLAB uses a pseudorandom number generator to produce random numbers between 0 and 1 that can be used to simulate experiments with random outcomes. Several examples are provided to demonstrate how to simulate common experiments like coin flips, die rolls, and testing processes with random outcomes. The document compares simulation results to theoretical probabilities to validate the simulations.

Uploaded by

misbahrajpoot875
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Simulating Simple Random

Experiments and Finding


Probability of Events

Instructor:
Dr. Muhammad Obaid Ullah 1
Random Number Generation in Matlab :
o To simulate random experiments, we
need a Source of Randomness.
o Matlab uses a computer algorithm,
referred to as a Pseudorandom
Number Generator, to produce a
sequence of numbers between 0 and
1. 2
rand()
Random Number Generation in Matlab :

>> rand
ans = 0.2105

>> rand(1)
ans = 0.9089

>> rand(1,5)
ans = 0.6411 0.1716 0.3605 0.9291 0.7694

4
Random Number Generation in Matlab :
X = rand(1,1e2); hist(X); grid
figure;
X = rand(1,1e4); hist(X,20); grid
figure;
X = rand(1,1e6); histogram(X); grid

5
Random Number Generation in Matlab :

6
Random Number Generation in Matlab :
o To simulate random experiments, we need a source of
randomness.
o Matlab uses a computer algorithm, referred to as a
pseudorandom number generator, to produce a sequence
of numbers between 0 and 1.
o Each number produced by rand() is in the
interval (0, 1).
o Each time we use rand, we get a new,
unpredictable number(s). 7
Random Number Generation in Matlab :
o Each time we use rand, we get new,
unpredictable number(s).
o Although, the generated number is
unpredictable, however it possesses some
properties (density function).

8
Random Number Generation in Matlab :
o To use the pseudo-random number
generator to simulate an experiment
that contains AN EVENT WITH
PROBABILITY p, we examine one
number, r, produced by the Matlab
algorithm and say that the event occurs
if r < p; otherwise it does not occur.
9
Random Number Generation in Matlab :

oRoughly speaking, the


function rand(1) < p
simulates a coin flip with
P[tail] = p.
10
Random Number Generation in Matlab :
rand < 0.5
rand(1,10) < 0.5

ans = 0
ans = 1 1 0 1 0 0 0 1 1 1

11
RANDOM EXPERIMENTS
WITH
EQUI-PROBABLE
OUTCOMES
12
EX. 1
Simulating Random Experiments – Example-1 :
o Simulate a sequence of four flips of a fair coin.

X = rand(1,4) < 0.5;


X = rand(1,4)
X = 0.9575 0.9649 0.1576 0.9706
X < 0.5
ans = 0 0 1 0
14
EX. 2
Simulating Random Experiments – Example-2 :
o Perform 75 experiments; in each experiment, flip a
coin 100 times and record the number of heads.

X = rand(75,100) < 0.5;


Y = sum(X,2);
o Each Yi is between 0 and 100 and generally around 50.
Y=
55
57
61 16
Random Number Generation in Matlab :

 ceil()
>> ceil(1.1)
ans = 2

>> ceil(1.5)
ans = 2

>> ceil(1.9)
ans = 2
17
Random Number Generation in Matlab :
X = ceil(4*rand(1,1e4)), hist(X); grid
Y = ceil(6*rand(1,1e4)), hist(Y,6); grid
Z = ceil(365*rand(1,1e5)), hist(Z,20); grid

X = 4 2 3 3 4 1 4 1 2 2 …
Y= 4 1 6 2 6 4 2 5 6 2 …
Z = 334 297 286 196 115 6 140 139 207 301 … 18
EX. 3
Simulating Random Experiments – Example-3 :
o A chip fabrication facility produces microprocessors.
Each p chip is tested and assigned a grade
𝑠 ∈ 𝑆 = {𝑠0 , 𝑠1 , 𝑠2 , 𝑠3 }.
o Suppose in testing a microprocessor that all four
grades have probability 0.25, independent of any
other microprocessor.

G = ceil(4*rand(1));
20
Simulating Random Experiments – Example-3 :
o A chip fabrication facility produces microprocessors. Each p
chip is tested and assigned a grade 𝑠 ∈ 𝑆 = {𝑠0 , 𝑠1 , 𝑠2 , 𝑠3 }.
o Suppose in testing a microprocessor that all four grades have
probability 0.25, independent of any other microprocessor.
o Simulate the testing of 100 microprocessors.
o Your output should be a 4 × 1 vector X such that Xi is the
number of grade i microprocessors.
G = ceil(4*rand(1,100)); hist(G,T) creates bins centered
around each T(j) and counts the
T = [1:4]; X = hist(G,T); number of elements of G that
bar(T,X); grid; fall into each bin. 21
Simulating Random Experiments – Example-3 :
X1 = 23 26 25 26
G = ceil(4*rand(1,1e2)) X2 = 23 26 25 26
X1 = hist(G,4) S = 100

N1 = numel(find(G==1));
N2 = numel(find(G==2));
N3 = numel(find(G==3));
N4 = numel(find(G==4));
X2 = [N1 N2 N3 N4]
S = sum(X2)
hist(G,4); grid;

Out of 100 values, there are 23 1s, 26 2s, 25 3s, and 26 4s.
22
Simulating Random Experiments – Example-3 :
o In testing n = 100 microprocessors, what is the probability
of exactly 25 microprocessors of each grade?
clear all; clc; close all;
% Example 1.43 on pp 37 of [Yates-2005]
N = 1e6; % No. of Trials.
s = 0; % Initialize Counter for Success Event.
tic;
for nn = 1:N;
G = ceil(4*rand(1,100));
X = hist(G,[1:4]);
if X(1)==25 && X(2)==25 && X(3)==25 && X(4)==25;
s = s+1;
end
end Elapsed time is 143.549149 seconds.
toc; P_4E = 0.0010
P_4E = s/N 23
Simulating Random Experiments– Example-3 :
o In testing n = 100 microprocessors, what is the probability
of exactly 25 microprocessors of each grade?

(factorial(100)/(factorial(25))^4)*(0.25)^100

ans = 0.0010
24
Probability of 𝒏𝒊 Occurrences of 𝒔𝒊 :

𝑠0 = Grade 1; 𝑠1 = Grade 2; 𝑠2 = Grade 3; 𝑠3 = Grade 4;


𝑝1 = 0.25; 𝑝2 = 0.25; 𝑝3 = 0.25; 𝑝4 = 0.25;
𝑛0 = 25; 𝑛1 = 25; 𝑛2 = 25; 𝑛3 = 25;
 𝑛 = 𝑛0 + 𝑛1 + 𝑛2 + 𝑛3 = 100
25
Multinomial Coefficient:

26
EX. 4
Simulating Random Experiments – Example-4:
How to simulate a coin toss experiment?
Fair Coin:
X = rand(1,1e4) < 0.5;
P_H = sum(X)/length(X) Biased Coin i.e., 𝑷 𝑯 ≠ 𝟎. 𝟓
X = rand(1,1e4) < 0.75;
P_H = 0.4986 P_H = sum(X)/length(X)
P_H = 0.7518

28
EX. 5
Simulating Random Experiments – Example-5:
o Simulate a fair coin tossing experiment.
o What is the simulated probability of obtaining three heads in
four coin tosses?
o Compare your simulation result with the analytical one.
N = 1e5; cn = 0; % Initialize Counter
for k = 1:N;
X = rand(1,4) < 0.5; % 4 Tosses of a Fair Coin
sx = sum(X);
if sx == 3;
cn = cn + 1;
% Exactly 3 Heads P3 = 0.2516
end
end
P3 = cn/N
P3_an = 0.2500
%========
N = 4; k = 3; p = 0.5; P3_an = nchoosek(N,k) * p^k * (1-p)^(N-k) 30
Simulating Random Experiments – Example-5:
o Simulate a fair coin tossing experiment.
o What is the simulated probability of obtaining three heads in
four coin tosses?
o Compare your simulation result with the analytical one.

N = 4; k = 3; p = 0.5;
P3_an = nchoosek(N,k) * p^k * (1-p)^(N-k)

31
Simulating Random Experiments:
How to simulate a die toss experiment?
Fair Die
ceil(6*rand(1,10))
OR ans = 6 2 4 3 6 4 6 5 1 4

floor(6*rand(1,10))+1
ans = 5 4 2 3 6 3 4 6 1 2
32
EX. 6
Simulating Random Experiments – Example-6 :

 Using computer simulations, Fair Die


Find the probability of ceil(6*rand(1))
obtaining an even number.
 Does it agree with the clear all; clc; close all;
M = 1e5;
theoretical results. X = ceil(6*rand(1,M));
% M Trials of die tossing exp.
odd = rem(X,2);
N_odd = sum(odd);
N_even = M - N_odd;
P_even = N_even / M

P_even = 0.4980 34
EX. 7
Simulating Random Experiments – Example-7 :
Find the probability of “snake eye” when a
pair of fair dice is tossed.

1
𝑃 ∎, ∎ =
36
= 0.0278
36
Simulating Random Experiments – Example-7 :
Find the probability of “snake eye” when a
pair of fair dice is tossed.
clear all; clc; close all;
% Find the probability of "snake eye".
% Problem 2.2 of [Kay-2006]
M = 1e6;
X = ceil(6*rand(M,2));
% M Trials of die-pair tossing exp.
%
Y = sum(X,2);
P_SE = 0.0277
Z = find(Y==2);
P_SE = numel(Z) / M
37
EX. 7b
Simulating Random Experiments – Example-7b :
With Two Dice, What’s the Probability of
Rolling Doubles?

6
𝑃 𝐃𝐨𝐮𝐛𝐥𝐞 = =
36
1
= 0.1667
6

39
Simulating Random Experiments – Example-7b :
With Two Dice, What’s the Probability of
Rolling Doubles?
clear all; clc; close all;
% Find the probability of "snake eye".
M = 1e7;
X = ceil(6*rand(M,2));
% M Trials of die-pair tossing exp. P_SE = 0.1664
%
Diff = diff(X,1,2);
% Y = diff(X,n,dim) is the nth difference along the dimension dim.
Z = find(Diff==0);
P_SE = numel(Z) / M
40
EX. 8
Simulating Random Experiments – Example-8 :
A single card is drawn at random from a well-shuffled
deck of playing cards. Find the probability of drawing an
ace.

42
Simulating Random Experiments – Example-8 :
% Card Values: First row corresponds
% to Aces, and last row corresponds to
% Kings.
1 2 3 4
Y = zeros(13,4);
5 6 7 8
for mm = 1:13;
9 10 11 12
Y(mm,:) = 4*mm-3:4*mm;
13 14 15 16
end
17 18 19 20
Y
21 22 23 24
25 26 27 28
29 30 31 32
33 34 35 36
37 38 39 40
41 42 43 44
45 46 47 48
49 50 51 52 43
Simulating Random Experiments – Example-8 :
A single card is drawn at random from a well-shuffled
deck of playing cards. Find the probability of drawing an
ace.
clear all; clc; close all;
% Simulation of Drawing an Ace
N = 1e6; % Number of draws.
X = ceil(52*rand(1,N)); P_Ace = 0.0770
aces = (1 <= X & X <= 4);
naces = sum(aces);
P_Ace = naces / N
44
Simulating Random Experiments – Example-8 :

P_Ace = 4/52
= 1/13 = 0.0769

45
randperm() = Random Permutation of Integers :
 p = randperm(n) returns a
row vector containing a
random permutation of the
integers from 1 to n inclusive
(without repeating
elements.)
 p = randperm(n,k) returns a
row vector containing k
UNIQUE integers selected
randomly from 1 to n
inclusive.
46
randperm() = Random Permutation of Integers :
r1 = randperm(6)
r1 = 4 2 1 6 5 3

r2 = randperm(8,4)
r2 = 5 6 3 7

47
randi() = Uniformly Distributed Pseudorandom Integers :
 X = randi(imax) returns a
pseudorandom scalar
integer between 1 and
imax.
 X = randi(imax,n) returns
an n-by-n matrix of
r = randi(10,5) pseudorandom integers
r=
6 7 2 8 9 drawn from the discrete
7 4 7 5 10 uniform distribution on
2 6 4 5 10 the interval [1,imax].
5 2 2 4 7
8 7 3 10 4 48
randi() Vs randperm() :
r1 = randi(10,1,5)
r2 = randperm(10,5)
r1 = 9 3 10 1 3
r2 = 5 4 10 2 1
o randi(n,1,k): For SAMPLING WITH REPLACEMENT, we use
randi(n,1,k).
o Randperm(): performs k-permutations (SAMPLING
WITHOUT REPLACEMENT).
49
EX. 9
Simulating Random Experiments – Example-9 :
Two cards are drawn from a freshly shuffled 52-card deck. What is the
probability that the first card is a queen and the second card is a 7.
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
% Card Values: First row corresponds
17 18 19 20
% to Aces, and last row corresponds to
21 22 23 24
% Kings.
25 26 27 28
Y = zeros(13,4);
29 30 31 32
for mm = 1:13;
33 34 35 36
Y(mm,:) = 4*mm-3:4*mm;
37 38 39 40
end
41 42 43 44
Y
45 46 47 48
49 50 51 52 51
Simulating Random Experiments – Example-9 :
Two cards are drawn from a freshly shuffled 52-card deck. What is the
probability that the first card is a queen and the second card is a 7.
clear all; clc; close all;
% Probability of Drawing a "Queen" Followed By a "7".
N = 5e6; % Number of draws.
P(AB)=P(A).P(B)
% =(4/52)*(4/51)
success = 0;
for nn = 1 : N; =0.0060
X = randperm(52,2);
if( X(1) >= 45 && X(1) <= 48 ) && ( X(2) >= 25 && X(2) <= 28 )
success = success + 1;
end
end
P_Q7 = success / N

P_Q7 = 0.0061
52
References:
• [1] [Kay-2008]
• [2] [Dolecek-2013]
• [3] [Yates-2005]
• [4] [Peebles]

53

You might also like