function probs = computeProbs(condVars, givenVars, conds, given)
% computeProbs computes the vector of probabilities 
% for all the combinations of conditions and givens
% by directly manipulating the joint distribution.

% start
if (nargin == 2)
    display('\begin{tabular}{c}');
    probs = computeProbs(condVars, givenVars, [], []);
    display('\end{tabular}');
else
    % recursing through combinations
    if (nargin == 4)
        % check if all conditions are specified
        szConds = size(conds);
        szGiven = size(given);
        if szConds(1) == length(condVars)
            if szGiven(1) == length(givenVars)
                % P(X|Y) = P(X,Y) / P(Y)
                if szGiven(1) > 0
                    cs = conds; 
                    for i = 1:szGiven(1)
                        cs(i + szConds(1), 1:2) = given(i, 1:2);
                    end
                    pXY = computeProb(cs);
                    pY = computeProb(given);
                    p = pXY ./ pY;
                else
                    p = computeProb(conds); 
                end
                probs = [p(1)];
                % display in latex
                sconds = condsToStr(conds);
                sgiven = condsToStr(given);
                display(sprintf('  $P(%s|%s) = %4.8f$\\\\', sconds, sgiven, probs));
            else
                % branch for current given
                i = szGiven(1) + 1;
                v = givenVars(i);
                % when var is 1
                given(i, 1:2) = [v 1];
                probs = computeProbs(condVars, givenVars, conds, given);
                % when var is 0
                given(i, 1:2) = [v 0];
                p2 = computeProbs(condVars, givenVars, conds, given);
                sz = size(probs);
                probs((sz(1)+1):(sz(1)*2), 1:sz(2)) = p2;
            end
        else
            % branch for current cond
            i = szConds(1) + 1;
            v = condVars(i);
            % when var is 1
            conds(i, 1:2) = [v 1];
            probs = computeProbs(condVars, givenVars, conds, given);
            % when var is 0
            conds(i, 1:2) = [v 0];
            p2 = computeProbs(condVars, givenVars, conds, given);
            sz = size(probs);
            probs((sz(1)+1):(sz(1)*2), 1:sz(2)) = p2;
        end
    else
        error('incorrect arguments.');
    end
end