Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added: Ex5 unit tests #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ex5/test_ex5.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
%!test test_sanity()

% https://class.coursera.org/ml-005/forum/thread?thread_id=2137

% 1 / 5 Regularized Linear Regression Cost Function
%!test test_linearRegCostFunction_cost()

% 2 / 5 Regularized Linear Regression Gradient
%!test test_linearRegCostFunction_grad()

% 3 / 5 Learning Curve
%!test test_learningCurve()

% 4 / 5 Polynomial Feature Mapping
%!test test_polyFeatures()

% 5 / 5 Cross Validation Curve
%!test test_validationCurve()
32 changes: 32 additions & 0 deletions ex5/test_learningCurve.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function test_learningCurve()
% 3 / 5 Learning Curve
epsilon = 1e-3;

[error_train error_val] = learningCurve([ones(10,1) sec(1:1.5:15)' tan(1:1.5:15)'], cot(1:3:30)', ...
[1 23.5 12.4; 1 64.3 10.1; 1 76.4 9.8; 1 34.2 15.2; 1 59.5 13.5], [13;24;53;34;23], 0.1);

et = [0.00000
0.00000
0.00003
0.03773
0.12896
0.29892
1.59607
341.18606
331.68731
325.76182];
ev = [5.0523e+02
5.9170e+02
7.5200e+02
5.3129e+02
3.4625e+02
7.3199e+02
1.6288e+03
7.3256e+04
4.9892e+04
1.12171e+05];

assert(error_train, et, epsilon);
assert(error_val/1000, ev/1000, epsilon);

endfunction
16 changes: 16 additions & 0 deletions ex5/test_linearRegCostFunction_cost.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function test_linearRegCostFunction_cost()
% 1 / 5 Regularized Linear Regression Cost Function
epsilon = 1e-4;

X = [1 2; 1 3; 1 4; 1 5; 1 6];
y = [7; 6; 4; 3; 2];
theta = [1; 1];
J = linearRegCostFunction(X, y, theta, 1);
assert(J, 5.6, epsilon);

% https://class.coursera.org/ml-005/forum/thread?thread_id=2137
[J] = linearRegCostFunction([ones(10,1) sec(1:1.5:15)' tan(1:1.5:15)'], cot(1:3:30)', ...
[0.15 0.25 0.35]', 0.1);
assert(J, 644.5304, epsilon);

endfunction
14 changes: 14 additions & 0 deletions ex5/test_linearRegCostFunction_grad.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function test_linearRegCostFunction_grad()
% 2 / 5 Regularized Linear Regression Gradient
epsilon = 1e-4;

[J grad] = linearRegCostFunction([1 2; 1 3; 1 4; 1 5; 1 6], [7; 6; 4; 3; 2], [1; 1], 1);
assert(J, 5.60, epsilon);
assert(grad, [0.60; 7.20], epsilon);

[J grad] = linearRegCostFunction([1 23.5 12.4; 1 64.3 10.1; 1 76.4 9.8; 1 34.2 15.2; 1 59.5 13.5], ...
[13;24;53;34;23], [3.2;2.3;1.2], 1.2);
assert(J, 6396.44119, epsilon);
assert(grad, [107.074; 6210.2529; 1258.7064], epsilon);

endfunction
10 changes: 10 additions & 0 deletions ex5/test_polyFeatures.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function test_polyFeatures()
% 4 / 5 Polynomial Feature Mapping
epsilon = 1e-3;
Xpoly = polyFeatures([2; 5; 7], 4);
a = [2 4 8 16
5 25 125 625
7 49 343 2401];
assert(Xpoly, a, epsilon);

endfunction
4 changes: 4 additions & 0 deletions ex5/test_sanity.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function test_sanity ()
% make sure tests are running correctly
assert(1,1);
endfunction
43 changes: 43 additions & 0 deletions ex5/test_validationCurve.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function test_validationCurve()
% 5 / 5 Cross Validation Curve
epsilon = 1e-3;

[lambda_vec error_train error_val] = validationCurve([ones(10,1) sec(1:1.5:15)' tan(1:1.5:15)'], cot(1:3:30)', ...
[1 23.5 12.4; 1 64.3 10.1; 1 76.4 9.8; 1 34.2 15.2; 1 59.5 13.5], [13;24;53;34;23]);

lv = [0.00000
0.00100
0.00300
0.01000
0.03000
0.10000
0.30000
1.00000
3.00000
10.00000];
etr = [325.751
325.751
325.751
325.751
325.752
325.762
325.847
326.723
332.616
364.812];
ecv = [1.1340e+05
1.1339e+05
1.1336e+05
1.1327e+05
1.1303e+05
1.1217e+05
1.0978e+05
1.0202e+05
8.4041e+04
4.8477e+04];

assert(lambda_vec, lv, epsilon);
assert(error_train, etr, epsilon);
assert(error_val/10000, ecv/10000, epsilon);

endfunction