function [ comp_log_post, comp_log_joint, log_lkld, all_log_lkld ] = compute_post_gmm( mvn_model, data )

% function [ comp_log_post, comp_log_joint, log_lkld, all_log_lkld ] = compute_post_gmm( mvn_model, data )
%
% mvn_model: structure array with M elements.
% data:      M by N matrix or real values.
%
% See also: do_em_gmm.m, estimate_K_gmm.m.
%
% By Guillaume Lathoud, 2006.
% lathoud@idiap.ch

  if nargin < 2
    error( [ mfilename ' needs 2 input arguments.' ] );
  end
  
  M = numel( mvn_model );

  D = numel( mvn_model( 1 ).mu );

  if size( data, 1 ) ~= D
    error( [ mfilename ': inconsistent sizes, "data" must be D by N.' ] );
  end

  N = size( data, 2 );

  %%%

  comp_log_joint = repmat( NaN, M, N );
  
  for m = 1:M
    
    comp_log_joint( m,: ) = log( mvn_model( m ).w ) + reshape( log_mvnpdf( data.', mvn_model( m ).mu(:).', mvn_model( m ).Sigma ), 1, [] );
    
  end
  
  if M > 1
    log_lkld = my_logsum_fast( comp_log_joint );
  else
    log_lkld = comp_log_joint;
  end
  
  all_log_lkld = sum( log_lkld );
  
  comp_log_post = comp_log_joint - repmat( log_lkld, M, 1 );
  