function L = logprobd(C,Y,X,I,P)
% LOGPROBD Log probability of a data set (rather than a single instance)
%
% L = LOGPROBD(C,Y,X,I,P) where C is the gcrf, Y is a cell of
% labelings, and X is a cell of data structures. I is an optional
% list of instance weights. P is an optional boolean argument where
% P==0 (default) indicates NaN labels indicates the nodes should be removed
% from the graph, while P==1 indicates the nodes should be
% marginalized.

% GCRF.LOGPROBD


% Jerod Weinman
% jerod@acm.org
    
% $Id: logprobd.m 1 2009-05-07 13:41:00Z weinman $
 
% Jerod Weinman
% jerod@acm.org
% Copyright 2004, 2008, 2009
 
%    This file is part of Matlab GridCRF.
%
%    Matlab GridCRF is free software: you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation, either version 3 of the License, or
%    (at your option) any later version.
%
%    Matlab GridCRF is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with Matlab GridCRF.  If not, see <http://www.gnu.org/licenses/>.
    
  L = 0;
  
  if nargin<4 || isempty(I)
	I = ones(size(Y));
  end;
  
  if nargin<5
    P = 0;
  end;
  
  for a=1:length(Y)
	ll = logprob(C,Y{a},X(a).node,X(a).hEdge,X(a).vEdge,P);
	
	if ll>0
	  warning('Positive probability for instance %d: %f',a,ll);
	end;
	
	L = L + I(a)*ll;
	
  end;
  
