subsref.m 18.4 KB
Newer Older
Stéphane Adjemian's avatar
Stéphane Adjemian committed
1
function r = subsref(o, S) % --*-- Unitary tests --*--
2

Stéphane Adjemian's avatar
Stéphane Adjemian committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16
% Overloads the subsref method for dseries class.
%
% INPUTS
% - o    [dseries]    Time series object instantiated by dseries.
% - S    [struct]     Matlab's structure array S with two fields, type and subs. The type field is
%                     a string containing '()', '{}', or '.', where '()' specifies integer
%                     subscripts, '{}' specifies cell array subscripts, and '.' specifies
%                     subscripted structure fields. The subs field is a cell array or a string
%                     containing the actual subscripts (see matlab's documentation).
%
% OUTPUTS
% - r    [dseries]    Depending on the calling sequence `r` is a transformation of `o` obtained
%                     by applying a public method on `o`, or a dseries object built by extracting
%                     a variable from `o`, or a dseries object containing a subsample.
17

Dóra Kocsis's avatar
Dóra Kocsis committed
18
% Copyright (C) 2011-2020 Dynare Team
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
%
% 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/>.

switch S(1).type
  case '.'
    switch S(1).subs
38
      case {'data','name','tex','dates','ops', 'tags'}        % Public members.
39 40 41
        if length(S)>1 && isequal(S(2).type,'()') && isempty(S(2).subs)
            error(['dseries::subsref: ' S(1).subs ' is not a method but a member!'])
        end
Stéphane Adjemian's avatar
Stéphane Adjemian committed
42
        r = builtin('subsref', o, S(1));
43 44
      case 'nobs'
        % Returns the number of observations.
Stéphane Adjemian's avatar
Stéphane Adjemian committed
45
        r = rows(o.data);
46 47
      case 'vobs'
        % Returns the number of variables.
Stéphane Adjemian's avatar
Stéphane Adjemian committed
48
        r = columns(o.data);
49 50
      case 'init'
        % Returns a dates object (first date).
Stéphane Adjemian's avatar
Stéphane Adjemian committed
51
        r = o.dates(1);
52 53
      case 'last'
        % Returns a dates object (last date).
Stéphane Adjemian's avatar
Stéphane Adjemian committed
54
        r = o.dates(end);
55
      case 'freq'
56
        % Returns an integer characterizing the data frequency (1, 2, 4, 12, 52 or 365)
Stéphane Adjemian's avatar
Stéphane Adjemian committed
57
        r = o.dates.freq;
58 59 60
      case 'length'
        error(['dseries::subsref: we do not support the length operator on ' ...
               'dseries. Please use ''nobs'' or ''vobs''']);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
61
      case 'save'
Stéphane Adjemian's avatar
Stéphane Adjemian committed
62
        % Save dseries object on disk (default is a mat file).
Stéphane Adjemian's avatar
Stéphane Adjemian committed
63
        r = NaN;
64 65 66
        if isequal(length(S),2)
            if strcmp(S(2).type,'()')
                if isempty(S(2).subs)
Stéphane Adjemian's avatar
Stéphane Adjemian committed
67
                    save(o);
68 69
                else
                    if isempty(S(2).subs{1})
Stéphane Adjemian's avatar
Stéphane Adjemian committed
70
                        save(o,'',S(2).subs{2});
71
                    else
Stéphane Adjemian's avatar
Stéphane Adjemian committed
72
                        save(o,S(2).subs{:});
73 74 75 76 77 78 79
                    end
                end
                S = shiftS(S,1);
            else
                error('dseries::subsref: Wrong syntax.')
            end
        elseif isequal(length(S),1)
Stéphane Adjemian's avatar
Stéphane Adjemian committed
80
            save(o);
81 82 83
        else
            error('dseries::subsref: Call to save method must come in last position!')
        end
84
      case 'struct'
Stéphane Adjemian's avatar
Stéphane Adjemian committed
85
        r = dseries2struct(o);
86
      case 'initialize'
87
        r = NaN;
88
        initialize(o);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
89 90 91 92 93 94 95 96 97 98 99 100 101
      case {'baxter_king_filter', 'baxter_king_filter_', ...
            'cumsum','cumsum_', ...
            'insert', ...
            'pop','pop_', ...
            'cumprod','cumprod_', ...
            'remove','remove_', ...
            'onesidedhptrend','onesidedhptrend_', ...
            'onesidedhpcycle','onesidedhpcycle_', ...
            'lag','lag_', ...
            'lead','lead_', ...
            'hptrend','hptrend_', ...
            'hpcycle','hpcycle_', ...
            'chain','chain_', ...
Stéphane Adjemian's avatar
Stéphane Adjemian committed
102
            'backcast','backcast_', ...
Stéphane Adjemian's avatar
Stéphane Adjemian committed
103 104 105
            'detrend','detrend_', ...
            'exist', ...
            'mean', ...
Stéphane Adjemian's avatar
Stéphane Adjemian committed
106
            'nanmean', ...
Stéphane Adjemian's avatar
Stéphane Adjemian committed
107 108 109 110 111
            'std', ...
            'center','center_', ...
            'log','log_', ...
            'exp','exp_', ...
            'ygrowth','ygrowth_', ...
Dóra Kocsis's avatar
Dóra Kocsis committed
112
            'hgrowth','hgrowth_', ...
Stéphane Adjemian's avatar
Stéphane Adjemian committed
113
            'qgrowth','qgrowth_', ...
Stéphane Adjemian's avatar
Stéphane Adjemian committed
114
            'mgrowth','mgrowth_', ...
115
            'dgrowth','dgrowth_', ...
Stéphane Adjemian's avatar
Stéphane Adjemian committed
116
            'ydiff','ydiff_', ...
Dóra Kocsis's avatar
Dóra Kocsis committed
117
            'hdiff','hdiff_', ...
Stéphane Adjemian's avatar
Stéphane Adjemian committed
118
            'qdiff','qdiff_', ...
Stéphane Adjemian's avatar
Stéphane Adjemian committed
119
            'diff', 'diff_', ...
Stéphane Adjemian's avatar
Stéphane Adjemian committed
120
            'mdiff','mdiff_', ...
Stéphane Adjemian's avatar
Stéphane Adjemian committed
121
            'abs','abs_', ...
Stéphane Adjemian's avatar
Stéphane Adjemian committed
122
            'flip','flip_', ...
Stéphane Adjemian's avatar
Stéphane Adjemian committed
123
            'isnan', ...
Houtan Bastani's avatar
Houtan Bastani committed
124
            'isinf', ...
Houtan Bastani's avatar
Houtan Bastani committed
125
            'isreal', ...
Stéphane Adjemian's avatar
Stéphane Adjemian committed
126
            'firstdate', ...
127
            'lastdate', ...
Stéphane Adjemian's avatar
Stéphane Adjemian committed
128 129
            'firstobservedperiod', ...
            'lastobservedperiod', ...
130
            'lineartrend', ...
Houtan Bastani's avatar
Houtan Bastani committed
131 132
            'resetops', 'resettags', ...
            'subsample'}
Stéphane Adjemian's avatar
Stéphane Adjemian committed
133 134
        if length(S)>1 && isequal(S(2).type,'()')
            if isempty(S(2).subs)
Stéphane Adjemian's avatar
Stéphane Adjemian committed
135
                r = feval(S(1).subs,o);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
136 137
                S = shiftS(S,1);
            else
Stéphane Adjemian's avatar
Stéphane Adjemian committed
138
                r = feval(S(1).subs,o,S(2).subs{:});
Stéphane Adjemian's avatar
Stéphane Adjemian committed
139 140 141
                S = shiftS(S,1);
            end
        else
Stéphane Adjemian's avatar
Stéphane Adjemian committed
142
            r = feval(S(1).subs,o);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
143
        end
144 145 146
      case 'size'
        if isequal(length(S),2) && strcmp(S(2).type,'()')
            if isempty(S(2).subs)
Stéphane Adjemian's avatar
Stéphane Adjemian committed
147 148
                [x,y] = size(o);
                r = [x, y];
149
            else
Stéphane Adjemian's avatar
Stéphane Adjemian committed
150
                r = size(o,S(2).subs{1});
151 152 153
            end
            S = shiftS(S,1);
        elseif isequal(length(S),1)
Stéphane Adjemian's avatar
Stéphane Adjemian committed
154 155
            [x,y] = size(o);
            r = [x, y];
156 157 158
        else
            error('dseries::subsref: Call to size method must come in last position!')
        end
159
      case {'set_names','rename','rename_','tex_rename','tex_rename_', 'tag'}
Stéphane Adjemian's avatar
Stéphane Adjemian committed
160
        r = feval(S(1).subs,o,S(2).subs{:});
161 162
        S = shiftS(S,1);
      case {'disp'}
Stéphane Adjemian's avatar
Stéphane Adjemian committed
163
        feval(S(1).subs,o);
164 165
        return
      otherwise                                                            % Extract a sub-object by selecting one variable.
Stéphane Adjemian's avatar
Stéphane Adjemian committed
166
        ndx = find(strcmp(S(1).subs,o.name));
167
        if ~isempty(ndx)
Stéphane Adjemian's avatar
Stéphane Adjemian committed
168 169 170 171 172 173 174
            r = dseries();
            r.data = o.data(:,ndx);
            r.name = o.name(ndx);
            r.tex = o.tex(ndx);
            r.dates = o.dates;
            r.ops = o.ops(ndx);
            tagnames = fieldnames(o.tags);
175
            for i=1:length(tagnames)
Stéphane Adjemian's avatar
Stéphane Adjemian committed
176
                r.tags.(tagnames{i}) = o.tags.(tagnames{i})(ndx);
177
            end
178 179 180 181 182 183 184
        else
            error('dseries::subsref: Unknown public method, public member or variable!')
        end
    end
  case '()'
    if ischar(S(1).subs{1}) && ~isdate(S(1).subs{1})
        % If ts is an empty dseries object, populate this object by reading data in a file.
Stéphane Adjemian's avatar
Stéphane Adjemian committed
185
        if isempty(o)
186
            if exist(S(1).subs{1}, 'file')
Stéphane Adjemian's avatar
Stéphane Adjemian committed
187
                r = dseries(S(1).subs{1});
188 189 190
            else
                error('dseries::subsref: Cannot find file %s', S(1).subs{1})
            end
191
        else
192
            error('dseries::subsref: dseries object is not empty!')
193
        end
194
    elseif isscalar(S(1).subs{1}) && isint(S(1).subs{1})
195 196
        % Input is also interpreted as a backward/forward operator
        if S(1).subs{1}>0
197
            r = lead(o, S(1).subs{1});
198
        elseif S(1).subs{1}<0
199
            r = lag(o, -S(1).subs{1});
200 201
        else
            % Do nothing.
Stéphane Adjemian's avatar
Stéphane Adjemian committed
202
            r = o;
203 204
        end
    elseif isdates(S(1).subs{1}) || isdate(S(1).subs{1})
Stéphane Adjemian's avatar
Stéphane Adjemian committed
205
        % Select observation(s) with date(s)
206 207 208 209 210 211
        if isdate(S(1).subs{1})
            Dates = dates(S(1).subs{1});
        else
            Dates = S(1).subs{1};
        end
        % Test if Dates is out of bounds
Stéphane Adjemian's avatar
Stéphane Adjemian committed
212 213
        if min(Dates)<min(o.dates)
            error(['dseries::subsref: Indices are out of bounds! Subsample cannot start before ' date2string(o.dates(1)) '.'])
214
        end
Stéphane Adjemian's avatar
Stéphane Adjemian committed
215 216
        if  max(Dates)>max(o.dates)
            error(['dseries::subsref: Indices are out of bounds! Subsample cannot end after ' date2string(o.dates(end)) '.'])
217 218
        end
        % Extract a subsample using a dates object
Stéphane Adjemian's avatar
Stéphane Adjemian committed
219 220 221 222
        [~,tdx] = intersect(o.dates.time,Dates.time,'rows');
        r = copy(o);
        r.data = r.data(tdx,:);
        r.dates = r.dates(tdx);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
223
    elseif isnumeric(S(1).subs{1}) && isequal(ndims(S(1).subs{1}), 2)
Stéphane Adjemian's avatar
Stéphane Adjemian committed
224
        if isempty(o)
Stéphane Adjemian's avatar
Stéphane Adjemian committed
225
            % Populate an empty dseries object.
Stéphane Adjemian's avatar
Stéphane Adjemian committed
226 227 228 229 230 231 232
            if isempty(o.dates)
                r = copy(o);
                r.dates = dates('1Y'):dates('1Y')+(rows(S(1).subs{1})-1);
                r.data = S(1).subs{1};
                r.name = default_name(columns(r.data));
                r.tex = name2tex(r.name);
                r.ops = cell(length(r.name), 1);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
233
            else
Stéphane Adjemian's avatar
Stéphane Adjemian committed
234 235 236 237 238 239
                r = copy(o);
                r.dates = r.dates:r.dates+(rows(S(1).subs{1})-1);
                r.data = S(1).subs{1};
                r.name = default_name(columns(r.data));
                r.tex = name2tex(r.name);
                r.ops = cell(length(r.name), 1);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
240 241 242 243
            end
        else
            error('dseries::subsref: It is not possible to populate a non empty dseries object!');
        end
244 245 246 247 248
    else
        error('dseries::subsref: I have no idea of what you are trying to do!')
    end
  case '{}'
    if iscellofchar(S(1).subs)
Stéphane Adjemian's avatar
Stéphane Adjemian committed
249
        r = extract(o,S(1).subs{:});
250 251
    elseif isequal(length(S(1).subs),1) && all(isint(S(1).subs{1}))
        idx = S(1).subs{1};
Stéphane Adjemian's avatar
Stéphane Adjemian committed
252
        if max(idx)>size(o.data,2) || min(idx)<1
253 254
            error('dseries::subsref: Indices are out of bounds!')
        end
Stéphane Adjemian's avatar
Stéphane Adjemian committed
255 256 257 258 259 260 261
        r = dseries();
        r.data = o.data(:,idx);
        r.name = o.name(idx);
        r.tex  = o.tex(idx);
        r.dates = o.dates;
        r.ops = o.ops(idx);
        tagnames = fieldnames(o.tags);
262
        for i=1:length(tagnames)
Stéphane Adjemian's avatar
Stéphane Adjemian committed
263
            r.tags.(tagnames{i}) = o.tags.(tagnames{i})(idx);
264
        end
265 266 267 268 269 270 271 272 273
    else
        error('dseries::subsref: What the Hell are you tryin'' to do?!')
    end
  otherwise
    error('dseries::subsref: What the Hell are you doin'' here?!')
end

S = shiftS(S,1);
if ~isempty(S)
Stéphane Adjemian's avatar
Stéphane Adjemian committed
274
    r = subsref(r, S);
275 276
end

277 278
return

279
%@test:1
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
 % Define a data set.
 A = [transpose(1:10),2*transpose(1:10)];

 % Define names
 A_name = {'A1';'A2'};

 % Instantiate a time series object.
 ts1 = dseries(A,[],A_name,[]);

 % Call the tested method.
 a = ts1(ts1.dates(2:9));

 % Expected results.
 e.data = [transpose(2:9),2*transpose(2:9)];
 e.nobs = 8;
 e.vobs = 2;
 e.name = {'A1';'A2'};
 e.freq = 1;
 e.init = dates(1,2);

 % Check the results.
 t(1) = dassert(a.data,e.data);
 t(2) = dassert(a.nobs,e.nobs);
 t(3) = dassert(a.vobs,e.vobs);
 t(4) = dassert(a.freq,e.freq);
 t(5) = dassert(a.init,e.init);
 T = all(t);
307 308 309
%@eof:1

%@test:2
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
 % Define a data set.
 A = [transpose(1:10),2*transpose(1:10)];

 % Define names
 A_name = {'A1';'A2'};

 % Instantiate a time series object.
 ts1 = dseries(A,[],A_name,[]);

 % Call the tested method.
 a = ts1.A1;

 % Expected results.
 e.data = transpose(1:10);
 e.nobs = 10;
 e.vobs = 1;
 e.name = {'A1'};
 e.freq = 1;
 e.init = dates(1,1);

 % Check the results.
 t(1) = dassert(a.data,e.data);
 t(2) = dassert(a.init,e.init);
 t(3) = dassert(a.nobs,e.nobs);
 t(4) = dassert(a.vobs,e.vobs);
 t(5) = dassert(a.freq,e.freq);
 T = all(t);
337 338 339
%@eof:2

%@test:3
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
 % Define a data set.
 A = [transpose(1:10),2*transpose(1:10)];

 % Define names
 A_name = {'A1';'A2'};

 % Instantiate a time series object.
 ts1 = dseries(A,[],A_name,[]);

 % Call the tested method.
 a = ts1.log;

 % Expected results.
 e.data = log(A);
 e.nobs = 10;
 e.vobs = 2;
 e.name = {'A1';'A2'};
 e.freq = 1;
 e.init = dates(1,1);

 % Check the results.
 t(1) = dassert(a.data,e.data);
 t(2) = dassert(a.nobs,e.nobs);
 t(3) = dassert(a.vobs,e.vobs);
 t(4) = dassert(a.freq,e.freq);
 t(5) = dassert(a.init,e.init);
 T = all(t);
367 368 369
%@eof:3

%@test:4
370 371 372 373 374 375
 % Create an empty dseries object.
 dataset = dseries();

 t = zeros(5,1);

 try
376 377
    dseries_src_root = strrep(which('initialize_dseries_class'),'initialize_dseries_class.m','');
    A = dseries([ dseries_src_root '../tests/data/dynseries_test_data.csv' ]);
378 379 380 381 382 383 384 385 386 387 388 389 390
    t(1) = 1;
 catch
    t = 0;
 end

 % Check the results.
 if length(t)>1
     t(2) = dassert(A.nobs,4);
     t(3) = dassert(A.vobs,4);
     t(4) = dassert(A.freq,4);
     t(5) = dassert(A.init,dates('1990Q1'));
 end
 T = all(t);
391 392 393
%@eof:4

%@test:5
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
 % Define a data set.
 A = [transpose(1:10),2*transpose(1:10),3*transpose(1:10)];

 % Define names
 A_name = {'A1';'A2';'B1'};

 % Instantiate a time series object.
 ts1 = dseries(A,[],A_name,[]);

 % Call the tested method.
 a = ts1{'A1','B1'};

 % Expected results.
 e.data = A(:,[1,3]);
 e.nobs = 10;
 e.vobs = 2;
 e.name = {'A1';'B1'};
 e.freq = 1;
 e.init = dates(1,1);

 t(1) = dassert(e.data,a.data);
 t(2) = dassert(e.nobs,a.nobs);
 t(3) = dassert(e.vobs,a.vobs);
 t(4) = dassert(e.name,a.name);
 t(5) = dassert(e.init,a.init);
 T = all(t);
420 421 422
%@eof:5

%@test:6
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
 % Define a data set.
 A = rand(10,24);

 % Define names
 A_name = {'GDP_1';'GDP_2';'GDP_3'; 'GDP_4'; 'GDP_5'; 'GDP_6'; 'GDP_7'; 'GDP_8'; 'GDP_9'; 'GDP_10'; 'GDP_11'; 'GDP_12'; 'HICP_1';'HICP_2';'HICP_3'; 'HICP_4'; 'HICP_5'; 'HICP_6'; 'HICP_7'; 'HICP_8'; 'HICP_9'; 'HICP_10'; 'HICP_11'; 'HICP_12';};

 % Instantiate a time series object.
 ts1 = dseries(A,[],A_name,[]);

 % Call the tested method.
 try
     a = ts1{'[GDP_[0-9]]'};
     t(1) = 1;
 catch
     t(1) = 0;
 end
 try
     b = ts1{'[[A-Z]*_1]'};
     t(2) = 1;
 catch
     t(2) = 0;
 end
 try
     warning off all
     c = ts1{'[A-Z]_1'};
     warning on all
     t(3) = 0;
 catch
     t(3) = 1;
 end

 % Expected results.
 e1.data = A(:,1:9);
 e1.nobs = 10;
 e1.vobs = 9;
 e1.name = {'GDP_1';'GDP_2';'GDP_3'; 'GDP_4'; 'GDP_5'; 'GDP_6'; 'GDP_7'; 'GDP_8'; 'GDP_9'};
 e1.freq = 1;
 e1.init = dates(1,1);
 e2.data = A(:,[1 13]);
 e2.nobs = 10;
 e2.vobs = 2;
 e2.name = {'GDP_1';'HICP_1'};
 e2.freq = 1;
 e2.init = dates(1,1);

 % Check results.
 t(4) = dassert(e1.data,a.data);
 t(5) = dassert(e1.nobs,a.nobs);
 t(6) = dassert(e1.vobs,a.vobs);
 t(7) = dassert(e1.name,a.name);
 t(8) = dassert(e1.init,a.init);
 t(9) = dassert(e2.data,b.data);
 t(10) = dassert(e2.nobs,b.nobs);
 t(11) = dassert(e2.vobs,b.vobs);
 t(12) = dassert(e2.name,b.name);
 t(13) = dassert(e2.init,b.init);
 T = all(t);
480 481 482
%@eof:6

%@test:7
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
 % Define a data set.
 A = [transpose(1:10),2*transpose(1:10)];

 % Define names
 A_name = {'A1';'A2'};

 % Instantiate a time series object.
 try
    ts1 = dseries(A,[],A_name,[]);
    ts1.save('ts1');
    t = 1;
 catch
    t = 0;
 end

 delete('ts1.mat');

 T = all(t);
501 502 503
%@eof:7

%@test:8
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
 % Define a data set.
 A = [transpose(1:10),2*transpose(1:10)];

 % Define names
 A_name = {'A1';'A2'};

 % Instantiate a time series object.
 try
    ts1 = dseries(A,[],A_name,[]);
    ts1.save('test_generated_data_file','m');
    delete('test_generated_data_file.m');
    t = 1;
 catch
    t = 0;
 end

 T = all(t);
521 522 523
%@eof:8

%@test:9
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
 % Define a data set.
 A = [transpose(1:60),2*transpose(1:60),3*transpose(1:60)];

 % Define names
 A_name = {'A1';'A2';'B1'};

 % Instantiate a time series object.
 ts1 = dseries(A,'1971Q1',A_name,[]);

 % Define the range of a subsample.
 range = dates('1971Q2'):dates('1971Q4');
 % Call the tested method.
 a = ts1(range);

 % Expected results.
 e.data = A(2:4,:);
 e.nobs = 3;
 e.vobs = 3;
 e.name = {'A1';'A2';'B1'};
 e.freq = 4;
 e.init = dates('1971Q2');

 t(1) = dassert(e.data,a.data);
 t(2) = dassert(e.nobs,a.nobs);
 t(3) = dassert(e.vobs,a.vobs);
 t(4) = dassert(e.name,a.name);
 t(5) = dassert(e.init,a.init);
 T = all(t);
552 553 554
%@eof:9

%@test:10
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
 % Define a data set.
 A = [transpose(1:60),2*transpose(1:60),3*transpose(1:60)];

 % Define names
 A_name = {'A1';'A2';'B1'};

 % Instantiate a time series object.
 ts1 = dseries(A,'1971Q1',A_name,[]);

 % Test the size method.
 B = ts1.size();
 C = ts1.size(1);
 D = ts1.size(2);
 E = ts1.size;

 t(1) = dassert(B,[60, 3]);
 t(2) = dassert(E,[60, 3]);
 t(3) = dassert(C,60);
 t(4) = dassert(D,3);
 T = all(t);
575 576 577
%@eof:10

%@test:11
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
 % Define a data set.
 A = [transpose(1:60),2*transpose(1:60),3*transpose(1:60)];

 % Define names
 A_name = {'A1';'A2';'B1'};

 % Instantiate a time series object.
 ts1 = dseries(A,'1971Q1',A_name,[]);

 % Test the size method.
 B = ts1{1};
 C = ts1{[1,3]};
 D = ts1{'A1'};

 t(1) = dassert(B.name{1},'A1');
 t(2) = dassert(B.data,A(:,1));
 t(3) = dassert(C.name{1},'A1');
 t(4) = dassert(C.data(:,1),A(:,1));
 t(5) = dassert(C.name{2},'B1');
 t(6) = dassert(C.data(:,2),A(:,3));
 t(7) = dassert(D.name{1},'A1');
 t(8) = dassert(D.data,A(:,1));
 T = all(t);
601 602 603
%@eof:11

%@test:12
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
 % Define a data set.
 A = [transpose(1:10),2*transpose(1:10)];

 % Define names
 A_name = {'A1';'A2'};

 % Instantiate a time series object.
 try
    ts1 = dseries(A,[],A_name,[]);
    ts1.save();
    t = 1;
 catch
    t = 0;
 end

 delete('dynare_series.mat')

 T = all(t);
622 623
%@eof:12

624
%@test:13
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
 try
     data = transpose(0:1:50);
     ts = dseries(data,'1950Q1');
     a = ts.lag;
     b = ts.lead;
     c = ts(-1);
     d = ts(1);
     t(1) = 1;
 catch
     t(1) = 0;
 end

 if t(1)>1
     t(2) = (a==c);
     t(3) = (b==d);
 end

 T = all(t);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
643
%@eof:13
644

645
%@test:14
646 647 648 649 650 651 652 653 654 655 656 657 658 659
 try
     ds = dseries(transpose(1:5));
     ts = ds(ds.dates(2:3));
     t(1) = 1;
 catch
     t(1) = 0;
 end

 if t(1)>1
     t(2) = isdseries(ts);
     t(3) = isequal(ts.data,ds.data(2:3));
 end

 T = all(t);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
660
%@eof:14
661

662
%@test:15
663 664 665 666 667 668 669 670 671
 try
     ds = dseries(transpose(1:5));
     ts = ds(ds.dates(2:6));
     t(1) = 0;
 catch
     t(1) = 1;
 end

 T = all(t);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
672
%@eof:15
673

674
%@test:16
675 676 677 678 679 680 681 682 683
 try
     ds = dseries(transpose(1:5));
     ts = ds(dates('1Y'):dates('6Y'));
     t(1) = 0;
 catch
     t(1) = 1;
 end

 T = all(t);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
684
%@eof:16
685

686
%@test:17
687 688 689 690 691 692 693 694 695
 try
     ds = dseries(transpose(1:5));
     ts = ds(dates('-2Y'):dates('4Y'));
     t(1) = 0;
 catch
     t(1) = 1;
 end

 T = all(t);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
%@eof:17

%@test:18
 try
     tseries = dseries();
     ts = tseries(ones(10,1));
     t(1) = true;
 catch
     t(1) = false;
 end

 if t(1)
     t(2) = ts.dates(1)==dates('1Y');
     t(3) = ts.dates(10)==dates('10Y');
 end

 T = all(t);
%@eof:18

%@test:19
 try
     tseries = dseries(dates('2000Q1'));
     ts = tseries(ones(5,1));
     t(1) = true;
 catch
     t(1) = false;
 end

 if t(1)
     t(2) = ts.dates(1)==dates('2000Q1');
     t(3) = ts.dates(5)==dates('2001Q1');
 end

 T = all(t);
730
%@eof:19
Dóra Kocsis's avatar
Dóra Kocsis committed
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746

%@test:20
 try
     tseries = dseries(dates('2000H1'));
     ts = tseries(ones(6,1));
     t(1) = true;
 catch
     t(1) = false;
 end

 if t(1)
     t(2) = ts.dates(1)==dates('2000H1');
     t(3) = ts.dates(6)==dates('2002H2');
 end

 T = all(t);
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
%@eof:20

%@test:21
 try
     tseries = dseries(dates('2000-01-01'));
     ts = tseries(ones(6,1));
     t(1) = true;
 catch
     t(1) = false;
 end

 if t(1)
     t(2) = ts.dates(1)==dates('2000-01-01');
     t(3) = ts.dates(6)==dates('2000-01-06');
 end

 T = all(t);
%@eof:21