horzcat.m 9.31 KB
Newer Older
1
function o = horzcat(varargin) % --*-- Unitary tests --*--
2 3 4

% Overloads horzcat method for dseries objects.
%
5
% INPUTS
6 7
%  o o1    dseries object.
%  o o2    dseries object.
8 9
%  o ...
%
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
%
% 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/>.

switch nargin
  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 50 51 52
    end
end

function a = concatenate(b,c)
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
[n,message] = common_strings_in_cell_arrays(b.name, c.name);
if isempty(b)
    a = c;
    return
end
if isempty(c)
    a = b;
    return
end
if n
    error(['dseries::horzcat: I cannot concatenate dseries objects with common variable names (' message ')!'])
end
if ~isequal(frequency(b),frequency(c))
    error('dseries::horzcat: All time series objects must have common frequency!')
else
    a = dseries();
end
d_nobs_flag = 0;
if ~isequal(nobs(b),nobs(c))
    d_nobs_flag = 1;
end
d_init_flag = 0;
if ~isequal(firstdate(b),firstdate(c))
    d_init_flag = 1;
end
78
a.ops = vertcat(b.ops,c.ops);
79 80
a.name = vertcat(b.name,c.name);
a.tex  = vertcat(b.tex,c.tex);
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
btagnames = fieldnames(b.tags);
ctagnames = fieldnames(c.tags);
atagnames = union(btagnames, ctagnames);
if isempty(atagnames)
    a.tags = struct();
else
    for i=1:length(atagnames)
        if ismember(atagnames{i}, btagnames) && ismember(atagnames{i}, ctagnames)
            a.tags.(atagnames{i}) = vertcat(b.tags.(atagnames{i}), b.tags.(atagnames{i}));
        elseif ismember(atagnames{i}, btagnames)
            a.tags.(atagnames{i}) = vertcat(b.tags.(atagnames{i}), cell(vobs(c), 1));
        elseif ismember(atagnames{i}, ctagnames)
            a.tags.(atagnames{i}) = vertcat(cell(vobs(b), 1), c.tags.(atagnames{i}));
        else
            error('dseries::horzcat: This is a bug!')
        end
    end
end

100 101 102 103 104 105 106 107 108 109
if ~( d_nobs_flag(1) || d_init_flag(1) )
    a.data = [b.data,c.data];
    a.dates = b.dates;
else
    nobs_b = nobs(b);
    nobs_c = nobs(c);
    if firstdate(b)<=firstdate(c)
        if firstdate(b)<firstdate(c)
            c.data = [NaN(firstdate(c)-firstdate(b), vobs(c)); c.data];
        end
110
    else
111
        b.data = [NaN(firstdate(b)-firstdate(c), vobs(b)); b.data];
112
    end
113 114 115 116 117 118
    b_last_date = firstdate(b)+nobs_b;
    c_last_date = firstdate(c)+nobs_c;
    if b_last_date<c_last_date
        b.data = [b.data; NaN(c_last_date-b_last_date, vobs(b))];
    elseif b_last_date>c_last_date
        c.data = [c.data; NaN(b_last_date-c_last_date, vobs(c))];
119
    end
120

121 122 123 124 125 126 127
    fillerdates = dates();
    if max(c.dates) < min(b.dates)
        fillerdates = max(c.dates):min(b.dates);
    end
    if max(b.dates) < min(c.dates)
        fillerdates = max(b.dates):min(c.dates);
    end
128

129 130 131 132
    if isempty(fillerdates)
        hd = [b.dates, c.dates];
    else
        hd = [b.dates, fillerdates, c.dates];
133 134
    end

135 136 137 138
    a.data = [b.data, c.data];
    a.dates = sort(unique(hd));
end

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
%@test:1
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
%$ B = [transpose(1:10),2*transpose(1:10)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$ B_name = {'B1';'B2'};
%$
%$ % Define expected results.
%$ e.init = dates(1,1);
%$ e.freq = 1;
%$ e.name = {'A1';'A2';'B1';'B2'};
%$ e.data = [A,B];
%$
%$ % Instantiate two time series objects.
%$ ts1 = dseries(A,[],A_name,[]);
%$ ts2 = dseries(B,[],B_name,[]);
%$
%$ % Call the tested method.
%$ ts3 = [ts1,ts2];
%$
%$ % Check the results.
%$
%$ t(1) = dassert(ts3.init,e.init);
%$ t(2) = dassert(ts3.freq,e.freq);
%$ t(3) = dassert(ts3.data,e.data);
%$ t(4) = dassert(ts3.name,e.name);
%$ T = all(t);
%@eof:1

%@test:2
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
%$ B = [transpose(5:12),2*transpose(5:12)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$ B_name = {'B1';'B2'};
%$
%$ % Define initial date
%$ A_init = 2001;
%$ B_init = 2005;
%$
%$ % Define expected results.
%$ e.init = dates('2001Y');
%$ e.freq = 1;
%$ e.name = {'A1';'A2';'B1';'B2'};
%$ e.data = [ [A; NaN(2,2)], [NaN(4,2); B]];
%$
%$ % Instantiate two time series objects.
%$ ts1 = dseries(A,A_init,A_name,[]);
%$ ts2 = dseries(B,B_init,B_name,[]);
%$
%$ % Call the tested method.
%$ ts3 = [ts1,ts2];
%$
%$ % Check the results.
%$ t(1) = dassert(ts3.init,e.init);
%$ t(2) = dassert(ts3.freq,e.freq);
%$ t(3) = dassert(ts3.data,e.data);
%$ t(4) = dassert(ts3.name,e.name);
%$ T = all(t);
%@eof:2

%@test:3
%$ % Define a data set.
%$ A = [transpose(1:7),2*transpose(1:7)];
%$ B = [transpose(5:11),2*transpose(5:11)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$ B_name = {'B1';'B2'};
%$
%$ % Define initial date
%$ A_init = '1950Q1';
%$ B_init = '1950Q3';
%$
%$ % Define expected results.
%$ e.freq = 4;
%$ e.init = dates('1950Q1');
%$ e.name = {'A1';'A2';'B1';'B2'};
%$ e.data = [ [A; NaN(2,2)], [NaN(2,2); B]];
%$
%$ % Instantiate two time series objects.
%$ ts1 = dseries(A,A_init,A_name,[]);
%$ ts2 = dseries(B,B_init,B_name,[]);
%$
%$ % Call the tested method.
%$ ts3 = [ts1,ts2];
%$
%$ % Check the results.
%$ t(1) = dassert(ts3.init,e.init);
%$ t(2) = dassert(ts3.freq,e.freq);
%$ t(3) = dassert(ts3.data,e.data);
%$ t(4) = dassert(ts3.name,e.name);
%$ T = all(t);
%@eof:3

%@test:4
%$ % Define a data set.
%$ A = [transpose(1:7),2*transpose(1:7)];
%$ B = [transpose(5:9),2*transpose(5:9)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$ B_name = {'B1';'B2'};
%$
%$ % Define initial date
%$ A_init = '1950Q1';
%$ B_init = '1950Q3';
%$
%$ % Define expected results.
%$ e.init = dates(A_init);
%$ e.freq = 4;
%$ e.name = {'A1';'A2';'B1';'B2'};
%$ e.data = [ A, [NaN(2,2); B]];
%$
%$ % Instantiate two time series objects.
%$ ts1 = dseries(A,A_init,A_name,[]);
%$ ts2 = dseries(B,B_init,B_name,[]);
%$
%$ % Call the tested method.
%$ ts3 = [ts1,ts2];
%$
%$ % Check the results.
%$ t(1) = dassert(ts3.init,e.init);
%$ t(2) = dassert(ts3.freq,e.freq);
%$ t(3) = dassert(ts3.data,e.data);
%$ t(4) = dassert(ts3.name,e.name);
%$ T = all(t);
%@eof:4

%@test:5
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
%$ B = [transpose(1:10),3*transpose(1:10)];
%$ C = [transpose(1:10),4*transpose(1:10)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$ B_name = {'B1';'B2'};
%$ C_name = {'C1';'C2'};
%$
%$ % Define expected results.
%$ e.init = dates(1,1);
%$ e.freq = 1;
%$ e.name = {'A1';'A2';'B1';'B2';'C1';'C2'};
%$ e.data = [A,B,C];
%$
%$ % Instantiate two time series objects.
%$ ts1 = dseries(A,[],A_name,[]);
%$ ts2 = dseries(B,[],B_name,[]);
%$ ts3 = dseries(C,[],C_name,[]);
%$
%$ % Call the tested method.
%$ ts4 = [ts1,ts2,ts3];
%$
%$ % Check the results.
%$ t(1) = dassert(ts4.init,e.init);
%$ t(2) = dassert(ts4.freq,e.freq);
%$ t(3) = dassert(ts4.data,e.data);
%$ t(4) = dassert(ts4.name,e.name);
%$ T = all(t);
%@eof:5

%@test:6
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
%$ B = [transpose(1:10),2*transpose(1:10)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$ B_name = {'B1';'A2'};
%$
%$ % Instantiate two time series objects.
%$ ts1 = dseries(A,[],A_name,[]);
%$ ts2 = dseries(B,[],B_name,[]);
%$
%$ % Call the tested method.
%$ try
%$   ts3 = [ts1,ts2];
%$   t = 0;
%$ catch
%$   t = 1;
%$ end
%$
%$ T = t;
%@eof:6

%@test:7
%$ % Define X
%$ X = randn(30,2);
%$
%$ % Instantiate two time series objects.
%$ ts1 = dseries();
%$ ts2 = dseries(randn(30,2),'1950Q2');
%$
%$ % Call the tested method.
%$ try
%$   ts3 = [ts1,ts2];
%$   t = 1;
%$ catch
%$   t = 0;
%$ end
%$
%$ if t(1)
%$   t(2) = dassert(ts3.freq,4);
%$   t(3) = dassert(ts3.data,X);
%$   t(4) = dassert(ts3.dates(1),dates('1950Q2'));
%$ end
%$
%$ T = t;
%@eof:7
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397

%@test:8
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
%$ B = [transpose(1:10),2*transpose(1:10)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$ B_name = {'B1';'B2'};
%$
%$ % Define expected results.
%$ e.init = dates(1,1);
%$ e.freq = 1;
%$ e.name = {'A1';'A2';'B1';'B2'};
%$ e.data = [A,B];
%$
%$ % Instantiate two time series objects.
%$ ts1 = dseries(A,[],A_name,[]);
%$ ts2 = dseries(B,[],B_name,[]);
%$ ts1.tag('t1');
%$ ts1.tag('t1', 'A1', 'Stock');
%$ ts1.tag('t1', 'A2', 'Flow');
%$ ts2.tag('t2');
%$ ts2.tag('t2', 'B1', 0);
%$ ts2.tag('t2', 'B2', 1);
%$
%$ % Call the tested method.
%$ try
%$   ts3 = [ts1,ts2];
%$   t(1) = true;
%$ catch
%$   t(1) = false;
%$ end
%$
%$ % Check the results.
%$ if t(1)
%$   t(2) = dassert(ts3.init,e.init);
%$   t(3) = dassert(ts3.freq,e.freq);
%$   t(4) = dassert(ts3.data,e.data);
%$   t(5) = dassert(ts3.name,e.name);
%$   t(6) = dassert(ts3.tags.t1,{'Stock';'Flow';[];[]});
%$   t(7) = dassert(ts3.tags.t2,{[];[];0;1});
%$ end
%$ T = all(t);
%@eof:8