-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtrec_eval.c
1970 lines (1775 loc) · 73.3 KB
/
trec_eval.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* trec_eval.c implements standard information retrieval evaluation
* measures. This code originated in the trec_eval program written by
* Chris Buckley and Gerard Salton. They originally published under
* this license:
*
* Copyright (c) 1991, 1990, 1984 - Gerard Salton, Chris Buckley.
* Permission is granted for use of this file in unmodified form for
* research purposes. Please contact the SMART project to obtain
* permission for other uses.
*
* This code was modified and redistributed with permission from Chris
* Buckley.
*
*/
#include "firstinclude.h"
#include "trec_eval.h"
#include "chash.h"
#include "str.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
/* Defined constants that are collection/purpose dependent */
/* Number of cutoffs for recall,precision, and rel_precis measures. */
/* CUTOFF_VALUES gives the number of retrieved docs that these
* evaluation measures are applied at. */
#define NUM_CUTOFF 9
#define CUTOFF_VALUES {5, 10, 15, 20, 30, 100, 200, 500, 1000}
/* Maximum fallout value, expressed in number of non-rel docs retrieved. */
/* (Make the approximation that number of non-rel docs in collection */
/* is equal to the number of number of docs in collection) */
#define MAX_FALL_RET 142
/* Maximum multiple of R (number of rel docs for this query) to calculate */
/* R-based precision at */
#define MAX_RPREC 2.0
/* Defined constants that are collection/purpose independent. If you
* change these, you probably need to change comments and documentation,
* and some variable names may not be appropriate any more! */
#define NUM_RP_PTS 11
#define THREE_PTS {2, 5, 8}
#define NUM_FR_PTS 11
#define NUM_PREC_PTS 11
typedef struct {
int qid; /* query id (for overall average
figures, this gives number of
queries in run) */
/* Summary Numbers over all queries */
long num_rel; /* Number of relevant docs */
long num_ret; /* Number of retrieved docs */
long num_rel_ret; /* Number of relevant retrieved docs */
float avg_doc_prec; /* Average of precision over all
relevant documents (query
independent) */
/* Measures after num_ret docs */
float exact_recall; /* Recall after num_ret docs */
float exact_precis; /* Precision after num_ret docs */
float exact_rel_precis; /* Relative Precision (or recall) */
/* Defined to be precision / max possible precision */
/* Measures after each document */
float recall_cut[NUM_CUTOFF]; /* Recall after cutoff[i] docs */
float precis_cut[NUM_CUTOFF]; /* precision after cutoff[i] docs.
If less than cutoff[i] docs
retrieved, then assume an
additional cutoff[i]-num_ret
non-relevant docs are retrieved. */
float rel_precis_cut /* Relative precision after cutoff[i] */
[NUM_CUTOFF]; /* docs */
/* Measures after each rel doc */
float av_recall_precis; /* average(integral) of precision at
all rel doc ranks */
float int_av_recall_precis; /* Same as above, but the precision
values have been interpolated, so
that prec(X) is actually MAX
prec(Y) for all Y >= X */
float int_recall_precis /* interpolated precision at 0.1 */
[NUM_RP_PTS]; /* increments of recall */
float int_av3_recall_precis; /* interpolated average at 3 intermediate
points */
float int_av11_recall_precis; /* interpolated average at NUM_RP_PTS
intermediate points (recall_level) */
/* Measures after each non-rel doc */
float fall_recall[NUM_FR_PTS]; /* max recall after each non-rel
doc, at 11 points starting at 0.0
and ending at MAX_FALL_RET
/num_docs */
float av_fall_recall; /* Average of fallout-recall, after
each non-rel doc until fallout of
MAX_FALL_RET / num_docs achieved */
/* Measures after R-related cutoffs. R is the number of relevant
* docs for a particular query, but note that these cutoffs are
* after R docs, whether relevant or non-relevant, have been
* retrieved. R-related cutoffs are really only applicable to a
* situtation where there are many relevant docs per query (or lots
* of queries). */
float R_recall_precis; /* Recall or precision after R docs
(note they are equal at this
point) */
float av_R_precis; /* Average (or integral) of
precision at each doc until R
docs have been retrieved */
float R_prec_cut[NUM_PREC_PTS]; /* Precision measured after
multiples of R docs have been
retrieved. 11 equal points, with
max multiple having value
MAX_RPREC */
float int_R_recall_precis; /* Interpolated precision after R
docs Prec(X) = MAX(prec(Y)) for
all Y>=X */
float int_av_R_precis; /* Interpolated */
float int_R_prec_cut /* Interpolated */
[NUM_PREC_PTS];
} TREC_EVAL;
typedef struct {
long int did; /* document id */
long int rank; /* Rank of this document */
char action; /* what action a user has taken with
doc */
char rel; /* whether doc judged relevant(1) or
not(0) */
char iter; /* Number of feedback runs for this
query */
char trtup_unused; /* Presently unused field */
float sim; /* similarity of did to qid */
} TR_TUP;
typedef struct {
int qid; /* query id */
long int num_tr; /* Number of tuples for tr_vec */
TR_TUP *tr; /* tuples. Invariant: tr sorted
increasing did */
} TR_VEC;
static int init_tr_trec_eval(),
tr_trec_eval(),
close_tr_trec_eval();
static int trvec_trec_eval();
static int comp_tr_tup_did(const void *, const void *),
comp_sim_docno(const void *, const void *);
static int get_trec_top();
static int form_and_eval_trvec();
typedef struct {
char *docno;
float sim;
long int rank;
} TEXT_TR;
typedef struct {
int qid;
long int num_text_tr;
long int max_num_text_tr;
TEXT_TR *text_tr;
} TREC_TOP;
#define TR_INCR 250
#define MAX(A,B) ((A) > (B) ? (A) : (B))
#define MIN(A,B) ((A) > (B) ? (B) : (A))
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#define UNDEF -1
#define MAX_LINE_LENGTH 100
/* currently I'm assuming there are only 1000 queries _ever_ used at
* TREC, which is probably unrealistic. This assumption makes things
* easier, and if there are more queries used (ie higher query numbers
* used) then the following number just needs to be increased. */
#define INTERPOLATED000 0
#define INTERPOLATED010 1
#define INTERPOLATED020 2
#define INTERPOLATED030 3
#define INTERPOLATED040 4
#define INTERPOLATED050 5
#define INTERPOLATED060 6
#define INTERPOLATED070 7
#define INTERPOLATED080 8
#define INTERPOLATED090 9
#define INTERPOLATED100 10
#define PRECISION_AT_5 0
#define PRECISION_AT_10 1
#define PRECISION_AT_15 2
#define PRECISION_AT_20 3
#define PRECISION_AT_30 4
#define PRECISION_AT_100 5
#define PRECISION_AT_200 6
#define PRECISION_AT_500 7
#define PRECISION_AT_1000 8
#define TREC_DOCUMENT_NUMBER_MAX_LENGTH 50
typedef struct {
int query_id; /* Holds query ID */
char trec_document_number[TREC_DOCUMENT_NUMBER_MAX_LENGTH];
/* TREC document ID */
float score; /* accumulator score of document */
} result_tuple;
struct treceval_qrels {
struct chash *judgements; /* Hash table that contains as keys
all relevance judgements in the
format of "QID TRECDOCNO", for
instance "381 FBIS3-1" */
int number_of_judged_queries; /* This many queries have relevance
judgements */
int lowest_query_id; /* The lowest query Id that has a
relevance judgement */
/* Each query contains this many judgements */
int *number_of_judgements_for_query;
};
struct treceval {
result_tuple *tuples; /* Stores unevaluated result tuples */
unsigned int cached_results; /* Number of tuples in cache */
unsigned int cache_size; /* Cache size in tuples */
};
static void evaluate_trec_results(unsigned int from_position,
unsigned int to_position, const struct treceval *result_cache,
const struct treceval_qrels *qrels,
struct treceval_results *evaluated_results) {
TREC_TOP trec_top;
TREC_EVAL eval_accum;
unsigned int up_to = from_position;
init_tr_trec_eval(&eval_accum);
trec_top.max_num_text_tr = 0;
trec_top.text_tr = NULL;
evaluated_results->queries = 0;
while (get_trec_top(&trec_top, result_cache, &up_to, to_position) != 0) {
form_and_eval_trvec(&eval_accum, &trec_top, qrels);
evaluated_results->queries++;
}
/* Finish evaluation computation */
close_tr_trec_eval(&eval_accum);
evaluated_results->retrieved = eval_accum.num_ret;
evaluated_results->relevant = eval_accum.num_rel;
evaluated_results->relevant_retrieved = eval_accum.num_rel_ret;
evaluated_results->interpolated_rp[INTERPOLATED000] =
eval_accum.int_recall_precis[0];
evaluated_results->interpolated_rp[INTERPOLATED010] =
eval_accum.int_recall_precis[1];
evaluated_results->interpolated_rp[INTERPOLATED020] =
eval_accum.int_recall_precis[2];
evaluated_results->interpolated_rp[INTERPOLATED030] =
eval_accum.int_recall_precis[3];
evaluated_results->interpolated_rp[INTERPOLATED040] =
eval_accum.int_recall_precis[4];
evaluated_results->interpolated_rp[INTERPOLATED050] =
eval_accum.int_recall_precis[5];
evaluated_results->interpolated_rp[INTERPOLATED060] =
eval_accum.int_recall_precis[6];
evaluated_results->interpolated_rp[INTERPOLATED070] =
eval_accum.int_recall_precis[7];
evaluated_results->interpolated_rp[INTERPOLATED080] =
eval_accum.int_recall_precis[8];
evaluated_results->interpolated_rp[INTERPOLATED090] =
eval_accum.int_recall_precis[9];
evaluated_results->interpolated_rp[INTERPOLATED100] =
eval_accum.int_recall_precis[10];
evaluated_results->average_precision = eval_accum.av_recall_precis;
evaluated_results->precision_at[PRECISION_AT_5]
= eval_accum.precis_cut[0];
evaluated_results->precision_at[PRECISION_AT_10]
= eval_accum.precis_cut[1];
evaluated_results->precision_at[PRECISION_AT_15]
= eval_accum.precis_cut[2];
evaluated_results->precision_at[PRECISION_AT_20]
= eval_accum.precis_cut[3];
evaluated_results->precision_at[PRECISION_AT_30]
= eval_accum.precis_cut[4];
evaluated_results->precision_at[PRECISION_AT_100]
= eval_accum.precis_cut[5];
evaluated_results->precision_at[PRECISION_AT_200]
= eval_accum.precis_cut[6];
evaluated_results->precision_at[PRECISION_AT_500]
= eval_accum.precis_cut[7];
evaluated_results->precision_at[PRECISION_AT_1000]
= eval_accum.precis_cut[8];
evaluated_results->rprecision = eval_accum.R_recall_precis;
if (trec_top.text_tr != NULL) {
free(trec_top.text_tr);
}
return;
}
static int form_and_eval_trvec(TREC_EVAL * eval_accum, TREC_TOP * trec_top,
struct treceval_qrels *qrels) {
TR_TUP *tr_tup;
long i;
TR_TUP *start_tr_tup = NULL; /* Space reserved for output TR_TUP
tuples */
long max_tr_tup = 0;
TR_VEC tr_vec;
char judgement[MAX_LINE_LENGTH];
void **dummy_pointer = NULL;
/* Reserve space for output tr_tups, if needed */
if (trec_top->num_text_tr > max_tr_tup) {
if (max_tr_tup > 0) {
(void) free((char *) start_tr_tup);
}
max_tr_tup += trec_top->num_text_tr;
if ((start_tr_tup = (TR_TUP *) malloc((unsigned) max_tr_tup
* sizeof(TR_TUP))) == NULL) {
return (UNDEF);
}
}
/* Sort trec_top by sim, breaking ties lexicographically using docno */
qsort((char *) trec_top->text_tr, (int) trec_top->num_text_tr,
sizeof(TEXT_TR), comp_sim_docno);
/* Add ranks to trec_top (starting at 1) */
for (i = 0; i < trec_top->num_text_tr; i++) {
trec_top->text_tr[i].rank = i + 1;
}
/* for each tuple in trec_top check the hash table which documents *
* are relevant. Once relevance is known, convert trec_top tuple *
* into TR_TUP. */
tr_tup = start_tr_tup;
for (i = 0; i < trec_top->num_text_tr; i++) {
sprintf(judgement, "%d %s", trec_top->qid,
trec_top->text_tr[i].docno);
if (chash_ptr_ptr_find(qrels->judgements, judgement,
&dummy_pointer) != CHASH_OK) {
/* Document is non-relevant */
tr_tup->rel = 0;
} else {
/* Document is relevant */
tr_tup->rel = 1;
}
tr_tup->did = i;
tr_tup->rank = trec_top->text_tr[i].rank;
tr_tup->sim = trec_top->text_tr[i].sim;
tr_tup->action = 0;
tr_tup->iter = 0;
tr_tup++;
}
/* Form and output the full TR_VEC object for this qid */
/* Sort trec_top tr_tups by did */
qsort((char *) start_tr_tup, (int) trec_top->num_text_tr, sizeof(TR_TUP),
comp_tr_tup_did);
tr_vec.qid = trec_top->qid;
tr_vec.num_tr = trec_top->num_text_tr;
tr_vec.tr = start_tr_tup;
/* Accumulate this tr_vec's eval figures in eval_accum */
tr_trec_eval(&tr_vec, eval_accum,
qrels->number_of_judgements_for_query[trec_top->qid -
qrels->lowest_query_id]);
free(start_tr_tup);
return (1);
}
static int comp_sim_docno(const void *vptr1, const void *vptr2) {
const TEXT_TR *ptr1 = vptr1,
*ptr2 = vptr2;
if (ptr1->sim > ptr2->sim)
return (-1);
if (ptr1->sim < ptr2->sim)
return (1);
return (strcmp(ptr2->docno, ptr1->docno));
}
static int comp_tr_tup_did(const void *vptr1, const void *vptr2) {
const TR_TUP *ptr1 = vptr1,
*ptr2 = vptr2;
return (ptr1->did - ptr2->did);
}
/* Get entire trec_top vector for next query */
static int get_trec_top(TREC_TOP * trec_top, struct treceval *result_cache,
unsigned int *up_to, unsigned int to_position) {
int qid;
/* have already processed all tuples in this result set */
if (*up_to >= to_position) {
return 0;
}
trec_top->num_text_tr = 0;
qid = trec_top->qid =
result_cache->tuples[trec_top->num_text_tr + *up_to].query_id;
while (qid == trec_top->qid) {
/* Make sure enough space is reserved for next tuple */
if (trec_top->num_text_tr >= trec_top->max_num_text_tr) {
if (trec_top->max_num_text_tr == 0) {
if ((trec_top->text_tr = (TEXT_TR *) malloc((unsigned)
TR_INCR * sizeof(TEXT_TR))) == NULL) {
return (0);
}
} else if ((trec_top->text_tr = (TEXT_TR *) realloc((char *)
trec_top->text_tr, (unsigned) (trec_top->max_num_text_tr
+ TR_INCR) * sizeof(TEXT_TR))) == NULL) {
return (0);
}
trec_top->max_num_text_tr += TR_INCR;
}
trec_top->text_tr[trec_top->num_text_tr].docno =
result_cache->tuples[trec_top->num_text_tr +
*up_to].trec_document_number;
trec_top->text_tr[trec_top->num_text_tr].sim =
result_cache->tuples[trec_top->num_text_tr + *up_to].score;
trec_top->num_text_tr++;
/* are we at the end of the results? */
if ((trec_top->num_text_tr + *up_to) >= to_position) {
*up_to += trec_top->num_text_tr;
return (1);
}
qid = result_cache->tuples[trec_top->num_text_tr + *up_to].query_id;
}
*up_to += trec_top->num_text_tr;
return (1);
}
/******************** PROCEDURE DESCRIPTION ************************
*0 Given a tr file, evaluate it using trec, returning the evaluation in eval
*2 tr_trec_eval (tr_file, eval, inst)
*3 char *tr_file;
*3 TREC_EVAL *eval;
*3 int inst;
*4 init_tr_trec_eval (spec, unused)
*5 "eval.tr_file"
*5 "eval.tr_file.rmode"
*5 "eval.trace"
*4 close_tr_trec_eval (inst)
*7 Evaluate the given tr_file, returning the average over all queries of
*7 each query's evaluation. Eval->qid will contain the number of queries
*7 evaluated. Tr_file is taken from the argument tr_file if
*7 that is valid, else from the spec parameter eval.tr_file.
*7 Return 1 if successful, 0 if no queries were evaluated, and UNDEF
*7 otherwise
*8 Call trvec_smeval for each query in tr_file, and simply average results.
*9 Note: Only the max iteration over all queries is averaged. Thus, if
*9 query 1 had one iteration of feedback, and query 2 had two iterations of
*9 feedback, query 1 will not be included in the final average (or counted
*9 in eval->qid).
***********************************************************************/
static int init_tr_trec_eval(TREC_EVAL * eval) {
memset(eval, 0, sizeof(*eval));
return (0);
}
static int tr_trec_eval(TR_VEC * tr_vec, TREC_EVAL * eval,
long int num_rel) {
long i;
int max_iter_achieved;
TREC_EVAL query_eval;
int max_iter = 0;
/* Check that max_iter has not been exceeded. If it has, then have
* to throw away all previous results. Also check to see that
* max_iter has been achieved. If not, then no docs were retrieved
* for this query on this iteration */
max_iter_achieved = 0;
for (i = 0; i < tr_vec->num_tr; i++) {
if (tr_vec->tr[i].iter > max_iter) {
memset((char *) eval, 0, sizeof(TREC_EVAL));
max_iter = tr_vec->tr[i].iter;
}
if (tr_vec->tr[i].iter == max_iter) {
max_iter_achieved++;
}
}
if (max_iter_achieved == 0) {
return (0);
}
/* Evaluate this query */
if (1 == trvec_trec_eval(tr_vec, &query_eval, num_rel)) {
if (query_eval.num_ret > 0) {
eval->qid++;
eval->num_rel += query_eval.num_rel;
eval->num_ret += query_eval.num_ret;
eval->num_rel_ret += query_eval.num_rel_ret;
eval->avg_doc_prec += query_eval.avg_doc_prec;
eval->exact_recall += query_eval.exact_recall;
eval->exact_precis += query_eval.exact_precis;
eval->exact_rel_precis += query_eval.exact_rel_precis;
eval->av_recall_precis += query_eval.av_recall_precis;
eval->int_av_recall_precis += query_eval.int_av_recall_precis;
eval->int_av3_recall_precis += query_eval.int_av3_recall_precis;
eval->int_av11_recall_precis
+= query_eval.int_av11_recall_precis;
eval->av_fall_recall += query_eval.av_fall_recall;
eval->R_recall_precis += query_eval.R_recall_precis;
eval->av_R_precis += query_eval.av_R_precis;
eval->int_R_recall_precis += query_eval.int_R_recall_precis;
eval->int_av_R_precis += query_eval.int_av_R_precis;
for (i = 0; i < NUM_CUTOFF; i++) {
eval->recall_cut[i] += query_eval.recall_cut[i];
eval->precis_cut[i] += query_eval.precis_cut[i];
eval->rel_precis_cut[i] += query_eval.rel_precis_cut[i];
}
for (i = 0; i < NUM_RP_PTS; i++)
eval->int_recall_precis[i]
+= query_eval.int_recall_precis[i];
for (i = 0; i < NUM_FR_PTS; i++)
eval->fall_recall[i] += query_eval.fall_recall[i];
for (i = 0; i < NUM_PREC_PTS; i++) {
eval->R_prec_cut[i] += query_eval.R_prec_cut[i];
eval->int_R_prec_cut[i] += query_eval.int_R_prec_cut[i];
}
}
}
return (0);
}
static int close_tr_trec_eval(TREC_EVAL * eval) {
long i;
/* Calculate averages (for those eval fields returning averages) */
if (eval->qid > 0) {
if (eval->num_rel > 0) {
eval->avg_doc_prec /= (float) eval->num_rel;
}
eval->exact_recall /= (float) eval->qid;
eval->exact_precis /= (float) eval->qid;
eval->exact_rel_precis /= (float) eval->qid;
eval->av_recall_precis /= (float) eval->qid;
eval->int_av_recall_precis /= (float) eval->qid;
eval->int_av3_recall_precis /= (float) eval->qid;
eval->int_av11_recall_precis /= (float) eval->qid;
eval->av_fall_recall /= (float) eval->qid;
eval->R_recall_precis /= (float) eval->qid;
eval->av_R_precis /= (float) eval->qid;
eval->int_R_recall_precis /= (float) eval->qid;
eval->int_av_R_precis /= (float) eval->qid;
for (i = 0; i < NUM_CUTOFF; i++) {
eval->recall_cut[i] /= (float) eval->qid;
eval->precis_cut[i] /= (float) eval->qid;
eval->rel_precis_cut[i] /= (float) eval->qid;
}
for (i = 0; i < NUM_RP_PTS; i++) {
eval->int_recall_precis[i] /= (float) eval->qid;
}
for (i = 0; i < NUM_FR_PTS; i++) {
eval->fall_recall[i] /= (float) eval->qid;
}
for (i = 0; i < NUM_PREC_PTS; i++) {
eval->R_prec_cut[i] /= (float) eval->qid;
eval->int_R_prec_cut[i] /= (float) eval->qid;
}
}
return (0);
}
/* cutoff values for recall precision output */
const static int cutoff[NUM_CUTOFF] = CUTOFF_VALUES;
const static int three_pts[3] = THREE_PTS;
static int compare_iter_rank(const void *, const void *);
static int trvec_trec_eval(TR_VEC * tr_vec, TREC_EVAL * eval,
long int num_rel) {
/* cutoff values for recall precision output */
double recall,
precis,
int_precis; /* current recall, precision and
interpolated precision values */
long i, j;
long rel_so_far;
long max_iter;
long cut_rp[NUM_RP_PTS]; /* number of rel docs needed to be
retrieved for each recall-prec
cutoff */
long cut_fr[NUM_FR_PTS]; /* number of non-rel docs needed to
be retrieved for each fall-recall
cutoff */
long cut_rprec[NUM_PREC_PTS]; /* Number of docs needed to be
retrieved for each R-based prec
cutoff */
long current_cutoff,
current_cut_rp,
current_cut_fr,
current_cut_rprec;
if (tr_vec == (TR_VEC *) NULL)
return (UNDEF);
/* Initialize everything to 0 */
memset((char *) eval, 0, sizeof(TREC_EVAL));
eval->qid = tr_vec->qid;
/* If no relevant docs, then just return */
if (tr_vec->num_tr == 0) {
return (0);
}
eval->num_rel = num_rel;
/* Evaluate only the docs on the last iteration of tr_vec */
/* Sort the tr tuples for this query by decreasing iter and
* increasing rank */
qsort((char *) tr_vec->tr, (int) tr_vec->num_tr, sizeof(TR_TUP),
compare_iter_rank);
max_iter = tr_vec->tr[0].iter;
rel_so_far = 0;
for (j = 0; j < tr_vec->num_tr; j++) {
if (tr_vec->tr[j].iter == max_iter) {
eval->num_ret++;
if (tr_vec->tr[j].rel)
rel_so_far++;
} else {
if (tr_vec->tr[j].rel)
eval->num_rel--;
}
}
eval->num_rel_ret = rel_so_far;
/* Discover cutoff values for this query */
current_cutoff = NUM_CUTOFF - 1;
while (current_cutoff > 0 && cutoff[current_cutoff] > eval->num_ret) {
current_cutoff--;
}
for (i = 0; i < NUM_RP_PTS; i++) {
cut_rp[i] = ((eval->num_rel * i) + NUM_RP_PTS - 2)
/ (NUM_RP_PTS - 1);
}
current_cut_rp = NUM_RP_PTS - 1;
while (current_cut_rp > 0 && cut_rp[current_cut_rp]
> eval->num_rel_ret) {
current_cut_rp--;
}
for (i = 0; i < NUM_FR_PTS; i++) {
cut_fr[i] = ((MAX_FALL_RET * i) + NUM_FR_PTS - 2) / (NUM_FR_PTS - 1);
}
current_cut_fr = NUM_FR_PTS - 1;
while (current_cut_fr > 0
&& cut_fr[current_cut_fr] > eval->num_ret - eval->num_rel_ret) {
current_cut_fr--;
}
for (i = 0; i < NUM_PREC_PTS; i++) {
cut_rprec[i] = (long int) (((MAX_RPREC * eval->num_rel * i)
+ NUM_PREC_PTS - 2) / (NUM_PREC_PTS - 1));
}
current_cut_rprec = NUM_PREC_PTS - 1;
while (current_cut_rprec > 0
&& cut_rprec[current_cut_rprec] > eval->num_ret) {
current_cut_rprec--;
}
/* Note for interpolated precision values (Prec(X) = MAX (PREC(Y))
* for all Y >= X) */
int_precis = (float) rel_so_far / (float) eval->num_ret;
for (j = eval->num_ret; j > 0; j--) {
recall = (float) rel_so_far / (float) eval->num_rel;
precis = (float) rel_so_far / (float) j;
if (int_precis < precis) {
int_precis = precis;
}
while (j == cutoff[current_cutoff]) {
eval->recall_cut[current_cutoff] = (float) recall;
eval->precis_cut[current_cutoff] = (float) precis;
eval->rel_precis_cut[current_cutoff]
= (float) (j > eval->num_rel ? recall : precis);
current_cutoff--;
}
while (j == cut_rprec[current_cut_rprec]) {
eval->R_prec_cut[current_cut_rprec] = (float) precis;
eval->int_R_prec_cut[current_cut_rprec] = (float) int_precis;
current_cut_rprec--;
}
if (j == eval->num_rel) {
eval->R_recall_precis = (float) precis;
eval->int_R_recall_precis = (float) int_precis;
}
if (j < eval->num_rel) {
eval->av_R_precis += (float) precis;
eval->int_av_R_precis += (float) int_precis;
}
if (tr_vec->tr[j - 1].rel) {
eval->int_av_recall_precis += (float) int_precis;
eval->av_recall_precis += (float) precis;
eval->avg_doc_prec += (float) precis;
while (rel_so_far == cut_rp[current_cut_rp]) {
eval->int_recall_precis[current_cut_rp] = (float) int_precis;
current_cut_rp--;
}
rel_so_far--;
} else {
/* Note: for fallout-recall, the recall at X non-rel docs is
* used for the recall 'after' (X-1) non-rel docs. Ie.
* recall_used(X-1 non-rel docs) = MAX (recall(Y)) for Y
* retrieved docs where X-1 non-rel retrieved */
while (current_cut_fr >= 0 &&
j - rel_so_far == cut_fr[current_cut_fr] + 1) {
eval->fall_recall[current_cut_fr] = (float) recall;
current_cut_fr--;
}
if (j - rel_so_far < MAX_FALL_RET) {
eval->av_fall_recall += (float) recall;
}
}
}
/* Fill in the 0.0 value for recall-precision (== max precision at
* any point in the retrieval ranking) */
eval->int_recall_precis[0] = (float) int_precis;
/* Fill in those cutoff values and averages that were not achieved
* because insufficient docs were retrieved. */
for (i = 0; i < NUM_CUTOFF; i++) {
if (eval->num_ret < cutoff[i]) {
eval->recall_cut[i] = ((float) eval->num_rel_ret /
(float) eval->num_rel);
eval->precis_cut[i] = ((float) eval->num_rel_ret /
(float) cutoff[i]);
eval->rel_precis_cut[i] = (cutoff[i] < eval->num_rel) ?
eval->precis_cut[i] : eval->recall_cut[i];
}
}
for (i = 0; i < NUM_FR_PTS; i++) {
if (eval->num_ret - eval->num_rel_ret < cut_fr[i]) {
eval->fall_recall[i] = (float) eval->num_rel_ret /
(float) eval->num_rel;
}
}
if (eval->num_ret - eval->num_rel_ret < MAX_FALL_RET) {
eval->av_fall_recall += ((MAX_FALL_RET -
(eval->num_ret - eval->num_rel_ret))
* ((float) eval->num_rel_ret / (float) eval->num_rel));
}
if (eval->num_rel > eval->num_ret) {
eval->R_recall_precis = (float) eval->num_rel_ret /
(float) eval->num_rel;
eval->int_R_recall_precis = (float) eval->num_rel_ret /
(float) eval->num_rel;
for (i = eval->num_ret; i < eval->num_rel; i++) {
eval->av_R_precis += (float) eval->num_rel_ret / (float) i;
eval->int_av_R_precis += (float) eval->num_rel_ret / (float) i;
}
}
for (i = 0; i < NUM_PREC_PTS; i++) {
if (eval->num_ret < cut_rprec[i]) {
eval->R_prec_cut[i] = (float) eval->num_rel_ret /
(float) cut_rprec[i];
eval->int_R_prec_cut[i] = (float) eval->num_rel_ret /
(float) cut_rprec[i];
}
}
/* The following cutoffs/averages are correct, since 0.0 should be
* averaged in for the non-retrieved docs: av_recall_precis,
* int_av_recall_prec, int_recall_prec, int_av3_recall_precis,
* int_av11_recall_precis */
/* Calculate other indirect evaluation measure averages. */
/* average recall-precis of 3 and 11 intermediate points */
eval->int_av3_recall_precis =
(eval->int_recall_precis[three_pts[0]] +
eval->int_recall_precis[three_pts[1]] +
eval->int_recall_precis[three_pts[2]]) / 3.0F;
for (i = 0; i < NUM_RP_PTS; i++) {
eval->int_av11_recall_precis += eval->int_recall_precis[i];
}
eval->int_av11_recall_precis /= NUM_RP_PTS;
/* Calculate all the other averages */
if (eval->num_rel_ret > 0) {
eval->av_recall_precis /= eval->num_rel;
eval->int_av_recall_precis /= eval->num_rel;
}
eval->av_fall_recall /= MAX_FALL_RET;
if (eval->num_rel) {
eval->av_R_precis /= eval->num_rel;
eval->int_av_R_precis /= eval->num_rel;
eval->exact_recall = (float) eval->num_rel_ret / eval->num_rel;
eval->exact_precis = (float) eval->num_rel_ret / eval->num_ret;
if (eval->num_rel > eval->num_ret)
eval->exact_rel_precis = eval->exact_precis;
else
eval->exact_rel_precis = eval->exact_recall;
}
return (1);
}
static int compare_iter_rank(const void *vptr1, const void *vptr2) {
const TR_TUP *tr1 = vptr1,
*tr2 = vptr2;
if (tr1->iter > tr2->iter) {
return (-1);
}
if (tr1->iter < tr2->iter) {
return (1);
}
if (tr1->rank < tr2->rank) {
return (-1);
}
if (tr1->rank > tr2->rank) {
return (1);
}
return (0);
}
/* Based on Falk Scholer's implementation in awk of the Wilcoxon
* Matched-Pairs Signed-Ranks Test or Wilcoxon Signed-Ranks Test */
/** Performs a Wilcoxon signed rank test for a paired-difference experiment
* at the 0.05 and 0.10 significance levels.
*
* The null hypothesis (probability distributions of samples 1 and 2
* are identical) is rejected if
* test statistic <= critical value for n = number of observations
*
* Note that this is a _two-tailed_ test, so that for a level of
* significance, ALPHA, the null hypothesis is rejected if the
* (absolute value of the) test statistic exceeds the critical value of
* the t-distribution at ALPHA/2 */
#define WILCOXON_TABLE_SIZE 50
#define Z_SCORE_TABLE_SIZE 410
#define NEGATIVE -1
#define POSITIVE 1
struct data_point {
float absolute_difference;
int sign;
};
static int compare_data_points(struct data_point *data_point1,
struct data_point *data_point2) {
if (data_point1->absolute_difference
> data_point2->absolute_difference) {
return (1);
}
if (data_point1->absolute_difference
< data_point2->absolute_difference) {
return (-1);
}
return (0);
}
static int calculate_statistics(unsigned int statistic_id,
struct treceval_statistics *stats, unsigned int number_of_queries,
float *measurements[2]) {
unsigned int i = 0;
float difference = (float) 0;
struct data_point *data_points = NULL;
float *ranks;
unsigned int non_zero_results_counter = 0;
unsigned int counter = 0;
float average = (float) 0;
unsigned int old_position = 0;
float number_of_positive_differences = (float) 0;
float number_of_negative_differences = (float) 0;
int number_of_queries_improved = 0;
int number_of_queries_degraded = 0;
float z = (float) 0;
float rounded_z = (float) 0;
int test_stat = 0;
/* Wilcoxon's table of critical values of T at alpha = .1 and .05 */
const int wilcoxon_critical_values[WILCOXON_TABLE_SIZE][2] = {
{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}, {1, -1},
{2, 1}, {4, 2}, {6, 6}, {8, 6}, {11, 8},
{14, 11}, {17, 14}, {21, 17}, {26, 21}, {30, 25},
{36, 30}, {41, 35}, {47, 40}, {54, 46}, {60, 52},
{68, 59}, {75, 66}, {83, 73}, {92, 81}, {101, 90},
{110, 98}, {120, 107}, {130, 117}, {141, 127}, {152, 137},
{163, 148}, {175, 159}, {188, 171}, {201, 183}, {214, 195},
{228, 208}, {242, 222}, {256, 235}, {271, 250}, {287, 264},
{303, 279}, {317, 295}, {336, 311}, {353, 327}, {371, 344},
{389, 361}, {408, 379}, {427, 397}, {446, 415}, {466, 434}
};
const float z_scores[Z_SCORE_TABLE_SIZE] = {
0.00000F, 0.00399F, 0.00798F, 0.01197F, 0.01595F,
0.01994F, 0.02392F, 0.02790F, 0.03188F, 0.03586F,
0.03983F, 0.04380F, 0.04776F, 0.05172F, 0.05567F,
1.05962F, 0.06356F, 0.06749F, 0.07142F, 0.07535F,
0.07926F, 0.08317F, 0.08706F, 0.09095F, 0.09483F,
0.09871F, 0.10257F, 0.10642F, 0.11026F, 0.11409F,
0.11791F, 0.12172F, 0.12552F, 0.12930F, 0.13307F,
0.13683F, 0.14058F, 0.14431F, 0.14803F, 0.15173F,
0.15542F, 0.15910F, 0.16276F, 0.16640F, 0.17003F,
0.17364F, 0.17724F, 0.18082F, 0.18439F, 0.18793F,
0.19146F, 0.19497F, 0.19847F, 0.20194F, 0.20540F,
0.20884F, 0.21226F, 0.21566F, 0.21904F, 0.22240F,
0.22575F, 0.22907F, 0.23237F, 0.23565F, 0.23891F,
0.24215F, 0.24537F, 0.24857F, 0.25175F, 0.25490F,
0.25804F, 0.26115F, 0.26424F, 0.26730F, 0.27035F,
0.27337F, 0.27637F, 0.27935F, 0.28230F, 0.28524F,
0.28814F, 0.29103F, 0.29389F, 0.29673F, 0.29955F,
0.30234F, 0.30511F, 0.30785F, 0.31057F, 0.31327F,
0.31594F, 0.31859F, 0.32121F, 0.32381F, 0.32639F,
0.32894F, 0.33147F, 0.33398F, 0.33646F, 0.33891F,
0.34134F, 0.34375F, 0.34614F, 0.34849F, 0.35083F,
0.35314F, 0.35543F, 0.35769F, 0.35993F, 0.36214F,
0.36433F, 0.36650F, 0.36864F, 0.37076F, 0.37286F,
0.37493F, 0.37698F, 0.37900F, 0.38100F, 0.38298F,
0.38493F, 0.38686F, 0.38877F, 0.39065F, 0.39251F,
0.39435F, 0.39617F, 0.39796F, 0.39973F, 0.40147F,
0.40320F, 0.40490F, 0.40658F, 0.40824F, 0.40988F,
0.41149F, 0.41308F, 0.41466F, 0.41621F, 0.41774F,
0.41924F, 0.42073F, 0.42220F, 0.42364F, 0.42507F,
0.42647F, 0.42785F, 0.42922F, 0.43056F, 0.43189F,
0.43319F, 0.43448F, 0.43574F, 0.43699F, 0.43822F,
0.43943F, 0.44062F, 0.44179F, 0.44295F, 0.44408F,
0.44520F, 0.44630F, 0.44738F, 0.44845F, 0.44950F,
0.45053F, 0.45154F, 0.45254F, 0.45352F, 0.45449F,
0.45543F, 0.45637F, 0.45728F, 0.45818F, 0.45907F,
0.45994F, 0.46080F, 0.46164F, 0.46246F, 0.46327F,
0.46407F, 0.46485F, 0.46562F, 0.46638F, 0.46712F,
0.46784F, 0.46856F, 0.46926F, 0.46995F, 0.47062F,
0.47128F, 0.47193F, 0.47257F, 0.47320F, 0.47381F,
0.47441F, 0.47500F, 0.47558F, 0.47615F, 0.47670F,
0.47725F, 0.47778F, 0.47831F, 0.47882F, 0.47932F,
0.47982F, 0.48030F, 0.48077F, 0.48124F, 0.48169F,
0.48214F, 0.48257F, 0.48300F, 0.48341F, 0.48382F,
0.48422F, 0.48461F, 0.48500F, 0.48537F, 0.48574F,
0.48610F, 0.48645F, 0.48679F, 0.48713F, 0.48745F,
0.48778F, 0.48809F, 0.48840F, 0.48870F, 0.48899F,
0.48928F, 0.48956F, 0.48983F, 0.49010F, 0.49036F,
0.49061F, 0.49086F, 0.49111F, 0.49134F, 0.49158F,
0.49180F, 0.49202F, 0.49224F, 0.49245F, 0.49266F,
0.49286F, 0.49305F, 0.49324F, 0.49343F, 0.49361F,
0.49379F, 0.49396F, 0.49413F, 0.49430F, 0.49446F,
0.49461F, 0.49477F, 0.49492F, 0.49506F, 0.49520F,
0.49534F, 0.49547F, 0.49560F, 0.49573F, 0.49585F,
0.49598F, 0.49609F, 0.49621F, 0.49632F, 0.49643F,
0.49653F, 0.49664F, 0.49674F, 0.49683F, 0.49693F,
0.49702F, 0.49711F, 0.49720F, 0.49728F, 0.49736F,
0.49744F, 0.49752F, 0.49760F, 0.49767F, 0.49774F,
0.49781F, 0.49788F, 0.49795F, 0.49801F, 0.49807F,
0.49813F, 0.49819F, 0.49825F, 0.49831F, 0.49836F,
0.49841F, 0.49846F, 0.49851F, 0.49856F, 0.49861F,
0.49865F, 0.49869F, 0.49874F, 0.49878F, 0.49882F,
0.49886F, 0.49889F, 0.49893F, 0.49896F, 0.49900F,
0.49903F, 0.49906F, 0.49910F, 0.49913F, 0.49916F,
0.49918F, 0.49921F, 0.49924F, 0.49926F, 0.49929F,
0.49931F, 0.49934F, 0.49936F, 0.49938F, 0.49940F,
0.49942F, 0.49944F, 0.49946F, 0.49948F, 0.49950F,
0.49952F, 0.49953F, 0.49955F, 0.49957F, 0.49958F,
0.49960F, 0.49961F, 0.49962F, 0.49964F, 0.49965F,
0.49966F, 0.49968F, 0.49969F, 0.49970F, 0.49971F,
0.49972F, 0.49973F, 0.49974F, 0.49975F, 0.49976F,
0.49977F, 0.49978F, 0.49978F, 0.49979F, 0.49980F,
0.49981F, 0.49981F, 0.49982F, 0.49983F, 0.49983F,
0.49984F, 0.49985F, 0.49985F, 0.49986F, 0.49986F,
0.49987F, 0.49987F, 0.49988F, 0.49988F, 0.49989F,
0.49989F, 0.49990F, 0.49990F, 0.49990F, 0.49991F,
0.49991F, 0.49992F, 0.49992F, 0.49992F, 0.49992F,
0.49993F, 0.49993F, 0.49993F, 0.49994F, 0.49994F,