annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@deterministic_CPD/deterministic_CPD.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 CPD = deterministic_CPD(bnet, self, fname, pfail)
wolffd@0 2 % DETERMINISTIC_CPD Make a tabular CPD representing a (noisy) deterministic function
wolffd@0 3 %
wolffd@0 4 % CPD = deterministic_CPD(bnet, self, fname)
wolffd@0 5 % This calls feval(fname, pvals) for each possible vector of parent values.
wolffd@0 6 % e.g., suppose there are 2 ternary parents, then pvals =
wolffd@0 7 % [1 1], [2 1], [3 1], [1 2], [2 2], [3 2], [1 3], [2 3], [3 3]
wolffd@0 8 % If v = feval(fname, pvals(i)), then
wolffd@0 9 % CPD(x | parents=pvals(i)) = 1 if x==v, and = 0 if x<>v
wolffd@0 10 % e.g., suppose X4 = X2 AND (NOT X3). Then
wolffd@0 11 % bnet.CPD{4} = deterministic_CPD(bnet, 4, inline('((x(1)-1) & ~(x(2)-1)) + 1'));
wolffd@0 12 % Note that x(1) refers pvals(1) = X2, and x(2) refers to pvals(2)=X3
wolffd@0 13 % See also boolean_CPD.
wolffd@0 14 %
wolffd@0 15 % CPD = deterministic_CPD(bnet, self, fname, pfail)
wolffd@0 16 % will put probability mass 1-pfail on f(parents), and distribute pfail over the other values.
wolffd@0 17 % This is useful for simulating noisy deterministic functions.
wolffd@0 18 % If pfail is omitted, it is set to 0.
wolffd@0 19 %
wolffd@0 20
wolffd@0 21
wolffd@0 22 if nargin==0
wolffd@0 23 % This occurs if we are trying to load an object from a file.
wolffd@0 24 CPD = tabular_CPD(bnet, self);
wolffd@0 25 return;
wolffd@0 26 elseif isa(bnet, 'deterministic_CPD')
wolffd@0 27 % This might occur if we are copying an object.
wolffd@0 28 CPD = bnet;
wolffd@0 29 return;
wolffd@0 30 end
wolffd@0 31
wolffd@0 32 if nargin < 4, pfail = 0; end
wolffd@0 33
wolffd@0 34 ps = parents(bnet.dag, self);
wolffd@0 35 ns = bnet.node_sizes;
wolffd@0 36 psizes = ns(ps);
wolffd@0 37 self_size = ns(self);
wolffd@0 38
wolffd@0 39 psucc = 1-pfail;
wolffd@0 40
wolffd@0 41 CPT = zeros(prod(psizes), self_size);
wolffd@0 42 pvals = zeros(1, length(ps));
wolffd@0 43 for i=1:prod(psizes)
wolffd@0 44 pvals = ind2subv(psizes, i);
wolffd@0 45 x = feval(fname, pvals);
wolffd@0 46 %fprintf('%d ', [pvals x]); fprintf('\n');
wolffd@0 47 if psucc == 1
wolffd@0 48 CPT(i, x) = 1;
wolffd@0 49 else
wolffd@0 50 CPT(i, x) = psucc;
wolffd@0 51 rest = mysetdiff(1:self_size, x);
wolffd@0 52 CPT(i, rest) = pfail/length(rest);
wolffd@0 53 end
wolffd@0 54 end
wolffd@0 55 CPT = reshape(CPT, [psizes self_size]);
wolffd@0 56
wolffd@0 57 CPD = tabular_CPD(bnet, self, 'CPT',CPT, 'clamped',1);
wolffd@0 58
wolffd@0 59