function [bel, scale, post] = init_belief_state(bnet, obs_nodes, vals)
% INIT_BELIEF_STATE Compute scaled-alpha(:,1) = Pr(Q(1) | O(1)) for a DBN.
% [bel, scale, post] = init_belief_state(bnet, obs_nodes, obs_vals)
%
% obs_nodes = the nodes observed in slice 1
% vals(:) = observations for slice 1
%
% bel{c} = scaled-alpha(c,t) for cluster c
% scale = Pr(O(t) | O(1:t-1)).
% post{k} = potential on clique k in the jtree for slice 1
%
% The method is a generalization of the following HMM equation:
% alpha(i,1) = prior(i) * obsmat(i,1)


% Swap in the 1-slice join tree.
% This is necessary if we are to prevent the hidden second slice from affecting
% the scale factor (likelihood) incorrectly.
jtree2 = bnet.jtree;
bnet.jtree = bnet.jtree1;


% Add evidence to slice 1
bnet.observed_nodes = obs_nodes; % bnet is not a return argument, so this change is local
bnet.values(obs_nodes) = vals;
% We only multiply on the CPTs for the nodes in slice 1
bnet = mk_ev_specific_pot(bnet, bnet.dbn.sl1_nodes_ass_to_cl_jt1);

bnet = collect_evidence(bnet);
bnet = distribute_evidence(bnet);
num_cliques = length(bnet.jtree.cliques);
for c=1:num_cliques
  [bnet.jtree.clpot{c}, lik] = normalize_potential(bnet.jtree.clpot{c});
end
if lik>0
  scale = 1/lik;
else
  scale = 0;
end
%lik = lik + (lik==0)*eps;
%scale = 1/lik;

% Extract alpha for slice 1
cluster = bnet.dbn.cluster;
num_clusters = length(cluster);
bel = cell(1,num_clusters);
for c=1:num_clusters
  cl = bnet.dbn.cl_con_sl1_cluster_jt1(c);
  bigm = bnet.jtree.clpot{cl};
  m = marginalize_potential(bigm, cluster{c});
  bel{c} = m;
end

% Extract the filtered posterior on all the cliques
post = cell(1,num_cliques);
for c=1:num_cliques
  post{c} = bnet.jtree.clpot{c};
end

% Swap back the 2-slice join tree
% Strictly, this is unnecessary, since bnet is a local variable.
bnet.jtree1 = bnet.jtree;
bnet.jtree = jtree2;


