Probability Manual

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 61

Probability & Statistics Session 2018

Lab No. 01
Introduction to MATLAB
Objective:
 To study the basic terminologies used in data analysis.
 To implement data analysis techniques using MATLAB .

Introduction:

MATLAB:
MATLAB is widely used in all areas of applied mathematics, in education and research at
universities, and in the industry. MATLAB stands for MATrix LABoratory and the software
is built up around vectors and matrices. This makes the software particularly useful for linear
algebra but MATLAB is also a great tool for solving algebraic and differential equations and
for numerical integration. MATLAB has powerful graphic tools and can produce nice
pictures in both 2D and 3D. It is also a programming language, and is one of the easiest
programming languages for writing mathematical programs. The MATLAB environment (on
most computer systems) consists of menus, buttons and a writing area similar to an ordinary
word processor.  MATLAB is case sensitive. This means that MATLAB knows a difference
between letters written as lower and upper-case letters. For example, MATLAB will
understand sin (2) but will not understand Sin(2).

Command window:
The writing area that you will see when you start MATLAB, is called the command window.
In this window you give the commands to MATLAB. Debugging is usually done in
command window. Command window is called MATLAB interface.

Workspace:
The Workspace browser enables you to view and interactively manage the contents of the
workspace in MATLAB. For each variable or object in the workspace. You can edit the
contents of scalar (1-by-1) variables directly in the Workspace browser. Right-click the
variable and select Edit Value. To edit other variables, double-click the variable name in the
Workspace browser to open it in the Variables editor.

Variables Editor:
The MATLAB Variables editor provides a convenient interface for viewing, modifying, and
plotting dataset arrays.
You can reorder variables in a dataset array using the Variables editor. You can enter new
data values directly into the Variables editor.
Command History window:

University of Engineering & Technology, Lahore


Faisalabad Campus Page 1 of 61
Probability & Statistics Session 2018

The commands you type in the command window are stored by MATLAB and can be viewed
in the Command History window. To repeat a command, you have already used, you can
simply double-click on the command in the history window, or use the <up arrow> at the
command prompt to iterate through the commands you have used until you reach the
command you desire to repeat.

Input command:
Request user input displays the text in prompt and waits for the user to input a value and
press the Return key. It is used for data entering.

Syntax:
x = input(prompt)
Zeros: It creates an array of all zeros.
Syntax:
X = zeros(sz1,...,szN)
Plot:
Plot creates a 2-D line plot of the data in Y versus the corresponding values in X.

Syntax:
plot(X,Y)
Procedure:
 I opened MATLAB in my PC and opened a new script.
 I created a zero matrix of 10 by 3 equal to a variable “A”. then I used input command in
for loop so that every time I run the program it ask the user to input the values of the
matrix.
 To call any entry we write the matric name and specified position of any entry i.e,
variable(row,coloum).
 To index a row after comma a colon is used and to index column colon is used before
comma.

Task 1: Plot the given four functions for 0≤ x ≤ 64 and step size of 0.5.
1. y=x
2. y=x(log 2 x ¿ ¿
3. y=x(log 2 x ¿ ¿2 4500

4. y=x2 4000
y=x
y=log2x
plot all the functions on same 2
y=x.*((log2(x)). )
3500
graph. y=x.
2

MATLAB code: 3000

clc 2500
clear ALL
2000
close ALL
x=0:0.5:64; 1500
y=x; 1000

University of Engineering & Technology,


500Lahore

Faisalabad Campus 0 Page 2 of 61


-500
0 10 20 30 40 50 60 70
Probability & Statistics Session 2018

plot(x,y);
hold on
y=x.*(log2(x));
plot(x,y);
hold on
y=x.*((log2(x)).^2);
plot(x,y);
hold on
y=x.^2;
plot(x,y)

Figure 1.2 Graph of given


Task 2: Find the out of given functions for x = [1,2,3,4,5] and y = [6,7,8,9,10].
5 5
1. ∑ x i ∑ yi 5
i=1 i=1

2. ∑ xi yi 6
i=1

5
3. ∑ log10 x i 7
i=1

5
4. √∑
i=1
5 x i+ y i 8

University of Engineering & Technology, Lahore


Faisalabad Campus Page 3 of 61
Probability & Statistics Session 2018

5
5. ∑|2 x i+5 y i| 9
i=1

6. e ∑ x i
i=1 10

Conclusion:
I studied how to read matrices in MATLAB using for loops and enter the data
in matrices to give it a form of table. Moreover, I learnt how to plot data in
MATLAB an how to use summation and products of matrices.

University of Engineering & Technology, Lahore


Faisalabad Campus Page 4 of 61
Probability & Statistics Session 2018

University of Engineering & Technology, Lahore


Faisalabad Campus Page 5 of 61
Probability & Statistics Session 2018

Lab 2
Introduction to Bar graph and pie charts
Objective:
To get familiar with bar graph, and pie charts in MATLAB.

Introduction:

Bar graph:
A bar graph is a graph that represents categorical data with rectangular bars with heights and
lengths proportional to the value that they represent. The bar can be plotted vertically on
horizontally.

Syntax:
bar(y)
bar(x,y)
 bar(y) creates a bar graph with one bar for each element in y. If y is a matrix,
then bar groups the bars according to the rows in y.
 bar(x,y) draws the bars at the locations specified by x.

Figure2.1: Bar graph


Types of bar graph:
Bar graph is of two types:
• Single bar chart
• Grouped bar chart
• Stacked bar chart

University of Engineering & Technology, Lahore


Faisalabad Campus Page 6 of 61
Probability & Statistics Session 2018

Barh command:
A barh graph displays the values in a vector or matrix as horizontal bars.

Syntax:
barh(y) , barh(x,y)
 barh(y) draws one horizontal bar for each element in y. If y is a matrix, barh groups the
bars produced by the elements in each row. The y-axis scale ranges from 1 up
tolength(y) when y is a vector, and 1 to size(y,1), which is the number of rows, when y is
a matrix. The values in y can be numeric or duration values.
 barh(x,y) draws a bar for each element in y at locations specified in x, where x is a
vector defining locations along the y-axis. The location values can be nonmonotonic, but
cannot contain duplicate values. If y is a matrix, barh groups the elements of each row
in y at corresponding locations in x. The values in x can be numeric, datetime, duration,
or categorical values.

Pie chart:
A pie chart is a circular statistical graphic which is circular statistical graphic which is
divided into slices to illustrate numerical proportion. In a ppie chart the arc length of each
slice is proportional to the quality is represents.

Syntax:
pie(X) , pie(X,labels)
pie(X,explode,labels)
 pie(X) draws a pie chart using the data in X. Each slice of the pie chart represents an
element in X.
 pie(X,labels) specifies text labels for the slices. The number of labels must equal the
number of slices. X must be numeric.
 pie(X,explode,labels) specifies text labels for the slices. The number of labels must equal
the number of slices.

Figure2.2: Pie chart

University of Engineering & Technology, Lahore


Faisalabad Campus Page 7 of 61
Probability & Statistics Session 2018

Text command:
Add text descriptions to data points.

Syntax:
text (x, y, txt)
 text(x,y,txt) adds a text description to one or more data points in the current axes using
the text specified by txt. To add text to one point, specify x and y as scalars in data units.
To add text to multiple points, specify x and y as vectors with equal length.

Procedure:
 I opened MATLAB in my PC and opened a new script.
 I declared two vectors with variable name “A” and “X”. Then I used bar command to plot
the values in a bar graph.
 To display the height of the bars I used text command and passed the label in string form.
Then make it center by horizon and vertical center.
 To change the properties of the bar graph can use the dot operator with the same property
syntax appeared in the command window.
 To plot the graph in horizontal direction I used barh command.
 I plotted the data in the form of group by declaring vector having more than one column.
 The I plotted the data in the form of stack by writing stacked in bar command.
 We can’t access the data of the bars in stacked form so we can access is by writing the
variable with bar number i.e. y(1).
 I used pie command to plot the graph in the form of pie chart.
 I used label to display the x axis vales and explode to differentiate it from other data .

(Lab Tasks with MATLAB code)


Task 1:
Bar graph:

Code:

syms x
a=[10 20 30 23 33 55]
b=[2001 2002 2003 2004 2006 2007]
x=bar(b,a)
title('Bar graph')
Results:

University of Engineering & Technology, Lahore


Faisalabad Campus Page 8 of 61
Probability & Statistics Session 2018

Fig.3.1: Bar graph

Task 2:
Histogram:

Code:

s= [ 37.5 34.2 47 45 46 45.5 31.2 55 54 48 32.5 33.7 47 ];


y=histogram(s);
xlabel('waiting time');
ylabel('people in firm');
Results:

Fig. 3.2: Histogram

University of Engineering & Technology, Lahore


Faisalabad Campus Page 9 of 61
Probability & Statistics Session 2018

Task 3:
Horizontal 3D bar graph:
Code:
c=[10 20 30 23 33 55]
d=[2001 2002 2003 2004 2006 2007]
z=bar3h(d,c,0.8,'stacke')
Results:

Fig. 3.2: Horizontal 3D bar graph

Task 4:
Horizontal bar graph:
Code:
e=[10 20 30 23 33 55]
f=[2001 2002 2003 2004 2006 2007]
w=barh(f,e,0.8,'stacke')
Results:

University of Engineering & Technology, Lahore


Faisalabad Campus Page 10 of 61
Probability & Statistics Session 2018

Fig. 3.3: Horizontal bar graph

Task 5:
Pie chart:

Code:

v= [ 37.5 34.2 47 45 46 45.6 55 4 ];


t=pie(v)
explode=[0 1 0 1 0 1 0 1]
pie (v,explode)
Results:

University of Engineering & Technology, Lahore


Faisalabad Campus Page 11 of 61
Probability & Statistics Session 2018

Fig. 3.4: Pie chart

Conclusion:
There are different commands built in MATLAB which we use for different bar graphs.

University of Engineering & Technology, Lahore


Faisalabad Campus Page 12 of 61
Probability & Statistics Session 2018

Lab 3
Probability of independent events
Objective:
To find probability of independent events using MATLAB.

Introduction:

Probability:
It is the branch of mathematics that deal with calculating the likelihood of given event
occurrence which is expressed in numbers between zero and one.

Probability model:
Probability model is the mathematical description of an uncertain situation.

Elements of Probability model:


1. Sample space:
The sample space Ω, which assigns to a set of possible outcomes of an experiment.

2. Probability law:
The probability law, assigns a non-negative number P(A) (called the probability of A) that
encodes or belief about the collective likelihood of elements of A.

Figure3.1: Elements of sample space


Experiment and event:
Experiment is an underline process and event is the subset of sample space.

Types of events:
There are two types of event.

1. Independent events:
Events that are not affected by previous events/outcomes are called independent events.

University of Engineering & Technology, Lahore


Faisalabad Campus Page 13 of 61
Probability & Statistics Session 2018

2. Dependent events:
Events that are affected by previous events/outcomes are called dependent events.

Discrete Probability law:


If the sample space consists of finite number of possible outcomes than probability of any
event is the sum of probabilities of its elements.

P{( s1, s2, s3,……, sn )}= P( s1 ¿ + P( s2) + P( s3)+ ……+ P( sn)

Discrete uniform Probability law:


If the sample space consists of N number of outcomes which are equally likely then the
probability of any event (let say A) is given by:
No of elements∈an event
P(A) =
Number of elements∈sample space
Properties of Probability law:
1. Nonnegativity:
P(A) ≥ 0 for any event A

2. Additivity:
If A and B are two disjoint events, then the probability of their union satisfies:
P (A U B) = P(A) + P(B)

3. Normalization
The probability of entire sample space Ω is equal to 1, that is
P (Ω) = 1

Properties of Probability:
 P (φ) = 0
 P( AC ) = 1 - P(A)
 P (A U B) = P(A) + P(B) (if A ∩ B = φ)
 P (A U B) = P(A) + P(B) – P (A ∩ B ) (if A ∩ B ≠ φ)

Procedure:
 I opened MATLAB in my PC and opened a new script.
 I declared a matrix of sample space then used sort command for simplicity.
 Then I check repetition of numbers in the matrix by using loops.
 If the number repeated the value of count increased by one and its probability = count /
number of elements in sample space defined as length(s) in the code.
 Then I plotted the probabilities of all numbers in bar graph.

University of Engineering & Technology, Lahore


Faisalabad Campus Page 14 of 61
Probability & Statistics Session 2018

LAB TASKS
Task 1:
Write a code to find probability of given sample space (containing repeating
events) and make its bar graph.
S={11,12,13,14,21,22,23,24,31,32,33,34,41,42,43,44}

Fig.No 3.2(MATLAB code)

Fig.No 3.3(MATLAB code)

University of Engineering & Technology, Lahore


Faisalabad Campus Page 15 of 61
Probability & Statistics Session 2018

Fig.No 3.4(MATLAB code)

Fig.No3.5(Bar Graph)

Task 2:
Write a code to find probability of given sample space (containing repeating
events) and make its bar graph.
S={1,2,3,4,5,6 }

University of Engineering & Technology, Lahore


Faisalabad Campus Page 16 of 61
Probability & Statistics Session 2018

Fig.No 3.6(MATLAB code)

Fig.No3.7(MATLAB code

University of Engineering & Technology, Lahore


Faisalabad Campus Page 17 of 61
Probability & Statistics Session 2018

Fig.No 3.8(Bar Graph)

Conclusion:

In this lab, we make a program for probability of independent event using MATLAB and plot
on the bar graph using default function.

Lab 4
Conditional Probability
Objective:
To implement condition probability using MATLAB.

Introduction:
Conditional Probability:
The conditional probability of an event B is the probability that the event will occur given the
knowledge that an event A has already occurred. This probability is written P(B|A), notation
for the probability of B given A. In the case where events A and B are independent (where

University of Engineering & Technology, Lahore


Faisalabad Campus Page 18 of 61
Probability & Statistics Session 2018

event A has no effect on the probability of event B), the conditional probability of


event B given event A is simply the probability of event B, that is P(B).
If events A and B are not independent, then the probability of the intersection of A and B (the
probability that both events occur) is defined by:
P (A and B) = P (B) * P (A | B).
From this definition, the conditional probability P(B|A) is easily obtained by dividing
by P(A):

P ( A ∩B)
P (A / B) =
P( B)

Where we assume that P (B) > 0; the conditional probability is undefined if the conditioning
event has zero probability.

Save command:
Save command saves all variables from the current workspace in a MATLAB formatted
binary file (MAT-file) called filename. If filename exists, save overwrites the file.

Syntax:
save(filename)
Load command:
Load variables from file into workspace

Syntax:
load(filename)
Sum command:
Sum command returns the sum of the elements of A along the first array dimension whose
size does not equal 1.

Syntax:
S = sum(A)
 sum(A,1) operates on successive elements in the columns of A and returns a row vector
of the sums of each column.
 sum(A,2) operates on successive elements in the rows of A and returns a column vector
of the sums of each row

University of Engineering & Technology, Lahore


Faisalabad Campus Page 19 of 61
Probability & Statistics Session 2018

Figure4.1: Column sum and row sum


LAB TASKS
Lab Task 1:
The HBO network cable network took summery of 500 subscribers to determine the favorite
show:

Male Female
GOT 80 20
West World 100 25
MPTH 50 225
Find the conditional probabilities P(GOT/M), P(GOT/F), P(WestWorld/M), P(WestWorld/F),
P(MPTH/M) and P(MPTH/F)
MATLAB code:
clc
clear all
close all
A=[20,80;25,100;200,75];
g=length(A);
sum1=0;
sum2=0;
sumf=0;
summ=0;
for i=1:g
sum1=sum1+A(i,1);
sum2=sum2+A(i,2);
end
sumr=sum1+sum2;
prob=A./sumr:
for i=1:g
sumf=sumf+prob(i,1);
summ=summ+prob(i,2);
end
sumr2=sumf+summ; %Total
sumf;
summ;
for i=1:g
p(i,1)=prob(i,1)./sumf:
p(i,2)=prob(i,2)/summ;
end

Results:

University of Engineering & Technology, Lahore


Faisalabad Campus Page 20 of 61
Probability & Statistics Session 2018

Lab task 2:
Find the Conditional probabilities of given weather condition.

MATLAB Code:
clc
clear all
values={'sunny','n';'sunny','n';'overcast','y';'rain','y';'rain','y';'overc
ast','n';'sunny','y'}
[a,~,c]=unique(values,'stable')
d=reshape(c,[],2)
[a,~,c]=unique(d,'stable')
d=reshape(c,[],2)
s=size(d);
s1=size(a);
N=0;
Y=0;
for i=1:s1(1)
for j=1:s(1)
if (d(j,1)==a(i))
if(d(j,2)==a(4))
N=N+1;
end
end
if (d(j,1)==a(i))
if(d(j,2)==a(5))
Y=Y+1;
end
end

end
A(i)=N
B(i)=Y
N=0;
Y=0;
end
values=[A;B]'
s=size(values);
TS=0;
for i=1:s(1)
for j=1:s(2)
TS=TS+values(i,j);
end
end
ProA=values/TS;
cs=0;
rs=0;

University of Engineering & Technology, Lahore


Faisalabad Campus Page 21 of 61
Probability & Statistics Session 2018

tr=0;
tc=0;
for i=1:s(1)
for j=1:s(2)
tr=tr+ProA(i,j);
end
rs(i)=tr;
tr=0;
end

for i=1:s(2)
for j=1:s(1)
tc=tc+ProA(j,i);
end
cs(i)=tc;
tc=0;
end
PAGF=ProA(:,1)/cs(1)
PAGM=ProA(:,2)/cs(2)

Results:

Lab 5
Bayes Theorem
Objective:
To implement Bayes Theorem using MATLAB.

Introduction:
Bayes Theorem:
Bayes' theorem, named after 18th-century British mathematician Thomas Bayes, is a
mathematical formula for determining conditional probability. The theorem provides a way to
revise existing predictions or theories (update probabilities) given new or additional evidence.

University of Engineering & Technology, Lahore


Faisalabad Campus Page 22 of 61
Probability & Statistics Session 2018

In finance, Bayes' theorem can be used to rate the risk of lending money to potential
borrowers.
In short Bayes theorem is used to find reverse conditional probability. It is used in data
mining and machine learning.
P ( A )∗P(B∨ A)
P(A|B) =  P( B)

Prior probability is required to find future probability.

Prior probability:
It is the probability of an event before new data. Is collected it is the best rational assessment
of probability of an outcome on the current knowledge before an experiment is performed.

Posterior probability:
Posterior probability is the revised probability of an event occurring after taking new
information it can be calculated by updating Prior probability by using Bayes' theorem.

Unique command:
If A is a table or timetable, then unique returns the unique rows in A in sorted order i.e the
command provides Unique values in array.

Syntax:
C = unique(A)
The values (or rows) in C return in the same order as in A. Let B= ([5 5 3 4] C=unique(B,
'stable') returns C = [5 3 4].

Reshape command:
The reshape command given blow reshapes A using the size vector, sz, to define size(B).

Syntax:
B = reshape(A,sz)
For example, reshape(A,[2,3]) reshapes A into a 2-by-3 matrix. sz must contain at least 2
elements, and prod(sz) must be the same as numel(A).

LAB TASKS
Lab Task 1:
The HBO network cable network took summery of 500 subscribers to determine the favorite
show:
Male Female
GOT 80 20
West World 100 25
MPTH 50 225
University of Engineering & Technology, Lahore
Faisalabad Campus Page 23 of 61
Probability & Statistics Session 2018

 Find the conditional probabilities P(GOT/M), P(GOT/F), P(WestWorld/M),


P(WestWorld/F), P(MPTH/M) and P(MPTH/F)
 Find the reverse conditional probabilities by using Bayes’ theorem
MATLAB code:
clc
clear all
close all
A=[20,80;25,100;200,75];
g=length(A);
sum1=0;
sum2=0;
sumf=0;
summ=0;
for i=1:g
sum1=sum1+A(i,1);
sum2=sum2+A(i,2);
end
sumr=sum1+sum2;
prob=A./sumr:
for i=1:g
sumf=sumf+prob(i,1);
summ=summ+prob(i,2);
end
sumr2=sumf+summ; %Total
sumf;
summ;
for i=1:g
p(i,1)=prob(i,1)./sumf:
p(i,2)=prob(i,2)/summ;
end

Results:

Lab task 2:
Find the Conditional and reverse conditional probabilities of given weather condition.

University of Engineering & Technology, Lahore


Faisalabad Campus Page 24 of 61
Probability & Statistics Session 2018

Figure5.1: Given weather condition data


MATLAB Code:
clc
clear all
values={'sunny','n';'sunny','n';'overcast','y';'rain','y';'rain','y';'overc
ast','n';'sunny','y'}
[a,~,c]=unique(values,'stable')
d=reshape(c,[],2)
[a,~,c]=unique(d,'stable')
d=reshape(c,[],2)
s=size(d);
s1=size(a);
N=0;
Y=0;
for i=1:s1(1)
for j=1:s(1)
if (d(j,1)==a(i))
if(d(j,2)==a(4))
N=N+1;
end
end
if (d(j,1)==a(i))
if(d(j,2)==a(5))
Y=Y+1;
end
end

end
A(i)=N
B(i)=Y
N=0;
Y=0;
end
values=[A;B]'
s=size(values);
TS=0;
for i=1:s(1)
for j=1:s(2)
TS=TS+values(i,j);
end
end

University of Engineering & Technology, Lahore


Faisalabad Campus Page 25 of 61
Probability & Statistics Session 2018

ProA=values/TS;
cs=0;
rs=0;
tr=0;
tc=0;
for i=1:s(1)
for j=1:s(2)
tr=tr+ProA(i,j);
end
rs(i)=tr;
tr=0;
end

for i=1:s(2)
for j=1:s(1)
tc=tc+ProA(j,i);
end
cs(i)=tc;
tc=0;
end
PAGF=ProA(:,1)/cs(1)
PAGM=ProA(:,2)/cs(2)

Results:

Lab 06

University of Engineering & Technology, Lahore


Faisalabad Campus Page 26 of 61
Probability & Statistics Session 2018

Introduction to Discrete Random Variables, Probability Mass


Function, and Cumulative Distribution Function

Objective:

To familiarized with random variables, its types, Probability Mass Function (PMF),
Cumulative Distribution Function (CDF) and its implementation in MATLAB.

Random variables:

Random variable is a mapping from an outcome s of a random experiment to a real number.


A random variable assigns real numbers to the elements of a sample space.

Types of Random variables

1. Discrete Random Variables: have a countable (finite or infinite) image


 S X ={ 0,1 }
 S X ={1,2,3,4 … … … }
2. Continuous Random Variables: have an uncountable image
 S X =¿
 S X =R

Probability Mass Function (PMF):

The Probability Mass Function (PMF) provides the probability of a particular point in the
sample space of a discrete random variable (R.V). For a countable S X {a0 , a2 , … ,a n },the PMF
is the set of probabilities as stated below:

Properties of Probability Mass Function (PMF):

The following are the properties of PMF.

• P1: 0 ≤ P X ( x ) ≤ 1

• P2: ∑ P X ( x )=1
x∈ SX

Cumulative Distribution Function (CDF):

University of Engineering & Technology, Lahore


Faisalabad Campus Page 27 of 61
Probability & Statistics Session 2018

In probability theory and statistics, the cumulative distribution function (CDF) of a real-
valued random variable X, or just distribution function of X,evaluated at x, is the probability
that X will take a value less than or equal to x. CDF is defined as:

Properties of CDF

The following are the properties of PMF.

1. P1: 0 ≤ F X ( x ) ≤1

2. P2: a ≤ b → F X ( a ) ≤ F X (b)

lim F X ( x )=0
3. P3: x→−∞

4. P4: lim
x→ ∞
F X ( x )=1

Expected Value of a Discrete Random Variable:

The expected value, expectation or mean of a discrete R.V is the “average” value of the
random variable. Expectation is computed by using the probability of each point in the R.V’s
image as the weight of that point. Mathematically, the expected value of a discrete random
variable is:

Variance and Standard Deviation of a Discrete Random Variable:

Variance of a R.V is a measure of “the amount of variation of a R.V around its mean”.
Mathematically, the variance of a discrete RV is defined as:

Standard Deviation is the square root of the variance.

University of Engineering & Technology, Lahore


Faisalabad Campus Page 28 of 61
Probability & Statistics Session 2018

Lab Work:

MATLAB code:

% PMF ,CDF
clc
clear all
close all
u={'HH','HT','TH','TT'};
%E=[2,1,1,0];
s=length(u);
% taking probabilities %
for i=1:s
if (u{i}=='HH')
X(i)=2;
end
if (u{i}=='HT')
X(i)=1;
end
if(u{i}=='TH')
X(i)=1;
end
if(u{i}=='HT')
X(i)=1;
end
if(u{i}=='TT')
X(i)=0;
end
end
a=unique(X,'stable')
s1=length(a);
count =0;
for i=1:s1
for j=1:s
if (X(j)==a(i))
count=count + 1;
end
end
c(i)=count;
count=0;
end
pmf = c/s;
stem(a,pmf);
Results:

University of Engineering & Technology, Lahore


Faisalabad Campus Page 29 of 61
Probability & Statistics Session 2018

Lab Tasks

Lab Task 1:

For the given sample space find the Probability Mass Function (PMF) and Cumulative
Distribution Function (CDF), Also plot the PMF∧CDF for the above sample space .

S=[0 12 3 4 5 ];

S=[21 1 0];

Write the MATLAB code for the PMF and CDF above provided sample space also plot them
and compare the results. (built in functions are not allowed they can only be used to verify

MATLAB Code:
s=[2 1 1 0];
for general code
%Probability Mass
Function (PMF)
s=input('s= ');
s=sort(s); x=unique(s);
k=0; plus=1; incre=1;
while
(plus<=length(s))
for
i=plus+1:length(s)
if(s(1,plus)==s(1,i)
)
plus=plus+1;
incre=incre+1;
end end
k=k+1;

pmf(k)=incre/len
gth(s);
plus=plus+1;
incre=1; end
pmf figure
bar(x,pmf,0.4) %bar
graph grid on
title('Probability Mass Function (PMF)')

%Cumulative Distribution
Function (CDF)
N=length(pmf);
cdf=zeros(1,N); for i=1:N
cdf(i)=sum(pmf(1:i)); end cdf
figure
University of Engineering & Technology, Lahore
Faisalabad Campus Page 30 of 61
Probability & Statistics Session 2018

stairs(x,cdf,'LineWidth',3) %stairs like


grid on;
title('Cumulative Distribution Function (CDF)')
Results:
For s=[2 1 1 0]

Fig.No.6.1: PMF and CDF of s=[2 1 1 0]

University of Engineering & Technology, Lahore


Faisalabad Campus Page 31 of 61
Probability & Statistics Session 2018

For s=[0 1 2 3 4 5]

Fig.No.6.4: PMF and CDF of s=[01 2 3 4 5]

Fig.No.6.5: PMF bar graph for s=[0 1 2 3 4 5]

Fig.No.6.6: CDF stairs graph for s=[0 1 2 3 4 5]

Lab Task 2:

For the following graphs given below, write a MATLAB code to find the mean, variance and
Standard deviation.

University of Engineering & Technology, Lahore


Faisalabad Campus Page 32 of 61
Probability & Statistics Session 2018

MATLAB Code for graph:

%Mean, Variance and Standard deivation for graph 1 (data is give below)
x1=[1 6 7 9 13];
prob1=[0.5 0.4 0.033
0.033 0.033]; mean1=0 ;
for i=1:length(x1)
w=x1(i).*prob1(i);
mean1=mean1+w; end
mean1 var1=0; for
i=1:length(x1)
n=((x1(i)-
mean1)^2).*prob1(i);
var1=var1+n; end var1
stand1=sqrt(var1)

Results:

Fig.No.6.7: Mean, Variance and Standard deviation on MATLAB


MATLAB Code for graph:

University of Engineering & Technology, Lahore


Faisalabad Campus Page 33 of 61
Probability & Statistics Session 2018

%Mean, Variance and Standard deivation for graph 2 (data is give below)
x2=[1 6 7 9 13];
prob2=[0.5 0.4 0.033
0.033 0.033]; mean2=0 ;
for i=1:length(x2)
z=x2(i).*prob2(i);
mean2=mean2+z; end
mean2 var2=0; for
i=1:length(x2)
m=((x2(i)-
mean2)^2).*prob2(i);
var2=var2+m; end var2
stand2=sqrt(var2)
Results:

Fig.No.6.8: Mean, Variance and Standard deviation on MATLAB

Conclusion:
Hence by performing this lab we are now familiar with PMF and CDF (Cumulative
Distribution Function) used for at specific point and at some interval respectively.

University of Engineering & Technology, Lahore


Faisalabad Campus Page 34 of 61
Probability & Statistics Session 2018

Lab 7
Binomial and Geometric distribution
Objective:
To familiarized with binomial and geometric distribution and its implementation in
MATLAB.

Introduction:
Binomial Distribution:
A binomial distribution can be thought of as simply the probability of a SUCCESS or
FAILURE outcome in an experiment or survey that is repeated multiple times.
A binomial experiment is a statistical experiment that has the following properties:

 The experiment consists of n repeated trials.

University of Engineering & Technology, Lahore


Faisalabad Campus Page 35 of 61
Probability & Statistics Session 2018

 Each trial can result in just two possible outcomes. We call one of these outcomes a
success and the other, a failure.
 The probability of success, denoted by P, is the same on every trial.
 The trials are independent; that is, the outcome on one trial does not affect the
outcome on other trials.
 A random variable 𝑿 is called a binomial random variable if it represents the total
number of successes in ‘n’ independent Bernoulli trials.

To find the probability density function of 𝑿 we have to find the probability of 𝒙 successes in
𝒏 independent trails. If we have x successes in 𝒏 trails, then the probability of each n-tuple
with 𝑥 successes and 𝒏 − 𝒙 failures is

n x n−x
P(X = x) = ( x ) p ( 1− p)
Binopdf command:
Binomial probability density function computes the binomial pdf at each of the values
in X using the corresponding number of trials in N and probability of success for each trial
in P. Y, N, and P can be vectors, matrices, or multidimensional arrays that all have the same
size.

Syntax:
Y = binopdf(X,N,P)

Geometric Distribution:
The geometric distribution is the number of trials needed to get first success in repeated
Bernoulli trials.


𝒙𝒕𝒉𝑥−


P(X = x) = p*(1− p)n− x


Where x= 1, 2, 3….., ∞

Geopdf command:
geopdf returns the probability density function (pdf) of the geometric distribution at each
value in x using the corresponding probabilities in p. x and p can be vectors, matrices, or
multidimensional arrays that all have the same size.

Syntax:
y = geopdf(x,p)

University of Engineering & Technology, Lahore


Faisalabad Campus Page 36 of 61
Probability & Statistics Session 2018

Lab work:
Code:
clc
clear all
close all
n=20
p=0.82
pd= makedist('binomial', n,p)
x=1:18
y=pdf(pd,x)
stem(x,y)
Results:

Geometric:
Code:
% Geometric distribution:
% is used when number of trials are needed to get the first success in
% repeated bernollis trials.

% Examples:
% the probability that a machine produce a defective item is 0.02. each
% item is checked as it is produced. what is the probability that exact 100
% items are checked to find 1 that is defective.
clc
clear all
close all
%n=20
p=0.02
x=1:100
y=geopdf(x,p)
%y=pdf(pd,x)
stem(x,y)

Results:

University of Engineering & Technology, Lahore


Faisalabad Campus Page 37 of 61
Probability & Statistics Session 2018

LAB TASKS
Lab Task 1:
Suppose that 80 percent of trees in a forest of 300 trees are infected with a virus, and we take
5 trees at random to test for the virus. What is the probability that exactly 2 of the 5trees have
the virus?
MATLAB Code: clear all clc n=5; x=2; p=0.8; b=0; for i=length(x)
com=(factorial(n))./((factorial(x)).*(factorial(n-x)));
w=com.*(p.^x).*((1-p).^(n-x)); b=b+w bin=sum(b) end
stem(x,b)

Results:

Fig. No. 7.3: MATLAB results


Lab Task 2:
Aja's favorite cereal is running a promotion that says 1-in-4 boxes of the cereal contain a
prize. Suppose that Aja is going to buy 5 boxes of this cereal, and let X represent the number
of prizes she wins in these boxes. Assume that these boxes represent a random sample and
assume that prizes are independent between boxes. What is the probability that she wins at
most 1 prize in the 5 boxes?

University of Engineering & Technology, Lahore


Faisalabad Campus Page 38 of 61
Probability & Statistics Session 2018

clear all clc n=5; x=0:1; p=0.25; b=0; for i=length(x)


com=(factorial(n))./((factorial(x)).*(factorial(n-x)));
w=com.*(p.^x).*((1-p).^(n-x)); b=b+w bin=sum(b) end
stem(x,b)
Results:

Fig. No. 7.4: MATLAB results

Lab Task 3:
The probability of 90 years old Pakistani male survives for at least another year is 0.82. if
twenty, 90 years old Pakistani males are randomly selected what is the probability at most 18
survives for at least one year.
MATLAB Code:
clear all clc n=20; x=1:18; p=0.82; b=0; for i=length(x)
com=(factorial(n))./((factorial(x)).*(factorial(n-x)));
w=com.*(p.^x).*((1-p).^(n-x)); b=b+w bin=sum(b) end
stem(x,b)
Results:

Fig. No. 7.5: MATLAB results


Lab Task 4:
Lilyana runs a cake decorating business, for which 10%, percent of her orders come over the
telephone. Let C be the number of cake orders Lilyana receives in a month until she first gets
an order over the telephone. Assume the method of placing each cake order is independent.
Find the probability that it takes fewer than 5 orders for Lilyana to get her first telephone
order of the month.
MATLAB Code:
clear all clc x=1:4;
p=0.1; b=0; w=0; for
i=length(x) w=p.*((1-
p).^(x-1)); b=b+w;
geo=sum(b) end
stem(x,b)
University of Engineering & Technology, Lahore
Faisalabad Campus Page 39 of 61
Probability & Statistics Session 2018

Results:

Fig. No. 7.6: MATLAB results

Lab Task 5:
Suppose you toss a fair coin repeatedly, and a "success" occurs when the coin lands with
heads facing up. What is the probability of observing exactly three tails ("failures") before
tossing a heads?
MATLAB Code:
clear all clc x=3;
p=0.5; b=0; w=0; for
i=length(x)
w=p.*((1-p).^(x));
b=b+w;
geo=sum(b) end
stem(x,b)

Result:

Fig. No. 7.7: MATLAB results

Lab Task 6:
The probability that a machine produces a defective item is 0.01. Each item is checked as it is
produced. Assuming that these are independent trials, what is the probability that exactly 100
items must be checked to find one that is defective?
MATLAB Code:
clear all clc x=100;
p=0.01; b=0; w=0; for
i=length(x) w=p.*((1-
p).^(x-1)); b=b+w;
geo=sum(b) end
stem(x,b)

University of Engineering & Technology, Lahore


Faisalabad Campus Page 40 of 61
Probability & Statistics Session 2018

Results:

Fig. No. 7.8: MATLAB results

Conclusion:
We have learned how to find the binomial, geometric, and Poisson distribution and its
implementation with an without using built-in commands MATLAB.

Lab 8
Poisson and Negative Binomial Distribution
Objective:
To familiarized with Poisson and negative binomial distribution and its implementation in
MATLAB.

Introduction:

Poisson Distribution:
Experiments yielding numerical values of a random variable X, the number of outcomes
occurring during a given time interval or in a specified region, are called Poisson
experiments.
The given time interval may be of any length, such as a minute, a day, a week, a month, or
even a year.
A random variable X is said to have a Poisson distribution if its probability density function
is given by:
−λ x

p(x)= e x !λ
University of Engineering & Technology, Lahore
Faisalabad Campus Page 41 of 61
Probability & Statistics Session 2018

Where 0<𝛌<∞ is a parameter.

Poisspdf command
poisspdf computes the Poisson pdf at each of the values in X using mean parameters
in lambda. X and lambda can be vectors, matrices, or multidimensional arrays that all have
the same size.

Syntax:
Y = poisspdf(X,lambda)

Negative Binomial Distribution:


The negative binomial experiment is almost the same as a binomial experiment with one
difference: a binomial experiment has a fixed number of trials.
The negative binomial is similar to the binomial with two differences

 The number of trials, n is not fixed.


 A random variable Y= the number of trials needed to make r successes.
 The “negative” part of negative binomial actually stems from the fact that one facet of
the binomial distribution is reversed: in a binomial experiment, you count the number of
Successes in a fixed number of trials; 
To find the number of trials ‘n’ needed to get fixed number of successes ‘x’

n−1 x
(1− p)n−x
P(X = x) = ( x−1 ) p
nbinpdf command
It returns the negative binomial pdf at each of the values in X using the corresponding
number of successes, R and probability of success in a single trial, P. X, R, and P can be
vectors, matrices, or multidimensional arrays that all have the same size, which is also the
size of Y. A scalar input for X, R, or P is expanded to a constant array with the same
dimensions as the other inputs.

Syntax:
Y = nbinpdf(X,R,P)

LAB TASKS
Lab Task 1:
A computer hard disk manufacturer has observed that flaws occur randomly in the
manufacturing process at the average rate of two flaws in a 4 GB hard disk and has found this
rate to be acceptable. What is the probability that a disk will be manufactured with no
defects?

University of Engineering & Technology, Lahore


Faisalabad Campus Page 42 of 61
Probability & Statistics Session 2018

MATLAB code:

Conclusion:
In this lab, we performed some Poisson and Negative Binomial Distribution on some basic
problems and find out there values.

Lab 9
Hypergeometric Distribution
Objective:
To familiarized with hypergeometric distribution and its implementation in MATLAB.

Introduction:
Hypergeometric Distribution:
The hyper geometric distribution is a discrete probability distribution that describes the
probability of x successes in n trials, without replacement, from a finite population size of N
that contains exactly K objects with that feature.
There are precisely two possible outcomes, which we conveniently call ‘failure’ (F) and
S‘success’ (S)
The hypergeometric distribution is a probability distribution that’s very similar to the
binomial distribution. In fact, the binomial distribution is a very good approximation of the
hypergeometric distribution as long as you are sampling 5% or less of the population.

University of Engineering & Technology, Lahore


Faisalabad Campus Page 43 of 61
Probability & Statistics Session 2018

Population and Samples:


The entire group of individuals to be studied is called population and sample is the subset of
that population

Figure9.1: Population and sample

Mathematically:

( kx )( N−k
n−x )
P(X = x) =
( Nn )
Where, X= random Variable
K= Total # of possible success
x= number of successes
N= Population size
n= sample size/number of trials

Mean of Hypergeometric Distribution


nK
E(X)= N

Variance of Hypergeometric Distribution


N −n nK
Var(X)= N−1 N

Hygepdf command:
It computes the hypergeometric pdf at each of the values in X using the corresponding size of
the population, N, number of items with the desired characteristic in the population, K, and
number of samples drawn.

Syntax:
Y = hygepdf(X,N,K,n)

University of Engineering & Technology, Lahore


Faisalabad Campus Page 44 of 61
Probability & Statistics Session 2018

LAB TASKS
Lab Task 1:
Suppose we select five cards from an ordinary deck of plain cards. What is the probability of
obtaining two or fewer hearts?

Fig. No. 9.2: Inputs Fig. No. 9.3: Results

Lab Task 2
A deck of cards contains 20 cards 6 red card and 14 black cards. 5 cards are drawn randomly
without replacement what is the probability that exactly 4 red cards are drawn. Calculate
mean and variance.

University of Engineering & Technology, Lahore


Faisalabad Campus Page 45 of 61
Probability & Statistics Session 2018

Fig. No. 9.4: Inputs Fig. No. 9.5: Results

Lab Task 3:
500 bolts manufactured by ABC company. 9 of which are defective. 3 bolts are drawn
without replacement what is the probability that exactly 1 defective bolt is drawn.

Fig. No. 9.6: Inputs Fig. No. 9.7: Results

Lab Task 4:
A bag contains 5 green bools and 9 blue balls 4 balls are drawn at random randomly without
replacement calculate the probability of having 3 green balls

University of Engineering & Technology, Lahore


Faisalabad Campus Page 46 of 61
Probability & Statistics Session 2018

Fig. No. 9.8: Inputs Fig. No. 9.9: Results

Conclusion:
In this lab session learnt how to create MATLAB code for Hyper-geometric
distribution without using built in functions.

Lab 10
Uniform Distribution

University of Engineering & Technology, Lahore


Faisalabad Campus Page 47 of 61
Probability & Statistics Session 2018

Objective:
To familiarized with Uniform and normal distribution and its implementation in MATLAB.

Introduction:
Uniform Distribution:
This distribution is defined by two parameters, a and b:

 a is the minimum.
 b is the maximum.

A random variable X is said to be uniform on the interval [a, b] if its probability density
function is of the form
1
f(x) = b−a

which is calculated from the formula of area of rectangle where a and b are constants.

Figure10.1: Graph of uniform distribution

Mean of uniform Distribution:


b+a
E(X)= 2

Variance of uniform Distribution:

Var(X)=¿ ¿
Normal/Gaussian Distribution:
A normal distribution, sometimes called the bell curve, is a distribution that occurs naturally
in many situations. 
A continuous random variable X is said to be Normal or Gaussian if it has a PDF of the form
2
− ( x−µ )
1 2σ
2

f ( x )= e
√2 π σ

University of Engineering & Technology, Lahore


Faisalabad Campus Page 48 of 61
Probability & Statistics Session 2018

where 𝝁 and 𝝈 are two scalar parameters characterizing the PDF, with 𝝈 assumed
nonnegative.

The 𝛍 parameter defines the location of the peak for normal distribution and is calculated by
using formula [𝑋] = 𝜇 where 𝜎 defines the width of the normal distribution calculated by
using formula 𝑉𝑎𝑟 [𝑋] = σ 2

Figure10.1: Graph of normal distribution

Standard Normal Distribution:


The normalization property of the normal PDF for the case where µ = 0 and σ = 1. A normal
random variable 𝑌 with zero mean and unit variance is said to be a standard normal. Its CDF
is denoted by 𝚽
2
y −t
1 2
ϕ ( y )=P ( Y ≤ y )=P (Y < y )= ∫e dt
√ 2 π −∞
Let 𝑿 be a normal random variable with mean µ and variance 𝝈𝟐. We “standardize” 𝑿 by
defining a new random variable 𝑌 given by
X −µ
Y=
σ

Thus, Y is a standard normal random variable. This fact allows us to calculate the probability
of any event defined in terms of X: we redefine the event in terms of Y, and then use the
standard normal.

makedist command:
Creates a probability distribution object for the distribution distname, using the default
parameter values.

Syntax:
pd = makedist(distname)

University of Engineering & Technology, Lahore


Faisalabad Campus Page 49 of 61
Probability & Statistics Session 2018

For normal distribution we will write pd = makedist('Normal') and to create a normal


distribution object with parameter use pd = makedist('Normal','mu',75,'sigma',10)

Pdf Command:
It returns output as an array containing the probability density function (PDF) for the
ProbDist object PD, evaluated at values in X

Syntax:
Y = pdf(PD, X) 

Codes for Uniform and normal distribution:

Figure10.3: Code to find uniform distribution in MATLAB

Figure10.4: Code to find uniform distribution of given task in MATLAB

The graph with high peak is for standard normal distribution.

University of Engineering & Technology, Lahore


Faisalabad Campus Page 50 of 61
Probability & Statistics Session 2018

Figure10.5: Code to find uniform distribution of given task in MATLAB

LAB TASKS
Lab tasks.

Q1. Plot different uniform distribution on a same plot.

Q2. Plot different normal distribution on a same plot.

Conclusion:

University of Engineering & Technology, Lahore


Faisalabad Campus Page 51 of 61
Probability & Statistics Session 2018

Uniform distribution and normal distribution is learned and the results are verified through
theoretical results. The 𝝁 and 𝝈 are two scalar parameters that controls the normal distribution. 𝛍
defines the location of the peak for normal distribution and 𝜎 defines the width of the normal
distribution. So as we increase the 𝛍 the peak of normal distribution increases and hence as we
increase the 𝜎 it will increase the width of the distribution.

University of Engineering & Technology, Lahore


Faisalabad Campus Page 52 of 61
Probability & Statistics Session 2018

Lab 11
Covariance
Objective:
To familiarized with Covariance and its implementation in MATLAB.

Introduction:
Covariance:
In mathematics and statistics, covariance is a measure of the relationship between two
random variables. The metric evaluates how much – to what extent – the variables change
together. In other words, it is essentially a measure of the variance between two variables.
However, the metric does not assess the dependency between variables.
Covariance measures how much the movement in one variable predicts the movement in a
corresponding variable. he units are computed by multiplying the units of the two variables.
The variance can take any positive or negative values. The values are interpreted as follows:
Positive covariance:
Indicates that two variables tend to move in the same direction i.e. increasing linear
relationship of two variables.
Negative covariance:
Reveals that two variables tend to move in inverse directions i.e. the decreasing linear
relationship of two variables
If covariance is zero the there is no relationship between two variables.

Figure11.1: Covariance

University of Engineering & Technology, Lahore


Faisalabad Campus Page 53 of 61
Probability & Statistics Session 2018

Average product of deviation measures extent to which variables covary, the degree of
linkage between them

Formula for Covariance:


Similar to variance, for theoretical reasons, average is typically computed using (N -1), not N.
Thus,
N
1
Sxy= ∑ ( Xi− X́ ) ( Yi−Ý )
N −1 i=0

Where N=Total data Points of Variable X or Y


Xi= ith Data Variable X
Yi= ith Data Variable Y

X́ =mean of Data Variable X

Ý =mean of Data Variable X

LAB TASKS
Lab Task 1:
Investigate relationship between cigarette smoking and lung capacity. Data: sample group
response data on smoking habits, and measured lung capacities, respectively

Figure11.2: Given task

University of Engineering & Technology, Lahore


Faisalabad Campus Page 54 of 61
Probability & Statistics Session 2018

Fig. No. 11.1: Matlab code and simultaion

Lab Task 2:
Rising Hill manufacturing wishes to study the relationship between the number of workers,
‘x’, and the number of tables produced, ‘y’. To do so 10 samples are obtained. Details are
given as follows; Find cov(x,y)

Figure11.5: Given task

MATLAB code:

University of Engineering & Technology, Lahore


Faisalabad Campus Page 55 of 61
Probability & Statistics Session 2018

Fig. No.11.2: Matlab code and simultaion

Conclusion:
In this lab, I implemented covariance in MATLAB using examples provided.

University of Engineering & Technology, Lahore


Faisalabad Campus Page 56 of 61
Probability & Statistics Session 2018

Lab 12
Correlation
Objective:
To familiarized with Correlation and its implementation in MATLAB.

Introduction:
Correlation:
A correlation is a statistical measure of the relationship between two variables. The measure
is best used in variables that demonstrate a linear relationship between each other. The fit of
the data can be visually represented in a scatterplot. Using a scatterplot, we can generally
assess the relationship between the variables and determine whether they are correlated or
not.
For example, height and weight are related; taller people tend to be heavier than shorter
people. The relationship isn't perfect. Hook’s Law: 𝐹=𝑘𝑥. The relationship between two
variables is linear and perfect.
The correlation coefficient is a value that indicates the strength of the relationship between
variables. The coefficient can take any values from -1 to 1. The interpretations of the values
are:
Perfect negative correlation:
If correlation is -1 The variables tend to move in opposite directions (i.e., when one variable
increases, the other variable decreases).

Figure12.1: Positive correlation


No correlation.
When correlation is 0 he variables do not have a relationship with each other.
Perfect positive correlation

University of Engineering & Technology, Lahore


Faisalabad Campus Page 57 of 61
Probability & Statistics Session 2018

If correlation is 1 the variables tend to move in the same direction (i.e., when one variable
increases, the other variable also increases).

Figure12.2: Negative correlation

Formula for Covariance:


N

∑ ( Xi− X́ )( Yi−Ý )
i=0
Sxy= N N

√∑
i=0
( Xi− X́ )
2
∑ ( Yi−Ý )
i=0
2

Where; N=Total data Points of Variable X or Y


Xi= ith Data Variable X
Yi= ith Data Variable Y

X́ =mean of Data Variable X

Ý =mean of Data Variable X

LAB TASKS
Lab Task 1:
Investigate relationship between cigarette smoking and lung capacity. Data: sample group
response data on smoking habits, and measured lung capacities, respectively

University of Engineering & Technology, Lahore


Faisalabad Campus Page 58 of 61
Probability & Statistics Session 2018

Figure12.3: Given task

Fig. No. 12.2: Matlab code

University of Engineering & Technology, Lahore


Faisalabad Campus Page 59 of 61
Probability & Statistics Session 2018

Fig. No. 12.3: Results

Lab Task 2:
The local ice cream shop keeps track of how much ice cream they sell versus the temperature
on that day, here are their figures for the last 12 days

Temprature 14.2 16.4 11.9 15.2 18.5 22.1 19.4 25.1 23.4 18.1 22.6 17.2

Sale 215 325 185 332 406 522 412 614 544 421 445 408

University of Engineering & Technology, Lahore


Faisalabad Campus Page 60 of 61
Probability & Statistics Session 2018

Figure 12.3: Matlab code and Results

Conclusion:
In this lab, I implemented correlation in MATLAB using examples provided.

University of Engineering & Technology, Lahore


Faisalabad Campus Page 61 of 61

You might also like