Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

AveragePrecisionConfusion.h

Go to the documentation of this file.
00001 #ifndef Impala_Core_Table_AveragePrecisionConfusion_h
00002 #define Impala_Core_Table_AveragePrecisionConfusion_h
00003 
00004 #include "Core/Table/AnnotationTable.h"
00005 
00006 namespace Impala
00007 {
00008 namespace Core
00009 {
00010 namespace Table
00011 {
00012 
00013 
00021 template <class T>
00022 inline Real64*
00023 AveragePrecisionConfusion(T* rank, AnnotationTable** annotations, int tc, int nClasses, int topN = -1)
00024 {
00025     ILOG_VAR(Impala.Core.Table.AveragePrecisionConfusion);
00026     int positiveCount = 0;
00027     Real64* conf;
00028     if (topN == -1)
00029     {
00030         topN = rank->Size();
00031         conf = new Real64[topN];
00032 
00033         for (int i=0 ; i < topN ; i++)
00034             conf[i] = 0;
00035     }
00036     
00037     // Get total positives
00038     int totPos = annotations[tc]->GetNrPositive();
00039 
00040     for (int i=0 ; i<topN ; i++)
00041     {
00042         if (positiveCount < totPos)
00043         {
00044             Quid q = rank->Get1(i);
00045             if (annotations[tc]->IsPositive(q))
00046             {
00047                 positiveCount++;
00048                 double precision = ((double)positiveCount)/((double)(i+1));
00049                 conf[tc] += precision;
00050             }
00051             else // Calculate AP loss (Best Case)
00052             { 
00053                 // Direct Loss
00054                 Real64 directLoss = ((double) positiveCount + 1) / ((double)(i+1));
00055 
00056 
00057                 // Minimum Future Loss
00058                 // (Occurs when next all positive examples are encountered)
00059                 int posLeft = totPos - positiveCount - 1;
00060                 Real64 futureLoss = 0;
00061                 if (posLeft > 0)
00062                 {
00063                     for (int j=i+2; j<=i+1+posLeft ; j++)
00064                     {
00065                         futureLoss += 1.0 / (double) j;
00066                     }
00067                 }
00068 
00069                 // Maximum extra gain
00070                 // (Occurs when removed example is found directly after rest)
00071                 Real64 maxGain = totPos / ((double) i + (double) posLeft + 2.0);
00072 
00073                 // Total
00074                 Real64 totalLoss = directLoss + futureLoss - maxGain;
00075 
00076                 // Now determine where loss goes
00077                 Real64 totPosClasses = 0;
00078                 for (int j = 0; j < nClasses ; j++)
00079                 {
00080                     Quid q = rank->Get1(i);
00081                     if (annotations[j]->IsPositive(q))
00082                         totPosClasses+= 1;
00083                 }
00084 
00085                 // Allocate loss
00086                 for (int j = 0; j < nClasses; j++)
00087                 {
00088                     Quid q = rank->Get1(i);
00089                     if (annotations[j]->IsPositive(q))
00090                         conf[j] += totalLoss / totPosClasses;
00091                 }
00092             }
00093         }
00094     }
00095     if (annotations[tc]->GetNrPositive() > 0)
00096         for(int i=0; i<topN; i++)
00097             conf[i] /= annotations[tc]->GetNrPositive();
00098     return conf;
00099 }
00100 
00101 
00111 template <class T>
00112 inline Real64*
00113 AveragePrecisionJudgedConfusion(T* rank, AnnotationTable** annotations, 
00114                                 int tc, int nClasses, int topN = -1,
00115                                 bool apOfFoundOnly = false)
00116 {
00117     ILOG_VAR(Impala.Core.Table.AveragePrecisionConfusion);
00118     int positiveCount = 0;
00119     Real64* conf;
00120     if (topN == -1)
00121     {
00122         topN = rank->Size();
00123         conf = new Real64[topN];
00124 
00125         for (int i=0 ; i < topN ; i++)
00126             conf[i] = 0;
00127     }
00128     
00129     // Get total positives
00130     int totPos = annotations[tc]->GetNrPositive();
00131 
00132     int i = 0;
00133     for (int t=0 ; t<topN ; t++)
00134     {
00135         if (positiveCount < totPos)
00136         {
00137             Quid q = rank->Get1(t);
00138             int qIndex = annotations[tc]->GetIndex(q);
00139             if (!(qIndex == annotations[tc]->Size())) // not annotated
00140             {
00141                 if (annotations[tc]->IsPositive(qIndex))
00142                 {
00143                     positiveCount++;
00144                     double precision = ((double)positiveCount)/((double)(i+1));
00145                     conf[tc] += precision;
00146                 }
00147                 else if (annotations[tc]->IsNegative(qIndex)) // Calculate AP loss (Best Case)
00148                 { 
00149                     // Direct Loss
00150                     Real64 directLoss = ((double) positiveCount + 1) / ((double)(i+1));
00151 
00152 
00153                     // Minimum Future Loss
00154                     // (Occurs when next all positive examples are encountered)
00155                     int posLeft = totPos - positiveCount - 1;
00156                     Real64 futureLoss = 0;
00157                     if (posLeft > 0)
00158                     {
00159                         for (int j=i+2; j<=i+1+posLeft ; j++)
00160                         {
00161                             futureLoss += 1.0 / (double) j;
00162                         }
00163                     }
00164 
00165                     // Maximum extra gain
00166                     // (Occurs when removed example is found directly after rest)
00167                     Real64 maxGain = totPos / ((double) i + (double) posLeft + 2.0);
00168 
00169                     // Total
00170                     Real64 totalLoss = directLoss + futureLoss - maxGain;
00171 
00172                     // Now determine where loss goes
00173                     Real64 totPosClasses = 0;
00174                     for (int j = 0; j < nClasses ; j++)
00175                     {
00176                         Quid q = rank->Get1(t);
00177                         int qInd = annotations[j]->GetIndex(q);
00178                         if (annotations[j]->IsPositive(qInd))
00179                             totPosClasses+= 1;
00180                     }
00181 
00182                     // Allocate loss
00183                     for (int j = 0; j < nClasses; j++)
00184                     {
00185                         Quid q = rank->Get1(t);
00186                         int qInd = annotations[j]->GetIndex(q);
00187                         if (annotations[j]->IsPositive(qInd))
00188                             conf[j] += totalLoss / totPosClasses;
00189                     }
00190                 }
00191                 if (annotations[tc]->IsPositive(qIndex) || annotations[tc]->IsNegative(qIndex))
00192                     i++;
00193             }
00194         }
00195     }
00196     int factor = (apOfFoundOnly) ? positiveCount : annotations[tc]->GetNrPositive();
00197     if (factor > 0)
00198         for(int i=0; i<topN; i++)
00199             conf[i] /= factor;
00200     return conf;
00201 }
00202 
00203 } // namespace Table
00204 } // namespace Core
00205 } // namespace Impala
00206 
00207 #endif

Generated on Fri Mar 19 09:31:19 2010 for ImpalaSrc by  doxygen 1.5.1