function bnet = init_var_marginals(bnet)
% INIT_VAR_MARGINALS Compute the prior marginal distribution and its mode on each hidden node.
% bnet = init_var_marginals(bnet)
%
% To use mk_pot_from_logistic, we need to know the distribution over the logistic node and its
% parents. Before we run the inference algorithm, we set these to their most likely values
% based on the evidence above them in the graph. We need to set the values of the hidden nodes so that
% cond_dist_bnode will work. However, we never change bnet.observed_nodes so that we can still
% distinguish real from fictitious evidence.

num_nodes = length(bnet.adj_mat);
bnet.jtree.hybrid.marginal = cell(1,num_nodes);
bnet.jtree.hybrid.var_iter = 1;
ns = bnet.node_sizes;
for i=1:num_nodes
  if ismember(i, bnet.observed_nodes) % don't change the value
    if ismember(i, bnet.cts_nodes)
      mu = bnet.values(i);
      Sigma = zeros(length(mu));
      bnet.jtree.hybrid.marginal{i}.mu = mu;
      bnet.jtree.hybrid.marginal{i}.Sigma = Sigma;
    end
  else 
    if ismember(i, bnet.discrete_nodes)
      CPT = cond_dist_node(bnet, i);
      x = argmax(CPT);
      bnet.values(i) = x;
      fprintf('node %d, Ptrue %5.3f\n', i, CPT(2));
      %if strcmp(bnet.bnodes{bnet.equiv_class(i)}.type, 'logistic')
      %	bnet.jtree.hybrid.marginal{i}.prob_true = CPT(2);
      %end
    else
      % If the discrete parents are hidden, we take a weighted average of the Gaussians
      dist = cond_dist_node(bnet, i);
      mu = dist.mu;
      Sigma = dist.Sigma;

      if 0
	bnode = bnet.bnodes{bnet.equiv_class(i)};
	dps = intersect(bnode.parents, bnet.discrete_nodes);
	if isempty(dps)
	  d = 1;
	else
	  d = bnet.values(dps(1));
	end
	%if i==3, d=2; end
	Sigma = bnode.cov(:,:,d);
	mu = bnode.mean(:,d);
      end
      %Sigma = zeros(length(mu));

      fprintf('node %d, initial mu = %5.3f, Sigma = %5.3f\n', i, mu, Sigma);
      bnet.values(i) = mu;
      bnet.jtree.hybrid.marginal{i}.mu = mu;
      bnet.jtree.hybrid.marginal{i}.Sigma = Sigma;
    end
  end
end

