annotate toolboxes/distance_learning/mlr/separationOracle/separationOracleNDCG.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] = separationOracleNDCG(q, D, pos, neg, k)
Daniel@0 2 %
Daniel@0 3 % [Y,Loss] = separationOracleNDCG(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-NDCG(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 % From Chakrabarti (KDD08)
Daniel@0 37 k = min(k, numPos);
Daniel@0 38
Daniel@0 39 cVneg = cumsum(Vneg);
Daniel@0 40
Daniel@0 41 Discount = zeros(k, 1);
Daniel@0 42 Discount(1:2) = 1;
Daniel@0 43 Discount(3:k) = 1./ log2(3:k);
Daniel@0 44
Daniel@0 45 DCGstar = sum(Discount);
Daniel@0 46
Daniel@0 47
Daniel@0 48 % Pre-compute the loss table
Daniel@0 49 LossTab = padarray( hankel(- Discount / DCGstar), ...
Daniel@0 50 max(0, [numNeg numPos] - k), 0, 'post');
Daniel@0 51 if sum(size(LossTab) > [numNeg, numPos])
Daniel@0 52 LossTab = LossTab(1:numNeg, 1:numPos);
Daniel@0 53 end
Daniel@0 54
Daniel@0 55 % 2010-01-17 09:13:41 by Brian McFee <bmcfee@cs.ucsd.edu>
Daniel@0 56 % initialize the score table
Daniel@0 57
Daniel@0 58 pcVneg = [0 cVneg];
Daniel@0 59 % Pre-compute cellScore
Daniel@0 60 cellValue = bsxfun(@times, Vpos / (numPos * numNeg), numNeg - 2 * ((1:numNeg)-1)');
Daniel@0 61 cellValue = bsxfun(@plus, (2 * pcVneg(1:numNeg) - cVneg(end))' / (numPos * numNeg), cellValue);
Daniel@0 62 cellValue = cellValue + LossTab;
Daniel@0 63
Daniel@0 64 S = zeros(numNeg, numPos);
Daniel@0 65 P = zeros(numNeg, numPos);
Daniel@0 66
Daniel@0 67 % Initialize first column
Daniel@0 68 P(:,1) = 1;
Daniel@0 69 S(:,1) = cellValue(:,1);
Daniel@0 70
Daniel@0 71 % Initialize first row
Daniel@0 72 P(1,:) = 1;
Daniel@0 73 S(1,:) = cumsum(cellValue(1,:));
Daniel@0 74
Daniel@0 75 % For the rest, use the recurrence
Daniel@0 76
Daniel@0 77 for g = 2:numPos
Daniel@0 78 [m, pointer] = cummax(S(:,g-1));
Daniel@0 79 P(:,g) = pointer;
Daniel@0 80 S(:,g) = m' + cellValue(:,g);
Daniel@0 81 end
Daniel@0 82
Daniel@0 83 % Now reconstruct the permutation from the DP table
Daniel@0 84 Y = nan * ones(n,1);
Daniel@0 85 [m,p] = max(S(:,numPos));
Daniel@0 86
Daniel@0 87 Loss = 1 + LossTab(p,numPos);
Daniel@0 88
Daniel@0 89 NegsBefore = zeros(numPos,1);
Daniel@0 90 NegsBefore(numPos) = p-1;
Daniel@0 91
Daniel@0 92 for a = numPos:-1:2
Daniel@0 93 p = P(p,a);
Daniel@0 94 NegsBefore(a-1) = p-1;
Daniel@0 95 Loss = Loss + LossTab(p,a-1);
Daniel@0 96 end
Daniel@0 97 Y((1:numPos)' + NegsBefore) = Ipos;
Daniel@0 98 Y(isnan(Y)) = Ineg;
Daniel@0 99
Daniel@0 100 end