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