#include <iostream>
#include <Rcpp.h>
#include <cmath>


RcppExport SEXP cmLL(
   SEXP _x,
   SEXP _y,
   SEXP _b0,
   SEXP _b1
)
{
   // process argument from R
   Rcpp::NumericVector x(_x);
   Rcpp::IntegerVector y(_y);

   double b0 = Rcpp::as<double>(_b0);
   double b1 = Rcpp::as<double>(_b1);

   int n = x.length();

   /*
   Rprintf("**************** in cmLL\n");
   Rprintf("sample size is: %ld\n",n);
   Rprintf("first and last x: %lf, %lf\n",x(0),x(n-1));
   Rprintf("first and last y: %lf, %lf\n",y(0),y(n-1));
   Rprintf("b0,b1: %lf, %lf\n",b0,b1);
   */


   double mll = 0.0;
   double eta=0.0;
   double eeta=0.0;
   double py1 = 0.0;
   for(int i=0;i<n;i++) {
      eta = b0 + b1*x(i);
      eeta = exp(eta);
      py1 = eeta/(1.0 + eeta);
      if(y(i)) {
         mll -= log(py1);
      } else {
         mll -= log(1.0 - py1);
      }
   }

   //use wrap to return computed totdim to R as part of a list
   Rcpp::List ret; //list to return
   ret["check"] = "check";
   ret["mLL"] = Rcpp::wrap(mll);

   //return
   return ret;
}

