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 / @noisyor_CPD / CPD_to_CPT.m @ 8:b5b38998ef3b
History | View | Annotate | Download (1.02 KB)
| 1 |
function CPT = CPD_to_CPT(CPD) |
|---|---|
| 2 |
% CPD_TO_CPT Convert the discrete CPD to tabular form (noisyor) |
| 3 |
% CPT = CPD_to_CPT(CPD) |
| 4 |
% |
| 5 |
% CPT(U1,...,Un, X) = Pr(X|U1,...,Un) where the Us are the parents (excluding leak). |
| 6 |
|
| 7 |
if ~isempty(CPD.CPT) |
| 8 |
CPT = CPD.CPT; % remember to flush cache if params change (e.g., during learning) |
| 9 |
return; |
| 10 |
end |
| 11 |
|
| 12 |
q = [CPD.leak_inhibit CPD.inhibit(:)']; |
| 13 |
% q(i) is the prob. that the i'th parent will be inhibited (flipped from 1 to 0). |
| 14 |
% q(1) is the leak inhibition probability, and length(q) = n + 1. |
| 15 |
|
| 16 |
if length(q)==1 |
| 17 |
CPT = [q 1-q]; |
| 18 |
return; |
| 19 |
end |
| 20 |
|
| 21 |
n = length(q); |
| 22 |
Bn = ind2subv(2*ones(1,n), 1:(2^n))-1; % all n bit vectors, with the left most column toggling fastest (LSB) |
| 23 |
CPT = zeros(2^n, 2); |
| 24 |
% Pr(X=0 | U_1 .. U_n) = prod_{i: U_i = on} q_i = prod_i q_i ^ U_i = exp(u' * log(q_i))
|
| 25 |
% This method is problematic when q contains zeros |
| 26 |
|
| 27 |
Q = repmat(q(:)', 2^n, 1); |
| 28 |
Q(logical(~Bn)) = 1; |
| 29 |
CPT(:,1) = prod(Q,2); |
| 30 |
CPT(:,2) = 1-CPT(:,1); |
| 31 |
|
| 32 |
CPT = reshape(CPT(2:2:end), 2*ones(1,n)); % skip cases in which the leak is off |
| 33 |
|
| 34 |
CPD.CPT = CPT; |