function  sumsq = PLM_sumsqfit(model_Zcongr,human_Zcongr)

%PLM_SUMSQFIT -- Compares PLMODEL1 performance against human data.
%
%  sumsq = PLM_sumsqfit(model_Zcongr)
%  sumsq = PLM_sumsqfit(model_Zcongr,human_Zcongr)
%
%  Typically HUMAN_ZCONGR is omitted as it defaults to the global
%  variable with the same name. It is a matrix of 6 rows (stimulus
%  types) and 32 columns (blocks).
%  MODEL_ZCONGR is either a 6xB matrix of z-transformed Prob_correct
%  or a cell array containing two 3xB matrices: {zP_congr,zP_incon}.
%  MODEL_ZCONGR may have more than 6 rows (e.g. 12), but only the first 
%  6 are used. The number of blocks (or columns) B of MODEL_ZCONGR must
%  be an integer multiple of the number of rows in HUMAN_ZCONGR.
%  The rows (or "conditions") in ZCONGR have the following indices:
%   1: congruent Gabor, contrast 0.23
%   2: incongruent Gabor, contrast 0.23
%   3: congruent Gabor, contrast 0.15
%   4: incongruent Gabor, contrast 0.15
%   5: congruent Gabor, contrast 0.10
%   6: incongruent Gabor, contrast 0.10
%  SUMSQ is the summed squared error.
%
%  Alternatively, MODEL_ZCONGR may be a 3xB matrix of model d-primes.
%  They are compared against the corresponding sums of congruent and
%  incongruent conditions in HUMAN_ZCONGR.
%
%  Example1:
%    Zcongr = PLM_Zcongr1(stim,Mparams) ;     % [6x32]
%    sumsq = PLM_sumsqfit(Zcongr) ;
%
%  Example2:
%    sumstats = PLM_summary(PLM_stats(stim_congr,resp_corr)) ;
%    sumsq = PLM_sumsqfit({sumstats.zP_congr',sumstats.zP_incon'}) ;
%
%  Example3:
%    dprime3 = PLM_dprime3(stim,Mparams) ;    % [3x32]
%    sumsq = PLM_sumsqfit(dprime3) ;
%
%  See also PLM_SUMSQFIT2, PLM_ZCONGR1, PLM_PARAMSEARCH, PLOT_PLM_FIT.

%  Original coding by Alexander Petrov, UC Irvine.
%  $Revision: 1.2 $  $Date: 2003/04/18 15:54 $

global HUMAN_ZCONGR

if (nargin==1)  human_Zcongr = HUMAN_ZCONGR ; end   % default

if (iscell(model_Zcongr))       % {sumstats.zP_congr',sumstats.zP_incon'}
   model_Zcongr = cat(1,model_Zcongr{:}) ;
   model_Zcongr([2,3,4,5],:) = model_Zcongr([4,2,5,3],:) ;
end

[h_rows,h_cols] = size(human_Zcongr) ;
[m_rows,m_cols] = size(model_Zcongr) ;

%-- Ignore the bottom (="other") part of MODEL_ZCONGR if necessary
if (m_rows > h_rows)
   model_Zcongr = model_Zcongr(1:h_rows,:) ;
   m_rows = h_rows ;
elseif ((m_rows==3) & (h_rows==6))  % special case: d' rather than zProb
   human_Zcongr = human_Zcongr([1 3 5],:) + human_Zcongr([2 4 6],:) ;
elseif (m_rows < h_rows)
   error('MODEL_ZCONGR has less rows (stimulus types) than HUMAN_ZCONGR.');
%else  (m_rows==h_rows)
end

%-- Average across sub-blocks if necessary
if (m_cols > h_cols)
   if (rem(m_cols,h_cols)~=0)
      error('Model N_blocks must be a multiple of human N_blocks.');
   end
   N = m_cols / h_cols ;
   model_Zcongr = reshape(model_Zcongr',[N h_cols , m_rows]) ;
   model_Zcongr = squeeze(mean(model_Zcongr,1))' ;
elseif (m_cols < h_cols)
   error('MODEL_ZCONGR has less blocks than HUMAN_ZCONGR.');
%else  (m_cols==h_cols)
end

%-- Return the sum of squares
sumsq = sum(sum((model_Zcongr-human_Zcongr).^2)) ;

%-- Return SUMSQ
%%%%% End of file PLM_SUMSQFIT.M
