about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/BNT/general/sample_dbn.m
diff options
context:
space:
mode:
authorziejd22017-09-28 15:04:40 -0500
committerziejd22017-09-28 15:04:40 -0500
commit8070dc963753142bb86c4ed698d91fd623ed28e7 (patch)
treed0f6dd8fc46a49b819aa55c1a90faa14d8448883 /sourcecodes/bnt-master/BNT/general/sample_dbn.m
parent7cc31810d53176e805532b2789955f4eedbce6bb (diff)
downloadBNW-8070dc963753142bb86c4ed698d91fd623ed28e7.tar.gz
BNW using Octave instead of Matlab.
This version of BNW should perform the same as the original version. The only difference is that it uses Octave instead of Matlab when running BayesNet Toolbox during parameter learning.

I am calling this BNW_1.02. It can be accessed at:
compbio.uthsc.edu/BNW_1.02
Diffstat (limited to 'sourcecodes/bnt-master/BNT/general/sample_dbn.m')
-rw-r--r--sourcecodes/bnt-master/BNT/general/sample_dbn.m73
1 files changed, 73 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/general/sample_dbn.m b/sourcecodes/bnt-master/BNT/general/sample_dbn.m
new file mode 100644
index 00000000..0477ac4a
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/general/sample_dbn.m
@@ -0,0 +1,73 @@
+function seq = sample_dbn(bnet, varargin)
+% SAMPLE_DBN Generate a random sequence from a DBN.
+% seq = sample_dbn(bnet, ...)
+%
+% seq{i,t} contains the values of the i'th node in the t'th slice.
+%
+% Optional arguments:
+%
+% length - length of sequence  to be generated (can also just use sample_dbn(bnet,T))
+% stop_test - name of a function which is used to decide when to stop;
+%   This will be called as feval(stop_test, seq(:,t))
+%   i.e., stop_test is passed a cell array containing all the nodes in the current slice.   
+% evidence - initial evidence; if evidence{i,t} is non-empty, this node won't be sampled.
+
+args = varargin;
+nargs = length(args);
+
+if (nargs == 1) & ~isstr(args{1})
+  % Old syntax: sample_dbn(bnet, T)
+  T = args{1};
+else
+  % get length
+  T = 1;
+  for i=1:2:nargs
+    switch args{i},
+     case 'length',      T = args{i+1}; 
+     case 'evidence',    T = size(args{i+1}, 2);
+    end
+  end
+end
+
+ss = length(bnet.intra);
+% set default arguments
+seq = cell(ss, T);
+stop_test = [];
+for i=1:2:nargs
+  switch args{i},
+   case 'evidence',    seq = args{i+1}; % initialise observed nodes
+   case 'stop_test',  stop_test = args{i+1};
+  end
+end
+
+t = 1;
+for i=1:ss
+  if ~isempty(stop_test) | isempty(seq{i,t})
+    ps = parents(bnet.dag, i);
+    e = bnet.equiv_class(i,1);
+    pvals = seq(ps);
+    seq{i,t} = sample_node(bnet.CPD{e}, pvals);
+    %fprintf('sample i=%d,t=%d,val=%d,ps\n', i, t, seq(i,t)); pvals(:)'
+  end
+end
+t = 2;
+done = 0;
+while ~done
+  for i=1:ss
+    if ~isempty(stop_test) | isempty(seq{i,t})
+      ps = parents(bnet.dag, i+ss) + (t-2)*ss;
+      e = bnet.equiv_class(i,2);
+      pvals = seq(ps);
+      seq{i,t} = sample_node(bnet.CPD{e}, pvals);
+      %fprintf('sample i=%d,t=%d,val=%d,ps\n', i, t, seq(i,t)); pvals(:)'
+    end
+  end
+  if ~isempty(stop_test)
+    done = feval(stop_test, seq(:,t));
+  else
+    if t==T
+      done = 1;
+    end
+  end
+  t = t + 1;
+end