0% found this document useful (0 votes)
28 views

PSLP Lab File

Uploaded by

sillybilli001
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)
28 views

PSLP Lab File

Uploaded by

sillybilli001
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/ 30

MAHARAJA SURAJMAL INSTITUTE OF

TECHNOLOGY

PRACTICLE FILE
PROBABLITY, STATISTCS AND LINEAR
PROGRAMMING
(BS-202)

Submitted by Submitted to
Name: Vipul Singh Name: Dr Maan
Prof. MaanSingh
Singh

Enrollment no.: 04596302822 Department: ECE


Branch/Sem: ECE-3, 4thsem
Batch: 2022-26
S.no. Name of Experiment Date of Page no. Signature
experiment
1. Installation of Scilab and
demonstration of simple 1-6
programming concepts like
matrix multiplication (scalar
and vector), loop, conditional
statements and plotting.
2. Program for demonstration 7-8
of theoretical probability
limits
3. Fitting of binomial 8-11
distributions for given n and p
4. Fitting of Binomial 12-14
distribution after computing
mean and variance.
5. Fitting of Poisson 15
Distributions for given value
of lamda.
6. Fitting of Poisson 16-17
Distributions after Computing
Mean.
7. Program to plot normal 18-21
distribution and exponential
distribution for various
parameters
8. Fitting of normal distribution 22-24
when parameters are given.
9. Fitting of Linear regression 25-26
line through given data set.
10. Fitting of Multiple Linear 27-28
regression curve through
given data set.
PROGRAM NO.- 1
Aim: Installation of Scilab and demonstration of simple programming concepts like matrix
multiplication (scalar and vector), loop, conditional statements and plotting.

A) Installation of Scilab

Here are the steps to install Scilab:

 Visit the official Scilab website at www.scilab.org and go to the "Download" page. Choose
the appropriate version of Scilab for your operating system. Scilab is available for
 Windows, Linux, and macOS.
 Click on the download link to start the download process.
 Choose the installation directory and follow the prompts to complete the installation
process.
 After the installation is complete, you can launch Scilab from the Start menu on Windows, or
from the Applications folder on macOS.
 When you launch Scilab for the first time, you may need to configure some settings, such as
the default working directory and the editor preferences.

B) Matrix Multiplication

Code:

clc;

mat1 (1 2 3; 4 5 6);

mat2 (2 3; 4 5; 6 7);

disp(" MATRIX A ----");

disp(mat1);

disp(" MATRIX B-");

disp(mat2);

disp(" MATRIX MULTIPLICATION--");

disp(mat1 mat2);

disp(" SCALAR MULTIPLICATION--");

disp([4]*mat1);

disp(". VECTOR MULTIPLICATION");

disp([4 2]*mat1);
C) Multiplication Table Using For Loop Example

Code:

clc;

disp("--- TABLE FOR USER USING LOOP STATEMENTS ---");

printf("\t");

number = input("Enter the value: ");

for i=1:10

printf("\t%d X %d = %d\n",number, i, number*i);

end

output :
D) Conditional Statement If-Else Example

Code:

Clc:

marks = input("Enter your marks: ");

total = 80;

per = (marks/total) 100;

if per<50 then

disp("vipul failed the exam 0.0");

else

disp("vipul just passed the best exam");

end

Output :
E) Quadratic plot (y=x²)

Code:

clc;

function f = curve(x)

f = x^2;

endfunction

x = (1:10);

y = curve(x);

xlabel("Values of X","color","blue");

ylabel("Values of Y","color", "red");

title('Quadratic Plot');

plot(x,y);

Output:
PROGRAM NO. - 2

Aim: Program for demonstration of theoretical probability limits.

A) Probability Of Tossing a Coin N Times

Code:

clc;
printf("----- PROBABILITY OF TOSSING COIN N TIMES----\n");
N= input(" Enter the no of tosses: ");
head =0;
for i=1:N
var= round(rand(1));
if var == 1 then
head =head +1
end
proh(i)=head/i;
end
prot =1-proh;
plot(1:N,proh, 'b');,
plot(1:N,prot, 'r');
xlabel("--- Number of tosses----")
ylabel ("---Probability Distibution----")
title("Plot-Probability of Tossing a Coin N Times ");
legend('Head','Tail');

Output :
B) Probability Of Getting a Six Øn Rolling a Dice N Times

Code:

clc;
printf(" PROBABILITY OF ROLLING DICE N TIMES--\n");
N = input(" Enter the no of dice rolls: ");
gotsix = 0;
for i = 1:N
var = ceil(rand(1)*6);
if var == 6 then
gotsix = gotsix + 1;
end
pros(i) = gotsix/i;
end
plot(pros,'b');
xlabel('-- Number of Dice Rolls---','b');
ylabel('-- Probability Distribution---','r');
title(' Plot of getting Six');

Output :
PROGRAM NO. - 3

Aim: Fitting of binomial distributions for given n and p.

A) 10 coins are thrown, find the Probability of getting 7 heads

Code:

clc;

n=input("Enter number of trials: ");

y=input("Enter number of success: ");

p=input("Enter probability of successes: ");

q=1-p;

r=factorial(n)/(factorial(y)* factorial(n-y))*(p^n)*(q^(n-y));

printf("Probability of Getting %d Heads: %f",y,r);

Output :
B) Let 'X' be binomial random variable with parameter n = 6 and p = 0.4. Find probability.

Code:

clc;

n=6;

y=input("Enter number of success: ");

p=0.4;

q=1-p;

r=factorial(n)/(factorial(y)* factorial(n-y))*(p^n)*(q^(n-y));

printf("Probability: %f",r);

Output:

C) A coin is tossed 6 times, calculate the Probability of getting 4 or more heads.

Code:

clc;

n=6;

p=0.5;

q=1-p;

total=0;

for x = 4:6

total = total + (factorial(n)/(factorial(x)*factorial(n-x))*(p^n)*(q^(n-x)));

end

printf("Probability of Getting 4 or more Heads: %f", total);


Output :

D) The probability that a bomb dropped from a plane will hit the target is 1/5. If 6 bombs
are dropped, Find the probability:

1) Exactly 2 bombs will hit the target.

Code:

clc;

n=6;

p=0.2;

q=1-p

y=2;

r=factorial(n)/(factorial(y)*factorial*(n-y))* (p^n)*(q^(n-y));

printf("Probability that Exactly 2 bombs will hit the target: %f",r);

Output:
II) Atleast 2 bombs will hit the target.

Code:

clc;

n=6;

p=0.2;

q=1-p;

total=0;

for x=2:6;

total = total + (factorial(n)/(factorial(x)*factorial(n-x))*(p^n)*(q^(n-x)));

end

printf("Probability that Atleast 2 bombs will hit the target: %f", total);

Output:
PROGRAM NO. - 4

Aim: Fitting of Binomial distribution after computing mean and variance.

7 coins are tossed and no. of heads are noted. The experiment is repeated 128 times and the
following distribution is obtained.

Frequency of Heads

No. of head 0 1 2 3 4 5 6 7 Total

Frequency 7 6 19 35 30 23 7 1 128

Fit the binomial distribution assuming nature of coin is not known, write Scilab code.

Code :

clc();

printf("Un-Baised Coin: Enter 1\n");

printf("Baised Coin: Enter 2\n");

choice = input("Enter your choice (1/2): ”);

f = [7 6 19 35 30 23 7 1];

x= [0 1 2 3 4 5 6 7];

n = 7;

N = sum(f);

sigma = 0;

for i = 1:length(x)

sigma = sigma + x(i)*f(i);

end

mu = sigma/N;

p = mu/n;

q = 1-p;

var = n*p*q;

printf("Calculated Mean(μ): %f\n",mu);


printf("Calculated Variance (σ^2): %f \n", var);

s = n;

r = 0;

select(choice)

case 1 then

p=0.5;

q=0.5;

data = [1 1 1 1 1 1 1];

for i = 1:length(data)

data(i) = data(i)*(factorial(n)/(factorial(r)*factorial(n-r))) * (p^s) * (q^r);

s = s -1;

r = r +1;

end

printf("... Binomial Probability Distribution For Un-Blased Coin--\n");

disp(data);

case 2 then

data= [1 1 1 1 1 1 1];

for i = 1:7

data(i) = data(i)*(factorial(n)/(factorial(r)*factorial(n-r)))* (p^s)* (q^r);

s = s-1;

r=r+1;

end

printf("..- Binomial Probability Distribution For Blased Coin--\n");

disp(data);

else printf("Not a vaild choice!\n");

end
Output :

A) When Coin is unbiased

B) When Coin is Biased


PROGRAM NO. - 5

Aim: Fitting of Poisson Distributions for given value of lamda.

• Consider X has a Poisson Distribution with a mean of 4. Determine the following

Probability:

i. P (X=0)

ii. P (X<3)

iii. P (X=4)

iv. P (X=8)

CODE:

clc;

lambda=4;

printf("Fitting of Poisson Distribution\n"); printf("Mean: %d\n",lambda);

function y=p(x) y=(exp(-lambda)* (lambda^x))/factorial(x);

endfunction

printf("1. P(X=0) = %f\n",p(0));

printf("2. P(X<3) = %f\n",(p(0)+p(1)+p(2)));

printf("3. P(X=4) = %f\n",p(4));

printf("4. P(X=8) = %f\n",p(8));

code:
PROGRAM NO. - 6

Aim: Fitting of Poisson Distributions after Computing Mean.

a) Consider that on an average 1 House in 1000 Houses catches fire in a year in a district. If there
are 2000 houses in that district, find the probability that exactly 5 houses will catch fire.

CODE:

n=2000,

printf("\n Fitting of Poisson Distribution after Computing Mean\n");

printf("\n Total Number of houses= %d\n",n);

p=1/1000;

printf("\n Probability that a House will catch fire=%f\n" ,p);

lambda = n* p;

printf("\n Mean = %d\n",lambda);

function y = p(x)

y=(exp(-lambda)* (lambda^x))/factorial(x)

endfunction

printf("\n Propability that Exactly 5 Houses will get fire:\n");

printf("\n P(x=5) = %f\n" ,p(5));

output :
b) The Distribution of the number of road Accident per day in a city is Poisson with Mean
4.Find the number of days out of 100 days that have

i. No Accident

ii. Atleast 2 Accident

iii..Atmost 3 Accident

iv. Between 2 and 5 Accident

CODE:

clc;

disp("Fitting of Poisson Distribution After computing mean");

l = 4;

printf("\nMean = %d\n",1);

N=100;

printf("\nTotal nummber of days = %d\n",N);

function y = p(x)

y = (exp(-l)*(l^x))/factorial(x);

endfunction

printf("1. Number of days that have no accidents = %f\n",*p(0));

printf("2. Number of days that have Atleast 2 Accidents=%f/\n",*(1-(p(0)+p(1))));

printf("3. Number of days that have Atmost 3 Accidents = %f\n",*(p(0)+p(1)+p(2)+p(3)));

printf("4. Number of days that have Between 2 and 5 Accidents %f\n",*(p(2)+p(3)+p(4)+p(5)));

Output :
PROGRAM NO.- 7

Aim: Program to plot Normal distribution and Exponential Distribution for various
parametric values.

a) A Normal Curve Have Mean, μ=20 and Varience σ=10t Find the area between x=26, x=38 and
Also between x=15, x=40.

CODE:

clc;

mean = 20;

variance = 100;

SD=sqrt(variance);

rp=sqrt(2* %pi);

printf("\n Progarm to plot Normal Distribution: \n");

printf("\n Mean = %d hours.\n", mean);

printf("\n Variance = %d\n", variance);

printf("\n Standard Deviation = %d hours.\n", SD);

function y=z(x)

y=(x-mean)/SD;

endfunction

function y=p(x)

y=exp(-(z(x)^2)/2)/(SD*rp);

endfunction

r1=0;

r2=0;

for i=26:38

r1=r1+p(i);

end

for j=15:40

r2=r2+p(j);
end

printf("\n Area under the curve: \n");

printf("\n 1.Between x=26 and x=38=%f\n",r1);

printf("\n 2.Between x=15 and x=40=%f\n",2);

x=linspace(-20,60,100);

plot(x,p);

OUTPUT:

Normal Distribution Graph:


b) A Power Supply unit for a computer component is considered to follow Exponential Distribution with
Mean life of 1200 hours. What is the probability that the component will fail in

i. First 300 hours

ii. Survived more than 1500 hours.

iii. Last between 1200 and 1500 hours.

CODE:
clc;

mean=1200;

lambda = 1/mean;

printf("\n Program to plot Exponetial Distribution:\n");

printf("\n Mean = %d \n",mean);

printf("\n Lambda =%f \n",lambda);

function y=f(x)

y = lambda*exp(-(lambda*x))

endfunction

r1=0;

r2=0;

r3=0;

for i=0:300

r1=r1+f(i);

end

for j=0:1500

r2=r2+f(j);

end

for u=1200:1500

r3=r3+f(u);

end
printf("\n Probability that the Computer Componenet: \n");

printf("\n 1.Will Fail in first 300 hours = %f\n",r1);

printf("\n 2. Will Survived More than 1500 hours =%f\n",r2);

printf("\n 3. Will last Between 1200 and 1500 hours =%f\n",r3);

x=linspace(0,10000,100);

plot(x,f(x));

OUTPUT:

Exponential Distribution Graph:


PROGRAM NO.- 8

Aim: Fitting of Normal distribution when parameters are given.


A sample of 100 dry cells are tested to find the length of life produce the following result μ = 12 hours
and σ = 3 hours. Assuming the data to be normally Distributed, what Percentage of cells are expected to
have life

i. More than 15 hours

ii. Less then 6 hours

iii. Between 10 and 14 hours

CODE:

clc;

N=100;

mean = 12;

SD=3;

rp=sqrt(2* %pi);

printf("\n Fitting of Normal Distribution When Parameters are given: \n");

printf("\n Mean = %d hours.\n", mean);

printf("\n Standard Deviation Nd hours.\n",SD);

function y=z(x)

y=(x-mean)/SD;

endfunction

function y=p(x)

y=exp(-(z(x)^2)/2)/(SD*rp);

endfunction

r1=0;

r2=0;

r3=0;

for i=0:15

r1=r1+p(i);

end
for j=0:6

r2=r2+p(j);

end

for u =10:14

r3=r3+p(u);

end

printf("\n Percentage of cells Expected to have life: \n");

printf("\n 1. More than 15 hours Nf \n",N*(1-r1));

printf("\n 2.Less than 6 hours = %f\n",N*r2);

printf("\n 3. Between 10 and 14 hours =%f\n",N*r3);

x=linspace(0,24,50);

plot(x,p);
OUTPUT:

Normal Distribution Graph:


PROGRAM NO.- 9

Aim: Fitting of Linear regression line through given data set.


X 2 4 6 8
Y 3 7 5 10

CODE:
clc;

x = [2 4 6 8];
y = [3 7 5 10];
x2 = x .* x; // Element-wise square of x
xy = x .* y; // Element-wise multiplication of x and y

n = length(x);

printf("\n Fitting of Linear Regression Line Through Given data set :\n");
disp("x = ", x);
disp("y = ", y);
disp("x2 = ", x2);
disp("xy = ", xy);
printf("\n Length, n = %d\n", n);

Ex = sum(x);
Ey = sum(y);
Ex2 = sum(x2);
Exy = sum(xy);

printf("\n Sigma x = %d\n", Ex);


printf("\n Sigma y = %d\n", Ey);
printf("\n Sigma x2 = %d\n", Ex2);
printf("\n Sigma xy = %d\n", Exy);

a = ((Ey * Ex2) - (Ex * Exy)) / ((n * Ex2) - (Ex^2));


b = ((n * Exy) - (Ex * Ey)) / ((n * Ex2) - (Ex^2));

printf("\n The Linear Equation is given by: Y = a + bX \n");


printf("\n After putting the values of a and b the Equation of Linear regression is:\n");
printf("\n y = %f + %f x \n", a, b);
OUTPUT:
PROGRAM NO.- 10

Aim: Fitting of Multiple Linear regression curve through given data set.
X 2 4 6 8
Y 3 7 5 10

CODE:
clc();

x = [2, 4, 6, 8];

y = [3, 7, 5, 10];

n = 4;

sigmax = sum(x);

sigmay = sum(y);

sigmaxy = sum(x .* y);

sigmax2y = sum((x .* x) .* y);

sigmax2 = sum(x .* x);

sigmax3 = sum(x.^3);

sigmax4 = sum(x.^4);

a = (n * sigmax2y - sigmax * sigmaxy) / (n * sigmax4 - sigmax2 * sigmax2);

b = (sigmax2 * sigmay - sigmax * sigmaxy) / (n * sigmax4 - sigmax2 * sigmax2);

c = (sigmay - a * sigmax2 - b * sigmax) / n;

printf("- Fitting of Quadratic Polynomial Regression -\n");

printf("\n n = %d\n", n);

printf("\n Σx = %.2f\n", sigmax);

printf("\n Σy = %.2f\n", sigmay);

printf("\n Σx^2 = %.2f\n", sigmax2);

printf("\n Σx^3 = %.2f\n", sigmax3);

printf("\n Σx^4 = %.2f\n", sigmax4);

printf("\n Σxy = %.2f\n", sigmaxy);

printf("\n Σx^2y = %.2f\n", sigmax2y);

printf("\n!!! Fitting Completed !!!\n");


printf("\n Fitted Quadratic Regression Polynomial: y = %.3fx^2 + %.3fx + %.3f\n", a, b, c);

OUTPUT :

You might also like