clear
format long e
format compact
global zxvar
load hpdata8.dat
zxvar=hpdata8;

param=[ ...
     5.000000000000000e-01                         0
     9.990000000000001e-01                         0
     9.999000000000000e-01                         0
     1.000000000000000e-05                         0
     4.137587354070704e-01     1.000000000000000e+00
     4.294873707152653e-01     1.000000000000000e+00
     2.063239153470431e-01     1.000000000000000e+00
     1.891290685186658e-01     1.000000000000000e+00
     2.198466017490339e-01     1.000000000000000e+00
     5.348908917217663e-02     1.000000000000000e+00
     4.660596327071319e-01     1.000000000000000e+00
     6.783280218726217e+00     1.000000000000000e+00
     2.140775142374973e-02     1.000000000000000e+00
     9.854448509776887e-01     1.000000000000000e+00
     1.006283992942716e+00     1.000000000000000e+00
    -3.425848137595026e-03     1.000000000000000e+00
     1.031685234724536e-01     1.000000000000000e+00
                         0                         0
     3.945116476482722e-02     1.000000000000000e+00
     1.742279708222372e-02     1.000000000000000e+00
     1.512168836651421e+00     1.000000000000000e+00
     3.717166490970895e-01     1.000000000000000e+00
                         0                         0
     1.849249894867786e-02     1.000000000000000e+00
    -7.350348431563070e-02     1.000000000000000e+00
     1.660364813765268e-02     1.000000000000000e+00
     8.928113536824138e-01     1.000000000000000e+00
                         0                         0
    -5.922089249262102e-02     1.000000000000000e+00
    -2.411153252039222e-02     1.000000000000000e+00
                         0                         0
                         0                         0
     6.268236840000000e-01     1.000000000000000e+00
                         0                         0
                         0                         0
    -8.067983102338594e-02     1.000000000000000e+00
     1.402640991016781e-01     1.000000000000000e+00
                         0                         0
     1.673616420606407e+00     1.000000000000000e+00
     4.451480824684930e-02     1.000000000000000e+00
     4.460027734379612e-01     1.000000000000000e+00
    -7.872559254132876e-02     1.000000000000000e+00
                         0                         0
     1.117425243314810e-01     1.000000000000000e+00
     1.325519798498154e+00     1.000000000000000e+00
    -5.574498562160438e-01     1.000000000000000e+00
    -1.980567589576947e-01     1.000000000000000e+00
                         0                         0
     6.659889695096745e-03     1.000000000000000e+00
     9.304268183713453e-02     1.000000000000000e+00
    -8.450196648967821e-03     1.000000000000000e+00
     5.372322697230603e-02     1.000000000000000e+00
                         0                         0
     3.720504658911062e-02     1.000000000000000e+00
     1.805971381930538e-02     1.000000000000000e+00
                         0                         0
                         0                         0
     5.017912050000000e-02     1.000000000000000e+00
                         0                         0
                         0                         0
     1.123683282911297e-01     1.000000000000000e+00
    -1.584561084130774e-01     1.000000000000000e+00
                         0                         0
    -6.666547869841324e-01     1.000000000000000e+00
    -3.690611708755397e-02     1.000000000000000e+00
    -5.150364420025015e-01     1.000000000000000e+00
    -1.004947829135008e-01     1.000000000000000e+00
                         0                         0
    -1.546977124735131e-01     1.000000000000000e+00
    -3.916308675808695e-01     1.000000000000000e+00
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
     1.067620284259237e-02     1.000000000000000e+00
     8.270266405177883e-03     1.000000000000000e+00
                         0                         0
     4.688050505050023e-03     1.000000000000000e+00
     5.180878477209107e-03     1.000000000000000e+00
                         0                         0
     1.448861488975959e-02     1.000000000000000e+00
                         0                         0
     1.018757118657809e-04     1.000000000000000e+00
     9.832085081938908e-05     1.000000000000000e+00
                         0                         0
                         0                         0
     5.066724675999384e-02     1.000000000000000e+00
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
     7.164855712204509e-03     1.000000000000000e+00
     4.237393558481665e-03     1.000000000000000e+00
                         0                         0
                         0                         0
                         0                         0
                         0                         0
     4.710931283835149e-03     1.000000000000000e+00
     1.377168179055845e+03     1.000000000000000e+00
     2.087999999999999e+00                         0
     1.950000000000000e+02                         0
                         0                         0
                         0                         0
     3.972792066443886e+01     1.000000000000000e+00
     2.164310405137044e-04     1.000000000000000e+00
     2.233801366927670e-04     1.000000000000000e+00
                         0                         0
     8.000000000000000e-01                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
     8.000000000000000e-01                         0
     8.000000000000000e-01                         0
];

param=[ ...
     5.000000000000000e-01                         0
     9.990000000000001e-01                         0
     9.999000000000000e-01                         0
     1.000000000000000e-05                         0
     4.137587354070704e-01     1.000000000000000e+00
     4.294873707152653e-01     1.000000000000000e+00
     2.063239153470431e-01     1.000000000000000e+00
     1.891290685186658e-01     1.000000000000000e+00
     2.198466017490339e-01     1.000000000000000e+00
     5.348908917217663e-02     1.000000000000000e+00
     4.660596327071319e-01     1.000000000000000e+00
     6.783280218726217e+00     1.000000000000000e+00
     2.140775142374973e-02     1.000000000000000e+00
     9.854448509776887e-01     1.000000000000000e+00
     1.006283992942716e+00     1.000000000000000e+00
    -3.425848137595026e-03     1.000000000000000e+00
     1.031685234724536e-01     1.000000000000000e+00
                         0                         0
     3.945116476482722e-02     1.000000000000000e+00
     1.742279708222372e-02     1.000000000000000e+00
     1.512168836651421e+00     1.000000000000000e+00
     3.717166490970895e-01     1.000000000000000e+00
                         0                         0
     1.849249894867786e-02     1.000000000000000e+00
    -7.350348431563070e-02     1.000000000000000e+00
     1.660364813765268e-02     1.000000000000000e+00
     8.928113536824138e-01     1.000000000000000e+00
                         0                         0
    -5.922089249262102e-02     1.000000000000000e+00
    -2.411153252039222e-02     1.000000000000000e+00
                         0                         0
                         0                         0
     6.268236840000000e-01     1.000000000000000e+00
                         0                         0
                         0                         0
    -8.067983102338594e-02     1.000000000000000e+00
     1.402640991016781e-01     1.000000000000000e+00
                         0                         0
     1.673616420606407e+00     1.000000000000000e+00
     4.451480824684930e-02     1.000000000000000e+00
     4.460027734379612e-01     1.000000000000000e+00
    -7.872559254132876e-02     1.000000000000000e+00
                         0                         0
     1.117425243314810e-01     1.000000000000000e+00
     1.325519798498154e+00     0.000000000000000e+00
    -5.574498562160438e-01     1.000000000000000e+00
    -1.980567589576947e-01     1.000000000000000e+00
                         0                         0
     6.659889695096745e-03     1.000000000000000e+00
     9.304268183713453e-02     1.000000000000000e+00
    -8.450196648967821e-03     1.000000000000000e+00
     5.372322697230603e-02     1.000000000000000e+00
                         0                         0
     3.720504658911062e-02     1.000000000000000e+00
     1.805971381930538e-02     1.000000000000000e+00
                         0                         0
                         0                         0
     5.017912050000000e-02     1.000000000000000e+00
                         0                         0
                         0                         0
     1.123683282911297e-01     1.000000000000000e+00
    -1.584561084130774e-01     1.000000000000000e+00
                         0                         0
    -6.666547869841324e-01     1.000000000000000e+00
    -3.690611708755397e-02     1.000000000000000e+00
    -5.150364420025015e-01     1.000000000000000e+00
    -1.004947829135008e-01     1.000000000000000e+00
                         0                         0
    -1.546977124735131e-01     1.000000000000000e+00
    -3.916308675808695e-01     0.000000000000000e+00
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
     1.067620284259237e-02     1.000000000000000e+00
     8.270266405177883e-03     1.000000000000000e+00
                         0                         0
     4.688050505050023e-03     1.000000000000000e+00
     5.180878477209107e-03     1.000000000000000e+00
                         0                         0
     1.448861488975959e-02     1.000000000000000e+00
                         0                         0
     1.018757118657809e-04     1.000000000000000e+00
     9.832085081938908e-05     1.000000000000000e+00
                         0                         0
                         0                         0
     5.066724675999384e-02     1.000000000000000e+00
                         0                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
     7.164855712204509e-03     1.000000000000000e+00
     4.237393558481665e-03     1.000000000000000e+00
                         0                         0
                         0                         0
                         0                         0
                         0                         0
     4.710931283835149e-03     1.000000000000000e+00
     1.377168179055845e+03     1.000000000000000e+00
     2.087999999999999e+00                         0
     1.950000000000000e+02                         0
                         0                         0
                         0                         0
     3.972792066443886e+01     1.000000000000000e+00
     2.164310405137044e-04     1.000000000000000e+00
     2.233801366927670e-04     1.000000000000000e+00
                         0                         0
     8.000000000000000e-01                         0
                         0                         0
                         0                         0
                         0                         0
                         0                         0
     8.000000000000000e-01                         0
     8.000000000000000e-01                         0
];
ind   = param(:,2);
param = param(:,1);

[L,Lt,dL,dLt]=hpse(param,ind);
[n,m]=size(dLt);
sum1=0;
for t=1:m;
  sum1=sum1+dLt(:,t)*dLt(:,t)';
end;
se=diag(sqrt(inv(sum1)));

disp('Results from Maximum Likelihood Estimation')
disp('------------------------------------------')
disp(' ')
disp('  [Theta, Standard Errors] ')
disp([param(find(ind)),se])
disp(' ')
fprintf('  L(Theta) = %g ',L)
disp(' ')
disp('  Output saved in seinfo.mat')
save seinfo

%for checking derivatives:
  
%del = diag(max(abs(param)*1e-4,1e-8));
%j=0; 
%for i=1:length(param);
%  if ind(i);
%    j=j+1;
%    [f1,f2]=hpse(param+del(:,i),ind);
%    [m1,m2]=hpse(param-del(:,i),ind);
%    b(i,1) = (f1-m1)/(2*del(i,i));
%    c(i,:) = (f2-m2)'/(2*del(i,i));
%  end;
%end; 
%return
