function y=convert(x,first,freq1,freq2);
%CONVERT converts a time series of frequency freq1 to a series of frequency
% freq2. If freq1 > freq2, sample points are averaged. If freq1 < freq2,
% the series is log-linearly interpolated.
%
% INPUTS:
% (i) x = time series to be converted
% (ii) Case 1: freq1 > freq2.
% To obtain the converted series, freq1/freq2 numbers of the
% unconverted series which are associated with each of the converted
% numbers are averaged. If for the first converted data point,
% there are less than freq1/freq2 numbers, first <=freq1/freq2 is
% the position of the subperiod starting the data off.
% Ex. (a) x: monthly data starting in 1955:08
% y: quarterly data starting in 1955:3 since the 8th month
% is in the 3rd quarter
% first = 2 since the third quarter is the average of data
% for month 7,8,& 9 and 8 is the 2nd in this group
% Ex. (b) x: quarterly data starting in 1955:4
% y: annual data starting in 1955
% first = 4 since the 1955 data point is the average of data
% for quarters 1-4 and 4 is the 4th in this group
% Case 2: freq2 > freq1.
% To obtain the converted series, the unconverted points are assigned
% to a particular subperiod of the converted series. Then the other
% points in the new series y are obtained by log-linear inter-
% polation. In this case, first is the subperiod to which the
% unconverted points are assigned.
% Ex. (a) x: quarterly data starting in 1955:3
% y: monthly data starting in 1955:09
% first = 3, so that each quarter's value is assigned to be
% value for the last month of every quarter.
% Ex. (b) x: annual data starting in 1955
% y: quarterly data starting in 1955:2
% first = 2, so that second quarter of every year is
% assigned the annual value.
% (ii) freq1 = frequency of x, the unconverted series
% (iii) freq2 = frequency of y, the converted series
%
% OUTPUT: y, the converted series with length that depends on freq1, freq2,
% first, and the length of x.
%
if freq1==freq2; error('Specify a NEW frequency for your data'); end;
n=length(x);
if freq1>freq2;
ratio=freq1/freq2;
nof=ratio-first+1;
y(1,1)=sum( x(1:nof))/nof;
nn=fix((n-nof)/ratio);
for i=2:nn+1;
y(i,1)=sum(x(ratio*(i-1)+1-(ratio-nof):ratio*i-(ratio-nof)))/ratio;
end;
if (ratio*(nn+1)-first+1 < n);
y(nn+2,1)=sum(x(ratio*(nn+1)-first+2:n))/(n-ratio*(nn+1)+first-1);
end;
else;
ratio=freq2/freq1;
lx=log(x);
ly=zeros(n*ratio,1);
for i=1:n;
ly(ratio*(i-1)+first,1)=lx(i);
end;
for i=1:ratio-1;
for j=i+first:ratio:ratio*(n-1)+first-1;
ly(j,1)=ly(j-i,1)*(1-i/ratio) + ly(j-i+ratio,1)*i/ratio;
end;
end;
if first>1;
beta=ols([ones(10,1),time(10)],ly(first:first+9,1));
ly(1:first-1,1)=beta(1)+beta(2)*time(first-1);
end;
if first