annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@discrete_CPD/prob_node.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 [P, p] = prob_node(CPD, self_ev, pev)
wolffd@0 2 % PROB_NODE Compute prod_m P(x(i,m)| x(pi_i,m), theta_i) for node i (discrete)
wolffd@0 3 % [P, p] = prob_node(CPD, self_ev, pev)
wolffd@0 4 %
wolffd@0 5 % self_ev(m) is the evidence on this node in case m.
wolffd@0 6 % pev(i,m) is the evidence on the i'th parent in case m (if there are any parents).
wolffd@0 7 % (These may also be cell arrays.)
wolffd@0 8 %
wolffd@0 9 % p(m) = P(x(i,m)| x(pi_i,m), theta_i)
wolffd@0 10 % P = prod p(m)
wolffd@0 11
wolffd@0 12 if iscell(self_ev), usecell = 1; else usecell = 0; end
wolffd@0 13
wolffd@0 14 ncases = length(self_ev);
wolffd@0 15 sz = dom_sizes(CPD);
wolffd@0 16
wolffd@0 17 nparents = length(sz)-1;
wolffd@0 18 if nparents == 0
wolffd@0 19 assert(isempty(pev));
wolffd@0 20 else
wolffd@0 21 assert(isequal(size(pev), [nparents ncases]));
wolffd@0 22 end
wolffd@0 23
wolffd@0 24 n = length(sz);
wolffd@0 25 dom = 1:n;
wolffd@0 26 p = zeros(1, ncases);
wolffd@0 27 if isa(CPD, 'tabular_CPD')
wolffd@0 28 % speed up by looking up CPT using index Zhang Yimin 2001-12-31
wolffd@0 29 if usecell
wolffd@0 30 if nparents == 0
wolffd@0 31 data = [cell2num(self_ev)];
wolffd@0 32 else
wolffd@0 33 data = [cell2num(pev); cell2num(self_ev)];
wolffd@0 34 end
wolffd@0 35 else
wolffd@0 36 if nparents == 0
wolffd@0 37 data = [self_ev];
wolffd@0 38 else
wolffd@0 39 data = [pev; self_ev];
wolffd@0 40 end
wolffd@0 41 end
wolffd@0 42
wolffd@0 43 indices = subv2ind(sz, data'); % each row of data' is a case
wolffd@0 44
wolffd@0 45 CPT=CPD_to_CPT(CPD);
wolffd@0 46 p = CPT(indices);
wolffd@0 47
wolffd@0 48 %get the prob list
wolffd@0 49 %cpt_size = prod(sz);
wolffd@0 50 %prob_list=reshape(CPT, cpt_size, 1);
wolffd@0 51 %for m=1:ncases %here we assume we get evidence for node and all its parents
wolffd@0 52 % idx=indices(m);
wolffd@0 53 % p(m)=prob_list(idx);
wolffd@0 54 %end
wolffd@0 55
wolffd@0 56 else % eg. softmax
wolffd@0 57
wolffd@0 58 for m=1:ncases
wolffd@0 59 if usecell
wolffd@0 60 if nparents == 0
wolffd@0 61 evidence = {self_ev{m}};
wolffd@0 62 else
wolffd@0 63 evidence = cell(1,n);
wolffd@0 64 evidence(1:n-1) = pev(:,m);
wolffd@0 65 evidence(n) = self_ev(m);
wolffd@0 66 end
wolffd@0 67 else
wolffd@0 68 if nparents == 0
wolffd@0 69 evidence = num2cell(self_ev(m));
wolffd@0 70 else
wolffd@0 71 evidence = num2cell([pev(:,m)', self_ev(m)]);
wolffd@0 72 end
wolffd@0 73 end
wolffd@0 74 T = convert_to_table(CPD, dom, evidence);
wolffd@0 75 p(m) = T;
wolffd@0 76 end
wolffd@0 77 end
wolffd@0 78
wolffd@0 79 P = prod(p);
wolffd@0 80
wolffd@0 81