wolffd@0: function [post,lik,lli] = logistK_eval(beta,x,y) wolffd@0: % [post,lik,lli] = logistK_eval(beta,x,y) wolffd@0: % wolffd@0: % Evaluate logistic regression model. wolffd@0: % wolffd@0: % INPUT wolffd@0: % beta dxk model coefficients (as returned by logistK) wolffd@0: % x dxn matrix of n input column vectors wolffd@0: % [y] kxn vector of class assignments wolffd@0: % wolffd@0: % OUTPUT wolffd@0: % post kxn fitted class posteriors wolffd@0: % lik 1xn vector of sample likelihoods wolffd@0: % lli log likelihood wolffd@0: % wolffd@0: % Let p(i,j) = exp(beta(:,j)'*x(:,i)), wolffd@0: % Class j posterior for observation i is: wolffd@0: % post(j,i) = p(i,j) / (p(i,1) + ... p(i,k)) wolffd@0: % The likelihood of observation i given soft class assignments wolffd@0: % y(:,i) is: wolffd@0: % lik(i) = prod(post(:,i).^y(:,i)) wolffd@0: % The log-likelihood of the model given the labeled samples is: wolffd@0: % lli = sum(log(lik)) wolffd@0: % wolffd@0: % See also logistK. wolffd@0: % wolffd@0: % David Martin wolffd@0: % May 7, 2002 wolffd@0: wolffd@0: % Copyright (C) 2002 David R. Martin wolffd@0: % wolffd@0: % This program is free software; you can redistribute it and/or wolffd@0: % modify it under the terms of the GNU General Public License as wolffd@0: % published by the Free Software Foundation; either version 2 of the wolffd@0: % License, or (at your option) any later version. wolffd@0: % wolffd@0: % This program is distributed in the hope that it will be useful, but wolffd@0: % WITHOUT ANY WARRANTY; without even the implied warranty of wolffd@0: % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU wolffd@0: % General Public License for more details. wolffd@0: % wolffd@0: % You should have received a copy of the GNU General Public License wolffd@0: % along with this program; if not, write to the Free Software wolffd@0: % Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA wolffd@0: % 02111-1307, USA, or see http://www.gnu.org/copyleft/gpl.html. wolffd@0: wolffd@0: error(nargchk(2,3,nargin)); wolffd@0: wolffd@0: % check sizes wolffd@0: if size(beta,1) ~= size(x,1), wolffd@0: error('Inputs beta,x not the same height.'); wolffd@0: end wolffd@0: if nargin > 3 & size(y,2) ~= size(x,2), wolffd@0: error('Inputs x,y not the same length.'); wolffd@0: end wolffd@0: wolffd@0: % get sizes wolffd@0: [d,k] = size(beta); wolffd@0: [d,n] = size(x); wolffd@0: wolffd@0: % class posteriors wolffd@0: post = zeros(k,n); wolffd@0: bx = zeros(k,n); wolffd@0: for j = 1:k, wolffd@0: bx(j,:) = beta(:,j)'*x; wolffd@0: end wolffd@0: for j = 1:k, wolffd@0: post(j,:) = 1 ./ sum(exp(bx - repmat(bx(j,:),k,1)),1); wolffd@0: end wolffd@0: clear bx; wolffd@0: wolffd@0: % likelihood of each sample wolffd@0: if nargout > 1, wolffd@0: y = y ./ repmat(sum(y,1),k,1); % L1-normalize class assignments wolffd@0: lik = prod(post.^y,1); wolffd@0: end wolffd@0: wolffd@0: % total log likelihood wolffd@0: if nargout > 2, wolffd@0: lli = sum(log(lik+eps)); wolffd@0: end; wolffd@0: wolffd@0: % eof