1function [o, p] = align_(o, p) % --*-- Unitary tests --*--
2
3% If necessay completes dseries object o and p so that they are defined on the same time range
4% (in place modification).
5%
6% INPUTS
7% - o [dseries]
8% - p [dseries]
9%
10% OUTPUTS
11% - o [dseries]
12% - p [dseries]
13
14% Copyright (C) 2013-2018 Dynare Team
15%
16% This file is part of Dynare.
17%
18% Dynare is free software: you can redistribute it and/or modify
19% it under the terms of the GNU General Public License as published by
20% the Free Software Foundation, either version 3 of the License, or
21% (at your option) any later version.
22%
23% Dynare is distributed in the hope that it will be useful,
24% but WITHOUT ANY WARRANTY; without even the implied warranty of
25% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26% GNU General Public License for more details.
27%
28% You should have received a copy of the GNU General Public License
29% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
30
31if ~isequal(frequency(o),frequency(p))
32    error(['dseries::align: ''' inputname(1) ''' and ''' inputname(2) ''' dseries objects must have common frequencies!'])
33end
34
35if isempty(intersect(o.dates,p.dates))
36    error(['dseries::align: ''' inputname(1) ''' and ''' inputname(2) ''' dseries object must have at least one common date!'])
37end
38
39init = min(firstdate(o),firstdate(p));
40last = max(lastdate(o),lastdate(p));
41
42if firstdate(p)>init
43    n = firstdate(p)-init;
44    p.data = [NaN(n, vobs(p)); p.data];
45end
46
47if firstdate(o)>init
48    n = firstdate(o)-init;
49    o.data = [NaN(n, vobs(o)); o.data];
50end
51
52if lastdate(p)<last
53    n = last-lastdate(p);
54    p.data = [p.data; NaN(n, vobs(p))];
55end
56
57if lastdate(o)<last
58    n = last-lastdate(o);
59    o.data = [o.data; NaN(n, vobs(o))];
60end
61
62o.dates = init:init+(nobs(o)-1);
63p.dates = init:init+(nobs(p)-1);
64
65%@test:1
66%$ % Define a datasets.
67%$ A = rand(8,3); B = rand(7,2);
68%$
69%$ % Define names
70%$ A_name = {'A1';'A2';'A3'};
71%$ B_name = {'B1';'B2'};
72%$
73%$ % Define initial dates
74%$ A_init = '1990Q1';
75%$ B_init = '1989Q2';
76%$
77%$ % Instantiate two dseries objects
78%$ ts1 = dseries(A,A_init,A_name);
79%$ ts2 = dseries(B,B_init,B_name);
80%$
81%$ try
82%$   [ts1, ts2] = align(ts1, ts2);
83%$   t(1) = 1;
84%$ catch
85%$   t(1) = 0;
86%$ end
87%$
88%$ if t(1)
89%$   t(2) = dassert(ts1.nobs,ts2.nobs);
90%$   t(3) = dassert(ts1.init,ts2.init);
91%$   t(4) = dassert(ts1.data,[NaN(3,3); A], 1e-15);
92%$   t(5) = dassert(ts2.data,[B; NaN(4,2)], 1e-15);
93%$   t(6) = dassert(ts1.dates, ts2.dates);
94%$ end
95%$ T = all(t);
96%@eof:1
97
98%@test:2
99%$ % Define a datasets.
100%$ A = rand(8,3); B = rand(7,2);
101%$
102%$ % Define names
103%$ A_name = {'A1';'A2';'A3'};
104%$ B_name = {'B1';'B2'};
105%$
106%$ % Define initial dates
107%$ A_init = '1990Q1';
108%$ B_init = '1990Q1';
109%$
110%$ % Instantiate two dseries objects
111%$ ts1 = dseries(A,A_init,A_name);
112%$ ts2 = dseries(B,B_init,B_name);
113%$
114%$ try
115%$   [ts1, ts2] = align(ts1, ts2);
116%$   t(1) = 1;
117%$ catch
118%$   t(1) = 0;
119%$ end
120%$
121%$ if t(1)
122%$   t(2) = dassert(ts1.nobs,ts2.nobs);
123%$   t(3) = dassert(ts1.init,ts2.init);
124%$   t(4) = dassert(ts1.data,A, 1e-15);
125%$   t(5) = dassert(ts2.data,[B; NaN(1,2)], 1e-15);
126%$   t(6) = dassert(ts1.dates, ts2.dates);
127%$ end
128%$ T = all(t);
129%@eof:2
130
131%@test:3
132%$ % Define a datasets.
133%$ A = rand(8,3); B = rand(7,2);
134%$
135%$ % Define names
136%$ A_name = {'A1';'A2';'A3'};
137%$ B_name = {'B1';'B2'};
138%$
139%$ % Define initial dates
140%$ A_init = '1990Q1';
141%$ B_init = '1990Q1';
142%$
143%$ % Instantiate two dseries objects
144%$ ts1 = dseries(A,A_init,A_name);
145%$ ts2 = dseries(B,B_init,B_name);
146%$
147%$ try
148%$   [ts2, ts1] = align(ts2, ts1);
149%$   t(1) = 1;
150%$ catch
151%$   t(1) = 0;
152%$ end
153%$
154%$ if t(1)
155%$   t(2) = dassert(ts1.nobs,ts2.nobs);
156%$   t(3) = dassert(ts1.init,ts2.init);
157%$   t(4) = dassert(ts1.data,A, 1e-15);
158%$   t(5) = dassert(ts2.data,[B; NaN(1,2)], 1e-15);
159%$   t(6) = dassert(ts1.dates, ts2.dates);
160%$ end
161%$ T = all(t);
162%@eof:3