comparison toolboxes/distance_learning/mlr/separationOracle/separationOracleMRR.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function [Y, Loss] = separationOracleMRR(q, D, pos, neg, k)
2 %
3 % [Y,Loss] = separationOracleMRR(q, D, pos, neg, k)
4 %
5 % q = index of the query point
6 % D = the current distance matrix
7 % pos = indices of relevant results for q
8 % neg = indices of irrelevant results for q
9 % k = length of the list to consider (unused in MRR)
10 %
11 % Y is a permutation 1:n corresponding to the maximally
12 % violated constraint
13 %
14 % Loss is the loss for Y, in this case, 1-MRR(Y)
15
16
17 % First, sort the documents in descending order of W'Phi(q,x)
18 % Phi = - (X(q) - X(x)) * (X(q) - X(x))'
19
20 % Sort the positive documents
21 ScorePos = - D(pos,q);
22 [Vpos, Ipos] = sort(full(ScorePos'), 'descend');
23 Ipos = pos(Ipos);
24
25 % Sort the negative documents
26 ScoreNeg = -D(neg,q);
27 [Vneg, Ineg] = sort(full(ScoreNeg'), 'descend');
28 Ineg = neg(Ineg);
29
30 % Now, solve the DP for the interleaving
31
32 numPos = length(pos);
33 numNeg = length(neg);
34 n = numPos + numNeg;
35
36 cVpos = cumsum(Vpos);
37 cVneg = cumsum(Vneg);
38
39
40 % Algorithm:
41 % For each RR score in 1/1, 1/2, ..., 1/(numNeg+1)
42 % Calculate maximum discriminant score for that precision level
43 MRR = ((1:(numNeg+1)).^-1)';
44
45
46 Discriminant = zeros(numNeg+1, 1);
47 Discriminant(end) = numPos * cVneg(end) - numNeg * cVpos(end);
48
49 % For the rest of the positions, we're interleaving one more negative
50 % example into the 2nd-through-last positives
51 offsets = 1 + binarysearch(Vneg, Vpos(2:end));
52
53 % How many of the remaining positives go before Vneg(a)?
54 NegsBefore = -bsxfun(@ge, offsets, (1:length(Vpos))');
55
56 % For the last position, all negatives come before all positives
57 NegsBefore(:,numNeg+1) = numNeg;
58
59 Discriminant(1:numNeg) = -2 * (offsets .* Vneg - cVpos(offsets));
60 Discriminant = sum(Discriminant) - cumsum(Discriminant) + Discriminant;
61
62
63 % Normalize discriminant scores
64 Discriminant = Discriminant / (numPos * numNeg);
65 [s, x] = max(Discriminant - MRR);
66
67 % Now we know that there are x-1 relevant docs in the max ranking
68 % Construct Y from NegsBefore(x,:)
69
70 Y = nan * ones(n,1);
71 Y((1:numPos)' + sum(NegsBefore(:,x:end),2)) = Ipos;
72 Y(isnan(Y)) = Ineg;
73
74 % Compute loss for this list
75 Loss = 1 - MRR(x);
76 end
77