cumprod_.m 7.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
function o = cumprod_(varargin) % --*-- Unitary tests --*--

% Overloads matlab's cumprod function for dseries objects.
%
% INPUTS
% - o     dseries object [mandatory].
% - d     dates object [optional]
% - v     dseries object with one observation [optional]
%
% OUTPUTS
% - o     dseries object.

% 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/>.

% Get the firt observation number where all the variables are observed (ie without NaNs)
idx = find(all(~isnan(varargin{1}.data), 2),1);
if isempty(idx)
    idx = 0;
end

% Is the first period where variables are observed common?
common_first_period_witout_nan = true;
if ~idx
    if any(~isnan(varargin{1}.data(:)))
        common_first_period_witout_nan = false;
    end
else
    if idx>1
        if any(any(~isnan(varargin{1}.data(1:idx-1,:))))
            common_first_period_witout_nan = false;
        end
    end
end

switch nargin
  case 1
52 53 54 55 56 57 58 59 60 61 62 63 64
    % Initialize the output.
    o = varargin{1};
    % Perform the cumulated sum
    if isequal(idx, 1)
        o.data = cumprod(o.data);
    else
        if common_first_period_witout_nan
            o.data(idx:end,:) = cumprod(o.data(idx:end,:));
        else
            o.data = cumprodnan(o.data);
        end
    end
    for i=1:vobs(o)
65 66 67 68 69
        if isempty(o.ops{i})
            o.ops(i) = {['cumprod(' o.name{i} ')']};
        else
            o.ops(i) = {['cumprod(' o.ops{i} ')']};
        end
70
    end
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  case 2
    if isdseries(varargin{2})
        if ~isequal(vobs(varargin{1}), vobs(varargin{2}))
            error('dseries::cumprod: First and second input arguments must be dseries objects with the same number of variables!')
        end
        if ~isequal(varargin{1}.name, varargin{2}.name)
            warning('dseries::cumprod: First and second input arguments must be dseries objects do not have the same variables!')
        end
        if ~isequal(nobs(varargin{2}),1)
            error('dseries::cumprod: Second input argument must be a dseries object with only one observation!')
        end
        o = cumprod_(varargin{1});
        o.data = bsxfun(@rdivide, o.data, o.data(1,:));
        o.data = bsxfun(@times, o.data, varargin{2}.data);
    elseif isdates(varargin{2})
        o = cumprod_(varargin{1});
        t = find(o.dates==varargin{2});
        if isempty(t)
            if varargin{2}==(firstdate(o)-1)
                return
            else
                error(['dseries::cumprod: date ' date2string(varargin{2}) ' is not in the sample!'])
            end
        end
        o.data = bsxfun(@rdivide, o.data, o.data(t,:));
    else
        error('dseries::cumprod: Second input argument must be a dseries object or a dates object!')
    end
  case 3
    if ~isdates(varargin{2})
        error('dseries::cumprod: Second input argument must be a dates object!')
    end
    if ~isdseries(varargin{3})
        error('dseries::cumprod: Third input argument must be a dseries object!')
    end
    if ~isequal(vobs(varargin{1}), vobs(varargin{3}))
        error('dseries::cumprod: First and third input arguments must be dseries objects with the same number of variables!')
    end
    if ~isequal(varargin{1}.name, varargin{3}.name)
        warning('dseries::cumprod: First and third input arguments must be dseries objects do not have the same variables!')
    end
    if ~isequal(nobs(varargin{3}),1)
        error('dseries::cumprod: Third input argument must be a dseries object with only one observation!')
    end
    o = cumprod_(varargin{1});
    t = find(o.dates==varargin{2});
    if isempty(t)
        if varargin{2}==(firstdate(o)-1)
            o.data = bsxfun(@times, o.data, varargin{3}.data);
            return
        else
            error(['dseries::cumprod: date ' date2string(varargin{2}) ' is not in the sample!'])
        end
    end
    o.data = bsxfun(@rdivide, o.data, o.data(t,:));
    o.data = bsxfun(@times, o.data, varargin{3}.data);
  otherwise
    error('dseries::cumprod: Wrong number of input arguments!')
end

%@test:1
%$ % Define a data set.
%$ A = 2*ones(4,1);
%$
%$ % Define names
%$ A_name = {'A1'};
%$
%$ % Instantiate a time series object.
%$ ts = dseries(A,[],A_name,[]);
%$
%$ % Call the tested method.
%$ try
%$     ts.cumprod_();
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ if t(1)
%$     t(2) = isequal(ts.data, cumprod(A));
151 152
%$     t(3) = isequal(ts.name{1}, 'A1');
%$     t(4) = isequal(ts.ops{1}, 'cumprod(A1)');
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
%$ end
%$
%$ T = all(t);
%@eof:1

%@test:2
%$ % Define a data set.
%$ A = 2*ones(4,1);
%$
%$ % Define names
%$ A_name = {'A1'};
%$
%$ % Instantiate a time series object.
%$ ts = dseries(A,[],A_name,[]);
%$
%$ % Call the tested method.
%$ try
%$     cumprod_(ts);
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ if t(1)
177 178 179
%$     t(2) = isequal(ts.data, cumprod(A));
%$     t(3) = isequal(ts.name{1},'A1');
%$     t(4) = isequal(ts.ops{1}, 'cumprod(A1)');
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
%$ end
%$
%$ T = all(t);
%@eof:2


%@test:3
%$ % Define a data set.
%$ A = 2*ones(7,1);
%$
%$ % Define names
%$ A_name = {'A1'};
%$
%$ % Instantiate a time series object.
%$ ts = dseries(A,[],A_name,[]);
%$
%$ % Call the tested method.
%$ try
%$   ts.cumprod_(dates('3Y'));
%$   t(1) = 1;
%$ catch
%$   t(1) = 0;
%$ end
%$
%$ % Expected results.
%$ ds = dseries([.25; .5; 1; 2; 4; 8; 16], [], A_name, []);
%$
%$ % Check the results.
%$ t(1) = dassert(ts.data, ds.data);
%$ T = all(t);
%@eof:3

%@test:4
%$ % Define a data set.
%$ A = 2*ones(7,1);
%$
%$ % Define names
%$ A_name = {'A1'};
%$
%$ % Instantiate a time series object.
%$ ts1 = dseries(A, [], A_name, []);
%$ ts2 = dseries(pi, [], A_name, []);
%$
%$ % Call the tested method.
%$ try
%$   ts3 = ts1.cumprod_(dates('3Y'), ts2);
%$   t(1) = 1;
%$ catch
%$   t(1) = 0;
%$ end
%$
%$ % Expected results.
%$ ts4 = dseries([.25; .5; 1; 2; 4; 8; 16]*pi, [], A_name, []);
%$
%$ % Check the results.
%$ if t(1)
%$   t(2) = dassert(ts3.data,ts4.data);
%$   t(3) = dassert(ts1.data,ts4.data);
%$ end
%$ T = all(t);
%@eof:4

%@test:5
%$ % Define a data set.
%$ A = 2*ones(7,1);
%$
%$ % Define names
%$ A_name = {'A1'};
%$
%$ % Instantiate a time series object.
%$ ts1 = dseries(A, [], A_name, []);
%$ ts2 = dseries(pi, [], A_name, []);
%$
%$ % Call the tested method.
%$ try
%$   ts3 = ts1.cumprod_(dates('3Y'),ts2);
%$   t(1) = 1;
%$ catch
%$   t(1) = 0;
%$ end
%$
%$ % Expected results.
%$ ts4 = dseries([.25; .5; 1; 2; 4; 8; 16]*pi, [], A_name, []);
%$
%$ % Check the results.
%$ if t(1)
%$   t(2) = dassert(ts3.data, ts4.data);
%$   t(3) = dassert(ts1.data, ts4.data);
%$ end
%$ T = all(t);
%@eof:5

%@test:6
%$ % Define a data set.
%$ A = [NaN, NaN; 1 NaN; 1 1; 1 1; 1 NaN];
%$
%$ % Instantiate a time series object.
%$ ts0 = dseries(A);
%$
%$ % Call the tested method.
%$ try
%$   ts0.cumprod_();
%$   t(1) = 1;
%$ catch
%$   t(1) = 0;
%$ end
%$
%$ % Check the results.
%$ if t(1)
%$   t(2) = dassert(ts0.data, A);
%$ end
%$ T = all(t);
%@eof:6