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