1function p = subsample(o, d1, d2)  % --*-- Unitary tests --*--
2
3% function p = subsample(o, d1, d2)
4
5% Returns the subsample of a series
6%
7% INPUTS
8% - o  [dseries]
9% - d1 [date]    beginning date of subsample
10% - d2 [date]    last date of subsample
11%
12% OUTPUTS
13% - o  [dseries]
14%
15% EXAMPLE
16% Define a dseries object as follows:
17%
18% >> o = dseries(transpose(1:5))
19%
20% then o.subsample(dates('2y'), dates('4y')) returns
21%
22%    | Variable_1
23% 2Y | 2
24% 3Y | 3
25% 4Y | 4
26
27% Copyright (C) 2019 Dynare Team
28%
29% This file is part of Dynare.
30%
31% Dynare is free software: you can redistribute it and/or modify
32% it under the terms of the GNU General Public License as published by
33% the Free Software Foundation, either version 3 of the License, or
34% (at your option) any later version.
35%
36% Dynare is distributed in the hope that it will be useful,
37% but WITHOUT ANY WARRANTY; without even the implied warranty of
38% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39% GNU General Public License for more details.
40%
41% You should have received a copy of the GNU General Public License
42% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
43
44if nargin < 1 || nargin > 3
45    error('function takes 1 to 3 arguments')
46end
47
48if nargin == 1
49    p = copy(o);
50    return
51end
52
53if ~isa(d1, 'dates')
54    if isstringdate(d1)
55        d1 = dates(d1);
56    else
57        error('the arguments to subsample must be dates')
58    end
59end
60
61if nargin == 2
62    d2 = d1;
63end
64
65if nargin == 3
66    if ~isa(d2, 'dates')
67        if isstringdate(d2)
68            d2 = dates(d2);
69        else
70            error('the arguments to subsample must be dates')
71        end
72    end
73    if d2 < d1
74        error('the second date must be greater than or equal to the first date')
75    end
76end
77
78if d1 < min(o.dates)
79    error(['the first argument must be greater than or equal to ' date2string(min(o.dates))])
80end
81
82if d2 > max(o.dates)
83    error(['the second argument must be less than or equal to ' date2string(max(o.dates))])
84end
85
86idx = find(o.dates == d1):find(o.dates == d2);
87
88p = dseries();
89p.data  = o.data(idx, :);
90p.name  = o.name;
91p.tex   = o.tex;
92p.dates = o.dates(idx);
93p.ops   = o.ops;
94p.tags  = o.tags;
95
96end
97
98%@test:1
99%$ try
100%$     data = transpose(0:50);
101%$     ts = dseries(data,'1950Q1');
102%$     d1 = dates('1951q3');
103%$     ts1 = ts.subsample(d1);
104%$     t(1) = true;
105%$ catch
106%$     t(1) = false;
107%$ end
108%$
109%$ if t(1)
110%$     t(2) = ts1 == dseries(6, '1951q3');
111%$ end
112%$
113%$ T = all(t);
114%@eof:1
115
116%@test:2
117%$ try
118%$     data = transpose(0:50);
119%$     ts = dseries(data,'1950Q1');
120%$     d1 = dates('1951q3');
121%$     d2 = dates('1952q3');
122%$     ts1 = ts.subsample(d1, d2);
123%$     t(1) = true;
124%$ catch
125%$     t(1) = false;
126%$ end
127%$
128%$ if t(1)
129%$     t(2) = all(ts1 == dseries((6:10)', '1951q3'));
130%$ end
131%$
132%$ T = all(t);
133%@eof:2
134
135%@test:3
136%$ try
137%$     data = transpose(0:50);
138%$     ts = dseries(data,'1950Q1');
139%$     d1 = '1951q3';
140%$     ts1 = ts.subsample(d1);
141%$     t(1) = true;
142%$ catch
143%$     t(1) = false;
144%$ end
145%$
146%$ if t(1)
147%$     t(2) = ts1 == dseries(6, '1951q3');
148%$ end
149%$
150%$ T = all(t);
151%@eof:3
152
153%@test:4
154%$ try
155%$     data = transpose(0:50);
156%$     ts = dseries(data,'1950Q1');
157%$     d1 = '1951q3';
158%$     d2 = '1952q3';
159%$     ts1 = ts.subsample(d1, d2);
160%$     t(1) = true;
161%$ catch
162%$     t(1) = false;
163%$ end
164%$
165%$ if t(1)
166%$     t(2) = all(ts1 == dseries((6:10)', '1951q3'));
167%$ end
168%$
169%$ T = all(t);
170%@eof:4
171