1function o = backcast_(o, p, diff)  % --*-- Unitary tests --*--
2
3% Copyright © 2019-2020 Dynare Team
4%
5% This file is part of Dynare.
6%
7% Dynare is free software: you can redistribute it and/or modify
8% it under the terms of the GNU General Public License as published by
9% the Free Software Foundation, either version 3 of the License, or
10% (at your option) any later version.
11%
12% Dynare is distributed in the hope that it will be useful,
13% but WITHOUT ANY WARRANTY; without even the implied warranty of
14% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15% GNU General Public License for more details.
16%
17% You should have received a copy of the GNU General Public License
18% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
19
20if nargin<3
21    % By default o is backcasted with growth rates of p.
22    diff = false;
23end
24
25if ~isequal(vobs(o), vobs(p))
26    error('dseries objects must have the same number of variables!')
27end
28
29if ~isequal(frequency(o), frequency(p))
30    error('dseries objects must have common frequencies!')
31end
32
33if firstdate(o)>lastdate(p)
34    error('The first period (%s) in the first dseries object cannot be posterior to the last period (%s) in the second object.\n ==> dseries objects have to overlap.', date2string(o.dates(1)), date2string(o.dates(end)))
35end
36
37if firstdate(o)<=firstdate(p)
38    error('The first period (%s) in the second dseries object does not preceed the first period (%s) in the first object.\n ==> Cannot backcast first dseries object.', date2string(p.dates(1)), date2string(o.dates(1)))
39end
40
41idp = find(o.dates(1)==p.dates);
42
43if diff
44    FirstDifference = p.data(2:idp,:)-p.data(1:idp-1,:);
45    CumulatedDifferences = rcumsum(FirstDifference);
46    o.data = [bsxfun(@minus, o.data(1), CumulatedDifferences); o.data];
47else
48    GrowthFactor = p.data(2:idp,:)./p.data(1:idp-1,:);
49    CumulatedGrowthFactors = rcumprod(GrowthFactor);
50    o.data = [bsxfun(@rdivide, o.data(1), CumulatedGrowthFactors); o.data];
51end
52
53o.dates = firstdate(p):lastdate(o);
54
55for i=1:o.vobs
56    if isempty(o.ops{i})
57        arg1 = o.name{i};
58    else
59        arg1 = o.ops{i};
60    end
61    if diff
62        o.ops(i) = {sprintf('backcast(%s, %s.%s, diff)', arg1, inputname(2), p.name{i})};
63    else
64        o.ops(i) = {sprintf('backcast(%s, %s.%s)', arg1, inputname(2), p.name{i})};
65    end
66end
67
68return
69
70%@test:1
71  y = ones(10,1);
72  for i=2:10
73    y(i) = 1.1*y(i-1);
74  end
75
76  ds = dseries(y(6:10), '1990Q1', 'toto');
77  ts = dseries(y(1:6), '1988Q4', 'popeye');
78
79  try
80    ds.backcast_(ts);
81    t(1) = true;
82  catch
83    t(1) = false;
84  end
85
86  if t(1)
87    t(2) = dassert(ds.freq,4);
88    t(3) = dassert(ds.init.freq,4);
89    t(4) = dassert(ds.init.time,[1988, 4]);
90    t(5) = dassert(ds.vobs,1);
91    t(6) = dassert(ds.nobs,10);
92    t(7) = abs(ds.data(1)-1)<1e-9;
93  end
94
95  T = all(t);
96%@eof:1
97
98%@test:2
99  y = ones(10,1);
100  for i=2:10
101    y(i) = y(i-1) + 1;
102  end
103
104  ds = dseries(y(6:10), '1990Q1', 'toto');
105  ts = dseries(y(1:6), '1988Q4', 'popeye');
106
107  try
108    ds.backcast_(ts, true);
109    t(1) = true;
110  catch
111    t(1) = false;
112  end
113
114  if t(1)
115    t(2) = dassert(ds.freq,4);
116    t(3) = dassert(ds.init.freq,4);
117    t(4) = dassert(ds.init.time,[1988, 4]);
118    t(5) = dassert(ds.vobs,1);
119    t(6) = dassert(ds.nobs,10);
120    t(7) = abs(ds.data(1)-1)<1e-9;
121  end
122
123  T = all(t);
124%@eof:2