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