function [estClass] = calcCentroidalParametricClass(testMat, db, classPriors) % calcParametricClass % assumes similarity measure is Hamming similarity % = counting common features. % output is a vector of class Labels corresponding to the test points % which are rows of testMat % % models the similarity s(x, mu1) by an exponential distribution % (which is max ent/max likelihood distribution) % then compares log posterior probability of each class. % test = test feature matrix where each row is a test feature vector % db = database of all training vectors [numTrain, prenumFeatures] = size(db); [numTest, junk] = size(testMat); numFeatures = prenumFeatures-1; trainLabel = db(:, prenumFeatures); numClasses = length(classPriors); %%%%%%% GET PARAMETRIC MODEL FOR EACH CLASS %%%%%%%%%%% % first we need a representative guy for each class for g = 1:numClasses index = getMostRepElement(db(find(db(:,prenumFeatures) ==g), 1:numFeatures)); muVect(g, :) = db(index,1:numFeatures); end % now we need to know what the descriptive statistic is. % for each class % here we assume the descriptive statistic is % mean sim(c1, mu1) for all points c1 in class c1. classMean = zeros(numClasses,1); % initialize to zero countClass = zeros(numClasses, 1); % initialize count to zero for j= 1:numTrain % get the similarity between the jth object in the database and % its class centroid classMean(trainLabel(j)) = classMean(trainLabel(j)) + getSimCounting(db(j,1:numFeatures), muVect(trainLabel(j), :)); countClass(trainLabel(j)) = countClass(trainLabel(j)) +1; end % final class means are: classMean = classMean./countClass; % since this is counting measure, the domain of possible % similarities is 0,1,2, ... numFeatures domainVector = 0:numFeatures; % Now numerically optimize the lambda parameter in the % exponential model for g = 1:numClasses lambda(g) = findLambda(classMean(g), domainVector); end % compute the normalization constants normC as well for g = 1:numClasses normC(g) = 1/(sum( exp(lambda(g)*domainVector))); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%% ITERATE THROUGH TEST POINTS %%%%%%%%%%%% for i = 1:numTest x = testMat(i, :); % pull off an x. % get simTest = s(x, classmean) for each class % and figure out how probable that similarity is for g = 1:numClasses simTest(g) = getSimCounting(x, muVect(g,:)); end % Now compute the log posterior for each class: logPosteriors = lambda.*simTest + log(normC) + log(classPriors); % classify as whoever has the largest logPosterior % this assumes 0-1 costs [who, estClass(i)] = max(logPosteriors); end %iteration over test points %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%