Skip to content

Commit fa40fc9

Browse files
committed
files added
0 parents  commit fa40fc9

35 files changed

+811
-0
lines changed

ECE514 Project Part 1 Instruction.pdf

95.4 KB
Binary file not shown.

ECE514 Project Part 2 Instruction.pdf

92.3 KB
Binary file not shown.

Report.docx

744 KB
Binary file not shown.
24 KB
Loading
23.7 KB
Loading

Results/Problem 1/normal_reject.png

37 KB
Loading

Results/Problem 1/normal_routine.png

51 KB
Loading

Results/Problem 1/uniform_reject.png

35 KB
Loading

Results/Problem 1/uniform_routine.png

49.1 KB
Loading

Results/Problem 2/e100.png

16.4 KB
Loading

Results/Problem 2/e1000.png

17.9 KB
Loading

Results/Problem 2/e10000.png

18.2 KB
Loading

Results/Problem 2/n100.png

14.9 KB
Loading

Results/Problem 2/n1000.png

17.4 KB
Loading

Results/Problem 2/n10000.png

17.3 KB
Loading

Results/Problem 2/u100.png

16.9 KB
Loading

Results/Problem 2/u1000.png

17.8 KB
Loading

Results/Problem 2/u10000.png

18.9 KB
Loading
102 KB
Loading
120 KB
Loading
126 KB
Loading

p1_exponential_routine_rejection.m

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
clc;
2+
clear;
3+
close all;
4+
5+
T1=100;
6+
T2=1000;
7+
T3=10000;
8+
9+
rng(42);
10+
%For Normal Random Variables with Mean=2 and Variance=2
11+
figure(1) % To plot Routine Method
12+
13+
e1 = exprnd((1/2),1,T1);
14+
subplot(2,2,1)
15+
h1=histogram(e1);
16+
me1 = mean(e1); ve1 = var(e1); se1 = std(e1);
17+
title("exp~(2)");
18+
xlabel("100 Samples (Routine Method)")
19+
ylabel("Frequency")
20+
%hold on;
21+
grid on;
22+
e2 = exprnd((1/2),1,T2);
23+
subplot(2,2,2)
24+
me2 = mean(e2); ve2 = var(e2); se2 = std(e2);
25+
h2=histogram(e2);
26+
grid on;
27+
title("exp~(2)")
28+
xlabel("1000 Samples (Routine Method)")
29+
ylabel("Frequency")
30+
e3= exprnd((1/2),1,T3);
31+
me3 = mean(e3); ve3 = var(e3); se3 = std(e3);
32+
subplot(2,2,3)
33+
h3=histogram(e3);
34+
grid on;
35+
title("exp~(2)")
36+
xlabel("10000 Samples (Routine Method)")
37+
ylabel("Frequency")
38+
39+
40+
41+
%Rejection Method
42+
figure(2); % To plot Rejection Method
43+
X1=rejection_exponential(T1);
44+
mx1 = mean(X1); vx1 = var(X1); sx1 = std(X1);
45+
disp("m1_rej" + mean(X1));
46+
disp("v1_rej" + var(X1))
47+
disp("s1_rej" + std(X1))
48+
subplot(2,2,1)
49+
X1=histogram(X1);
50+
grid on;
51+
title("exp~(2)")
52+
xlabel("100 Samples (Rejection Method)")
53+
ylabel("Frequency")
54+
X2=rejection_exponential(T2);
55+
mx2 = mean(X2); vx2 = var(X2); sx2 = std(X2);
56+
disp("m3_rej" + mean(X2));
57+
disp("v3_rej" + var(X2))
58+
disp("s3_rej" + std(X2))
59+
subplot(2,2,2)
60+
X2=histogram(X2);
61+
grid on;
62+
title("exp~(2)")
63+
xlabel("1000 Samples (Rejection Method)")
64+
ylabel("Frequency")
65+
X3=rejection_exponential(T3);
66+
mx3 = mean(X3); vx3 = var(X3); sx3 = std(X3);
67+
subplot(2,2,3)
68+
X3=histogram(X3);
69+
grid on;
70+
title("exp~(2)");
71+
xlabel("10000 Samples (Rejection Method)")
72+
ylabel("Frequency")
73+
74+
function [X] = rejection_exponential(N)
75+
c = 1;
76+
X = zeros(N,1); % Preallocate memory
77+
f=@(x)(2*exp(-2*x));
78+
for i = 1:N
79+
accept = false;
80+
while accept == false
81+
u = rand();
82+
v = 4.*rand();
83+
if c*u <= f(v)
84+
X(i) = v;
85+
accept = true;
86+
end
87+
end
88+
end
89+
end

p1_normal_routine_rejection.m

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
clc;
2+
clear;
3+
close all;
4+
5+
T1=100;
6+
T2=1000;
7+
T3=10000;
8+
9+
rng(7);
10+
11+
%For Normal Random Variables with Mean=2 and Variance=2
12+
figure(1) % To plot Routine Method
13+
14+
n1=normrnd(2,sqrt(2),[T1,1]);
15+
subplot(2,2,1)
16+
h1=histogram(n1);
17+
title("N~(2,2), Mean= " + mean(n1) + " Var= " + var(n1) + " Std = " + std(n1));
18+
xlabel("100 Samples (Routine Method)")
19+
ylabel("Frequency")
20+
%hold on;
21+
grid on;
22+
n2= normrnd(2,sqrt(2),[T2,1]);
23+
subplot(2,2,2)
24+
h2=histogram(n2);
25+
grid on;
26+
title("N~(2,2), Mean= " + mean(n2) + " Var= " + var(n2)+ " Std = " + std(n2))
27+
xlabel("1000 Samples (Routine Method)")
28+
ylabel("Frequency")
29+
n3=normrnd(2,sqrt(2),[T3,1]);
30+
subplot(2,2,3)
31+
h3=histogram(n3);
32+
grid on;
33+
title("N~(2,2),Mean= " + mean(n3) + "Var= " + var(n3)+ " Std = " + std(n3))
34+
xlabel("10000 Samples (Routine Method)")
35+
ylabel("Frequency")
36+
37+
38+
39+
%Rejection Method
40+
figure(2); % To plot Rejection Method
41+
X1=rejection_normal(T1);
42+
mx1 = mean(X1); vx1 = var(X1); sx1 = std(X1);
43+
disp("m1_rej" + mean(X1));
44+
disp("v1_rej" + var(X1))
45+
disp("s1_rej" + std(X1))
46+
subplot(2,2,1)
47+
X1=histogram(X1);
48+
grid on;
49+
title("N~(2,2),T = 100, m = "+ mx1 + " v = " +vx1+ " std = " + sx1)
50+
X2=rejection_normal(T2);
51+
mx2 = mean(X2); vx2 = var(X2); sx2 = std(X2);
52+
disp("m3_rej" + mean(X2));
53+
disp("v3_rej" + var(X2))
54+
disp("s3_rej" + std(X2))
55+
subplot(2,2,2)
56+
X2=histogram(X2);
57+
grid on;
58+
title("N~(2,2),T = 1000, m = "+ mx2 + " v = " +vx2+ " std = " + sx2)
59+
X3=rejection_normal(T3);
60+
mx3 = mean(X3); vx3 = var(X3); sx3 = std(X3);
61+
subplot(2,2,3)
62+
X3=histogram(X3);
63+
grid on;
64+
title("N~(2,2),T = 10000, m = "+ mx3 + " v = " +vx3+ " std = " + sx3);
65+
66+
67+
function [X] = rejection_normal(N)
68+
%a = -4;
69+
%b = 4;
70+
f=@(x)exp(-((x-2).^2)/4)*(1/(sqrt(2*pi)));
71+
cmax = 1/(2*sqrt(2*pi))*exp(-1/4);
72+
%cmax = 1/(sqrt(2*2*pi));
73+
X = zeros(1,N);
74+
for i = 1:N
75+
accept = false;
76+
while accept == false
77+
u = rand(); %fx
78+
v = 2+2.*randn(); %gx
79+
if cmax*u <= f(v)
80+
X(i) = v;
81+
accept = true;
82+
83+
end
84+
end
85+
end
86+
end
87+

p1_uniform_routine_rejection.m

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
clc;
2+
clear;
3+
close all;
4+
5+
T1=100;
6+
T2=1000;
7+
T3=10000;
8+
a = 2;
9+
b = 4;
10+
rng(42);
11+
12+
%For Uniform Random Variables with a=2 and b=4
13+
figure(1) % To plot Routine Method
14+
u1 = a + (b-a)*rand(1,T1);
15+
subplot(2,2,1)
16+
disp("m1 " + mean(u1))
17+
disp("v1 " + var(u1))
18+
disp("s1 " + std(u1))
19+
h1=histogram(u1);
20+
title("U~(2,4), Mean= " + mean(u1) + " Var= " + var(u1)+ " Std = " + std(u1));
21+
xlabel("100 Samples (Routine Method)")
22+
ylabel("Frequency")
23+
%hold on;
24+
grid on;
25+
u2 = a + (b-a)*rand(1,T2);
26+
subplot(2,2,2)
27+
disp("mean2: " + mean(u2))
28+
disp("var2: " + var(u2))
29+
disp("s3 " + std(u2))
30+
h2=histogram(u2);
31+
grid on;
32+
title("U~(2,4),Mean= " + mean(u2) + " Var= " + var(u2) + " Std = " + std(u2))
33+
xlabel("1000 Samples (Routine Method)")
34+
ylabel("Frequency")
35+
u3 = a + (b-a)*rand(1,T3);
36+
subplot(2,2,3)
37+
h3=histogram(u3);
38+
disp("m3 " +mean(u3));
39+
disp("v3 " + var(u3))
40+
disp("s3 " + std(u3))
41+
grid on;
42+
title("U~(2,4),Mean= " + mean(u3) + "Var= " + var(u3)+ " Std = " + std(u3))
43+
xlabel("10000 Samples (Routine Method)")
44+
ylabel("Frequency")
45+
46+
47+
48+
%Rejection Method
49+
figure(2); % To plot Rejection Method
50+
X1=rejection_uniform(T1);
51+
subplot(2,2,1)
52+
mx1 = mean(X1); vx1 = var(X1); sx1 = std(X1);
53+
X1=histogram(X1);
54+
grid on;
55+
title("U~(2,4),T = 100, m = "+ mx1 + " v = " +vx1+ " std = " + sx1)
56+
X2=rejection_uniform(T2);
57+
mx2 = mean(X2); vx2 = var(X2); sx2 = std(X2);
58+
subplot(2,2,2)
59+
X2=histogram(X2);
60+
grid on;
61+
title("U~(2,4),T = 1000, m = "+ mx2 + " v = " +vx2+ " std = " + sx2)
62+
X3=rejection_uniform(T3);
63+
mx3 = mean(X3); vx3 = var(X3); sx3 = std(X3);
64+
subplot(2,2,3)
65+
disp("X3" + mean(X3))
66+
X3=histogram(X3);
67+
grid on;
68+
title("U~(2,4),T = 10000, m = "+ mx3 + " v = " +vx3+ " std = " + sx3)
69+
70+
71+
function [X] = rejection_uniform(N)
72+
c = 0.5;
73+
X = zeros(N,1); % Preallocate memory
74+
for i = 1:N
75+
accept = false;
76+
while accept == false
77+
u = rand();
78+
v = unifrnd(2,4);
79+
if c*u <= 0.5
80+
X(i) = v;
81+
accept = true;
82+
end
83+
end
84+
end
85+
86+
end

p2_transform_exponential.m

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
clc;
2+
clear;
3+
close all;
4+
5+
T1 = 100;
6+
T2 = 1000;
7+
T3 = 10000;
8+
lambda = 2;
9+
10+
% For 100 samples
11+
for i = 1:length(T1)
12+
gx = zeros(T1(i),1);
13+
n = 1;
14+
while n <= T1(i)
15+
fx = exprnd(lambda,T1(i),1);
16+
gx(n) = sum(fx)/T1(i);
17+
n = n + 1;
18+
end
19+
figure(1);
20+
subplot(1,1,i)
21+
histogram(gx)
22+
title("TRANSFORMED EXPONENTIAL RANDOM VARIABLE", T1(i))
23+
xlabel("100 Samples");
24+
ylabel("Frequency");
25+
end
26+
27+
% 1000 Samples
28+
for i = 1:length(T2)
29+
gx = zeros(T2(i),1);
30+
n = 1;
31+
while n <= T2(i)
32+
fx = exprnd(lambda,T2(i),1);
33+
gx(n) = sum(fx)/T2(i);
34+
n = n + 1;
35+
end
36+
figure(2);
37+
subplot(1,1,i)
38+
histogram(gx)
39+
title("TRANSFORMED EXPONENTIAL RANDOM VARIABLE", T2(i))
40+
xlabel("1000 Samples");
41+
ylabel("Frequency");
42+
end
43+
44+
% 10000 samples
45+
for i = 1:length(T3)
46+
gx = zeros(T3(i),1);
47+
n = 1;
48+
while n <= T3(i)
49+
fx = exprnd(lambda,T3(i),1);
50+
gx(n) = sum(fx)/T3(i);
51+
n = n + 1;
52+
end
53+
figure(3);
54+
subplot(1,1,i)
55+
histogram(gx)
56+
title("TRANSFORMED EXPONENTIAL RANDOM VARIABLE", T3(i))
57+
xlabel("10000 Samples");
58+
ylabel("Frequency");
59+
end

p2_transform_normal.m

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
clc;
2+
clear;
3+
close all;
4+
5+
T1 = 100;
6+
T2 = 1000;
7+
T3 = 10000;
8+
lambda = 2;
9+
10+
% For 100 samples
11+
for i = 1:length(T1)
12+
gx = zeros(T1(i),1);
13+
n = 1;
14+
while n <= T1(i)
15+
fx = normrnd(2,2,[T1,1]);
16+
gx(n) = sum(fx)/T1(i);
17+
n = n + 1;
18+
end
19+
figure(1);
20+
subplot(1,1,i)
21+
histogram(gx)
22+
title("TRANSFORMED NORMAL RANDOM VARIABLE", T1(i))
23+
xlabel("100 Samples");
24+
ylabel("Frequency");
25+
end
26+
27+
% 1000 Samples
28+
for i = 1:length(T2)
29+
gx = zeros(T2(i),1);
30+
n = 1;
31+
while n <= T2(i)
32+
fx = normrnd(2,2,[T2,1]);
33+
gx(n) = sum(fx)/T2(i);
34+
n = n + 1;
35+
end
36+
figure(2);
37+
subplot(1,1,i)
38+
histogram(gx)
39+
title("TRANSFORMED NORMAL RANDOM VARIABLE", T2(i))
40+
xlabel("1000 Samples");
41+
ylabel("Frequency");
42+
end
43+
44+
% 10000 samples
45+
for i = 1:length(T3)
46+
gx = zeros(T3(i),1);
47+
n = 1;
48+
while n <= T3(i)
49+
fx = normrnd(2,2,[T3,1]);
50+
gx(n) = sum(fx)/T3(i);
51+
n = n + 1;
52+
end
53+
figure(3);
54+
subplot(1,1,i)
55+
histogram(gx)
56+
title("TRANSFORMED NORMAL RANDOM VARIABLE", T3(i))
57+
xlabel("10000 Samples");
58+
ylabel("Frequency");
59+
end

0 commit comments

Comments
 (0)