vertcat.m 4.21 KB
Newer Older
1
function o = vertcat(varargin) % --*-- Unitary tests --*--
2

3 4
% Overloads vertcat method for dseries objects.
%
Stéphane Adjemian's avatar
Stéphane Adjemian committed
5
% INPUTS
6 7 8 9
%  o o1    dseries object.
%  o o2    dseries object.
%  o ...
%
Stéphane Adjemian's avatar
Stéphane Adjemian committed
10
% OUTPUTS
11 12
%  o o     dseries object.
%
Stéphane Adjemian's avatar
Stéphane Adjemian committed
13
% EXAMPLE 1
14
%  If o1, o2 and o3 are dseries objects containing the same variables over different samples, the following syntax:
Stéphane Adjemian's avatar
Stéphane Adjemian committed
15
%
16 17 18
%    o = [o1; o2; o3] ;
%
%  merges the samples.
19

20
% Copyright (C) 2013-2017 Dynare Team
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
%
% 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/>.

if nargin==0
38
    o = dseries();
39
elseif nargin == 1
40
    o = copy(varargin{1});
41
elseif nargin>1
42
    o = copy(varargin{1});
43
    for i=2:nargin
44
        o = vertcat_(o, varargin{i});
45 46 47 48
    end
end

function d = vertcat_(b, c)
Stéphane Adjemian's avatar
Stéphane Adjemian committed
49 50 51 52 53 54 55 56 57 58 59 60
d = NaN;
if ~isequal(frequency(b), frequency(c))
    error('dseries::vertcat: Frequencies must be common!')
end
if ~isequal(vobs(b), vobs(c))
    error('dseries::vertcat: Number of variables must be common!')
end
reorder_variables_in_c = false;
if ~isequal(b.name, c.name)
    [t, idx] = ismember(b.name, c.name);
    if all(t)
        reorder_variables_in_c = true;
61
    else
Stéphane Adjemian's avatar
Stéphane Adjemian committed
62
        error('dseries::vertcat: Variables must be common!')
63
    end
Stéphane Adjemian's avatar
Stéphane Adjemian committed
64 65 66 67 68 69 70 71
end
d = b;
if reorder_variables_in_c
    d.data = [b.data; c.data(:,idx)];
else
    d.data = [b.data; c.data];
end
d.dates = [b.dates; c.dates];
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

%@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 = {'A1';'A2'};
%$
%$ % Define expected results.
%$ e.init = dates(1,1);
%$ e.freq = 1;
%$ e.name = {'A1';'A2'};
%$ e.data = [A;B];
%$
%$ % Instantiate two time series objects.
%$ ts1 = dseries(A,[],A_name,[]);
%$ ts2 = dseries(B,[],B_name,[]);
%$
%$ % Call the tested method.
93 94 95 96 97 98
%$ try
%$   ts3 = [ts1;ts2];
%$   t(1) = 1;
%$ catch
%$   t(1) = 0;
%$ end
99 100
%$
%$ % Check the results.
101 102 103 104 105 106 107
%$ 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.nobs,20);
%$ end
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
%$ T = all(t);
%@eof:1

%@test:2
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
%$ B = [transpose(1:10),2*transpose(1:10)];
%$ C = [transpose(1:10),3*transpose(1:10)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$ B_name = {'A1';'A2'};
%$ C_name = {'A1';'A2'};
%$
%$ % Define expected results.
%$ e.init = dates(1,1);
%$ e.freq = 1;
%$ e.name = {'A1';'A2'};
%$ 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.
134 135 136 137 138 139
%$ try
%$   ts4 = [ts1; ts2; ts3];
%£   t(1) = 1;
%$ catch
%$   t(1) = 0;
%$ end
140 141
%$
%$
142 143 144 145 146 147 148 149
%$ % Check the results.
%$ if t(1)
%$   t(2) = dassert(ts4.init,e.init);
%$   t(3) = dassert(ts4.freq,e.freq);
%$   t(4) = dassert(ts4.data,e.data);
%$   t(5) = dassert(ts4.name,e.name);
%$   t(6) = dassert(ts4.nobs,30);
%$ end
150 151
%$ T = all(t);
%@eof:2
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

%@test:3
%$ A = dseries([ones(5,1), 2*ones(5,1)],'1938Q4',{'A1', 'A2'});
%$ B = dseries([2*ones(2,1), ones(2,1)],'1945Q3',{'A2', 'A1'});
%$
%$ try
%$    C = [A; B];
%$    t(1) = true;
%$ catch
%$    t(1) = false;
%$ end
%$
%$ % Check the results.
%$ if t(1)
%$    t(2) = dassert(C.data,[ones(7,1), 2*ones(7,1)]);
%$ end
%$ T = all(t);
%@eof:3
170 171 172

%@test:4
%$ A = dseries(ones(3, 1), '1990Q1');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
173
%$ B = dseries(2*ones(3, 1), '1990Q4');
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
%$
%$ try
%$    C = [A; B];
%$    t(1) = true;
%$ catch
%$    t(1) = false;
%$ end
%$
%$ % Check the results.
%$ if t(1)
%$    t(2) = dassert(C.data, [ones(3,1); 2*ones(3,1)]);
%$    t(3) = dassert(A.data, ones(3,1));
%$    t(4) = dassert(B.data, 2*ones(3,1));
%$ end
%$ T = all(t);
%@eof:4