00001 #ifndef Impala_Core_Table_AveragePrecision_h
00002 #define Impala_Core_Table_AveragePrecision_h
00003
00004 #include "Core/Table/AnnotationTable.h"
00005
00006 namespace Impala
00007 {
00008 namespace Core
00009 {
00010 namespace Table
00011 {
00012
00013
00017 template <class T>
00018 inline Real64
00019 AveragePrecision(T* rank, AnnotationTable* groundTruth, int topN = -1)
00020 {
00021 ILOG_VAR(Impala.Core.Table.AveragePrecision);
00022 Real64 ap = 0;
00023 int positiveCount = 0;
00024 if (topN == -1)
00025 topN = rank->Size();
00026 for (int i=0 ; i<topN ; i++)
00027 {
00028 Quid q = rank->Get1(i);
00029 if (groundTruth->IsPositive(q))
00030 {
00031 positiveCount++;
00032 double precision = ((double)positiveCount)/((double)(i+1));
00033 ap += precision;
00034 }
00035 }
00036 if (groundTruth->GetNrPositive() > 0)
00037 ap /= groundTruth->GetNrPositive();
00038 return ap;
00039 }
00040
00048 template <class T>
00049 inline Real64
00050 AveragePrecisionJudged(T* rank, AnnotationTable* groundTruth,
00051 bool skipIsNegative, int topN = -1,
00052 bool apOfFoundOnly = false)
00053 {
00054 ILOG_VAR(Impala.Core.Table.AveragePrecision);
00055 Real64 ap = 0;
00056 int positiveCount = 0;
00057 int curRank = 0;
00058 if (topN == -1)
00059 topN = rank->Size();
00060 for (int i=0 ; i<topN ; i++)
00061 {
00062 Quid q = rank->Get1(i);
00063 int qIndex = groundTruth->GetIndex(q);
00064 if (qIndex == groundTruth->Size())
00065 continue;
00066 if (groundTruth->IsPositive(qIndex))
00067 {
00068 positiveCount++;
00069 double precision = ((double)positiveCount)/((double)(curRank+1));
00070 ap += precision;
00071 }
00072 if (groundTruth->IsPositive(qIndex) || skipIsNegative
00073 || groundTruth->IsNegative(qIndex))
00074 {
00075 curRank++;
00076 }
00077 }
00078 int factor = (apOfFoundOnly) ? positiveCount : groundTruth->GetNrPositive();
00079 if (factor > 0)
00080 ap /= factor;
00081 return ap;
00082 }
00083
00084 }
00085 }
00086 }
00087
00088 #endif