chain_.m 3.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
function o = chain_(o, p)  % --*-- Unitary tests --*--

% Copyright (C) 2014-2017 Dynare Team
%
% 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/>.

20 21 22 23 24 25 26 27
noinputname = false;

if isempty(inputname(1))
    % If method is called with dot notation (eg o.chain_(p) insteady of chain(o, p)), input names
    % are not defined.
    noinputname = true;
end

28
if vobs(o)-vobs(p)
29 30 31 32 33
    if noinputname
        error(['dseries::chain: dseries objects must have the same number of variables!'])
    else
        error(['dseries::chain: dseries objects ' inputname(1) ' and ' inputname(2) ' must have the same number of variables!'])
    end
34 35 36
end

if frequency(o)-frequency(p)
37 38 39 40 41
    if noinputname
        error(['dseries::chain: dseries objects must have common frequencies!'])
    else
        error(['dseries::chain: dseries objects ' inputname(1) ' and ' inputname(2) ' must have common frequencies!'])
    end
42 43 44
end

if lastdate(o)<firstdate(p)
45 46 47 48 49
    if noinputname
        error(['dseries::chain: The last date in first dseries object (' date2string(o.dates(end)) ') must not preceed the first date in the second dseries object (' date2string(p.dates(1)) ')!'])
    else
        error(['dseries::chain: The last date in ' inputname(1) ' (' date2string(o.dates(end)) ') must not preceed the first date in ' inputname(2) ' (' date2string(p.dates(1)) ')!'])
    end
50 51 52 53 54 55 56 57 58 59
end

tdx = find(sum(bsxfun(@eq, p.dates.time, o.dates.time(end,:)),2)==2);
GrowthFactor = p.data(tdx+1:end,:)./p.data(tdx:end-1,:);
CumulatedGrowthFactors = cumprod(GrowthFactor);

o.data = [o.data; bsxfun(@times,CumulatedGrowthFactors, o.data(end,:))];

o.dates = firstdate(o):firstdate(o)+nobs(o);

Stéphane Adjemian's avatar
Stéphane Adjemian committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
for i=1:o.vobs
    if isempty(o.ops{i})
        if noinputname
            o.ops(i) = {sprintf('chain(%s, %s)', o.name{i}, p.name{i})};
        else
            o.ops(i) = {sprintf('chain(%s, %s.%s)', o.name{i}, inputname(2), p.name{i})};
        end
    else
        if noinputname
            o.ops(i) = {sprintf('chain(%s, %s)', o.ops{i}, p.name{i})};
        else
            o.ops(i) = {sprintf('chain(%s, %s.%s)', o.ops{i}, inputname(2), p.name{i})};
        end
    end
end


77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
%@test:1
%$ try
%$     ts = dseries([1; 2; 3; 4],dates('1950Q1')) ;
%$     us = dseries([3; 4; 5; 6],dates('1950Q3')) ;
%$     ts.chain_(us);
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ if t(1)
%$     t(2) = dassert(ts.freq,4);
%$     t(3) = dassert(ts.init.freq,4);
%$     t(4) = dassert(ts.init.time,[1950, 1]);
%$     t(5) = dassert(ts.vobs,1);
%$     t(6) = dassert(ts.nobs,6);
%$     t(7) = isequal(ts.data,transpose(1:6));
%$ end
%$
%$ T = all(t);
%@eof:1