function[parameters,K1Q_X,rinfQ,sigma_cP,L0,L1,hess,K0Q_cP, K1Q_cP,sigma_e,para1,u1t_star]=...
    ML_estimation_7(yields,L,mature,dt,macro,NFACT)

options  =  optimset('fminsearch');
options  =  optimset(options,'Algorithm','active-set');

options  =  optimset(options , 'TolFun'      , 1e-20);
options  =  optimset(options , 'TolX'        , 1e-15);
options  =  optimset(options , 'Display'     , 'on');
options  =  optimset(options , 'Diagnostics' , 'off');
options  =  optimset(options , 'LargeScale'  , 'off');
options  =  optimset(options , 'MaxFunEvals' , 50000) ;
options  =  optimset(options , 'MaxIter' , 50000) ;

   % options=optimset('MaxFunEvals',500000,'MaxIter',500000,'Display','Off','Algorithm','active-set');


cov_mat = cov(yields);
[eig_vec,eig_val] = eig(cov_mat);
eig_vec = eig_vec(:,end:-1:1);
% eig_vec(:,1) = -eig_vec(:,1);

W = eig_vec(:,1:NFACT)';
pc1 = yields*W';
N1=cols(pc1);

dat=[pc1 macro];
y1 = dat(L+1:end,:);
N=cols(y1);
x1 = prepare(dat,L);
x1=x1(L+1:end,:);
T=rows(y1);
para1 = x1\y1;


K0P_cP= para1(end,:)';
K1P_cP= para1(1:end-1,:)';
u1t_star = y1 - x1*para1;
var_1 = (u1t_star'*u1t_star)/(rows(y1)-cols(x1));
sigma_cP0 = chol(var_1)';

% Q-dynamics
% starting values
rinfQ =0.01;% mean(yields(:,end));
if NFACT==3
K1Q_X = [.99,.98,.9];
elseif NFACT==4
K1Q_X = [.99,.98,.90,.80];
elseif NFACT==5
 K1Q_X = [.99,.98,.95,.85,.75];
else
  K1Q_X = [.99,.98,.95,ones(1,NFACT-3).*0.75];
end
parameters0 = convert_ind2v(sort(K1Q_X,'descend'),rinfQ,sigma_cP0);
%  [LLF,llk,penalty,sigma_e,K0Q_cP, K1Q_cP] = ....
%      jsz_like_L_macro5(parameters0,yields, W, mature, dt, u1t_star',L,NFACT);
% K1Q_cP
% optimization
disp('optimization');
% [parameters,FVAL,EXITFLAG,OUTPUT] =  fminunc('jsz_like', parameters, options, yields, W, mature, dt);
[parameters1,FVAL1,EXITFLAG,OUTPUT] =  ...
    fminsearch('jsz_like_L_macro7', parameters0, options, yields, ...
    W, mature, dt,u1t_star',L,NFACT);
parameters1
%CSMINWEL
OUTPUT
 [FVAL,parameters,gh,hess,itct,fcount,retcodeh] = ...
     csminwel('jsz_like_L_macro7', parameters1,eye(length(parameters1))*.0001,[],1e-15,10000,yields, W, mature, dt,u1t_star',L,NFACT);

 [LLF,llk,penalty,sigma_e,K0Q_cP, K1Q_cP] = ....
     jsz_like_L_macro7(parameters,yields, W, mature, dt, u1t_star',L,NFACT);
[K1Q_X,rinfQ,sigma_cP] = convert_v5ind(parameters,NFACT);


L0=(K0P_cP(1:N1)-K0Q_cP);
L1=(K1P_cP(1:N1,:)-[(K1Q_cP+eye(N1)) zeros(N1,(N*L)-N1)]);


