PROBABILITY, STATISTICS AND LINEAR PROGRAMMING LAB FILE
PAPER CODE: BS-252
STUDENT NAME:AKSHAT GUPTA
ENROLLMENT NUMBER: 35227912723
PROGRAM: [Link] (CST)
FACULTY NAME: Dr Nikhat Zulekha
[1]
/
TRINITY INSTITUTE OF INNOVATION AND PROFESSIONAL
STUDIES
VISION
To attain global excellence through education, innovation, research, and
work ethics with the commitment to serve humanity.
MISSION
M1. To promote diversification by adopting advancement in science, technology,
management, and allied discipline through continuous learning.
M2. To foster moral values in students and equip them for developing sustainable
solutions to serve both national and global needs in society and industry.
M3. To digitize educational resources and process for enhanced teaching and
effective learning.
M4. To cultivate an environment supporting incubation, product development,
technology transfer, capacity building and entrepreneurship.
M5. To encourage faculty-student networking with alumni, industry, institutions,
and other stakeholders for collective engagement.
[2]
PROBABILITY, STATISTICS AND LINEAR PROGRAMMING LAB
PAPER CODE: BS-252
COURSE OBJECTIVE
1. To understand probability and probability distributions.
2. To understand methods of summarization and visualization of data.
3. To understand the use of various hypothesis-testing techniques.
4. To understand methods for solving linear programming problems.
COURSE OUTCOMES
At the end of the course student will be able to:
C.252.1 Apply probability concepts and describe probability distributions to solve
real-world problems. (Unit I)
C.252.2 Analyze joint distributions and visualize data using statistical tools.
(Unit II)
C.252.3 Evaluate hypothesis-testing techniques and apply regression and correlation
methods for analyzing data and making predictions. (Unit III)
C.252.4 Formulate and solve linear programming problems using mathematical
methods. (Unit IV)
[3]
PROGRAM OUTCOMES (POs):
Engineering Knowledge: Apply the knowledge of basic sciences and engineering fundamentals to solve
1 engineering problems.
Problem Analysis: Analyze the complex engineering problems and give solutions related to chemical &
2 allied industries.
Design/ development of solutions: Identify the chemical engineering problems, design and formulate
3 solutions to solve both industrial & social related problems.
Conduct investigations of complex problems: Design & conduct experiments, analyze and interpret the
4
resulting data to solve Chemical Engineering problems.
Modern tool usage: Apply appropriate techniques, resources and modern engineering & IT tools for the
5
design, modeling, simulation and analysis studies.
The engineer and society: Assess societal, health, safety, legal and cultural issues and their consequent
6 responsibilities relevant to professional engineering practice.
Environment and sustainability: Understand the relationship between society, environment and work
7 towards sustainable development.
Ethics: Understand their professional and ethical responsibility and enhance their commitment towards
8 best engineering practices.
Individual and team work: Function effectively as a member or a leader in diverse teams, and be
9
competent to carry out multidisciplinary tasks.
Communication: Communicate effectively in both verbal & non-verbal and able to comprehend &
10 write effective reports.
Project management and finance: Understand the engineering and management principles to manage
11 the multidisciplinary projects in whatsoever position they are employed.
Life-long learning: Recognize the need of self education and life-long learning process in order to
12 keep abreast with the ongoing developments in the field of engineering.
[4]
Mapping of Course Outcomes with Program Outcomes
BS-252 Probability Statistics and Linear Programming Lab
PO PO PO PO PO PO PO PO PO PO PO PO
01 02 03 04 05 06 07 08 09 10 11 12
[Link]-252.1 3 3 2 2 2 - - - 2 2 2 3
[Link]-252.2 3 3 2 2 2 - - - 2 2 2 3
[Link]-252.3 3 3 3 3 2 - - - 2 2 2 3
[Link]-252.4 3 3 3 3 2 - - - 2 2 2 3
[5]
INDEX
No. Title of Lab Experiment Date 𝘙1 𝘙𝟐𝟐 𝘙𝟑𝟑 𝘙𝟒𝟒 Remarks/ Sign
1. Demonstrations of SCILAB to simple
programming concepts like addition,
multiplication and transpose of matrices
(using loops)
2. Fitting of Binomial
Fitting of Poisson distribution after
3. computing mean
4. Fitting Standard Normal Distribution
Compute covariance and correlation for two
5.
datasets
Write a program to visualize statistical data
6. using histogram.
7. Fitting of regression lines
Perform a Chi-square test for goodness of fit
8. for a given dataset
Program for solving Assignment Problem
9. of three variables.
Program for forming first simplex table
10. for a three variable linear programming
problem.
Program for solving Transportation
11. Problem of three variables.
Perform t-test for comparing means of two
12. samples.
[6]
EXPERIMENT 1(A):PROGRAM FORFINDING MATRIX ADDITION
Theory: A Matrix is a rectangular array of numbers, expressions or symbols, arranged in rows and
columns. Matrix addition is commutative and associative just like addition of arithmetic
[Link] or subtraction can be performed on two matrices if and only if they are of same
order.
𝑎11 𝑎12 𝑎13 𝑏11 𝑏12 𝑏13
𝐴 = (𝑎21 𝑎22 𝑎23) 𝐵 = (𝑏21 𝑏22 𝑏23)
𝑎31 𝑎32 𝑎33 𝑏31 𝑏32 𝑏33
𝑎11 ± 𝑏11 𝑎12 ± 𝑏12 𝑎13 ± 𝑏13
Then 𝐴 ± 𝐵 = (𝑎21 ± 𝑏21 𝑎22 ± 𝑏22 𝑎23 ± 𝑏23)
𝑎31 ± 𝑏31 𝑎32 ± 𝑏32 𝑎33 ± 𝑏33
Matrix Addition Algorithm:
• Declare variables and initialize necessary variables
• Enter the elements of matrices row wise using loops
• Add the corresponding elements of the matrices using nested loops • Print the resultant matrix as
console output
CODE& OUTPUT
clc;
clearall;
m=input("Enter the no. of rows: ");
n=input ("Enter the no. of columns: ");
disp ("Enter the first matrix: ");
for i=1:m
for j = 1:n
A(i,j)= input("");
end
end
disp ("Enter the second matrix: ");
for i=1:m
for j=1:n
B(i, j)= input(" ");
end
end
for i=1:m
for j=1:n
C(i,j)=A(i,j)+B(i,j)
end
end
disp("The first matrix is ");
[7]
disp (A);
disp" The second matrix is ");
disp (B);
disp("The sum of two matrices is ");
disp (C);
Output:
[8]
[9]
EXPERIMENT 1(B):PROGRAM TO COMPUTE MATRIX MULTIPLICATION
Theory: Matrix product 𝐴𝐵 is possible only if number of columns in matrix 𝐴 are same as number of
rows in matrix 𝐵.
𝑎11 𝑎12 𝑎13 𝑏11 𝑏12
𝐴 = (𝑎21 𝑎22 𝑎23) 𝐵 = (𝑏21 𝑏22 )
𝑏31 𝑏32
𝐶 = 𝐴𝐵 = (𝑐11 = 𝑎11𝑏11 + 𝑎12 𝑏21 + 𝑎13𝑏31𝑐12 = 𝑎11𝑏12 + 𝑎12 𝑏22 + 𝑎13𝑏32 )
𝑐21 = 𝑎21𝑏11 + 𝑎22 𝑏21 + 𝑎23𝑏31𝑐22 = 𝑎21𝑏12 + 𝑎22 𝑏22 + 𝑎23𝑏32
Note that: (i) 𝐴𝑚×𝑛𝐵𝑛×𝑘 = 𝐶𝑚×𝑘 (ii)
𝐴𝐵 ≠ 𝐵𝐴 in general
Matrix Multiplication Algorithm:
• Declare variables and initialize necessary variables
• Enter the elements of matrices row wise using loops
• Check the number of rows and column of first and second matrices
• If number of rows of first matrix is equal to the number of columns of second matrix, go to next
step. Otherwise, print ’Matrices are not conformable for multiplication’ and abort.
• Multiply the matrices using nested loops
• Print the product in matrix form as console output
CODE AND OUTPUT:
Clc;
m=input ("Enter no of rows: ");
n=input("Entry/no of columns: ");
disp ("Enter fuist matrix: ");
for i=1:m
for j=1:n
A(i,j)= input("");
end
end
disp("Enter second matrix: “);
[10]
for i=1:m
for j=1:n
B(i,j) = input("");
end
end
p=zeros (m,n);
for i 1 m
for j= 1:n
for k=1:n
p(i,j)=p(i,j)+A(i,k) B(k,j)
end
end
end
disp("The first matrix is: ");
disp (A);
disp ("The second mabrix is: ");
disp (B);
disp("The product of two/matrices are: ");
disp (P);
[11]
OUTPUT:
[12]
EXPERIMENT 1(C):PROGRAM TO COMPUTE MATRIX TRANSPOSE
Theory:The matrix 𝐴′ or 𝐴𝑇 obtained by interchanging rows and columns of a matrix 𝐴 is known
as its transpose.
1 3 5 1 2 0
𝐴=(2 −1 4) 𝐴 = ( 3
𝑇 −1 2)
0 2 3 5 4 3
Matrix Transpose Algorithm:
• Declare variables and initialize necessary variables
• Enter the elements of matrix by row wise using loop
• Interchange rows to columns using nested loops
• Print the transposed matrix as console output
CODE & OUTPUT
clc;
clearall,
close all;
no of m= input("Enter no of rou Mows: ")
n= input("Enter no of columns: ");
disp("Enter fupst matrix: ");
for i=1:nm
for j = fin
A(i,j)= input(" ");
end
end
disp ("Transpose");
for i=1:m
for j=1:n
C(j)=A(i,j)
end
[13]
[14]
EXPERIMENT 2: PROGRAM FOR FITTING BINOMIAL DISTRIBUTION WITH GIVEN
N AND P
Theory: A series of independent trials which result in one of the two mutually
exclusive outcomes ‘success’ or ‘failure’ such that the probability of success (or
failure) in each trial is constant, then such repeated independent trials are called as
‘Bernoulli trials’. A discrete random variable which results in only one of the two
possible outcomes (success or failure) is called Binomial variable.
i. Let there be 𝑛 independent finite trials in an experiment such that
Each trial has only two possible outcomes success and failure
ii. Probability of success (𝑝) and probability of failure (𝑞) are
constant for all the trials and 𝑝 + 𝑞 = 1.
Then if a random variable 𝑋𝑋 denotes the number of successes in 𝑛 trials, then
𝑃(𝑋𝑋 = 𝑟) = 𝑛𝐶𝑟𝑝𝑟𝑞𝑛−𝑟or 𝑃(𝑟) = 𝑛𝐶𝑟𝑞𝑛−𝑟𝑝𝑟
∴ Binomial distribution may be given as(𝑞 + 𝑝)𝑟
CODE & OUTPUT
disp("Enter no of
observations"); clearall;
n = input(" ");
disp("Value of p");
p = input(" ");
disp("Enter the value of x");
for i = 1:n
X(i, 1) =
input(" "); end
disp("Enter no of frequency");
for j = 1:n
F(j, 1) = input(" "); end
EF = sum(F) * binomial(p, n - 1);
disp("Given frequencies");
disp(F);
disp("Expected frequencies");
disp(EF);
plot2d3(0:n-1, F);
plot2d(0:n-1, EF);
[16]
EXPERIMENT 3: FITTING POISSON DISTRIBUTION AFTER COMPUTING MEAN
Theory: Poisson distribution with 𝑃(𝑟)= e -𝜆 𝜆r is a limiting case of Binomial distribution,
under r!
the conditions 𝑖. 𝑛 → ∞ 𝑖𝑖. 𝑝 → 0 𝑖𝑖𝑖. 𝑛𝑝 = 𝜆 is finite
Algorithm for fitting Poisson distribution after computing mean:
• Input the number of observations
• Enter the elements 𝑥𝑖 using loop
• Enter the corresponding frequencies 𝑓𝑖 using loop
• Calculate theoretical frequencies using 𝑃 (𝑥i) = e-𝜆 𝜆xi
𝑟!
• Display 𝑃(𝑥𝑖)
• Plot the graph between expected and theoretical frequencies using plot2d command
CODE & OUTPUT
clear; disp("Enter no of
observations");
n = input("\"); disp("Enter
the value of x");
for i = 1:n
X(1, i) = input(" ");
end
[17]
disp("Enter no of frequency");
for j = 1:n
F(1, j) = input(" ");
end
disp("Mean of the distribution is");
M = sum(F .* X) / sum(F); disp(M);
for i = 1:n
P(1, i) = sum(F) * exp(-M) * M^X(i) / factorial(X(i)); end
disp("Expected frequencies are");
disp(P);
plot2d(X, P);
[18]
[19]
EXPERIMENT 4: PROGRAM FOR FITTING STANDARD NORMAL DISTRIBUTION
Theory: The normal distribution developed by Gauss is a continuous distribution and is very useful
in practical applications. It can be considered as the limiting form of the Binomial Distribution when
the number of trials (𝑛), is very large and neither 𝑝 nor 𝑞 is very small. The probability curve of a
normal variate𝑥 with mean 𝜇 and standard deviation 𝜎 is given by:
Any normal variate x with mean 𝜇 and
standard deviation 𝜎 is changed to a
standard normal variate and
hence the probability density function of 𝑧
is given by:
The normal distribution with mean 𝜇 and
variance 𝜎2 is denoted by 𝑁 (𝜇 , 𝜎2).
The adjoining figure shows a normal distribution curve for standard normal variate 𝑧.
CODE & OUTPUT
clear;
clc;
x = [5.1, 5.5, 5.8, 6.0, 6.1, 6.2, 6.3, 6.5, 6.6, 6.8];
n = length(x);
mu = mean(x);
sigma = stdev(x);
xmin = min(x) - 1;
xmax = max(x) + 1;
x_fit = linspace(xmin, xmax, 100);
pdf_fit = (1 / (sigma * sqrt(2 * %pi))) * exp(-((x_fit - mu).^2) / (2 * sigma^2));
clf;
histplot(10, x);
xlabel("Data");
plot(x_fit, pdf_fit * n * (xmax - xmin) / 10, "r-", 'LineWidth', 2);
legend(["Histogram", "Fitted Normal Distribution"], "location", "upper_right");
ylabel("Frequency");
title("Fitting a Normal Distribution");
[21]
[22]
[23]
EXPERIMENT 5: COMPUTE COVARIANCE AND CORRELATION FOR 2 DATASETS
Theory: Correlation is a measure of association between two variables, which may be dependent or
independent. Whenever two variables 𝑥 and 𝑦 are so related; that increase in one is accompanied by
an
increase or decrease in the other, then the variables are said to be correlated. Coefficient of correlation
(𝑟) lies between −1 and +1,
2. Compute the mean of each dataset.
3. Calculate the covariance using the formula:
Cov(X, Y) = sum((X - mean(X)) .* (Y - mean(Y))) / (n - 1)
4. Compute the correlation using the formula: Corr(X, Y) = Cov(X, Y) / (std(X) * std(Y))
5. Display the results.
CODE & OUTPUT
clc; clear;
X = [1,2,3,4,5];
Y = [2,4,6,8,10];
n = length(X);
mean_X = mean(X); mean_Y
= mean(Y);
[24]
covariance = sum((X - mean_X) .* (Y - mean_Y)) / (n - 1);
std_X = stdev(X); std_Y
= stdev(Y);
correlation = covariance / (std_X * std_Y);
disp("Covariance"); disp(covariance);
disp("Correlation");
disp(correlation);
[24]
[25]
PERIMENT 6: PROGRAM TO VISUALIZE STATISTICAL DATA USING HISTOGRAM
Theory: A histogram is a visual representation of the distribution of quantitative data. It is
represented by a set of rectangles, adjacent to each other, where each bar represents a kind of data.
Algorithm:
1. Input the dataset to be visualized.
2. Define the number of bins for the histogram.
3. Use the histplot function to generate and display the histogram.
4. Customize the plot with labels and title for better visualization.
CODE & OUTPUT
clc; clear;
close;
m = input("Enter the no. of data points: ");
disp("Enter the data values--");
data = zeros(1, m);
for i = 1:m
data(1, i) = input("x = ");
end
b = input("Enter the number of bins: ");
histplot(b, data);
xlabel("Data Values"); ylabel("Frequency");
title("Histogram Representation of Data");
[26]
EXPERIMENT 7: PROGRAM FOR FITTING REGRESSION LINES
Theory: Regression describes the functional relationship between dependent and independent
variables; which helps us to make estimates of one variable from the other. If we plot the observations
of the linear regression between two variables,
actually two straight lines can approximately be drawn
through the scatter diagram. One line estimates values of 𝑦
for specified values of 𝑥 (known as line of regression of 𝑦
on 𝑥); and other predicts values of 𝑥 from given values of 𝑦
(called line of regression of 𝑥 on 𝑦). Here, Point of
intersection of two lines of regression is (𝑥, 𝑦), Where 𝑥
and 𝑦 are the means of 𝑥 series and 𝑦 series.
Algorithm for fitting line of regression: 𝑦 = 𝑎 + 𝑏𝑥
•Enter the number of data points
•Enter the elements 𝑥𝑖 using loop
•Enter the corresponding points 𝑦𝑖 using loop
•Evaluate ∑ 𝑥, ∑ 𝑦, ∑ 𝑥𝑦 and ∑ 𝑥2
•Solve the normal equations ∑ 𝑦 = 𝑎𝑛 + 𝑏 ∑ 𝑥 and ∑ 𝑥𝑦 = 𝑎 ∑ 𝑥 + 𝑏 ∑ 𝑥2 to find the values of
𝑎 and 𝑏.
• The line of regression of y on x is: 𝑦 = 𝑎 + 𝑏𝑥
• Define the x range and plot the line of regression usingplot (𝑥, 𝑎 + 𝑏 ∗ 𝑥) command
CODE & OUTPUT
clc;
clear;
close;
n = input('Enter the number of data points: ');
printf("Enter the values of
xi\n"); x = zeros(1, n); for i = 1:n
x(i) = input(''); end
printf("Enter the values of
yi\n"); y = zeros(1, n); for i = 1:n
y(i) = input(''); end
sumx = 0;
[27]
sumy = 0;
sumxy = 0;
sumx2 = 0;
for i = 1:n sumx = sumx +
x(i); sumx2 = sumx2 + x(i)
* x(i); sumy = sumy + y(i);
sumxy = sumxy + x(i) * y(i);
end
a = ((sumx2 * sumy - sumx * sumxy) / (n * sumx2 - sumx * sumx)); b
= ((n * sumxy - sumx * sumy) / (n * sumx2 - sumx * sumx));
printf('The line is Y = %3.3f + %3.3f * X\n', a, b);
x_values = linspace(min(x), max(x), 100);
y_values = a + b * x_values;
plot(x_values, y_values);
xlabel('X-axis'); ylabel('Y-
axis'); title('Linear Regression
Line');[31]
[32]
[33]
EXPERIMENT 8: PERFORM A CHI-SQUARE TEST FOR GOODNESS TO FIT FOR A
GIVEN DATA
Theory: Chi-square (𝜒2) is a right tailed test and describes the magnitude of discrepancy between theory
and observation. If𝜒2 = 0; the observed and expected frequencies completely coincide, 𝜒 provides
a measure of correspondence between theory and observations. It is one of the simplest and most general
tests and can be used to perform
1. test of significance of sample variance
2. test of goodness of fit
3. test of independence of attributes in a contingency table
Chi-Square (𝜒2) Test for Testing Goodness of Fit
If 𝑂𝑖 (𝑖 = 1,2, … , 𝑛) be a set of observed (experimental) frequencies and 𝐸𝑖 (𝑖 = 1,2, … , 𝑛) be the
(𝑂𝑖−𝐸𝑖 )2
2 = ∑𝑛𝑖=1
corresponding set of expected (theoretical) frequencies, then 𝜒 𝐸𝑖 ; ; with degrees of
freedom (𝜈) = 𝑛 − 1.
There are some underlying conditions for applying 𝜒2 test such as:
1. Sum of frequencies should be large (at least 50)
2. No theoretical cell-frequencies should be small, if small (less than 5) theoretical frequencies occur,
regrouping of two or more cells must be done before calculating (𝑂𝑖 − 𝐸𝑖). Degree of freedom is
determined by the number of classes after regrouping.
Algorithm:
1. Input observed frequencies, expected frequencies and degrees of freedom (𝑑. 𝑓. = 𝑛 − 1) where 𝑛 is the
number of categories.
2. Compute Chi-square statistic (𝜒2)
3. Compare the calculated value of 𝜒2 with the critical value from the chi-square table for the given degree of
freedom and significance level α.
4. Decision:
• If 𝜒2(𝑐𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑒𝑑) > 𝜒2(𝑐𝑟𝑖𝑡𝑖𝑐𝑎𝑙), reject the null hypothesis 𝐻0.
• Otherwise, fail to reject 𝐻0.
5. Output: The 𝜒2 value and whether 𝐻0is rejected or not.
CODE & OUTPUT
clc; clear;
observed = [50, 30, 20]; // Replace with your dataset expected =
[40, 40, 20]; // Replace with your dataset
if size(observed) <> size(expected) then error("The observed and expected frequencies must have the
same size."); end
chi_square_stat = sum(((observed - expected).^2)./expected);
n = length(observed);
df = n - 1;
alpha = 0.05;
chi_square_critical = chi2inv(1 - alpha, df);
disp("Chi-Square Statistic: " + string(chi_square_stat)); disp("Degrees
of Freedom: " + string(df));
disp("Critical Value: " + string(chi_square_critical));
if chi_square_stat > chi_square_critical then
disp("Reject the null hypothesis (H0): The data does not fit the expected distribution.");
else disp("Fail to reject the null hypothesis (H0): The data fits the expected
distribution."); end