Skip to content

Commit

Permalink
Merge pull request #365 from karlnapf/master
Browse files Browse the repository at this point in the history
added new error measure for regression
  • Loading branch information
Soeren Sonnenburg committed Feb 2, 2012
2 parents 3eab618 + 2d9564c commit 3a9377d
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/interfaces/modular/Evaluation.i
Expand Up @@ -15,6 +15,7 @@
%rename(MulticlassAccuracy) CMulticlassAccuracy;
%rename(MeanAbsoluteError) CMeanAbsoluteError;
%rename(MeanSquaredError) CMeanSquaredError;
%rename(MeanSquaredLogError) CMeanSquaredLogError;
%rename(ROCEvaluation) CROCEvaluation;
%rename(PRCEvaluation) CPRCEvaluation;
%rename(AccuracyMeasure) CAccuracyMeasure;
Expand All @@ -38,6 +39,7 @@
%include <shogun/evaluation/MulticlassAccuracy.h>
%include <shogun/evaluation/MeanAbsoluteError.h>
%include <shogun/evaluation/MeanSquaredError.h>
%include <shogun/evaluation/MeanSquaredLogError.h>
%include <shogun/evaluation/ROCEvaluation.h>
%include <shogun/evaluation/PRCEvaluation.h>
%include <shogun/evaluation/CrossValidation.h>
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/modular/Evaluation_includes.i
Expand Up @@ -6,6 +6,7 @@
#include <shogun/evaluation/MulticlassAccuracy.h>
#include <shogun/evaluation/MeanAbsoluteError.h>
#include <shogun/evaluation/MeanSquaredError.h>
#include <shogun/evaluation/MeanSquaredLogError.h>
#include <shogun/evaluation/ROCEvaluation.h>
#include <shogun/evaluation/PRCEvaluation.h>
#include <shogun/evaluation/CrossValidation.h>
Expand Down
36 changes: 36 additions & 0 deletions src/shogun/evaluation/MeanSquaredLogError.cpp
@@ -0,0 +1,36 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2012 Heiko Strathmann
* Copyright (C) 2012 Berlin Institute of Technology and Max-Planck-Society
*/

#include <shogun/evaluation/MeanSquaredLogError.h>
#include <shogun/features/Labels.h>
#include <shogun/mathematics/Math.h>

using namespace shogun;

float64_t CMeanSquaredLogError::evaluate(CLabels* predicted, CLabels* ground_truth)
{
ASSERT(predicted->get_num_labels()==ground_truth->get_num_labels());
int32_t length=predicted->get_num_labels();
float64_t msle=0.0;
for (int32_t i=0; i<length; i++)
{
float64_t prediction=predicted->get_label(i);
float64_t truth=ground_truth->get_label(i);

if (prediction<0 || truth<0)
SG_ERROR("Negative label[%d] in %s is not allowed!\n", i, get_name());

float64_t a=CMath::log(prediction+1);
float64_t b=CMath::log(truth+1);
msle+=CMath::sq(a-b);
}
msle /= length;
return CMath::sqrt(msle);
}
60 changes: 60 additions & 0 deletions src/shogun/evaluation/MeanSquaredLogError.h
@@ -0,0 +1,60 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2012 Heiko Strathmann
* Copyright (C) 2012 Berlin Institute of Technology and Max-Planck-Society
*/

#ifndef __MEANSQUAREDLOGERROR__
#define __MEANSQUAREDLOGERROR__

#include <shogun/evaluation/Evaluation.h>
#include <shogun/features/Labels.h>

namespace shogun
{

class CLabels;

/** @brief Class CMeanSquaredLogError
* used to compute an error of regression model.
*
* Formally, for real labels \f$ L,R, |L|=|R|, L_i, R_i > -1\f$ mean squared
* log error is estimated as
*
* \f[
* \sqrt{\frac{1}{|L|} \sum_{i=1}^{|L|} (\log{L_i+1} - \log{R_i+1})^2}
* \f]
*
*/
class CMeanSquaredLogError: public CEvaluation
{
public:
/** constructor */
CMeanSquaredLogError() : CEvaluation() {};

/** destructor */
virtual ~CMeanSquaredLogError() {};

/** evaluate mean squared log error
* @param predicted labels for evaluating
* @param ground_truth labels assumed to be correct
* @return mean squared error
*/
virtual float64_t evaluate(CLabels* predicted, CLabels* ground_truth);

inline EEvaluationDirection get_evaluation_direction()
{
return ED_MINIMIZE;
}

/** get name */
virtual inline const char* get_name() const { return "MeanSquaredLogError"; }
};

}

#endif /* __MEANSQUAREDLOGERROR__ */

0 comments on commit 3a9377d

Please sign in to comment.