Skip to content

Commit

Permalink
Tests (trivial and nontrivial) for costFunction and gradient
Browse files Browse the repository at this point in the history
  • Loading branch information
cod3monk3y committed Mar 29, 2014
1 parent 1fc21db commit 7a69a14
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ex2/test_costfunction_minimal.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function test_costfunction ()
% an extremely simple cost function calculation with
% one negative and one positive class
X = [1 1; 2 2]; % minimal 2D feature matrix
y = [0 1]'; % at least one negative and one positive
theta = [0 0]'; % simplify calculations by using 0 for theta

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

assert(J, 0.69315, 1e-5);
assert(grad, [-1/4 -1/4]');

% NOTE: I don't think it's important for the cost calculation to
% set the first column to ones (FOR THIS TEST), but I may be incorrect
% in this assumption.

endfunction
21 changes: 21 additions & 0 deletions ex2/test_costfunction_nontrivial.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function test_costfunction_nontrivial ()
% simple two-entry matrix with n+1 features including
% the first column as ones-vector
X = [ 1 2 3; % non-trivial matrix
1 4 5 ];
y = [0 1]'; % one positive, one negative
theta = [.1 .2 .3]'; % non-trivial theta

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

% non-trivial cost
% ERROR if you get this result... you have something backwards
% assert(J, 1.3536, 1e-4 );

% the correct result
assert(J, 0.85363, 1e-4 );

% non-grivial gradient
grad_exp = [ 0.35951 0.63584 0.99534 ]';
assert(grad, grad_exp, 1e-4);
endfunction
5 changes: 5 additions & 0 deletions ex2/test_ex2.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@

%!test test_sigmoid()

%!test test_costfunction_minimal()

%!test test_costfunction_nontrivial()

% !test test_gradient()
7 changes: 7 additions & 0 deletions ex2/test_sigmoid.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,12 @@ function test_sigmoid ()
[0 0.5 1 ;
0.5 1 0 ;
1 0 0.5], 1e-6);


%
% Apurva Dubey
%
assert(sigmoid([1 2 3]), [0.73106 0.88080 0.95257], 1e-4);
assert(sigmoid([4 -2 -3]), [0.98201 0.119203 0.047426], 1e-4);

endfunction

0 comments on commit 7a69a14

Please sign in to comment.