annotate toolboxes/FullBNT-1.0.7/bnt/general/convert_dbn_CPDs_to_tables1.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 CPDpot = convert_dbn_CPDs_to_tables1(bnet, evidence)
wolffd@0 2 % CONVERT_DBN_CPDS_TO_TABLES Convert CPDs of (possibly instantiated) DBN nodes to tables
wolffd@0 3 % CPDpot = convert_dbn_CPDs_to_tables(bnet, evidence)
wolffd@0 4 %
wolffd@0 5 % CPDpot{n,t} is a table containing P(n,t|pa(n,t), ev)
wolffd@0 6 % All hidden nodes are assumed to be discrete
wolffd@0 7 % We assume the observed nodes are the same in every slice
wolffd@0 8 %
wolffd@0 9 % Evaluating the conditional likelihood of the evidence can be very slow,
wolffd@0 10 % so we take pains to vectorize where possible, i.e., we try to avoid
wolffd@0 11 % calling convert_to_table
wolffd@0 12
wolffd@0 13 [ss T] = size(evidence);
wolffd@0 14 %obs_bitv = ~isemptycell(evidence(:));
wolffd@0 15 obs_bitv = zeros(1, 2*ss);
wolffd@0 16 obs_bitv(bnet.observed) = 1;
wolffd@0 17 obs_bitv(bnet.observed+ss) = 1;
wolffd@0 18
wolffd@0 19 ns = bnet.node_sizes(:);
wolffd@0 20 CPDpot = cell(ss,T);
wolffd@0 21
wolffd@0 22 for n=1:ss
wolffd@0 23 % slice 1
wolffd@0 24 t = 1;
wolffd@0 25 ps = parents(bnet.dag, n);
wolffd@0 26 e = bnet.equiv_class(n, 1);
wolffd@0 27 if ~any(obs_bitv(ps))
wolffd@0 28 CPDpot{n,t} = convert_CPD_to_table_hidden_ps(bnet.CPD{e}, evidence{n,t});
wolffd@0 29 else
wolffd@0 30 CPDpot{n,t} = convert_to_table(bnet.CPD{e}, [ps n], evidence(:,1));
wolffd@0 31 end
wolffd@0 32
wolffd@0 33 % slices 2..T
wolffd@0 34 debug = 1;
wolffd@0 35 if ~obs_bitv(n)
wolffd@0 36 CPDpot = helper_hidden_child(bnet, evidence, n, CPDpot, obs_bitv, debug);
wolffd@0 37 else
wolffd@0 38 CPDpot = helper_obs_child(bnet, evidence, n, CPDpot, obs_bitv, debug);
wolffd@0 39 end
wolffd@0 40 end
wolffd@0 41
wolffd@0 42 if 0
wolffd@0 43 CPDpot2 = convert_dbn_CPDs_to_tables_slow(bnet, evidence);
wolffd@0 44 for t=1:T
wolffd@0 45 for n=1:ss
wolffd@0 46 if ~approxeq(CPDpot{n,t}, CPDpot2{n,t})
wolffd@0 47 fprintf('CPDpot n=%d, t=%d\n',n,t);
wolffd@0 48 keyboard
wolffd@0 49 end
wolffd@0 50 end
wolffd@0 51 end
wolffd@0 52 end
wolffd@0 53
wolffd@0 54
wolffd@0 55 % special cases: c=child, p=parents, d=discrete, h=hidden, 1=1slice
wolffd@0 56 % if c=h=1 then c=d=1, since hidden nodes must be discrete
wolffd@0 57 % c=h c=d p=h p=d p=1 method
wolffd@0 58 % ---------------------------
wolffd@0 59 % 1 1 1 1 - replicate CPT
wolffd@0 60 % 0 1 1 1 1 dhmm
wolffd@0 61 % 0 0 1 1 1 ghmm
wolffd@0 62 % - 1 - 1 - evaluate CPT on evidence
wolffd@0 63 % other loop
wolffd@0 64
wolffd@0 65 %%%%%%%
wolffd@0 66 function CPDpot = helper_hidden_child(bnet, evidence, n, CPDpot, obs_bitv, debug)
wolffd@0 67
wolffd@0 68 [ss T] = size(evidence);
wolffd@0 69 self = n+ss;
wolffd@0 70 ps = parents(bnet.dag, self);
wolffd@0 71 e = bnet.equiv_class(n, 2);
wolffd@0 72 ns = bnet.node_sizes(:);
wolffd@0 73 if ~any(obs_bitv(ps)) % all parents are hidden (hence discrete)
wolffd@0 74 if debug, fprintf('node %d is hidden, all ps are hidden\n', n); end
wolffd@0 75 if myismember(n, bnet.dnodes)
wolffd@0 76 %CPT = CPD_to_CPT(bnet.CPD{e});
wolffd@0 77 %CPT = reshape(CPT, [prod(ns(ps)) ns(self)]);
wolffd@0 78 CPT = convert_CPD_to_table_hidden_ps(bnet.CPD{e}, []);
wolffd@0 79 CPDpot(n,2:T) = num2cell(repmat(CPT, [1 1 T-1]), [1 2]);
wolffd@0 80 else
wolffd@0 81 error(['hidden cts node disallowed'])
wolffd@0 82 end
wolffd@0 83 else % some parents are observed - slow
wolffd@0 84 if mysubset(ps, bnet.dnodes) % all parents are discrete
wolffd@0 85 % given CPT(p1, p2, p3, p4, c), where p1,p3 are observed
wolffd@0 86 % we create CPT([p2 p4 c], [p1 p3]).
wolffd@0 87 % We then convert all observed p1,p3 into indices ndx
wolffd@0 88 % and return CPT(:, ndx)
wolffd@0 89 CPT = CPD_to_CPT(bnet.CPD{e});
wolffd@0 90 domain = [ps self];
wolffd@0 91 % if dom is [3 7 8] and 3,8 are observed, odom_rel = [1 3], hdom_rel = 2,
wolffd@0 92 % odom = [3 8], hdom = 7
wolffd@0 93 odom_rel = find(obs_bitv(domain));
wolffd@0 94 hdom_rel = find(~obs_bitv(domain));
wolffd@0 95 odom = domain(odom_rel);
wolffd@0 96 hdom = domain(hdom_rel);
wolffd@0 97 CPT = permute(CPT, [hdom_rel odom_rel]);
wolffd@0 98 CPT = reshape(CPT, prod(ns(hdom)), prod(ns(odom)));
wolffd@0 99 parents_in_same_slice = all(ps > ss);
wolffd@0 100 if parents_in_same_slice
wolffd@0 101 if debug, fprintf('node %d is hidden, some ps are obs, all ps discrete, 1 slice\n', n); end
wolffd@0 102 data = cell2num(evidence(odom-ss,2:T)); %data(i,t) = val of i'th obs parent at t+1
wolffd@0 103 else
wolffd@0 104 if debug, fprintf('node %d is hidden, some ps are obs, all ps discrete, 2 slice\n', n); end
wolffd@0 105 data = zeros(length(odom), T-1);
wolffd@0 106 for t=2:T
wolffd@0 107 ev = evidence(:,t-1:t);
wolffd@0 108 data(:,t-1) = cell2num(ev(odom));
wolffd@0 109 end
wolffd@0 110 end
wolffd@0 111 ndx = subv2ind(ns(odom), data'); % ndx(t) encodes data(:,t)
wolffd@0 112 CPDpot(n,2:T) = num2cell(CPT(:, ndx), [1 2]);
wolffd@0 113 else % some parents are cts - v slow
wolffd@0 114 if debug, fprintf('node %d is hidden, some ps are obs, some ps cts\n', n); end
wolffd@0 115 for t=2:T
wolffd@0 116 CPDpot{n,t} = convert_to_table(bnet.CPD{e}, [ps self], evidence(:,t-1:t));
wolffd@0 117 end
wolffd@0 118 end
wolffd@0 119 end
wolffd@0 120
wolffd@0 121 %%%%%%%
wolffd@0 122 function CPDpot = helper_obs_child(bnet, evidence, n, CPDpot, obs_bitv, debug)
wolffd@0 123
wolffd@0 124 [ss T] = size(evidence);
wolffd@0 125 self = n+ss;
wolffd@0 126 ps = parents(bnet.dag, self);
wolffd@0 127 e = bnet.equiv_class(n, 2);
wolffd@0 128 ns = bnet.node_sizes(:);
wolffd@0 129 if ~any(obs_bitv(ps)) % all parents are hidden
wolffd@0 130 parents_in_same_slice = all(ps > ss);
wolffd@0 131 if parents_in_same_slice
wolffd@0 132 if debug, fprintf('node %d is obs, all ps are hidden, 1 slice\n', n); end
wolffd@0 133 ps1 = ps - ss;
wolffd@0 134 if myismember(n, bnet.dnodes)
wolffd@0 135 CPT = CPD_to_CPT(bnet.CPD{e});
wolffd@0 136 CPT = reshape(CPT, [prod(ns(ps)) ns(self)]); % what if no parents?
wolffd@0 137 obslik = eval_pdf_cond_multinomial(cell2num(evidence(n,2:T)), CPT);
wolffd@0 138 CPDpot(n,2:T) = num2cell(obslik, 1);
wolffd@0 139 else
wolffd@0 140 S = struct(bnet.CPD{e});
wolffd@0 141 obslik = eval_pdf_cond_gauss(cell2num(evidence(n,2:T)), S.mean, S.cov);
wolffd@0 142 CPDpot(n,2:T) = num2cell(obslik, 1);
wolffd@0 143 end
wolffd@0 144 else % parents span 2 slices - slow
wolffd@0 145 if debug, fprintf('node %d is obs, all ps are hidden , 2 slice\n', n); end
wolffd@0 146 for t=2:T
wolffd@0 147 CPDpot{n,t} = convert_to_table(bnet.CPD{e}, [ps self], evidence(:,t-1:t));
wolffd@0 148 end
wolffd@0 149 end
wolffd@0 150 else
wolffd@0 151 if isempty(ps) % observed root
wolffd@0 152 if debug, fprintf('node %d is obs, no ps\n', n); end
wolffd@0 153 CPT = CPD_to_CPT(bnet.CPD{e});
wolffd@0 154 data = cell2num(evidence(n,2:T));
wolffd@0 155 CPDpot(n,2:T) = CPT(data);
wolffd@0 156 else % some parents are observed - slow
wolffd@0 157 if debug, fprintf('node %d is obs, some ps are obs\n', n); end
wolffd@0 158 for t=2:T
wolffd@0 159 CPDpot{n,t} = convert_to_table(bnet.CPD{e}, [ps self], evidence(:,t-1:t));
wolffd@0 160 end
wolffd@0 161 end
wolffd@0 162 end