1function o = horzcat(varargin) % --*-- Unitary tests --*--
2
3% Overloads horzcat method for dseries objects.
4%
5% INPUTS
6%  o o1    dseries object.
7%  o o2    dseries object.
8%  o ...
9%
10% OUTPUTS
11%  o o     dseries object.
12%
13% EXAMPLE 1
14%  If o1, o2 and o3 are dseries objects the following syntax:
15%
16%    o = [o1, o2, o3] ;
17%
18%  defines a dseries object o containing the variables appearing in o1, o2 and o3.
19%
20% REMARKS
21%  o o1, o2, ... must not have common variables.
22
23% Copyright (C) 2011-2017 Dynare Team
24%
25% This file is part of Dynare.
26%
27% Dynare is free software: you can redistribute it and/or modify
28% it under the terms of the GNU General Public License as published by
29% the Free Software Foundation, either version 3 of the License, or
30% (at your option) any later version.
31%
32% Dynare is distributed in the hope that it will be useful,
33% but WITHOUT ANY WARRANTY; without even the implied warranty of
34% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35% GNU General Public License for more details.
36%
37% You should have received a copy of the GNU General Public License
38% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
39
40switch nargin
41  case 0
42    o = dseries();
43  case 1
44    o = varargin{1};
45  otherwise
46    o = concatenate(copy(varargin{1}), copy(varargin{2}));
47    if nargin>2
48        o = horzcat(o, varargin{3:end});
49    end
50end
51
52function a = concatenate(b,c)
53[n,message] = common_strings_in_cell_arrays(b.name, c.name);
54if isempty(b)
55    a = c;
56    return
57end
58if isempty(c)
59    a = b;
60    return
61end
62if n
63    error(['dseries::horzcat: I cannot concatenate dseries objects with common variable names (' message ')!'])
64end
65if ~isequal(frequency(b),frequency(c))
66    error('dseries::horzcat: All time series objects must have common frequency!')
67else
68    a = dseries();
69end
70d_nobs_flag = 0;
71if ~isequal(nobs(b),nobs(c))
72    d_nobs_flag = 1;
73end
74d_init_flag = 0;
75if ~isequal(firstdate(b),firstdate(c))
76    d_init_flag = 1;
77end
78a.ops = vertcat(b.ops,c.ops);
79a.name = vertcat(b.name,c.name);
80a.tex  = vertcat(b.tex,c.tex);
81btagnames = fieldnames(b.tags);
82ctagnames = fieldnames(c.tags);
83atagnames = union(btagnames, ctagnames);
84if isempty(atagnames)
85    a.tags = struct();
86else
87    for i=1:length(atagnames)
88        if ismember(atagnames{i}, btagnames) && ismember(atagnames{i}, ctagnames)
89            a.tags.(atagnames{i}) = vertcat(b.tags.(atagnames{i}), b.tags.(atagnames{i}));
90        elseif ismember(atagnames{i}, btagnames)
91            a.tags.(atagnames{i}) = vertcat(b.tags.(atagnames{i}), cell(vobs(c), 1));
92        elseif ismember(atagnames{i}, ctagnames)
93            a.tags.(atagnames{i}) = vertcat(cell(vobs(b), 1), c.tags.(atagnames{i}));
94        else
95            error('dseries::horzcat: This is a bug!')
96        end
97    end
98end
99
100if ~( d_nobs_flag(1) || d_init_flag(1) )
101    a.data = [b.data,c.data];
102    a.dates = b.dates;
103else
104    nobs_b = nobs(b);
105    nobs_c = nobs(c);
106    if firstdate(b)<=firstdate(c)
107        if firstdate(b)<firstdate(c)
108            c.data = [NaN(firstdate(c)-firstdate(b), vobs(c)); c.data];
109        end
110    else
111        b.data = [NaN(firstdate(b)-firstdate(c), vobs(b)); b.data];
112    end
113    b_last_date = firstdate(b)+nobs_b;
114    c_last_date = firstdate(c)+nobs_c;
115    if b_last_date<c_last_date
116        b.data = [b.data; NaN(c_last_date-b_last_date, vobs(b))];
117    elseif b_last_date>c_last_date
118        c.data = [c.data; NaN(b_last_date-c_last_date, vobs(c))];
119    end
120
121    fillerdates = dates();
122    if max(c.dates) < min(b.dates)
123        fillerdates = max(c.dates):min(b.dates);
124    end
125    if max(b.dates) < min(c.dates)
126        fillerdates = max(b.dates):min(c.dates);
127    end
128
129    if isempty(fillerdates)
130        hd = [b.dates, c.dates];
131    else
132        hd = [b.dates, fillerdates, c.dates];
133    end
134
135    a.data = [b.data, c.data];
136    a.dates = sort(unique(hd));
137end
138
139%@test:1
140%$ % Define a data set.
141%$ A = [transpose(1:10),2*transpose(1:10)];
142%$ B = [transpose(1:10),2*transpose(1:10)];
143%$
144%$ % Define names
145%$ A_name = {'A1';'A2'};
146%$ B_name = {'B1';'B2'};
147%$
148%$ % Define expected results.
149%$ e.init = dates(1,1);
150%$ e.freq = 1;
151%$ e.name = {'A1';'A2';'B1';'B2'};
152%$ e.data = [A,B];
153%$
154%$ % Instantiate two time series objects.
155%$ ts1 = dseries(A,[],A_name,[]);
156%$ ts2 = dseries(B,[],B_name,[]);
157%$
158%$ % Call the tested method.
159%$ ts3 = [ts1,ts2];
160%$
161%$ % Check the results.
162%$
163%$ t(1) = dassert(ts3.init,e.init);
164%$ t(2) = dassert(ts3.freq,e.freq);
165%$ t(3) = dassert(ts3.data,e.data);
166%$ t(4) = dassert(ts3.name,e.name);
167%$ T = all(t);
168%@eof:1
169
170%@test:2
171%$ % Define a data set.
172%$ A = [transpose(1:10),2*transpose(1:10)];
173%$ B = [transpose(5:12),2*transpose(5:12)];
174%$
175%$ % Define names
176%$ A_name = {'A1';'A2'};
177%$ B_name = {'B1';'B2'};
178%$
179%$ % Define initial date
180%$ A_init = 2001;
181%$ B_init = 2005;
182%$
183%$ % Define expected results.
184%$ e.init = dates('2001Y');
185%$ e.freq = 1;
186%$ e.name = {'A1';'A2';'B1';'B2'};
187%$ e.data = [ [A; NaN(2,2)], [NaN(4,2); B]];
188%$
189%$ % Instantiate two time series objects.
190%$ ts1 = dseries(A,A_init,A_name,[]);
191%$ ts2 = dseries(B,B_init,B_name,[]);
192%$
193%$ % Call the tested method.
194%$ ts3 = [ts1,ts2];
195%$
196%$ % Check the results.
197%$ t(1) = dassert(ts3.init,e.init);
198%$ t(2) = dassert(ts3.freq,e.freq);
199%$ t(3) = dassert(ts3.data,e.data);
200%$ t(4) = dassert(ts3.name,e.name);
201%$ T = all(t);
202%@eof:2
203
204%@test:3
205%$ % Define a data set.
206%$ A = [transpose(1:7),2*transpose(1:7)];
207%$ B = [transpose(5:11),2*transpose(5:11)];
208%$
209%$ % Define names
210%$ A_name = {'A1';'A2'};
211%$ B_name = {'B1';'B2'};
212%$
213%$ % Define initial date
214%$ A_init = '1950Q1';
215%$ B_init = '1950Q3';
216%$
217%$ % Define expected results.
218%$ e.freq = 4;
219%$ e.init = dates('1950Q1');
220%$ e.name = {'A1';'A2';'B1';'B2'};
221%$ e.data = [ [A; NaN(2,2)], [NaN(2,2); B]];
222%$
223%$ % Instantiate two time series objects.
224%$ ts1 = dseries(A,A_init,A_name,[]);
225%$ ts2 = dseries(B,B_init,B_name,[]);
226%$
227%$ % Call the tested method.
228%$ ts3 = [ts1,ts2];
229%$
230%$ % Check the results.
231%$ t(1) = dassert(ts3.init,e.init);
232%$ t(2) = dassert(ts3.freq,e.freq);
233%$ t(3) = dassert(ts3.data,e.data);
234%$ t(4) = dassert(ts3.name,e.name);
235%$ T = all(t);
236%@eof:3
237
238%@test:4
239%$ % Define a data set.
240%$ A = [transpose(1:7),2*transpose(1:7)];
241%$ B = [transpose(5:9),2*transpose(5:9)];
242%$
243%$ % Define names
244%$ A_name = {'A1';'A2'};
245%$ B_name = {'B1';'B2'};
246%$
247%$ % Define initial date
248%$ A_init = '1950Q1';
249%$ B_init = '1950Q3';
250%$
251%$ % Define expected results.
252%$ e.init = dates(A_init);
253%$ e.freq = 4;
254%$ e.name = {'A1';'A2';'B1';'B2'};
255%$ e.data = [ A, [NaN(2,2); B]];
256%$
257%$ % Instantiate two time series objects.
258%$ ts1 = dseries(A,A_init,A_name,[]);
259%$ ts2 = dseries(B,B_init,B_name,[]);
260%$
261%$ % Call the tested method.
262%$ ts3 = [ts1,ts2];
263%$
264%$ % Check the results.
265%$ t(1) = dassert(ts3.init,e.init);
266%$ t(2) = dassert(ts3.freq,e.freq);
267%$ t(3) = dassert(ts3.data,e.data);
268%$ t(4) = dassert(ts3.name,e.name);
269%$ T = all(t);
270%@eof:4
271
272%@test:5
273%$ % Define a data set.
274%$ A = [transpose(1:10),2*transpose(1:10)];
275%$ B = [transpose(1:10),3*transpose(1:10)];
276%$ C = [transpose(1:10),4*transpose(1:10)];
277%$
278%$ % Define names
279%$ A_name = {'A1';'A2'};
280%$ B_name = {'B1';'B2'};
281%$ C_name = {'C1';'C2'};
282%$
283%$ % Define expected results.
284%$ e.init = dates(1,1);
285%$ e.freq = 1;
286%$ e.name = {'A1';'A2';'B1';'B2';'C1';'C2'};
287%$ e.data = [A,B,C];
288%$
289%$ % Instantiate two time series objects.
290%$ ts1 = dseries(A,[],A_name,[]);
291%$ ts2 = dseries(B,[],B_name,[]);
292%$ ts3 = dseries(C,[],C_name,[]);
293%$
294%$ % Call the tested method.
295%$ ts4 = [ts1,ts2,ts3];
296%$
297%$ % Check the results.
298%$ t(1) = dassert(ts4.init,e.init);
299%$ t(2) = dassert(ts4.freq,e.freq);
300%$ t(3) = dassert(ts4.data,e.data);
301%$ t(4) = dassert(ts4.name,e.name);
302%$ T = all(t);
303%@eof:5
304
305%@test:6
306%$ % Define a data set.
307%$ A = [transpose(1:10),2*transpose(1:10)];
308%$ B = [transpose(1:10),2*transpose(1:10)];
309%$
310%$ % Define names
311%$ A_name = {'A1';'A2'};
312%$ B_name = {'B1';'A2'};
313%$
314%$ % Instantiate two time series objects.
315%$ ts1 = dseries(A,[],A_name,[]);
316%$ ts2 = dseries(B,[],B_name,[]);
317%$
318%$ % Call the tested method.
319%$ try
320%$   ts3 = [ts1,ts2];
321%$   t = 0;
322%$ catch
323%$   t = 1;
324%$ end
325%$
326%$ T = t;
327%@eof:6
328
329%@test:7
330%$ % Define X
331%$ X = randn(30,2);
332%$
333%$ % Instantiate two time series objects.
334%$ ts1 = dseries();
335%$ ts2 = dseries(randn(30,2),'1950Q2');
336%$
337%$ % Call the tested method.
338%$ try
339%$   ts3 = [ts1,ts2];
340%$   t = 1;
341%$ catch
342%$   t = 0;
343%$ end
344%$
345%$ if t(1)
346%$   t(2) = dassert(ts3.freq,4);
347%$   t(3) = dassert(ts3.data,X);
348%$   t(4) = dassert(ts3.dates(1),dates('1950Q2'));
349%$ end
350%$
351%$ T = t;
352%@eof:7
353
354%@test:8
355%$ % Define a data set.
356%$ A = [transpose(1:10),2*transpose(1:10)];
357%$ B = [transpose(1:10),2*transpose(1:10)];
358%$
359%$ % Define names
360%$ A_name = {'A1';'A2'};
361%$ B_name = {'B1';'B2'};
362%$
363%$ % Define expected results.
364%$ e.init = dates(1,1);
365%$ e.freq = 1;
366%$ e.name = {'A1';'A2';'B1';'B2'};
367%$ e.data = [A,B];
368%$
369%$ % Instantiate two time series objects.
370%$ ts1 = dseries(A,[],A_name,[]);
371%$ ts2 = dseries(B,[],B_name,[]);
372%$ ts1.tag('t1');
373%$ ts1.tag('t1', 'A1', 'Stock');
374%$ ts1.tag('t1', 'A2', 'Flow');
375%$ ts2.tag('t2');
376%$ ts2.tag('t2', 'B1', 0);
377%$ ts2.tag('t2', 'B2', 1);
378%$
379%$ % Call the tested method.
380%$ try
381%$   ts3 = [ts1,ts2];
382%$   t(1) = true;
383%$ catch
384%$   t(1) = false;
385%$ end
386%$
387%$ % Check the results.
388%$ if t(1)
389%$   t(2) = dassert(ts3.init,e.init);
390%$   t(3) = dassert(ts3.freq,e.freq);
391%$   t(4) = dassert(ts3.data,e.data);
392%$   t(5) = dassert(ts3.name,e.name);
393%$   t(6) = dassert(ts3.tags.t1,{'Stock';'Flow';[];[]});
394%$   t(7) = dassert(ts3.tags.t2,{[];[];0;1});
395%$ end
396%$ T = all(t);
397%@eof:8
398