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