function val = objJSZ(theta,Y,WN,mats) 
% objective function is sum of negative log likelihoods
% global Y WN mats ;
N = size(WN,1) ;

pars = theta2pars_jsz(theta, N) ;
gamma = ones(N*(N+1),1) ;

%check restrictions on parameter space
valid = true ;
%diagonal elements of Sigma positive and bounded away from zero
 if any(diag(pars.L)< 1e-7)
    valid = false ;
 end 
 if any(diag(pars.L)>1) 
    valid = false ;
 end
% eigenvalues of Phi.Q not explosive
if any(pars.lamQ>1) 
    valid = false ;
end
if any(pars.dlamQ >0)
    valid = false;
end

% if parameters satisfy restriction on param space
if valid == true
    % evaluate likelihood function and return sum of negative logliks
    res_llk = jsz_llk(Y, WN, diag(pars.lamQ-1),[], [], [], pars.Omega_cP, mats, 1, [], 1, gamma,[],[],[],[]) ;
    val = sum(res_llk.llk) ;
else
    % else return penalty value
    val = 1e6;
end

end

