function  [yt,yd]=tmtrend(y,n)
%TMTREND  Detrend time series.
%         [yt,yd]=tmtrend(y,n) calculates the series yt(:,i) to be 
%
%             yt(t,i)= b0(i) + b1(i)*t + ... bn(i)*t^n,   t=1,..T, i=1,..k
% 
%         where b0(i), .. bn(i) are least-squares estimates in a regression
%         of y(:,i) on t, t^2, .. t^n, t=[1:T]'.  The smaller is the
%         integer n, the smooter is the trend series.
%
%         Also returned are the series with no trend:  yd(:,i)=y(:,i)-
%         yt(:,i), i=1,...k
%

%         Ellen R. McGrattan,  4-23-87

[T,k]=size(y);
t=time(T);
for i=1:n; x=[x,t.^i]; end;
x=[ones(T,1),x];
beta=ols(x,y);
yt=x*beta;
yd=y-yt;


return
yt=ones(T,1)*beta(1,:);
for i=2:n+1; yt=yt+t.^(i-1)*beta(i,:); end;
