function score = calc_dbn_score(data, interslice, nodesizes, CPTs)

% CALC_DBN_SCORE Calculates the BIC score for a single DBN
%
% score = calc_dbn_score(data, interslice, nodesizes, CPTs)
%
% data(t, n) is the t'th sample for node n
% interslice(i,j) is an edge from node i in t-1 to node j in t
% nodesizes(n) is the arity of node n
% CPTs{n} is the CPT for node n

% APRENDIZAJE AUTOMÁTICO (G. Pajares y J. M. de la Cruz, Eds.)
% Editorial: RA-MA
% Capítulo 6: Redes Bayesianas
% Autores: Diane Oyen, Eva Besada-Portas

[T N] = size(data);
s = zeros(1,N);

for child=1:N
  parents = find(interslice(:,child));
  counts = compute_counts([data(1:end-1, parents) data(2:end, child)]', ...
    nodesizes([parents; child]));
  [s(child) ll] = bic_score_family(counts, CPTs{child}, T);
end

% this is in log space, so sum them all together
score = sum(s);