From 7a69a1424ca5c9140d78b515a1844fd0e7d4ca60 Mon Sep 17 00:00:00 2001 From: "Eric Borts (Lambda)" Date: Fri, 28 Mar 2014 18:33:18 -0600 Subject: [PATCH] Tests (trivial and nontrivial) for costFunction and gradient --- ex2/test_costfunction_minimal.m | 17 +++++++++++++++++ ex2/test_costfunction_nontrivial.m | 21 +++++++++++++++++++++ ex2/test_ex2.m | 5 +++++ ex2/test_sigmoid.m | 7 +++++++ 4 files changed, 50 insertions(+) create mode 100644 ex2/test_costfunction_minimal.m create mode 100644 ex2/test_costfunction_nontrivial.m diff --git a/ex2/test_costfunction_minimal.m b/ex2/test_costfunction_minimal.m new file mode 100644 index 0000000..81a258b --- /dev/null +++ b/ex2/test_costfunction_minimal.m @@ -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 diff --git a/ex2/test_costfunction_nontrivial.m b/ex2/test_costfunction_nontrivial.m new file mode 100644 index 0000000..2e0c5a6 --- /dev/null +++ b/ex2/test_costfunction_nontrivial.m @@ -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 diff --git a/ex2/test_ex2.m b/ex2/test_ex2.m index a274885..d7267e3 100644 --- a/ex2/test_ex2.m +++ b/ex2/test_ex2.m @@ -2,3 +2,8 @@ %!test test_sigmoid() +%!test test_costfunction_minimal() + +%!test test_costfunction_nontrivial() + +% !test test_gradient() \ No newline at end of file diff --git a/ex2/test_sigmoid.m b/ex2/test_sigmoid.m index d29e102..87dfccd 100644 --- a/ex2/test_sigmoid.m +++ b/ex2/test_sigmoid.m @@ -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