annotate toolboxes/distance_learning/mlr/separationOracle/separationOracleKNN.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [Y, Loss] = separationOracleKNN(q, D, pos, neg, k)
Daniel@0 2 %
Daniel@0 3 % [Y,Loss] = separationOracleKNN(q, D, pos, neg, k)
Daniel@0 4 %
Daniel@0 5 % q = index of the query point
Daniel@0 6 % D = the current distance matrix
Daniel@0 7 % pos = indices of relevant results for q
Daniel@0 8 % neg = indices of irrelevant results for q
Daniel@0 9 % k = length of the list to consider
Daniel@0 10 %
Daniel@0 11 % Y is a permutation 1:n corresponding to the maximally
Daniel@0 12 % violated constraint
Daniel@0 13 %
Daniel@0 14 % Loss is the loss for Y, in this case, 1-Prec@k(Y)
Daniel@0 15
Daniel@0 16
Daniel@0 17 % First, sort the documents in descending order of W'Phi(q,x)
Daniel@0 18 % Phi = - (X(q) - X(x)) * (X(q) - X(x))'
Daniel@0 19
Daniel@0 20 % Sort the positive documents
Daniel@0 21 ScorePos = - D(pos,q);
Daniel@0 22 [Vpos, Ipos] = sort(full(ScorePos'), 'descend');
Daniel@0 23 Ipos = pos(Ipos);
Daniel@0 24
Daniel@0 25 % Sort the negative documents
Daniel@0 26 ScoreNeg = - D(neg,q);
Daniel@0 27 [Vneg, Ineg] = sort(full(ScoreNeg'), 'descend');
Daniel@0 28 Ineg = neg(Ineg);
Daniel@0 29
Daniel@0 30 % Now, solve the DP for the interleaving
Daniel@0 31
Daniel@0 32 numPos = length(pos);
Daniel@0 33 numNeg = length(neg);
Daniel@0 34 n = numPos + numNeg;
Daniel@0 35
Daniel@0 36 cVpos = cumsum(Vpos);
Daniel@0 37 cVneg = cumsum(Vneg);
Daniel@0 38
Daniel@0 39
Daniel@0 40 % If we don't have enough positive (or negative) examples, scale k down
Daniel@0 41 k = min([k, numPos, numNeg]);
Daniel@0 42
Daniel@0 43 % Algorithm:
Daniel@0 44 % For each precision score in 0, 1/k, 2/k, ... 1
Daniel@0 45 % Calculate maximum discriminant score for that precision level
Daniel@0 46 KNN = (0:(1/k):1)' > 0.5;
Daniel@0 47 Discriminant = zeros(k+1, 1);
Daniel@0 48 NegsBefore = zeros(numPos,k+1);
Daniel@0 49
Daniel@0 50 % For 0 precision, all positives go after the first k negatives
Daniel@0 51
Daniel@0 52 NegsBefore(:,1) = k + binarysearch(Vpos, Vneg(k+1:end));
Daniel@0 53 Discriminant(1) = Vpos * (numNeg - 2 * NegsBefore(:,1)) + numPos * cVneg(end) ...
Daniel@0 54 - 2 * sum(cVneg(NegsBefore((NegsBefore(:,1) > 0),1)));
Daniel@0 55
Daniel@0 56
Daniel@0 57
Daniel@0 58 % For precision (a-1)/k, swap the (a-1)'th positive doc
Daniel@0 59 % into the top (k-a) negative docs
Daniel@0 60
Daniel@0 61 for a = 2:(k+1)
Daniel@0 62 NegsBefore(:,a) = NegsBefore(:,a-1);
Daniel@0 63
Daniel@0 64 % We have a-1 positives, and k - (a-1) negatives
Daniel@0 65 NegsBefore(a-1, a) = binarysearch(Vpos(a-1), Vneg(1:(k-a+1)));
Daniel@0 66
Daniel@0 67 % There were NegsBefore(a-1,a-1) negatives before (a-1)
Daniel@0 68 % Now there are NegsBefore(a,a-1)
Daniel@0 69
Daniel@0 70 Discriminant(a) = Discriminant(a-1) ...
Daniel@0 71 + 2 * (NegsBefore(a-1,a-1) - NegsBefore(a-1,a)) * Vpos(a-1);
Daniel@0 72
Daniel@0 73 if NegsBefore(a-1,a-1) > 0
Daniel@0 74 Discriminant(a) = Discriminant(a) + 2 * cVneg(NegsBefore(a-1,a-1));
Daniel@0 75 end
Daniel@0 76 if NegsBefore(a-1,a) > 0
Daniel@0 77 Discriminant(a) = Discriminant(a) - 2 * cVneg(NegsBefore(a-1,a));
Daniel@0 78 end
Daniel@0 79 end
Daniel@0 80
Daniel@0 81 % Normalize discriminant scores
Daniel@0 82 Discriminant = Discriminant / (numPos * numNeg);
Daniel@0 83 [s, x] = max(Discriminant - KNN);
Daniel@0 84
Daniel@0 85 % Now we know that there are x-1 relevant docs in the max ranking
Daniel@0 86 % Construct Y from NegsBefore(x,:)
Daniel@0 87
Daniel@0 88 Y = nan * ones(n,1);
Daniel@0 89 Y((1:numPos)' + NegsBefore(:,x)) = Ipos;
Daniel@0 90 if sum(isnan(Y)) ~= length(Ineg)
Daniel@0 91 keyboard;
Daniel@0 92 end
Daniel@0 93 Y(isnan(Y)) = Ineg;
Daniel@0 94
Daniel@0 95 % Compute loss for this list
Daniel@0 96 Loss = 1 - KNN(x);
Daniel@0 97 end
Daniel@0 98