function y = excess_r(yields, mat_ind)
% Create excess returns as in Cochrane and Piazzesi (2005 AER),
% using simulated yields from our model.
% Then, we would use the realized excess return to test if the 
% CP feature holds with our model.
% Holding period return: r_{n,t+1} = n*y_{n,t} - (n-1)*y_{n-1,t+1}
% Excess return: rx_{n,t+1} = r_{n,t+1} - y_{1,t}
% time interval is one year

% INPUT:
%       yields: jjxT, annualized yields for maturities of 1-jj month
%       mat_ind: Kx1, maturities of excess return we compute 
% OUTPUT:
%        rx: (K-1)x(T-12), excess return for n = 2,3,...,K
%                          t= 13,14,...,T

yn = yields(mat_ind,:);  % KxT
nmat = repmat(mat_ind/12,1,size(yields,2));
nyn = yn .* nmat;
r = nyn(2:end,1:end-12) - nyn(1:end-1,13:end);
rx = r - nyn(1,1:end-12);

% yn = yields(mat_ind,:)/12;  % KxT, monthly yields
% [K,T] = size(yields);
% nmat = repmat(mat_ind,1,T);
% nyn = yn .* nmat;
% r = nyn(2:end,1:end-1) - nyn(1:end-1,2:end);
% rx_temp = (r - nyn(1,1:end-1));
% rx = nan(K-1,T-12);
% for i = 12:T-1
%     rx(:,i-11) = mean(rx_temp(:,i-11:i),2);
% end

y = rx;


end
 