function LogLike = LogLikePlusOrderID(D,CC,prior,option,OIDfact)
% Usage
%  LogLike = LogLikePlusOrderID(D,CC,prior,option)
%
% input:
%   D(N,M) - data matrix containing N vectors of length M
%   CC(M,K) - values for K cluster component vectors, each of length M
%   prior(K) - prior probabilities corresponding to K clustered components
%   option:
%     option ==1 => treat first cluster as centered about 0
%     option ==0 => treat first cluster the same as others
% output:
%   LogLike - Total of marginal log likelihood + order identification criteria


% determine size of arrays
[M,K] = size(CC);
[N,M] = size(D);

% Compute Log Likelihood of data give principle components
LogLike = LogLikeGivenCC(D,CC,option);

% Compute renormalization constant for numerical stability
maxLogLike = max(LogLike,[],2);

prior = prior/sum(prior);

% Compute posterior class probabilities
prob = exp(LogLike-repmat(maxLogLike,[1,K])).*repmat(prior,[N,1]);

% Compute the log likelihood for each data vector
LogLike = log(sum(prob,2))+maxLogLike;

% Compute the log likelihood + order ID criter for the entire data set
LogLike = sum(LogLike) + OrderID2(K,N,M,OIDfact);
%LogLike = sum(LogLike);

