PATTERN RECOGNITION LAB 2 TA : Nouf Al-Harbi::
Lab objective: Illustrate the uniform distribution of a random variable using Matlab 2
Theoretical Concept Part 1 3
Suppose a die is rolled. What is the probability that the die will land on 5 ? On 4, on 2 ….? Dice Experiment 4
When a die is rolled there are 6 possible outcomes represented by: X = { 1, 2, 3, 4, 5, 6 }. Each outcome is equally likely to occur If a die is rolled 1200 times T hen, each of outcome should occur 1200/6 = 200 times Frequency F(X) Outcome x Frequency distribution 5
Dice Experiment What’s the probability for occurring each outcome..? P(X = 6) = 200/1200 =1/6 P(X=3)=P(X=1)=200/1200=1/6 A probability distribution is a table or an equation that links each outcome of a statistical experiment with its probability of occurrence If each outcome has the same probability then probability density function is called “uniform distribution” Probability P(X=x) Outcome x 1/ probability distribution 6
What’s uniform distribution..? We obtain a uniform density function when the outcomes of an experiment (random process) are equally likely to occur. 7
Practical Applying Part 2 8
Applying dice experiment by Matlab 1. Generate N random values uniformly distributed in the closed range [1,6]. 2. Find the frequency distribution of each outcome (1-6) (i.e. how many times each outcome occur?) 3. Find the probability density function p(x) 4. Plot p 9
Generate N random values uniformly distributed in the closed range [1,6]. Step 1 10
rand function rand(1,N) Generates N random values uniformly distributed in the open range ]0,1[. Write the following in Matlab & see the result: r = rand(1,20) generates 1-D array of one row and 20 columns Random values range between 0 and 1 To change the period we can use fix function 11
fix function x = fix( 6 * r ) + 1; Writing the previous line converts r into random values in the closed period [1,6] For Dice Experiment, What are the values of vector x represent..? 12
Find the frequency distribution of each outcome (1-6) Step 2 13
Find the frequency distribution of outcome we’ll make a counter for each outcome Event no …1200 outcome …2 N
Find the probability density function p(x) Step 3 15
Find the probability density function p(x) 16 We can easily calculate t he probability the outcome frequency divided by the no. of events P=f/N
Plot the probability density function p(x) Step 4 17
plot p(x) 18 plot function has different forms, depending on input arguments. If you have two vectors y and x plot (x,y) produces a graph of y versus x If you have one vector x plot(x) produces a graph of columns of x versus their index To change the axis scale, that is x starts from xmin to xmax and y starts from ymin to ymax use the command: axis([xmin xmax ymin ymax])
plot p(x) 19 If we have more than one graph, we can use figure command to create a new figure window It’s useful to avoid draw the new graph over the previous one For more information about plot function and its forms type help plot on command window
1.N = 100; 2.r = rand(1,N); 3.x = fix( 6 * r ) + 1; 4.f = zeros(1,6); 5.for i = 1 : N 6.if x(i) == 1 f(1) = f(1) + 1; 7.elseif x(i) == 2 f(2) = f(2) + 1; 8.elseif x(i) == 3 f(3) = f(3) + 1; 9.elseif x(i) == 4 f(4) = f(4) + 1; 10.elseif x(i) == 5 f(5) = f(5) + 1; 11.else f(6) = f(6) + 1; 12.end 13.end 14.F 15.plot(f) 16.axis([ ]) 17.p = f /N 18.figure, plot(p) 19.axis([ ]) Full code Try larger values of N: (1000,10000) and notice the graph 20
Write a Matlab function to illustrate a uniform distribution of coin experiment. A function should take the number of events N as an argument Exercise 1 21