JackStat / ModelMetrics

Rapid Calculation of Model Metrics
29 stars 9 forks source link

poisson log loss #20

Open spedygiorgio opened 7 years ago

spedygiorgio commented 7 years ago

The following function could be a nice add for the package, since it implements the Poisson Log Loss in Rcpp (also thanks to SO ) and originally written in R in the MLmetrics package:

#include <Rcpp.h>
#include <math.h>
using namespace Rcpp;

// [[Rcpp::export]]
double poissonLogLoss(NumericVector predicted, NumericVector actual) {
  NumericVector temp, y_pred_new;
  double out; 
  const double eps=1e-15;

  y_pred_new=pmax(predicted,eps);
  temp = log(gamma(actual + 1)) + y_pred_new - log(y_pred_new)*actual;
  out=mean(temp); // using sugar implementation
  return out;
}
JackStat commented 7 years ago

@spedygiorgio Can you open a pull req for this. It would be a great addition. You can also get credit for the work. Let me know Tyler

spedygiorgio commented 7 years ago

@JackStat I hope to pull it tomorrow