wolffd@0: function bnet = bayes_update_params(bnet, cases, clamped) wolffd@0: % BAYES_UPDATE_PARAMS Bayesian parameter updating given completely observed data wolffd@0: % bnet = bayes_update_params(bnet, cases, clamped) wolffd@0: % wolffd@0: % If there is a missing data, you must use EM. wolffd@0: % cases(i,m) is the value assigned to node i in case m (this can also be a cell array). wolffd@0: % clamped(i,m) = 1 if node i was set by intervention in case m (default: clamped = zeros). wolffd@0: % Clamped nodes are not updated. wolffd@0: % If there is a single case, clamped is a list of the clamped nodes, not a bit vector. wolffd@0: wolffd@0: wolffd@0: %if iscell(cases), usecell = 1; else usecell = 0; end wolffd@0: wolffd@0: n = length(bnet.dag); wolffd@0: ncases = size(cases, 2); wolffd@0: if n ~= size(cases, 1) wolffd@0: error('data must be of size nnodes * ncases'); wolffd@0: end wolffd@0: wolffd@0: if ncases == 1 % clamped is a list of nodes wolffd@0: if nargin < 3, clamped = []; end wolffd@0: clamp_set = clamped; wolffd@0: clamped = zeros(n,1); wolffd@0: clamped(clamp_set) = 1; wolffd@0: else % each row of clamped is a bit vector wolffd@0: if nargin < 3, clamped = zeros(n,ncases); end wolffd@0: end wolffd@0: wolffd@0: for i=1:n wolffd@0: e = bnet.equiv_class(i); wolffd@0: if adjustable_CPD(bnet.CPD{e}) wolffd@0: u = find(clamped(i,:)==0); wolffd@0: ps = parents(bnet.dag, i); wolffd@0: bnet.CPD{e} = bayes_update_params(bnet.CPD{e}, cases(i,u), cases(ps,u)); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: