LCOV - code coverage report
Current view: top level - source/tests - TestLogLogisticLoss.cpp (source / functions) Hit Total Coverage
Test: LibForBES Unit Tests Lines: 47 47 100.0 %
Date: 2016-04-18 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * File:   TestLogLogisticLoss.cpp
       3             :  * Author: Pantelis Sopasakis
       4             :  *
       5             :  * Created on Oct 29, 2015, 7:38:05 PM
       6             :  * 
       7             :  * ForBES is free software: you can redistribute it and/or modify
       8             :  * it under the terms of the GNU Lesser General Public License as published by
       9             :  * the Free Software Foundation, either version 3 of the License, or
      10             :  * (at your option) any later version.
      11             :  *  
      12             :  * ForBES is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      15             :  * GNU Lesser General Public License for more details.
      16             :  * 
      17             :  * You should have received a copy of the GNU Lesser General Public License
      18             :  * along with ForBES. If not, see <http://www.gnu.org/licenses/>.
      19             :  */
      20             : 
      21             : #include "TestLogLogisticLoss.h"
      22             : #include "LogLogisticLoss.h"
      23             : 
      24             : const static double DATA_X[] = {
      25             :         0.537667139546100,
      26             :         1.833885014595086,
      27             :         -2.258846861003648,
      28             :         0.862173320368121,
      29             :         0.318765239858981,
      30             :         -1.307688296305273,
      31             :         -0.433592022305684,
      32             :         0.342624466538650,
      33             :         3.578396939725760,
      34             :         2.769437029884877
      35             :     };
      36             : 
      37           1 : CPPUNIT_TEST_SUITE_REGISTRATION(TestLogLogisticLoss);
      38             : 
      39           1 : TestLogLogisticLoss::TestLogLogisticLoss() {
      40           1 : }
      41             : 
      42           2 : TestLogLogisticLoss::~TestLogLogisticLoss() {
      43           2 : }
      44             : 
      45           1 : void TestLogLogisticLoss::setUp() {
      46           1 : }
      47             : 
      48           1 : void TestLogLogisticLoss::tearDown() {
      49           1 : }
      50             : 
      51           1 : void TestLogLogisticLoss::testCall() {
      52           1 :     const double mu = 1.5;
      53           1 :     Function *logLogisticLoss = new LogLogisticLoss(mu);
      54           1 :     const size_t n = 10;
      55           1 :     const double * xdata = DATA_X;
      56             :     const double ddata[n] = {
      57             :         -0.013569096140251,
      58             :         -0.333434615170239,
      59             :         -0.431122888787024,
      60             :         0.407923343445659,
      61             :         0.404836067818807,
      62             :         -0.257685527804887,
      63             :         -0.057441551301617,
      64             :         -0.101942255039910,
      65             :         -0.226500118101661,
      66             :         0.490999700556483
      67           1 :     };
      68             : 
      69           1 :     Matrix x(n, 1, xdata);
      70           2 :     Matrix d(n, 1, ddata);
      71             : 
      72             :     double f;
      73           2 :     Matrix grad(n, 1);
      74           2 :     Matrix Hd(n, 1);
      75             : 
      76           1 :     const double tol = 1e-12;
      77           1 :     const double f_expected = 10.455339600285200;
      78             : 
      79             :     const double grad_expected_data[n] = {
      80             :         -0.553095647613005,
      81             :         -0.206664161288562,
      82             :         -1.358116380141062,
      83             :         -0.445328215242575,
      84             :         -0.631465046697517,
      85             :         -1.180689100643326,
      86             :         -0.910096624790372,
      87             :         -0.622758155942074,
      88             :         -0.040743067205105,
      89             :         -0.088497390570449
      90           1 :     };
      91             : 
      92             :     const double Hd_expected_data[n] = {
      93             :         -0.004737683170800,
      94             :         -0.059414973349659,
      95             :         -0.055383330523630,
      96             :         0.127727625721715,
      97             :         0.148021416496942,
      98             :         -0.064766147765657,
      99             :         -0.020559061512420,
     100             :         -0.037128015811522,
     101             :         -0.008977649776764,
     102             :         0.040888588516473,
     103           1 :     };
     104             : 
     105           2 :     Matrix grad_expected(n, 1, grad_expected_data);
     106           2 :     Matrix Hd_expected(n, 1, Hd_expected_data);
     107             : 
     108           1 :     _ASSERT(logLogisticLoss->category().defines_f());
     109           1 :     int status = logLogisticLoss->call(x, f, grad);
     110           1 :     _ASSERT_EQ(ForBESUtils::STATUS_OK, status);
     111           1 :     _ASSERT_NUM_EQ(f_expected, f, tol);
     112           1 :     _ASSERT_EQ(grad_expected, grad);
     113             : 
     114           1 :     status = logLogisticLoss->hessianProduct(x, d, Hd);
     115           1 :     _ASSERT_EQ(ForBESUtils::STATUS_OK, status);
     116           1 :     _ASSERT_EQ(Hd_expected, Hd);
     117             : 
     118             :     double f2;
     119           1 :     status = logLogisticLoss->call(x, f2);
     120           1 :     _ASSERT_EQ(ForBESUtils::STATUS_OK, status);
     121           1 :     _ASSERT_NUM_EQ(f, f2, tol);
     122             : 
     123           1 :     x.set(1, 0, 40);
     124           1 :     x.set(2, 0, 34);
     125             : 
     126             :     double f3;
     127           1 :     status = logLogisticLoss->call(x, f3);
     128           1 :     _ASSERT_EQ(ForBESUtils::STATUS_OK, status);
     129           1 :     _ASSERT_NUM_EQ(6.695659275314881, f3, tol);
     130             : 
     131             :     double f4;
     132           1 :     x.set(0, 0, -70);
     133           1 :     status = logLogisticLoss->call(x, f4);
     134           1 :     _ASSERT_EQ(ForBESUtils::STATUS_OK, status);
     135           1 :     _ASSERT_NUM_EQ(1.110056258258362e+02, f4, tol);
     136             : 
     137           2 :     delete logLogisticLoss;
     138             : 
     139           4 : }

Generated by: LCOV version 1.10