-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lab2.m
253 lines (218 loc) · 7.17 KB
/
Lab2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
% Model Estimation and Discriminant Functions
% SYDE 372 - Lab 2
% Ali Akram 20526098
% Presish Bhattachan 20553154
% Chenlei Shen 20457272
% Timothy Tsang 20556306
%% Section 2 - Model Estimation 1D
% Load data for Section 2
load('N:\SYDE372-Labs\SYDE372-Lab2\lab2_1.mat')
% Setup variables
a_minVal = min(a);
a_maxVal = max(a);
b_minVal = min(b);
b_maxVal = max(b);
x_a = a_minVal:0.01:a_maxVal;
x_b = b_minVal:0.01:b_maxVal;
a_mean = 5;
a_sd = 1;
b_lambda = 1;
%%
% Section 2 - Part 1
% 1) Parametric Estimation - Gaussian
% Set A
[a_gauMean_1D, a_gauVar_1D ] = gaussianParamEstimation_1D(a);
a_normGaussian = normalGaussianDistributionPDF(a_gauMean_1D, a_gauVar_1D, x_a);
a_trueNormGaussian = normalGaussianDistributionPDF(a_mean, a_sd, x_a);
figure;
title('Parametric Estimation - Gaussian for Set A (1D)');
box on
hold on;
plot(x_a, a_normGaussian);
hold on;
plot(x_a, a_trueNormGaussian, 'Color', 'r');
hold on;
legend('Estimated p(x)','True p(x)');
xlabel('x');
ylabel('p(x)');
% Set B
[b_gauMean_1D, b_gauVar_1D ] = gaussianParamEstimation_1D(b);
b_normGaussian = normalGaussianDistributionPDF(b_gauMean_1D, b_gauVar_1D, x_b);
b_trueNormGaussian = normalExponentialDistributionPDF(b_lambda, x_b);
figure;
title('Parametric Estimation - Gaussian for Set B (1D)');
box on
hold on;
plot(x_b, b_normGaussian);
hold on;
plot(x_b, b_trueNormGaussian, 'Color', 'r');
hold on;
legend('Estimated p(x)','True p(x)');
xlabel('x');
ylabel('p(x)');
%%
% Section 2 - Part 2
% 2) Parametric Estimation - Exponential
% Set A
[a_expLambda_1D] = exponentialParamEstimation_1D(a);
a_normExponential = normalExponentialDistributionPDF(a_expLambda_1D, x_a);
a_trueNormExponential = normalGaussianDistributionPDF(a_mean, a_sd, x_a);
figure;
title('Parametric Estimation - Exponential for Set A (1D)');
box on
hold on;
plot(x_a, a_normExponential);
hold on;
plot(x_a, a_trueNormExponential, 'Color', 'r');
hold on;
legend('Estimated p(x)','True p(x)');
xlabel('x');
ylabel('p(x)');
% Set B
[b_expLambda_1D] = exponentialParamEstimation_1D(b);
b_normExponential = normalExponentialDistributionPDF(b_expLambda_1D, x_b);
b_trueNormExponential = normalExponentialDistributionPDF(b_lambda, x_b);
figure;
title('Parametric Estimation - Exponential for Set B (1D)');
box on
hold on;
plot(x_b, b_normExponential);
hold on;
plot(x_b, b_trueNormExponential, 'Color', 'r');
hold on;
legend('Estimated p(x)','True p(x)');
xlabel('x');
ylabel('p(x)');
%%
% Section 2 - Part 3
% 3) Parametric Estimation - Uniform
% Set A
[a_uniA_1D, a_uniB_1D] = uniformParamEstimation_1D(a);
a_normUniform = normalUniformDistributionPDF(a_uniA_1D, a_uniB_1D, x_a);
a_trueNormUniform = normalGaussianDistributionPDF(a_mean, a_sd, x_a);
figure;
title('Parametric Estimation - Uniform for Set A (1D)');
box on
hold on;
plot(x_a, a_normUniform);
hold on;
plot(x_a, a_trueNormUniform, 'Color', 'r');
hold on;
legend('Estimated p(x)','True p(x)');
xlabel('x');
ylabel('p(x)');
% Set B
[b_uniA_1D, b_uniB_1D] = uniformParamEstimation_1D(b);
b_normUniform = normalUniformDistributionPDF(b_uniA_1D, b_uniB_1D, x_b);
b_trueNormUniform = normalExponentialDistributionPDF(b_lambda, x_b);
figure;
title('Parametric Estimation - Uniform for Set B (1D)');
box on
hold on;
plot(x_b, b_normUniform);
hold on;
plot(x_b, b_trueNormUniform, 'Color', 'r');
hold on;
legend('Estimated p(x)','True p(x)');
xlabel('x');
ylabel('p(x)');
%%
% Section 2 - Part 4
% 4) Non-Parametric Estimation
% For Gaussian Windows, assuming an h = 1
sd_1 = 0.1;
sd_2 = 0.4;
% Set A
[a_parzanDensity1_1D] = parzanWindowEstimation_1D(a,sd_1);
[a_parzanDensity2_1D] = parzanWindowEstimation_1D(a,sd_2);
a_trueNormUniform = normalGaussianDistributionPDF(a_mean, a_sd, x_a);
% Set B
[b_parzanDensity1_1D] = parzanWindowEstimation_1D(b,sd_1);
[b_parzanDensity2_1D] = parzanWindowEstimation_1D(b,sd_2);
b_trueNormUniform = normalExponentialDistributionPDF(b_lambda, x_b);
% Plot for SD = 0.1
figure;
title('Non Parametric Estimation - Parzan Method for SD = 0.1 (1D)');
box on
hold on;
plot(x_a, a_parzanDensity1_1D);
hold on;
plot(x_b, b_parzanDensity1_1D);
hold on;
plot(x_a, a_trueNormUniform, 'Color', 'g');
hold on;
plot(x_b, b_trueNormUniform, 'Color', 'black');
hold on;
legend('Parzan for set A with SD = 0.1','Parzan for set A with SD = 0.1','True p(x) for Set A', 'True p(x) for Set B');
xlabel('x');
ylabel('p(x)');
% Plot for SD = 0.4
figure;
title('Non Parametric Estimation - Parzan Method for SD = 0.4 (1D)');
box on
hold on;
plot(x_a, a_parzanDensity2_1D);
hold on;
plot(x_b, b_parzanDensity2_1D);
hold on;
plot(x_a, a_trueNormUniform, 'Color', 'g');
hold on;
plot(x_b, b_trueNormUniform, 'Color', 'black');
hold on;
legend('Parzan for set A with SD = 0.4','Parzan for set A with SD = 0.4','True p(x) for Set A', 'True p(x) for Set B');
xlabel('x');
ylabel('p(x)');
%% Section 3 - Model Estimation 2-D Case
% Load data for Section 3
load('N:\SYDE372-Labs\SYDE372-Lab2\lab2_2.mat')
%% Section 3 - Part 1
% First get the covarience and mean of the values
min_x = min([al(:,1);bl(:,1); cl(:,1)])-1;
max_x = max([al(:,1);bl(:,1); cl(:,1)])+1;
min_y = min([al(:,2);bl(:,2); cl(:,2)])-1;
max_y = max([al(:,2);bl(:,2); cl(:,2)])+1;
x = min_x:1:max_x;
y = min_y:1:max_y;
[x1, y1] = meshgrid(x, y);
% Find means for data set A, B, C
mean_al = [mean(al(:,1)); mean(al(:,2))];
mean_bl = [mean(bl(:,1)); mean(bl(:,2))];
mean_cl = [mean(cl(:,1)); mean(cl(:,2))];
% Find covariances for data set A, B, C
cov_al = getCov(al,mean_al);
cov_bl = getCov(bl,mean_bl);
cov_cl = getCov(cl,mean_cl);
% Now will use the testing data to get the rest of the components
Class_a_b_ML = getMap(cov_al,cov_bl,mean_al',mean_bl',x1,y1);
Class_a_c_ML = getMap(cov_al,cov_cl,mean_al',mean_cl',x1,y1);
Class_b_c_ML = getMap(cov_bl,cov_cl,mean_bl',mean_cl',x1,y1);
% Plot
Mesh_Grid_Plot_1 = mapClassifyMulticlass(x1,y1,Class_a_b_ML,Class_a_c_ML,Class_b_c_ML);
ParametricPlot2d(x1, y1, Mesh_Grid_Plot_1, at, bt, ct);
%% Section 3 - Part 2
% 4) Non-Parametric Estimation
% Setup
precision = 500;
variance = 400;
% Make Window Function
win = makeGaussianWindow(-200, -200, 200, 200, variance, precision);
res = [1 min_x min_y max_x max_y];
% 2D Parzan Window Estimation for A, B, C
[al_pdf, al_x, al_y] = parzenWindowEstimation_2D( al, res, win );
[bl_pdf, bl_x, bl_y] = parzenWindowEstimation_2D( bl, res, win );
[cl_pdf, cl_x, cl_y] = parzenWindowEstimation_2D( cl, res, win );
% Plot
Mesh_Grid_Plot_2 = mlNonParametric(al_pdf, bl_pdf, cl_pdf);
NonparametricPlot2d(al_x, al_y, Mesh_Grid_Plot_2, at, bt, ct);
%% Section 4 - Sequential Discriminants
% Load data for Section 4
load('N:\SYDE372-Labs\SYDE372-Lab2\lab2_3.mat')
% Mesh Grid for MED Classification
x1 = linspace(min([a(:,1);b(:,1)]), max([a(:,1);b(:,1)]), 100);
y1 = linspace(min([a(:,2);b(:,2)]), max([a(:,2);b(:,2)]), 100);
% Deliverable 1: Learn 3 sequential classifiers
performSequentialEstimation(x1,y1,a,b,1,1);
performSequentialEstimation(x1,y1,a,b,1,2);
performSequentialEstimation(x1,y1,a,b,1,3);
% Deliverable 3: J = 5 with 20 calculations
performSequentialEstimation(x1,y1,a,b,5,0);