function bnet = compile_params(bnet)
% COMPILE_PARAMS Do any checks/pre-processing on the new parameters.
% 
% BNET = COMPILE_PARAMS(BNET) should be called after all the parameters of the bnodes have been
% set to their correct values.

% Do some simple sanity checks
N = length(bnet.members_of_equiv_class);
ns = bnet.node_sizes;
bnet.all_tabular = 1;
for i=1:N
  if isempty(bnet.bnodes{i})
    error(['you forgot to create bnode ' num2str(i)]);
  end
  switch bnet.bnodes{i}.type
    case 'tabular',
      CPT = bnet.bnodes{i}.CPT;
      if ~isequal(mysize(CPT), ns(family(bnet.adj_mat,i)))
	error(['the size of the CPT for bnode ' num2str(i) ' is wrong']);
      end
      if ~approxeq(CPT, mk_stochastic(CPT))
	error(['the CPT for bnode ' num2str(i) ' does not sum to 1']);
      end
    otherwise
      bnet.all_tabular = 0;
  end
end
    
% For HMMs/LDSs, we now create the various matrices.
% For jtree, the standard approach is to initialize the clique potentials.
% However, the BNT approach is to wait until the evidence has arrived.
% (Hence it is not strictly necessary to call this function unless using the HMM or LDS algo.)
% For details, see "A Variational Approximation for Bayesian Networks with Discrete and
% Continuous Latent Variables", Murphy, UAI 99.

switch bnet.algo
  case 'hmm',
    [bnet.hmm.prior, bnet.hmm.transmat, bnet.hmm.obsmat, bnet.hmm.obsmat1] = dbn_to_hmm(bnet);
  case 'lds',
    [bnet.hmm.trans_mat, bnet.hmm.trans_cov, bnet.hmm.obs_mat, bnet.hmm.obs_cov, ...
	  bnet.hmm.init_state, bnet.hmm.init_cov] = dbn_to_lds(bnet);
end
