function FAR_stat = CAPM_FAR(R, F, lambda)
%{
    This function implements Kleibergen (2009) FAR for (C)CAPM
    
    INPUT 
    R       : n by T, a matrix of asset returns
    F       : k by T, a matrix of risk factors
    lambda  : risk premium under the null
    
    OUTPUT  
    far_stat: FAR statistic
%}
    [n, T]=size(R);                       % get n, T
    F_bar=F-mean(F,2)*ones(1,T);          % derive F_bar
    Q=F_bar*F_bar'/T; 
    ln=ones(n-1,1);                       % a vector of ones
    Rs=R(1:(n-1),:)-kron(ln,R(n,:));      % delete the n_th asset
    F_exp=[F;ones(1,T)];
    B_exp=Rs*F_exp'/(F_exp*F_exp');
    Sigma_tt=1/T*(Rs-B_exp*F_exp)*(Rs-B_exp*F_exp)';
    c=T/(1+lambda'/(Q)*lambda);
    Bhat=B_exp(:,1:end-1);
    FAR_stat=c*(mean(Rs,2)-Bhat*lambda)'/(Sigma_tt)*(mean(Rs,2)-Bhat*lambda);
end 