Skip to content

Commit

Permalink
Merge pull request #1 from etjones/master
Browse files Browse the repository at this point in the history
Added test_costFunctionReg to ex2 tests
  • Loading branch information
cod3monk3y committed Mar 29, 2014
2 parents c025989 + a9747f6 commit 71a22e5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
32 changes: 32 additions & 0 deletions ex2/test_costFunctionReg.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function test_costFunctionReg ()
tolerance = 1e-3;
% Test 1, w/ lambda = 3
% From TA Colin Beckingham, at https://class.coursera.org/ml-005/forum/thread?thread_id=943#post-4593
theta = [2; 1; -9];
X = magic(3);
y = [1; -0.2; 3];
lambda = 3;

J_exp = 50.971;
grad_exp = [-6.1081 -7.1932 -12.3540];

[J, grad] = costFunctionReg(theta, X, y, lambda);

assert(J, J_exp, tolerance );
assert(grad, grad_exp, tolerance);

% Test 2, w/ lambda = 0.1
% From TA Colin Beckingham, at https://class.coursera.org/ml-005/forum/thread?thread_id=943#post-4666
theta = [2; 1; -9];
X = magic(3);
y = [1; -0.2; 3];
lambda = 0.1;
J_exp_2 = 11.338;
grad_exp_2 = [-6.1081 -8.1598 -3.6540];

[J, grad] = costFunctionReg(theta, X, y, lambda);

assert(J, J_exp_2, tolerance );
assert(grad, grad_exp_2, tolerance);

endfunction
4 changes: 3 additions & 1 deletion ex2/test_ex2.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@

%!test test_costfunction_nontrivial()

%!test test_predict()
%!test test_predict()

%!test test_costFunctionReg()
16 changes: 8 additions & 8 deletions ex2/test_predict.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ function test_predict ()
1 1];

% all these should pass
assert(predict([1 1]', X), logical([1 1 1 1]'));
assert(predict([0 0]', X), logical([1 1 1 1]'));
assert(predict([1 1]', X), [1 1 1 1]');
assert(predict([0 0]', X), [1 1 1 1]');

% fail ... according to some math...
assert(predict([-1 -1]', X), logical([1 0 0 0]'));
assert(predict([-1 0]', X), logical([1 1 0 0]'));
assert(predict([0 -1]', X), logical([1 0 1 0]'));
assert(predict([-1 -1]', X), logical([1 0 0 0]'));
assert(predict([-1 -1]', X), [1 0 0 0]');
assert(predict([-1 0]', X), [1 1 0 0]');
assert(predict([0 -1]', X), [1 0 1 0]');
assert(predict([-1 -1]', X), [1 0 0 0]');

% fail or pass ... based on the same math as above (which
% is intentionally not shared)
assert(predict([1.1 -1]', X), logical([1 0 1 1]'));
assert(predict([-1 1.1]', X), logical([1 1 0 1]'));
assert(predict([1.1 -1]', X), [1 0 1 1]');
assert(predict([-1 1.1]', X), [1 1 0 1]');

endfunction

0 comments on commit 71a22e5

Please sign in to comment.