annotate toolboxes/FullBNT-1.0.7/KPMstats/logistK_eval.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [post,lik,lli] = logistK_eval(beta,x,y)
wolffd@0 2 % [post,lik,lli] = logistK_eval(beta,x,y)
wolffd@0 3 %
wolffd@0 4 % Evaluate logistic regression model.
wolffd@0 5 %
wolffd@0 6 % INPUT
wolffd@0 7 % beta dxk model coefficients (as returned by logistK)
wolffd@0 8 % x dxn matrix of n input column vectors
wolffd@0 9 % [y] kxn vector of class assignments
wolffd@0 10 %
wolffd@0 11 % OUTPUT
wolffd@0 12 % post kxn fitted class posteriors
wolffd@0 13 % lik 1xn vector of sample likelihoods
wolffd@0 14 % lli log likelihood
wolffd@0 15 %
wolffd@0 16 % Let p(i,j) = exp(beta(:,j)'*x(:,i)),
wolffd@0 17 % Class j posterior for observation i is:
wolffd@0 18 % post(j,i) = p(i,j) / (p(i,1) + ... p(i,k))
wolffd@0 19 % The likelihood of observation i given soft class assignments
wolffd@0 20 % y(:,i) is:
wolffd@0 21 % lik(i) = prod(post(:,i).^y(:,i))
wolffd@0 22 % The log-likelihood of the model given the labeled samples is:
wolffd@0 23 % lli = sum(log(lik))
wolffd@0 24 %
wolffd@0 25 % See also logistK.
wolffd@0 26 %
wolffd@0 27 % David Martin <dmartin@eecs.berkeley.edu>
wolffd@0 28 % May 7, 2002
wolffd@0 29
wolffd@0 30 % Copyright (C) 2002 David R. Martin <dmartin@eecs.berkeley.edu>
wolffd@0 31 %
wolffd@0 32 % This program is free software; you can redistribute it and/or
wolffd@0 33 % modify it under the terms of the GNU General Public License as
wolffd@0 34 % published by the Free Software Foundation; either version 2 of the
wolffd@0 35 % License, or (at your option) any later version.
wolffd@0 36 %
wolffd@0 37 % This program is distributed in the hope that it will be useful, but
wolffd@0 38 % WITHOUT ANY WARRANTY; without even the implied warranty of
wolffd@0 39 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
wolffd@0 40 % General Public License for more details.
wolffd@0 41 %
wolffd@0 42 % You should have received a copy of the GNU General Public License
wolffd@0 43 % along with this program; if not, write to the Free Software
wolffd@0 44 % Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
wolffd@0 45 % 02111-1307, USA, or see http://www.gnu.org/copyleft/gpl.html.
wolffd@0 46
wolffd@0 47 error(nargchk(2,3,nargin));
wolffd@0 48
wolffd@0 49 % check sizes
wolffd@0 50 if size(beta,1) ~= size(x,1),
wolffd@0 51 error('Inputs beta,x not the same height.');
wolffd@0 52 end
wolffd@0 53 if nargin > 3 & size(y,2) ~= size(x,2),
wolffd@0 54 error('Inputs x,y not the same length.');
wolffd@0 55 end
wolffd@0 56
wolffd@0 57 % get sizes
wolffd@0 58 [d,k] = size(beta);
wolffd@0 59 [d,n] = size(x);
wolffd@0 60
wolffd@0 61 % class posteriors
wolffd@0 62 post = zeros(k,n);
wolffd@0 63 bx = zeros(k,n);
wolffd@0 64 for j = 1:k,
wolffd@0 65 bx(j,:) = beta(:,j)'*x;
wolffd@0 66 end
wolffd@0 67 for j = 1:k,
wolffd@0 68 post(j,:) = 1 ./ sum(exp(bx - repmat(bx(j,:),k,1)),1);
wolffd@0 69 end
wolffd@0 70 clear bx;
wolffd@0 71
wolffd@0 72 % likelihood of each sample
wolffd@0 73 if nargout > 1,
wolffd@0 74 y = y ./ repmat(sum(y,1),k,1); % L1-normalize class assignments
wolffd@0 75 lik = prod(post.^y,1);
wolffd@0 76 end
wolffd@0 77
wolffd@0 78 % total log likelihood
wolffd@0 79 if nargout > 2,
wolffd@0 80 lli = sum(log(lik+eps));
wolffd@0 81 end;
wolffd@0 82
wolffd@0 83 % eof