function [r_ER,r_GR] = AH(X,kmax,plot)
% ==========================================================================
% Test for the number of factors in a panel of series by means of the
% eigenvalue criteria by Ahn and Horenstein (ECMA, 2013).
% --------------------------------------------------------------------------
% SYNTAX: AH(X,kmax)
% where:  
%      - X = matrix of standardized and demeaned data (TxN)  
%						- kmax = maximum number of factors to test
% ---------------------------------------------------------------------------
% OUTPUT: maxER, maxGR (scalars)
% where:
%						-	r_ER = factors selected using the Eigenvalue Ratio criterion
%						-	r_GR = factors selected using the Growth Ratio criterion
% ---------------------------------------------------------------------------
% REFERENCES:
%	
%	[1] Ahn, S. C., and Horenstein, A. R. (2013). Eigenvalue ratio test for 
%     the number of factors. Econometrica, 81(3), 1203-1227.												[AH13]
% ---------------------------------------------------------------------------
% Author: Claudio Lissona (claudio.lissona2@unibo.it)
% Last update: 12/Sep/2023
% Version: MATLAB 2021b
% Required Toolboxes: /
% ----------------------------------------------------------------------------

if nargin<2; kmax = ceil(sqrt(size(X,2))); end																																																																											% if no kmax, set according to N		
if nargin<3; kmax = ceil(sqrt(size(X,2))); plot = 0; end

Xn = X;  Xn(isnan(Xn)) = 0;																																																																																														% set (if any) NaNs to sample mean
[~,d] = eigs(cov(Xn),kmax,'LM');																																																																														           % extract eigenvalues

ER = diag(d(1:kmax-1,1:kmax-1)./d(2:kmax,2:kmax));																																																																							% Eigenvalue Ratio (ER) criterion

V = cumsum(flipud(diag(d))); dd = diag(d./flipud(V));																																																																			 % cumulative eigenvalues
GR = log(1+ dd(1:end-1))./log(1+dd(2:end));																																																																													 % Growth Ratio (GR) criterion

maxER = max(ER); maxGR = max(GR);																																																																																				    % max of ratios
r_ER = find(ER==max(ER)); r_GR = find(GR==max(GR));                                                                      % extract n. of selected factors
ER;
GR;


%%%%%%%%%%%%
%%% PLOT %%%
%%%%%%%%%%%%
if plot==1
   sgtitle({'     Ahn and Horenstein (ECMA, 2013)';''},'Fontweight','bold','FontSize',18)
   subplot(1,3,1); bar(diag(d)); title('Screeplot'); 
   subplot(1,3,2); bar(ER); hold on; bar(maxER,'red'); title('Eigenvalue Ratio')
   subplot(1,3,3); bar(GR); hold on; bar(maxGR,'red'); title('Growth Ratio')
   set(gcf, 'Units', 'centimeters','color',.85*ones(1,3));
   set(gcf, 'Position',  [4.5   9   40   12]);
   set(findall(gcf,'type','text'), 'FontSize', 12, 'Color', 'k','FontName', 'Times')
end