annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@deterministic_CPD/deterministic_CPD.m @ 0:cc4b1211e677 tip

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