src/mlpack/tests/logistic_regression_test.cpp
BOOST_AUTO_TEST_CASE(LogisticRegressionFunctionRandomEvaluate)
{
  const size_t points = 1000;
  const size_t dimension = 10;
  const size_t trials = 50;

  // Create a random dataset.
  arma::mat data;
  data.randu(dimension, points);
  // Create random responses.
  arma::Row<size_t> responses(points);
  for (size_t i = 0; i < points; ++i)
    responses[i] = math::RandInt(0, 2);

  LogisticRegressionFunction<> lrf(data, responses,
      0.0 /* no regularization */);

  // Run a bunch of trials.
  for (size_t i = 0; i < trials; ++i)
  {
    // Generate a random set of parameters.
    arma::rowvec parameters;
    parameters.randu(dimension + 1);

    // Hand-calculate the loss function.
    double loglikelihood = 0.0;
    for (size_t j = 0; j < points; ++j)
    {
      const double sigmoid = (1.0 / (1.0 + exp(-parameters[0] -
          arma::dot(data.col(j), parameters.subvec(1, dimension)))));
      if (responses[j] == 1.0)
        loglikelihood += log(std::pow(sigmoid, responses[j]));
      else
        loglikelihood += log(std::pow(1.0 - sigmoid, 1.0 - responses[j]));
    }

    BOOST_REQUIRE_CLOSE(lrf.Evaluate(parameters), -loglikelihood, 1e-5);
  }
}
/**
 * A more complicated test for the LogisticRegressionFunction.
 */
A more complicated test for the LogisticRegressionFunction.
A more complicated test for the LogisticRegressionFunction .
BOOST_AUTO_TEST_CASE(LogisticRegressionFunctionRandomEvaluate)
{
  const size_t points = 1000;
  const size_t dimension = 10;
  const size_t trials = 50;
  arma::mat data;
  data.randu(dimension, points);
  arma::Row<size_t> responses(points);
  for (size_t i = 0; i < points; ++i)
    responses[i] = math::RandInt(0, 2);
  LogisticRegressionFunction<> lrf(data, responses,
      0.0 );
  for (size_t i = 0; i < trials; ++i)
  {
    arma::rowvec parameters;
    parameters.randu(dimension + 1);
    double loglikelihood = 0.0;
    for (size_t j = 0; j < points; ++j)
    {
      const double sigmoid = (1.0 / (1.0 + exp(-parameters[0] -
          arma::dot(data.col(j), parameters.subvec(1, dimension)))));
      if (responses[j] == 1.0)
        loglikelihood += log(std::pow(sigmoid, responses[j]));
      else
        loglikelihood += log(std::pow(1.0 - sigmoid, 1.0 - responses[j]));
    }
    BOOST_REQUIRE_CLOSE(lrf.Evaluate(parameters), -loglikelihood, 1e-5);
  }
}
BOOST_AUTO_TEST_CASE ( LogisticRegressionFunctionRandomEvaluate ) { const size_t points = 1000 ; const size_t dimension = 10 ; const size_t trials = 50 ; arma :: mat data ; data . randu ( dimension , points ) ; arma :: Row < size_t > responses ( points ) ; for ( size_t i = 0 ; i < points ; ++ i ) responses [ i ] = math :: RandInt ( 0 , 2 ) ; LogisticRegressionFunction < > lrf ( data , responses , 0.0 ) ; for ( size_t i = 0 ; i < trials ; ++ i ) { arma :: rowvec parameters ; parameters . randu ( dimension + 1 ) ; double loglikelihood = 0.0 ; for ( size_t j = 0 ; j < points ; ++ j ) { const double sigmoid = ( 1.0 / ( 1.0 + exp ( - parameters [ 0 ] - arma :: dot ( data . col ( j ) , parameters . subvec ( 1 , dimension ) ) ) ) ) ; if ( responses [ j ] == 1.0 ) loglikelihood += log ( std :: pow ( sigmoid , responses [ j ] ) ) ; else loglikelihood += log ( std :: pow ( 1.0 - sigmoid , 1.0 - responses [ j ] ) ) ; } BOOST_REQUIRE_CLOSE ( lrf . Evaluate ( parameters ) , - loglikelihood , 1e-5 ) ; } }
A more complicated test for the LogisticRegressionFunction.
A more complicated test for the LogisticRegressionFunction .
// Create a random dataset. // Create random responses. /* no regularization */ // Run a bunch of trials. // Generate a random set of parameters. // Hand-calculate the loss function.