function[output] = objective_mis(X, center,index,V, v, trans, alpha, t, group_bean, J_06, N_all,N, Z_06, W, mark, J_06_c, lag, prev)



% X Columns : (1:age, 2:belief 06, 3:belief08, 4:cheat04, 5:cheat06, 6:cheat08, 7:verbal06, 8:out_of_ten06, 9:out_of_ten08, 10:# obs in current cell, 11:propensity in current cell, 12:# obs in lag cell, 13:propensity in lag cell, 14:id)
%% GMM PROCEDURE

if trans == 1
    
    v_08 = X(:,11)./(ones(N_all,1) - alpha*ones(N_all,1)); 
    h_08(1) = integral(v_08(1), t);
    for ii = 2:N_all
        if X(ii,11) == X(ii-1,11)
            h_08(ii) = h_08(ii-1);
        else
            h_08(ii) = integral(v_08(ii), t);
        end
    end

    v_06 = X(:,13)./(ones(N_all,1) - alpha*ones(N_all,1));
    h_06(1) = integral(v_06(1), t);
    for ii = 2:N_all
        if X(ii,13) == X(ii-1,13)
            h_06(ii) = h_06(ii-1);
        else
            h_06(ii) = integral(v_06(ii), t);
        end
    end
    

        
    f_08 = log( (h_08')./ (ones(N_all,1)- h_08' ));
    f_06 = log( (h_06')./ (ones(N_all,1)- h_06' ));
        
    
elseif trans == 2
    
    % not using smoothing, equal 1 if bigger than1
    h_08 = X(:,11)./(ones(N_all,1) - alpha*ones(N_all,1));
    h_08 = h_08.*(h_08<=1)+ones(N_all,1).*(h_08>1);
    
    h_06 = X(:,13)./(ones(N_all,1) - alpha*ones(N_all,1));
    h_06 = h_06.*(h_06<=1)+ones(N_all,1).*(h_06>1);

 
    f_08 = log( (h_08+( (100*X(:,10)).^(-1) ))./ (ones(N_all,1)- h_08 +( (X(:,10).*100).^(-1) )));
    f_06 = log( (h_06 +( (100*X(:,12)).^(-1) ))./ (ones(N_all,1)- h_06 +( (X(:,12).*100).^(-1) )));

elseif trans == 3
    

    f_08 = log( (X(:,11)+( (100*X(:,10)).^(-1) ))./ (ones(N_all,1)-X(:,11)+( (X(:,10).*100).^(-1) )));
    f_06 = log( (X(:,13)+( (100*X(:,12)).^(-1) ))./ (ones(N_all,1)-X(:,13)+( (X(:,12).*100).^(-1) )));   

        
end



diff_f = (f_08 - f_06).*(X(:,11) ~= X(:,13));



S_zlambda = (1/N)*Z_06*(diff_f.*mark);
S_zlambda(J_06+1) = (1/N)*sum(f_06.*mark);
S_zlambda(J_06+2) = (1/N)*sum(f_08.*mark);
S_zlambda(J_06+3:J_06+6) = (1/N)*V*(diff_f.*mark);


S_zlambda = S_zlambda-center.*[ index; ones(6,1)];


if prev == 0 
    % diff in variables;
    % constant;
    S_zx(:,1) = zeros(J_06,1);
    % age;
    S_zx(:,2) = (1/N)*Z_06*2*mark;
    % belief
    S_zx(:,3) = (1/N)*Z_06*((X(:,3) - X(:,2)).*mark);

    % 06 
    % constant
    S_zx(J_06 + 1,1) = 1;
    % age
    S_zx(J_06+1,2) = (1/N)*sum(((X(:,1))-2*ones(N_all,1)).*mark);
    % belief
    S_zx(J_06+1,3) = (1/N)*sum(X(:,2).*mark);

    % 08 
    % constant
    S_zx(J_06+2,1) = 1;
    % age
    S_zx(J_06+2,2) = (1/N)*sum(X(:,1).*mark);
    % belief
    S_zx(J_06+2,3) = (1/N)*sum(X(:,3).*mark);

    % Verbal times diff
    %constant
    S_zx(J_06+3:J_06+6,1) = zeros(4,1);
    % age
    S_zx(J_06+3:J_06+6,2) = (1/N)*V*2*mark;
    % belief
    S_zx(J_06+3:J_06+6,3) = (1/N)*V*((X(:,3) - X(:,2)).*mark);

    if v ==1 || v == 3 
        S_zx(:,4) = zeros(J_06+6,1);
        % AGE SQ
        % diff of age_sq
        S_zx(1:J_06,4) = (1/N)*Z_06*(( (X(:,1).^2)-((X(:,1)-2*ones(N_all,1)).^2)).*mark);
        % 06
        S_zx(J_06+1,4) = (1/N)*sum(((X(:,1)-2*ones(N_all,1)).^2).*mark);
        % 08
        S_zx(J_06+2,4) = (1/N)*sum((X(:,1).^2).*mark);
        % verbal*diff 
        S_zx(J_06+3:J_06+6,4) = (1/N)*V*(( (X(:,1).^2)-((X(:,1)-2*ones(N_all,1)).^2)).*mark);
    elseif v == 2
        S_zx(:,4) = zeros(J_06+6,1);
        % belief sq
        %diff
        S_zx(1:J_06,4) = (1/N)*Z_06*(((X(:,3).^2)-(X(:,2).^2)).*mark);
        % 06
        S_zx(J_06+1,4) = (1/N)*sum((X(:,2).^2).*mark);
        % 08
        S_zx(J_06+2,4) = (1/N)*sum((X(:,3).^2).*mark); 
        % verbal*diff 
        S_zx(J_06+3:J_06+6,4) = (1/N)*V*(((X(:,3).^2)-(X(:,2).^2)).*mark);
    end

    if v ==3 
        S_zx(:,5) = zeros(J_06+6,1);
        % belief sq
        %diff
        S_zx(1:J_06,5) = (1/N)*Z_06*(((X(:,3).^2)-0.1*(X(:,2).^2)).*mark);
        % 06
        S_zx(J_06+1,5) = (1/N)*sum((X(:,2).^2).*mark);
        % 08
        S_zx(J_06+2,5) = (1/N)*sum((X(:,3).^2).*mark);
        % verbal*diff 
        S_zx(J_06+3:J_06+6,5) = (1/N)*V*(((X(:,3).^2)-0.1*(X(:,2).^2)).*mark);
    end
        

    if v == 4
        S_zx = S_zx(:,2:end);  
    end

    if lag == 1
        S_zx(:,end+1) = zeros(J_06+6,1);
        % belief sq
        %diff
        S_zx(1:J_06,end) = (1/N)*Z_06*((X(:,5) - X(:,4)).*mark);
        % 06
        S_zx(J_06+1,end) = (1/N)*sum(X(:,4).*mark);
        % 08
        S_zx(J_06+2,end) = (1/N)*sum(X(:,5).*mark);
        % verbal*diff 
        S_zx(J_06+3:J_06+6,end) = (1/N)*V*((X(:,5)-X(:,4)).*mark);
    end
elseif prev == 1 
    % diff in variables;
    % constant;
    S_zx(:,1) = zeros(J_06,1);
    % age;
    S_zx(:,2) = (1/N)*Z_06*2*mark;
    % belief
    S_zx(:,3) = (1/N)*Z_06*((X(:,3) - X(:,2)).*mark);
    % prev
    S_zx(:,4) = (1/N)*Z_06*((X(:,9) - X(:,8)).*mark);

    % 06 
    % constant
    S_zx(J_06 + 1,1) = 1;
    % age
    S_zx(J_06+1,2) = (1/N)*sum(((X(:,1))-2*ones(N_all,1)).*mark);
    % belief
    S_zx(J_06+1,3) = (1/N)*sum(X(:,2).*mark);
    % prev
    S_zx(J_06+1,4) = (1/N)*sum(X(:,8).*mark);

    % 08 
    % constant
    S_zx(J_06+2,1) = 1;
    % age
    S_zx(J_06+2,2) = (1/N)*sum(X(:,1).*mark);
    % belief
    S_zx(J_06+2,3) = (1/N)*sum(X(:,3).*mark);
    % prev
    S_zx(J_06+2,4) = (1/N)*sum(X(:,9).*mark);    

    % Verbal times diff
    %constant
    S_zx(J_06+3:J_06+6,1) = zeros(4,1);
    % age
    S_zx(J_06+3:J_06+6,2) = (1/N)*V*2*mark;
    % belief
    S_zx(J_06+3:J_06+6,3) = (1/N)*V*((X(:,3) - X(:,2)).*mark);
    % prev
    S_zx(J_06+3:J_06+6,4) = (1/N)*V*((X(:,9) - X(:,8)).*mark);    

    if v ==1 || v == 3 
        S_zx(:,5) = zeros(J_06+6,1);
        % AGE SQ
        % diff of age_sq
        S_zx(1:J_06,5) = (1/N)*Z_06*(( (X(:,1).^2)-((X(:,1)-2*ones(N_all,1)).^2)).*mark);
        % 06
        S_zx(J_06+1,5) = (1/N)*sum(((X(:,1)-2*ones(N_all,1)).^2).*mark);
        % 08
        S_zx(J_06+2,5) = (1/N)*sum((X(:,1).^2).*mark);
        % verbal*diff 
        S_zx(J_06+3:J_06+6,5) = (1/N)*V*(( (X(:,1).^2)-((X(:,1)-2*ones(N_all,1)).^2)).*mark);
    elseif v == 2
        S_zx(:,5) = zeros(J_06+6,1);
        % belief sq
        %diff
        S_zx(1:J_06,5) = (1/N)*Z_06*(((X(:,3).^2)-(X(:,2).^2)).*mark);
        % 06
        S_zx(J_06+1,5) = (1/N)*sum((X(:,2).^2).*mark);
        % 08
        S_zx(J_06+2,5) = (1/N)*sum((X(:,3).^2).*mark); 
        % verbal*diff 
        S_zx(J_06+3:J_06+6,5) = (1/N)*V*(((X(:,3).^2)-(X(:,2).^2)).*mark);
        
        S_zx(:,6) = zeros(J_06+6,1);
        % prev sq
        %diff
        S_zx(1:J_06,6) = (1/N)*Z_06*(((X(:,9).^2)-(X(:,8).^2)).*mark);
        % 06
        S_zx(J_06+1,6) = (1/N)*sum((X(:,8).^2).*mark);
        % 08
        S_zx(J_06+2,6) = (1/N)*sum((X(:,9).^2).*mark); 
        % verbal*diff 
        S_zx(J_06+3:J_06+6,6) = (1/N)*V*(((X(:,9).^2)-(X(:,8).^2)).*mark);
    end

    if v ==3 
        S_zx(:,6) = zeros(J_06+6,1);
        % belief sq
        %diff
        S_zx(1:J_06,6) = (1/N)*Z_06*(((X(:,3).^2)-(X(:,2).^2)).*mark);
        % 06
        S_zx(J_06+1,6) = (1/N)*sum((X(:,2).^2).*mark);
        % 08
        S_zx(J_06+2,6) = (1/N)*sum((X(:,3).^2).*mark); 
        % verbal*diff 
        S_zx(J_06+3:J_06+6,6) = (1/N)*V*(((X(:,3).^2)-(X(:,2).^2)).*mark);
    end
        
    if v == 3 
        S_zx(:,7) = zeros(J_06+6,1);
        % prev sq
        %diff
        S_zx(1:J_06,7) = (1/N)*Z_06*(((X(:,9).^2)-(X(:,8).^2)).*mark);
        % 06
        S_zx(J_06+1,7) = (1/N)*sum((X(:,8).^2).*mark);
        % 08
        S_zx(J_06+2,7) = (1/N)*sum((X(:,9).^2).*mark); 
        % verbal*diff 
        S_zx(J_06+3:J_06+6,7) = (1/N)*V*(((X(:,9).^2)-(X(:,8).^2)).*mark);
    end
    
        
    if v == 4
        S_zx = S_zx(:,2:end);  
    end

    if v == 5    
        S_zx = [ S_zx(1:J_06,2:end); S_zx(J_06+3:J_06+6,2:end)];
        W = W(1:end-2,1:end-2);
    end

    if lag == 1
        S_zx(:,end+1) = zeros(J_06+6,1);
        % belief sq
        %diff
        S_zx(1:J_06,end) = (1/N)*Z_06*((X(:,5) - X(:,4)).*mark);
        % 06
        S_zx(J_06+1,end) = (1/N)*sum(X(:,4).*mark);
        % 08
        S_zx(J_06+2,end) = (1/N)*sum(X(:,5).*mark);
        % verbal*diff 
        S_zx(J_06+3:J_06+6,end) = (1/N)*V*((X(:,5)-X(:,4)).*mark);
    end    
    
end    
    
theta = (inv(S_zx'*W*S_zx))*S_zx'*W*S_zlambda;





%% value of objective function


obj = (S_zlambda-S_zx*theta)'*W*(S_zlambda-S_zx*theta);


  


output = [ alpha t group_bean N J_06_c theta' obj]; 































