Code simplification using dynTime class.

time-shift
Stéphane Adjemian (Scylla) 2011-09-09 16:28:56 +02:00
parent 8127048d81
commit 62d6d71cc7
4 changed files with 77 additions and 107 deletions

View File

@ -61,13 +61,12 @@ function ts = dynSeries(a,b,c,d)
%! @strong{This function is called by:}
%! @sp 2
%! @strong{This function calls:}
%! @ref{set_time}
%! @ref{@@dynTime/dynTime}, @ref{@@dynTime/setTime}, @ref{@@dynTime/setFreq}
%!
%! @end deftypefn
%@eod:
% Copyright (C) 2011 Dynare Team
% stephane DOT adjemian AT univ DASH lemans DOT fr
%
% This file is part of Dynare.
%
@ -84,6 +83,8 @@ function ts = dynSeries(a,b,c,d)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
ts = struct;
ts.data = [];
@ -92,9 +93,6 @@ ts.vobs = 0;
ts.name = [];
ts.tex = [];
ts.freq = [];
ts.time = [];
ts.init = [];
ts.last = [];
ts.Time = dynTime();
ts = class(ts,'dynSeries');
@ -119,57 +117,25 @@ switch nargin
weekly = findstr('W',b);
if ~isempty(quaterly)
ts.freq = 4;
ts.init = [str2num(b(1:quaterly-1)) str2num(b(quaterly+1:end))];
ts = set_time(ts);
ts.last = ts.time(end,:);
ts.Time = ts.Time.setSize(ts.nobs);
ts.Time = ts.Time.setFreq(4);
ts.Time = ts.Time.setTime(1,[str2num(b(1:quaterly-1)) str2num(b(quaterly+1:end))]);
init = dynDates(b);
end
if ~isempty(monthly)
ts.freq = 12;
ts.init = [str2num(b(1:monthly-1)) str2num(b(monthly+1:end))];
ts = set_time(ts);
ts.last = ts.time(end,:);
ts.Time = ts.Time.setSize(ts.nobs);
ts.Time = ts.Time.setFreq(12);
ts.Time = ts.Time.setTime(1,[str2num(b(1:monthly-1)) str2num(b(monthly+1:end))]);
init = dynDates(b);
end
if ~isempty(weekly)
ts.freq = 52;
ts.init = [str2num(b(1:weekly-1)) str2num(b(weekly+1:end))];
ts = set_time(ts);
ts.last = ts.time(end,:);
ts.Time = ts.Time.setSize(ts.nobs);
ts.Time = ts.Time.setFreq(52);
ts.Time = ts.Time.setTime(1,[str2num(b(1:weekly-1)) str2num(b(weekly+1:end))]);
init = dynDates(b);
end
if isempty(quaterly) && isempty(monthly) && isempty(weekly)
error('dynSeries:: Using a string as a second input argument, I can only handle weekly (W), monthly (M) or quaterly (Q) data!');
end
else% If b is not a string then yearly data are assumed.
ts.init = [b 1];
ts.freq = 1;
ts = set_time(ts);
ts.last = ts.time(end,:);
ts.Time = ts.Time.setSize(ts.nobs);
ts.Time = ts.Time.setFreq(1);
ts.Time = ts.Time.setTime(1,[b 1]);
init = dynDates(b);
end
ts.Time = ts.Time.setFreq(ts.freq);
ts.Time = ts.Time.setTime(dynDates(b):dynDates(b)+ts.nobs);
else% If b is empty.
ts.freq = 1;
ts.init = 1;
ts.last = ts.nobs;
ts = set_time(ts);
ts.init = ts.time(1,:);
ts.last = ts.time(end,:);
ts.Time = ts.Time.setSize(ts.nobs);
ts.Time = ts.Time.setFreq(1);
ts.Time = ts.Time.setTime(1,[1 1]);
ts.Time = ts.Time.setTime([transpose(1:ts.nobs) ones(ts.nobs,1)]);
end
% Get the names of the variables.
if ~isempty(c)
@ -225,6 +191,7 @@ end
%$ ts2 = dynSeries(A,B2);
%$ ts3 = dynSeries(A,B3);
%$ ts4 = dynSeries(A,B4);
%$
%$ % Check the results.
%$ t(1) = dyn_assert(getTime(ts1),e1.Time);
%$ t(2) = dyn_assert(getTime(ts2),e2.Time);
@ -235,4 +202,4 @@ end
%$ t(7) = dyn_assert(ts3.freq,e3.freq);
%$ t(8) = dyn_assert(ts4.freq,e4.freq);
%$ T = all(t);
%@eof:1
%@eof:1

View File

@ -1,27 +1,45 @@
function time = getTime(ts)
% Copyright (C) 2011 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
if ts.freq==1
time = ts.time(:,1);
time = ts.Time(:,1);
return
end
time = [];
switch ts.freq
case 4
for i=1:ts.nobs
time = char(time,[num2str(ts.time(i,1)) 'Q' num2str(ts.time(i,2))]);
time = [num2str(ts.Time(1,1)) 'Q' num2str(ts.Time(1,2))];
for i=2:ts.nobs
time = char(time,[num2str(ts.Time(i,1)) 'Q' num2str(ts.Time(i,2))]);
end
case 12
for i=1:ts.nobs
time = char(time,[num2str(ts.time(i,1)) 'M' num2str(ts.time(i,2))]);
time = [num2str(ts.Time(1,1)) 'M' num2str(ts.Time(1,2))];
for i=2:ts.nobs
time = char(time,[num2str(ts.Time(i,1)) 'M' num2str(ts.Time(i,2))]);
end
case 52
for i=1:ts.nobs
time = char(time,[num2str(ts.time(i,1)) 'W' num2str(ts.time(i,2))]);
time = [num2str(ts.Time(1,1)) 'W' num2str(ts.Time(1,2))];
for i=2:ts.nobs
time = char(time,[num2str(ts.Time(i,1)) 'W' num2str(ts.Time(i,2))]);
end
otherwise
error('dynSeries::getTime: Unknown type of frequency!')
end
time = time(2:end,:);
end

View File

@ -31,7 +31,7 @@ function a = horzcat(varargin)
%! @ref{descriptive_statistics}
%!
%! @strong{This function calls:}
%! @ref{dynSeries}, @ref{private/horzcat2}
%! @ref{dynSeries}, @ref{private/horzcat2}
%!
%! @strong{Remark 1.} It is assumed that the two time series objects have the same frequencies. The two time series objects can cover
%! different time ranges.
@ -94,7 +94,8 @@ end
%$ ts3 = [ts1,ts2];
%$
%$ % Check the results.
%$ t(1) = dyn_assert(ts3.time,e.time);
%$
%$ t(1) = dyn_assert(ts3.Time.time,e.time);
%$ t(2) = dyn_assert(ts3.freq,e.freq);
%$ t(3) = dyn_assert(ts3.data,e.data);
%$ t(4) = dyn_assert(ts3.name,e.name);
@ -129,7 +130,7 @@ end
%$ ts3 = [ts1,ts2];
%$
%$ % Check the results.
%$ t(1) = dyn_assert(ts3.time,e.time);
%$ t(1) = dyn_assert(ts3.Time.time,e.time);
%$ t(2) = dyn_assert(ts3.freq,e.freq);
%$ t(3) = dyn_assert(ts3.data,e.data);
%$ t(4) = dyn_assert(ts3.name,e.name);
@ -164,7 +165,7 @@ end
%$ ts3 = [ts1,ts2];
%$
%$ % Check the results.
%$ t(1) = dyn_assert(ts3.time,e.time);
%$ t(1) = dyn_assert(ts3.Time.time,e.time);
%$ t(2) = dyn_assert(ts3.freq,e.freq);
%$ t(3) = dyn_assert(ts3.data,e.data);
%$ t(4) = dyn_assert(ts3.name,e.name);
@ -199,7 +200,7 @@ end
%$ ts3 = [ts1,ts2];
%$
%$ % Check the results.
%$ t(1) = dyn_assert(ts3.time,e.time);
%$ t(1) = dyn_assert(ts3.Time.time,e.time);
%$ t(2) = dyn_assert(ts3.freq,e.freq);
%$ t(3) = dyn_assert(ts3.data,e.data);
%$ t(4) = dyn_assert(ts3.name,e.name);
@ -233,7 +234,7 @@ end
%$ ts4 = [ts1,ts2,ts3];
%$
%$ % Check the results.
%$ t(1) = dyn_assert(ts4.time,e.time);
%$ t(1) = dyn_assert(ts4.Time.time,e.time);
%$ t(2) = dyn_assert(ts4.freq,e.freq);
%$ t(3) = dyn_assert(ts4.data,e.data);
%$ t(4) = dyn_assert(ts4.name,e.name);

View File

@ -39,6 +39,25 @@ function a = horzcat2(b,c)
%! @end deftypefn
%@eod:
% Copyright (C) 2011 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
if ~(isa(b,'dynSeries') && isa(c,'dynSeries'))
error('dynSeries::horzcat: All input arguments have to be Dynare time series objects!')
end
@ -58,47 +77,13 @@ else
end
d_init_flag = 0;
if isequal(b.init,c.init)
a.init = b.init;
else
% set a.init equal to min(b.init,c.init)
if b.init(1)<c.init(1)
d_init_flag = 1;
a.init = b.init;
elseif b.init(1)==c.init(1)
if b.init(2)<c.init(2)
d_init_flag = 1;
a.init = b.init;
else
d_init_flag = 2;
a.init = c.init;
end
else
d_init_flag = 2;
a.init = c.init;
end
if ~isequal(b.Time(1),c.Time(1))
d_init_flag = 1;
end
d_last_flag = 0;
if isequal(b.last,c.last)
a.last = b.last;
else
% set a.last equal to max(b.last,c.last)
if b.last(1)<c.last(1)
d_last_flag = 2;
a.last = c.last;
elseif b.last(1)==c.last(1)
if b.last(2)<c.last(2)
d_last_flag = 2;
a.last = c.last;
else
d_last_flag = 1;
a.last = b.last;
end
else
d_last_flag = 1;
a.last = b.last;
end
if ~isequal(b.Time(end),c.Time(end))
d_last_flag = 1;
end
a.vobs = b.vobs+c.vobs;
@ -106,18 +91,17 @@ a.name = char(b.name,c.name);
a.tex = char(b.tex,c.tex);
if ~( d_nobs_flag(1) || d_init_flag(1) || d_last_flag(1) )
a.time = b.time;
a.Time = b.Time;
a.data = [b.data,c.data];
else
[junk,ib] = setdiff(b.time,c.time,'rows');
[junk,ic] = setdiff(c.time,b.time,'rows');
[junk,jb,jc] = intersect(b.time,c.time,'rows');
a.time = [b.time(ib,:); b.time(jb,:); c.time(ic,:)];
a.time = sortrows(a.time,[1 2]);
a.nobs = rows(a.time);
[JUNK,IB] = setdiff(b.Time(:),c.Time(:),'rows');
[JUNK,IC] = setdiff(c.Time(:),b.Time(:),'rows');
[JUNK,JB,JC] = intersect(b.Time(:),c.Time(:),'rows');
a.Time = a.Time.setTime(sortrows([b.Time(IB); b.Time(JB); c.Time(IC)],[1 2]));
a.nobs = rows(a.Time(:));
a.data = NaN(a.nobs,a.vobs);
[junk,ia,ib] = intersect(a.time,b.time,'rows');
[junk,ia,ib] = intersect(a.Time(:),b.Time(:),'rows');
a.data(ia,1:b.vobs) = b.data;
[junk,ia,ic] = intersect(a.time,c.time,'rows');
[junk,ia,ic] = intersect(a.Time(:),c.Time(:),'rows');
a.data(ia,b.vobs+(1:c.vobs)) = c.data;
end