function [overlap] = getoverlap(posscores,negscores); % fits a decision boundary at 45 degrees and, roughly speaking, adds up % how far on the wrong side of this boundary the test points are using an % appropriate scale. % One of the two classes is called "pos" and the other is "neg" % It scales the distances to minimize a negative log prob cost, where the % probability of giving a datapoint the "pos" label is given by % 1/(1+exp(-distance/scale)) % and "distance" is how far the point is from the decision boundary on % the positive side. We want p(pos) to be big for pos cases and small % for neg cases. % posscores is the scores under the "pos" model minus the scores under the % "neg" model for the "pos" cases; % negscores is the same quantities for the neg cases. % If posscores were all positive and negscores were all negative, we % would make the scale very small so the probabilities were all 1 or 0 % and the overlap would be zero. If the classes overlap a lot its best to % use a big scale so that the probabilities are all very soft. tiny=10^(-50); adjusttemp=1; xpos=posscores/2; %the scale should not matter but this prevents problems. xneg=negscores/2; %spread=max([xpos;xneg])-min([xpos;xneg]); %xpos=xpos/spread; %xneg=xneg/spread; thresh=0; z=0; % temperature=exp(-z); improvement=1; oldcost=exp(100); %to start the while loop while improvement>.000001 beta=exp(z); posderiv=sum( 1./(1+exp(-beta*(thresh-xpos))) ); negderiv= - sum( 1./(1+exp(-beta*(xneg-thresh))) ); deriv=posderiv+negderiv; %we ignore a factor of exp(z) in these derivs to help stability; thresh=thresh - .01*deriv; poscost=sum(-log(tiny+1./(1+exp(-beta*(xpos-thresh))))); negcost=sum(-log(tiny+1./(1+exp(-beta*(thresh-xneg))))); cost=negcost+poscost; % fprintf(1, 'thresh= %4.6f temp= %4.6f bits= %4.4f \n', ... % thresh, exp(-z), cost/log(2)); improvement=oldcost-cost; oldcost=cost; end; if adjusttemp==1 improvement=1; oldcost=exp(100); %to start the while loop while improvement>.00001 beta=exp(z); posderiv=sum( 1./(1+exp(-beta*(thresh-xpos))) ); negderiv= - sum( 1./(1+exp(-beta*(xneg-thresh))) ); deriv=posderiv+negderiv; %we ignore a factor of exp(z) in these derivs to help stability; thresh=thresh - .01*deriv; poszderiv= sum( beta*(thresh-xpos) .* 1./(1+exp(-beta*(thresh-xpos))) ); negzderiv= sum( beta*(xneg-thresh) .* 1./(1+exp(-beta*(xneg-thresh))) ); zderiv=poszderiv+negzderiv; %we ignore a factor of exp(z) in these derivs to help stability; z = z - .01*zderiv; poscost=sum(-log(tiny+1./(1+exp(-beta*(xpos-thresh))))); negcost=sum(-log(tiny+1./(1+exp(-beta*(thresh-xneg))))); cost=negcost+poscost; % fprintf(1, 'thresh= %4.6f temp= %4.6f bits= %4.4f \n', ... % thresh, exp(-z), cost/log(2)); improvement=oldcost-cost; oldcost=cost; end; end; overlap= cost/log(2);