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

options  =  optimset('fminsearch');
options  =  optimset('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' , 10000) ;

   % 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:3)';
d_yields=yields-lagmatrix(yields,1);
d_yields(1,:)=d_yields(2,:);
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
K1Q_X = .9;
parameters0 =sort(K1Q_X,'descend')';
% optimization
disp('optimization');
% [parameters,FVAL,EXITFLAG,OUTPUT] =  fminunc('jsz_like', parameters, options, yields, W, mature, dt);
[parameters1,FVAL1,EXITFLAG,OUTPUT] =  ...
    fminsearch('jsz_like_L_macro_d', parameters0, options, d_yields, W, mature, dt,L);
%CSMINWEL
OUTPUT
 [FVAL,parameters,gh,hess,itct,fcount,retcodeh] = ...
     csminwel('jsz_like_L_macro_d', parameters1,eye(length(parameters1))*.001,[],1e-25,10000,d_yields, W, mature, dt,L);

 [LLF,llk,penalty,sigma_e,K0Q_cP, K1Q_cP] = ....
     jsz_like_L_macro_d(parameters,yields, W, mature, dt,L);
[K1Q_X,rinfQ,sigma_cP] = convert_v2ind(parameters);


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


