function [bnet, loglik] = propagate_var_jtree(bnet)
% PROPOGATE_VAR_JTREE Iteratively maximize the variational params.
% [bnet, ll] = propogate_var_jtree(bnet)

thresh = 1e-4;
max_iter = 20;
converged = 0;
num_iter = 1;
loglik = 0;
previous_loglik = -inf;
verbose = 1;

bnet = init_var_marginals(bnet);
while ~converged & (num_iter <= max_iter)
  [bnet, loglik] = propagate_jtree(bnet);
  if verbose, fprintf(1, 'variational iteration %d, ll = %8.4f\n', num_iter, loglik); end
  bnet = update_var_marginals(bnet);
  num_iter = num_iter + 1;
  converged = em_converged(loglik, previous_loglik, thresh);
  previous_loglik = loglik;
end

fprintf(1, '%d var. iterations, ll = %8.4f\n', num_iter-1, loglik);

