annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@boolean_CPD/boolean_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 = boolean_CPD(bnet, self, ftype, fname, pfail)
Daniel@0 2 % BOOLEAN_CPD Make a tabular CPD representing a (noisy) boolean function
Daniel@0 3 %
Daniel@0 4 % CPD = boolean_cpd(bnet, self, 'inline', f) uses the inline function f
Daniel@0 5 % to specify the CPT.
Daniel@0 6 % e.g., suppose X4 = X2 AND (NOT X3). Then we can write
Daniel@0 7 % bnet.CPD{4} = boolean_CPD(bnet, 4, 'inline', inline('(x(1) & ~x(2)'));
Daniel@0 8 % Note that x(1) refers pvals(1) = X2, and x(2) refers to pvals(2)=X3.
Daniel@0 9 %
Daniel@0 10 % CPD = boolean_cpd(bnet, self, 'named', f) assumes f is a function name.
Daniel@0 11 % f can be built-in to matlab, or a file.
Daniel@0 12 % e.g., If X4 = X2 AND X3, we can write
Daniel@0 13 % bnet.CPD{4} = boolean_CPD(bnet, 4, 'named', 'and');
Daniel@0 14 % e.g., If X4 = X2 OR X3, we can write
Daniel@0 15 % bnet.CPD{4} = boolean_CPD(bnet, 4, 'named', 'any');
Daniel@0 16 %
Daniel@0 17 % CPD = boolean_cpd(bnet, self, 'rnd') makes a random non-redundant bool fn.
Daniel@0 18 %
Daniel@0 19 % CPD = boolean_CPD(bnet, self, 'inline'/'named', f, pfail)
Daniel@0 20 % will put probability mass 1-pfail on f(parents), and put pfail on the other value.
Daniel@0 21 % This is useful for simulating noisy boolean functions.
Daniel@0 22 % If pfail is omitted, it is set to 0.
Daniel@0 23 % (Note that adding noise to a random (non-redundant) boolean function just creates a different
Daniel@0 24 % (potentially redundant) random boolean function.)
Daniel@0 25 %
Daniel@0 26 % Note: This cannot be used to simulate a noisy-OR gate.
Daniel@0 27 % Example: suppose C has parents A and B, and the
Daniel@0 28 % link of A->C fails with prob pA and the link B->C fails with pB.
Daniel@0 29 % Then the noisy-OR gate defines the following distribution
Daniel@0 30 %
Daniel@0 31 % A B P(C=0)
Daniel@0 32 % 0 0 1.0
Daniel@0 33 % 1 0 pA
Daniel@0 34 % 0 1 pB
Daniel@0 35 % 1 1 pA * PB
Daniel@0 36 %
Daniel@0 37 % By contrast, boolean_CPD(bnet, C, 'any', p) would define
Daniel@0 38 %
Daniel@0 39 % A B P(C=0)
Daniel@0 40 % 0 0 1-p
Daniel@0 41 % 1 0 p
Daniel@0 42 % 0 1 p
Daniel@0 43 % 1 1 p
Daniel@0 44
Daniel@0 45
Daniel@0 46 if nargin==0
Daniel@0 47 % This occurs if we are trying to load an object from a file.
Daniel@0 48 CPD = tabular_CPD(bnet, self);
Daniel@0 49 return;
Daniel@0 50 elseif isa(bnet, 'boolean_CPD')
Daniel@0 51 % This might occur if we are copying an object.
Daniel@0 52 CPD = bnet;
Daniel@0 53 return;
Daniel@0 54 end
Daniel@0 55
Daniel@0 56 if nargin < 5, pfail = 0; end
Daniel@0 57
Daniel@0 58 ps = parents(bnet.dag, self);
Daniel@0 59 ns = bnet.node_sizes;
Daniel@0 60 psizes = ns(ps);
Daniel@0 61 self_size = ns(self);
Daniel@0 62
Daniel@0 63 psucc = 1-pfail;
Daniel@0 64
Daniel@0 65 k = length(ps);
Daniel@0 66 switch ftype
Daniel@0 67 case 'inline', f = eval_bool_fn(fname, k);
Daniel@0 68 case 'named', f = eval_bool_fn(fname, k);
Daniel@0 69 case 'rnd', f = mk_rnd_bool_fn(k);
Daniel@0 70 otherwise, error(['unknown function type ' ftype]);
Daniel@0 71 end
Daniel@0 72
Daniel@0 73 CPT = zeros(prod(psizes), self_size);
Daniel@0 74 ndx = find(f==0);
Daniel@0 75 CPT(ndx, 1) = psucc;
Daniel@0 76 CPT(ndx, 2) = pfail;
Daniel@0 77 ndx = find(f==1);
Daniel@0 78 CPT(ndx, 2) = psucc;
Daniel@0 79 CPT(ndx, 1) = pfail;
Daniel@0 80 if k > 0
Daniel@0 81 CPT = reshape(CPT, [psizes self_size]);
Daniel@0 82 end
Daniel@0 83
Daniel@0 84 clamp = 1;
Daniel@0 85 CPD = tabular_CPD(bnet, self, CPT, [], clamp);
Daniel@0 86
Daniel@0 87
Daniel@0 88
Daniel@0 89 %%%%%%%%%%%%
Daniel@0 90
Daniel@0 91 function f = eval_bool_fn(fname, n)
Daniel@0 92 % EVAL_BOOL_FN Evaluate a boolean function on all bit vectors of length n
Daniel@0 93 % f = eval_bool_fn(fname, n)
Daniel@0 94 %
Daniel@0 95 % e.g. f = eval_bool_fn(inline('x(1) & x(3)'), 3)
Daniel@0 96 % returns 0 0 0 0 0 1 0 1
Daniel@0 97
Daniel@0 98 ns = 2*ones(1, n);
Daniel@0 99 f = zeros(1, 2^n);
Daniel@0 100 bits = ind2subv(ns, 1:2^n);
Daniel@0 101 for i=1:2^n
Daniel@0 102 f(i) = feval(fname, bits(i,:)-1);
Daniel@0 103 end
Daniel@0 104
Daniel@0 105 %%%%%%%%%%%%%%%
Daniel@0 106
Daniel@0 107 function f = mk_rnd_bool_fn(n)
Daniel@0 108 % MK_RND_BOOL_FN Make a random bit vector of length n that encodes a non-redundant boolean function
Daniel@0 109 % f = mk_rnd_bool_fn(n)
Daniel@0 110
Daniel@0 111 red = 1;
Daniel@0 112 while red
Daniel@0 113 f = sample_discrete([0.5 0.5], 2^n, 1)-1;
Daniel@0 114 red = redundant_bool_fn(f);
Daniel@0 115 end
Daniel@0 116
Daniel@0 117 %%%%%%%%
Daniel@0 118
Daniel@0 119
Daniel@0 120 function red = redundant_bool_fn(f)
Daniel@0 121 % REDUNDANT_BOOL_FN Does a boolean function depend on all its input values?
Daniel@0 122 % r = redundant_bool_fn(f)
Daniel@0 123 %
Daniel@0 124 % f is a vector of length 2^n, representing the output for each bit vector.
Daniel@0 125 % An input is redundant if there is no assignment to the other bits
Daniel@0 126 % which changes the output e.g., input 1 is redundant if u(2:n) s.t.,
Daniel@0 127 % f([0 u(2:n)]) <> f([1 u(2:n)]).
Daniel@0 128 % A function is redundant it it has any redundant inputs.
Daniel@0 129
Daniel@0 130 n = log2(length(f));
Daniel@0 131 ns = 2*ones(1,n);
Daniel@0 132 red = 0;
Daniel@0 133 for i=1:n
Daniel@0 134 ens = ns;
Daniel@0 135 ens(i) = 1;
Daniel@0 136 U = ind2subv(ens, 1:2^(n-1));
Daniel@0 137 U(:,i) = 1;
Daniel@0 138 f1 = f(subv2ind(ns, U));
Daniel@0 139 U(:,i) = 2;
Daniel@0 140 f2 = f(subv2ind(ns, U));
Daniel@0 141 if isequal(f1, f2)
Daniel@0 142 red = 1;
Daniel@0 143 return;
Daniel@0 144 end
Daniel@0 145 end
Daniel@0 146
Daniel@0 147
Daniel@0 148 %%%%%%%%%%
Daniel@0 149
Daniel@0 150 function [b, iter] = rnd_truth_table(N)
Daniel@0 151 % RND_TRUTH_TABLE Construct the output of a random truth table s.t. each input is non-redundant
Daniel@0 152 % b = rnd_truth_table(N)
Daniel@0 153 %
Daniel@0 154 % N is the number of inputs.
Daniel@0 155 % b is a random bit string of length N, representing the output of the truth table.
Daniel@0 156 % Non-redundant means that, for each input position k,
Daniel@0 157 % there are at least two bit patterns, u and v, that differ only in the k'th position,
Daniel@0 158 % s.t., f(u) ~= f(v), where f is the function represented by b.
Daniel@0 159 % We use rejection sampling to ensure non-redundancy.
Daniel@0 160 %
Daniel@0 161 % Example: b = [0 0 0 1 0 0 0 1] is indep of 3rd input (AND of inputs 1 and 2)
Daniel@0 162
Daniel@0 163 bits = ind2subv(2*ones(1,N), 1:2^N)-1;
Daniel@0 164 redundant = 1;
Daniel@0 165 iter = 0;
Daniel@0 166 while redundant & (iter < 4)
Daniel@0 167 iter = iter + 1;
Daniel@0 168 b = sample_discrete([0.5 0.5], 1, 2^N)-1;
Daniel@0 169 redundant = 0;
Daniel@0 170 for i=1:N
Daniel@0 171 on = find(bits(:,i)==1);
Daniel@0 172 off = find(bits(:,i)==0);
Daniel@0 173 if isequal(b(on), b(off))
Daniel@0 174 redundant = 1;
Daniel@0 175 break;
Daniel@0 176 end
Daniel@0 177 end
Daniel@0 178 end
Daniel@0 179