Bayesian Networks

Copyright Tristan Aubrey-Jones April 2007.

calculateProb.m

home Home   up Up   ( Download )


function result = calculateProb(sample, conds, given) % CALCULATEPROB calculates the probability of the % conditions conds occuring for all cases where given occurs. % % The parameters 'conds' and 'given' are matrices where each row % is a condition. Each condition is a two column row vector where % the left value cond(1) is the column index of the variable in % the event, and the right value cond(2) is the value that it should % take for the event to be true. % % e.g. calculateProb([1 1; 1 0; 1 0], [2 1], []) = 1/3; % calculateProb([1 1; 1 0; 1 0], [2 1], [2 1]) = 1; % calculateProb([1 1; 1 0; 1 0], [2 1], [1 1]) = 1; sz = size(sample); cz = size(conds); gz = size(given); tot = 0.0; count = 0; % for all events in sample for i = 1:sz(1) % check if all given conditions apply alltrue = 1; for ci = 1:gz(1) % check current condition c = given(ci,1:gz(2)); if sample(i, c(1)) ~= c(2) alltrue = 0; break; end end % if given true if alltrue == 1 % increment "out of" count count = count + 1; % check if all conditions apply alltrue = 1; for ci = 1:cz(1) % check current condition c = conds(ci, 1:cz(2)); if sample(i, c(1)) ~= c(2) alltrue = 0; break; end end % if conditions hold add to if alltrue == 1 tot = tot + 1; end end end % return probability of conds occuring given that % given already has if count > 0 result = tot / count; else % impossible result = 0; end