00001 #ifndef Impala_Core_Training_BalancedAveragePrecision_h
00002 #define Impala_Core_Training_BalancedAveragePrecision_h
00003
00004 #include "Core/Training/Evaluation.h"
00005 #include "Core/Table/Sort.h"
00006
00007
00008 namespace Impala
00009 {
00010 namespace Core
00011 {
00012 namespace Training
00013 {
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 class BalancedAveragePrecision : public Evaluation
00028 {
00029 public:
00030
00031 BalancedAveragePrecision(Table::AnnotationTable* annotation) :
00032 Evaluation(annotation)
00033 {
00034 }
00035
00036 virtual double
00037 Compute(Table::ScoreTable* table)
00038 {
00039 Sort(table, 2, false);
00040 return ComputeSub(table);
00041
00042 }
00043
00044 virtual double
00045 ComputeReversed(Table::ScoreTable* table)
00046 {
00047 Sort(table, 2, false);
00048 Reverse(table);
00049 return ComputeSub(table);
00050
00051 }
00052
00053 private:
00054
00055 double
00056 ComputeSub(Table::ScoreTable* table)
00057 {
00058
00059 double ap = 0;
00060 int positiveCount = 0;
00061 for (int i=0 ; i<table->Size() ; i++)
00062 {
00063 Quid q = table->Get1(i);
00064 if (mAnnotation->IsPositive(q))
00065 {
00066 positiveCount++;
00067 double precision = ((double)positiveCount)/((double)(i+1));
00068 ap += precision;
00069 }
00070 }
00071 if (positiveCount > 0)
00072 ap /= (double)positiveCount;
00073
00074
00075 double wap = 0;
00076 double WminusR = table->Size() - positiveCount;
00077 for (int i=1 ; i<=positiveCount ; i++)
00078 {
00079 wap = wap + ((double)i)/(WminusR + (double)i);
00080 }
00081 wap /= (double)positiveCount;
00082
00083
00084 double bap = 1.0;
00085 if (wap!=1.0)
00086 bap = (ap - wap)/(1-wap);
00087
00088 return bap;
00089 }
00090 };
00091
00092 }
00093 }
00094 }
00095
00096 #endif