Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
36 views

Stochastic Systems Assignment: Table 3.1+3.2 Implementation On Matlab

Syed Hassaan Ahmed implemented probability mass functions, probability density functions, cumulative distribution functions, and key metrics like mean and variance for 10 common random variables in MATLAB. This included Bernoulli, binomial, geometric, negative binomial, Poisson, uniform, exponential, Gaussian, gamma, and Rayleigh distributions. Code examples were provided for the PDF, PMF, and CDF of each distribution using common parameters like p, n, λ, σ, and α. Key metrics like the expected value and variance were also calculated and compared to theoretical values from tables 3.1 and 3.2.

Uploaded by

Kashif Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Stochastic Systems Assignment: Table 3.1+3.2 Implementation On Matlab

Syed Hassaan Ahmed implemented probability mass functions, probability density functions, cumulative distribution functions, and key metrics like mean and variance for 10 common random variables in MATLAB. This included Bernoulli, binomial, geometric, negative binomial, Poisson, uniform, exponential, Gaussian, gamma, and Rayleigh distributions. Code examples were provided for the PDF, PMF, and CDF of each distribution using common parameters like p, n, λ, σ, and α. Key metrics like the expected value and variance were also calculated and compared to theoretical values from tables 3.1 and 3.2.

Uploaded by

Kashif Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Stochastic Systems Assignment

Title of Assignment

Table 3.1+3.2 implementation on Matlab

Submitted to:
Sir Dr. Salman
Submitted by:
Syed Hassaan Ahmed Bukhari
Reg # 7339
Ms-83/Electrical(control) Engineering

College of Engineering & Mechanical Engineering, NUST

Syed Hassaan Ahmed(7339)

Page 1

Question 1:

a.) Write Matlab code for PDF,CDF and PMF for Table 3.1
& 3,2?

Table 3.1:

1. Bernoulli Random Variable:

p=0:1
p_0=1-p %pmf=p for sucees=0
p_1=p
%pmf=p for sucees=1
Mean=mean(p_1)
Var=var(p_1)
%in table
E=p
VAR=p.*(1-p)
plot(p,p_1)

Syed Hassaan Ahmed(7339)

Page 2

1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

0.1

0.2

0.3

0.4

0.5

0.6

2.For Binomial Random Variable:


Here for p=0.2,
N=24
For PDF:
n=24;
p=0.2
for k=0:n
pmf=(nchoosek(24,k))*(p^k)*((1-p)^(n-k) )
hold on
stem(k,pmf)
hold off
end
MEAN=mean(pmf) % acc to Matlab command
VARiance=var(pmf) %acc to Matlab command
E=n*p %acc to table 3.1
Var=n*p*(1-p) %acc to table 3.1

Syed Hassaan Ahmed(7339)

Page 3

0.7

0.8

0.9

0.2
0.18
0.16
0.14
0.12
0.1
0.08
0.06
0.04
0.02
0

10

15

Here for p=0.5,


N=24
n=24;
p=0.5
for k=0:n
pmf=(nchoosek(24,k))*(p^k)*((1-p)^(n-k) )

hold on
stem(k,pmf)
hold off
end
MEAN=mean(pmf) % acc to Matlab command
VARiance=var(pmf) %acc to Matlab command
E=n*p %acc to table 3.1
Var=n*p*(1-p) %acc to table 3.1

Syed Hassaan Ahmed(7339)

Page 4

20

25

0.18
0.16
0.14
0.12
0.1
0.08
0.06
0.04
0.02
0

10

15

For CDF:
CDF=integration(PDF)

3. For Geometric Random Variable:

n=15;
p=0.5
%using Geometric R.V
n=15;
p=0.5
for k=1:n
pmf=(p)*((1-p)^(k-1) )

%P[M=k]=(1-p)^(k-1) *P

Syed Hassaan Ahmed(7339)

Page 5

20

25

hold on
stem(k,pmf)
hold off
end
MEAN=mean(pmf) % acc to Matlab command
VARiance=var(pmf) %acc to Matlab command
E=(1-p)/p %acc to table 3.1
Var=(1-p)/p^2 %acc to table 3.1

0.5
0.45
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0

10

n=15;
p=0.3
%using Geometric R.V
n=15;
p=0.3
for k=1:n
pmf=(p)*((1-p)^(k-1) )

%P[M=k]=(1-p)^(k-1) *P

hold on
stem(k,pmf)

Syed Hassaan Ahmed(7339)

Page 6

15

hold off
end
MEAN=mean(pmf) % acc to Matlab command
VARiance=var(pmf) %acc to Matlab command
E=(1-p)/p %acc to table 3.1
Var=(1-p)/p^2 %acc to table 3.1

0.35
0.3
0.25
0.2
0.15
0.1
0.05
0

10

For CDF:
n=15;
p=0.3
for k=1:n

pmf=(p)*((1-p)^(k-1) ) %P[M=k]=(1-p)^(k-1) *P
T=@(k) pmf;
CDF=integral(T,1,15,'ArrayValued',true)
hold on
stem(k,cdf)
hold off
end
MEAN=mean(pmf) % acc to Matlab command
VARiance=var(pmf) %acc to Matlab command
E=(1-p)/p %acc to table 3.1
Var=(1-p)/p^2 %acc to table 3.1

Syed Hassaan Ahmed(7339)

Page 7

15

4.5
4
3.5
3
2.5
2
1.5
1
0.5
0

10

15

4) For Negative Binomail Random Variable:


For PDF/PMF:
%USING nEGATIVE bINOMIAL r.v
n=10;
r=1;
p=0.5
for k=r:r+n
pmf=((factorial(k-1))/((factorial(r-1))*(factorial(k-r))))*((p^r)*((1-p)^(kr)))

hold on
stem(k,pmf)
hold off
end
MEAN=mean(pmf) % acc to Matlab command
VARiance=var(pmf) %acc to Matlab command
E=r/p %acc to table 3.1
Var=(r*(1-p))/p^2 %acc to table 3.1

Syed Hassaan Ahmed(7339)

Page 8

0.5
0.45
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0

10

11

For CDF:
%USING nEGATIVE bINOMIAL r.v
n=10;
r=1;
p=0.5
for k=r:r+n
pmf=((factorial(k-1))/((factorial(r-1))*(factorial(k-r))))*((p^r)*((1-p)^(kr)))
T=@(k) pmf;
CDF=integral(T,1,10,'ArrayValued',true)
hold on
stem(k,CDF)
hold off
end
MEAN=mean(pmf) % acc to Matlab command
VARiance=var(pmf) %acc to Matlab command
E=r/p %acc to table 3.1
Var=(r*(1-p))/p^2 %acc to table 3.1

Syed Hassaan Ahmed(7339)

Page 9

5) For poisson Random Variable:


FOR PDF/PMF:
At Alfa=0.75
n=24;
p=0.5
e=2.713
alfa=0.75
for k=0:n
pmf=(alfa^k/factorial(k))*e^-alfa
hold on
stem(k,pmf)
hold off
end
MEAN=mean(pmf) % acc to Matlab command
VARiance=var(pmf) %acc to Matlab command
E=alfa %acc to table 3.1
Var=alfa %acc to table 3.1
0.5
0.45
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0

10

15

At alfa=n*p=24*0.5=12
Syed Hassaan Ahmed(7339)

Page 10

20

25

n=24;
p=0.5
e=2.713
alfa=n*p
for k=0:n
pmf=(alfa^k/factorial(k))*e^-alfa
hold on
stem(k,pmf)
hold off
end
MEAN=mean(pmf) % acc to Matlab command
VARiance=var(pmf) %acc to Matlab command
E=alfa %acc to table 3.1
Var=alfa %acc to table 3.1
0.12

0.1

0.08

0.06

0.04

0.02

10

15

FOR CDF:
At Alfa=0.75
n=24;
p=0.5
e=2.713
alfa=0.75
for k=0:n

Syed Hassaan Ahmed(7339)

Page 11

20

25

pmf=(alfa^k/factorial(k))*e^-alfa
T=@(k) pmf;
CDF=integral(T,1,24,'ArrayValued',true)
hold on
stem(k,CDF)
hold off
end
MEAN=mean(pmf) % acc to Matlab command
VARiance=var(pmf) %acc to Matlab command
E=alfa %acc to table 3.1
Var=alfa %acc to table 3.1

TABLE 3.2:
6) For Uniform Random Variable:
For PDF:

a=1
b=10
for x=a:b
cdf=(x-a)./(b-a) ;
pdf=polyder(cdf) %as pdf=differentiation(cdf)
hold on
subplot(2,1,1)
plot(x,cdf,'b')
subplot(2,1,2)
plot(x,pdf,'b')
hold off
end
MEAN=mean(cdf) % acc to Matlab command
VARiance=var(cdf) %acc to Matlab command
E=(a+b)/2 %acc to table 3.1
Var=(b-a)^2/12 %acc to table 3.1

Syed Hassaan Ahmed(7339)

Page 12

1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

For CDF:

a=1
b=10
for x=a:b
cdf=(x-a)./(b-a) ;
pdf=polyder(cdf) %as pdf=differentiation(cdf)
T=@(x) pdf;
CDF=integral(T,1,10,'ArrayValued',true)
hold on
subplot(2,1,1)
plot(x,cdf,'b')
subplot(2,1,2)
plot(x,pdf,'b')
hold off
end
MEAN=mean(cdf) % acc to Matlab command
VARiance=var(cdf) %acc to Matlab command
E=(a+b)/2 %acc to table 3.1
Var=(b-a)^2/12 %acc to table 3.1

Syed Hassaan Ahmed(7339)

Page 13

10

7) For Exponential Random Variable:


FOR PDF:
n=15
p=0.4
alfa=n*p
T=10
%T=time in seconds
lameda=alfa/T
for x=0:n
pdf=lameda*exp(-lameda.*x)
hold on
plot(x,pdf)
hold off
end
MEAN=mean(pdf) % acc to Matlab command
VARiance=var(pdf) %acc to Matlab command
E=1/lameda %acc to table 3.1
Var=1/lameda^2 %acc to table 3.1

0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

Syed Hassaan Ahmed(7339)

10

15

Page 14

20

25

FOR CDF:
n=15
p=0.4
alfa=n*p
T=10
%T=time in seconds
lameda=alfa/T
for x=0:n
pdf=lameda*exp(-lameda.*x)
T=@(x) pdf;
CDF=integral(T,0,15,'ArrayValued',true)
hold on
plot(x,pdf)
hold off
end
MEAN=mean(pdf) % acc to Matlab command
VARiance=var(pdf) %acc to Matlab command
E=1/lameda %acc to table 3.1
Var=1/lameda^2 %acc to table 3.1

8. For Guassian Random Variable:


FOR PDF:
%For Guassian Random Variable
sigma=0.5
m=5;
for x=0:10
pdf=(exp(-(x-m)^2)/(2*sigma^2))./((sqrt(2*pi))*sigma)

Syed Hassaan Ahmed(7339)

Page 15

hold on
plot(x,pdf)
hold off
end
MEAN=mean(pdf) % acc to Matlab command
VARiance=var(pdf) %acc to Matlab command
E=m %acc to table 3.1
Var=sigma.^2 %acc to table 3.1

For Sigma=0.4;0.5;0.6
1.6
1.4
1.2
1
0.8
0.6
0.4
0.2
0

FOR CDF:
%For Guassian Random Variable
sigma=0.5
m=5;
for x=0:10
pdf=(exp(-(x-m)^2)/(2*sigma^2))./((sqrt(2*pi))*sigma)
T=@(x) pdf;
CDF=integral(T,0,10,'ArrayValued',true)

hold on
plot(x,pdf)

Syed Hassaan Ahmed(7339)

Page 16

10

hold off
end
MEAN=mean(pdf) % acc to Matlab command
VARiance=var(pdf) %acc to Matlab command
E=m %acc to table 3.1
Var=sigma.^2 %acc to table 3.1

9. For Gamma Random Variable:


FOR PDF:
alfa=0.5
m=0;
lameda=1;
z=0.5;
for x=0:10
fun=@(x) x.^(z-1).*exp(-x);
T=integral(fun,0,10)

%T=gamma function

pdf=(lameda.*(lameda.*x).^(alfa-1)).*(exp(-lameda.*x))./T
hold on
plot(x,pdf)
hold off
end
MEAN=mean(pdf) % acc to Matlab command
VARiance=var(pdf) %acc to Matlab command
E=alfa/lameda %acc to table 3.1
Var=alfa/lameda.^2 %acc to table 3.1

For Alfa=0.5;1;2 and Lameda=1:

Syed Hassaan Ahmed(7339)

Page 17

0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

FOR CDF:
alfa=0.5
m=0;
lameda=1;
z=0.5;
for x=0:10
fun=@(x) x.^(z-1).*exp(-x);
T=integral(fun,0,10)

%T=gamma function

pdf=(lameda.*(lameda.*x).^(alfa-1)).*(exp(-lameda.*x))./T
T=@(x) pdf;
CDF=integral(T,0,10,'ArrayValued',true)
hold on
plot(x,pdf)
hold off
end
MEAN=mean(pdf) % acc to Matlab command
VARiance=var(pdf) %acc to Matlab command
E=alfa/lameda %acc to table 3.1
Var=alfa/lameda.^2 %acc to table 3.1

Syed Hassaan Ahmed(7339)

Page 18

10

10. For Rayleigh Random Variable:


FOR PDF:

%For Guassian Random Variable


sigma=0.5
alfa=1;
for x=0:10
pdf=(x./alfa.^2).*(exp((-x^2)./(2*sigma.^2)))
hold on
plot(x,pdf)
hold off
end
MEAN=mean(pdf) % acc to Matlab command
VARiance=var(pdf) %acc to Matlab command
E=alfa.*(sqrt(pi./2)) %acc to table 3.1
Var=(2-pi/2).*alfa.^2 %acc to table 3.1

For Alfa=1;0.7;0.5
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

Syed Hassaan Ahmed(7339)

Page 19

10

FOR CDF:
%For Guassian Random Variable
sigma=0.5
alfa=1;
for x=0:10
pdf=(x./alfa.^2).*(exp((-x^2)./(2*sigma.^2)))
T=@(x) pdf;
CDF=integral(T,0,10,'ArrayValued',true)

hold on
plot(x,pdf)
hold off
end
MEAN=mean(pdf) % acc to Matlab command
VARiance=var(pdf) %acc to Matlab command
E=alfa.*(sqrt(pi./2)) %acc to table 3.1
Var=(2-pi/2).*alfa.^2 %acc to table 3.1

11. For Cauchy Random Variable:


For PDF:
%For Cauchhy Random Variable
alfa=5;
for x=0:10
pdf=(alfa./pi)./((x.^2)+(alfa.^2))
hold on
plot(x,pdf)
hold off
end
MEAN=mean(pdf) % acc to Matlab command
VARiance=var(pdf) %acc to Matlab command

Syed Hassaan Ahmed(7339)

Page 20

0.07

0.06

0.05

0.04

0.03

0.02

0.01

For CDF:
%For Cauchhy Random Variable
alfa=5;
for x=0:10
pdf=(alfa./pi)./((x.^2)+(alfa.^2))
T=@(x) pdf;
CDF=integral(T,0,10,'ArrayValued',true)
hold on
plot(x,pdf)
hold off
end
MEAN=mean(pdf) % acc to Matlab command
VARiance=var(pdf) %acc to Matlab command

12.For Laplacian Random Variable:


FOR PDF:
%For Cauchhy Random Variable
alfa=5;
for x=0:10

Syed Hassaan Ahmed(7339)

Page 21

10

pdf=(alfa./2).*(exp(-alfa.*x))
hold on
plot(x,pdf)
hold off
end
MEAN=mean(pdf) % acc to Matlab command
VARiance=var(pdf) %acc to Matlab command
E=0 %acc to table 3.2
Var=2/alfa.^2 %acc to table 3.2

2.5

1.5

0.5

FOR CDF:
%For Cauchhy Random Variable
alfa=5;
for x=0:10
pdf=(alfa./2).*(exp(-alfa.*x))
T=@(x) pdf;
CDF=integral(T,0,10,'ArrayValued',true)
hold on
plot(x,pdf)
hold off

Syed Hassaan Ahmed(7339)

Page 22

10

end
MEAN=mean(pdf) % acc to Matlab command
VARiance=var(pdf) %acc to Matlab command
E=0 %acc to table 3.2
Var=2/alfa.^2 %acc to table 3.2

b.) Write Matlab code for Discarded Fraction using


Manual Method ?

Syed Hassaan Ahmed(7339)

Page 23

You might also like