Skip to content
Snippets Groups Projects
time-series.rst 73.74 KiB

Time Series

Dynare provides a MATLAB/Octave class for handling time series data, which is based on a class for handling dates. Dynare also provides a new type for dates, so that the basic user does not have to worry about class and methods for dates. Below, you will first find the class and methods used for creating and dealing with dates and then the class used for using time series.

Dates

Dates in a mod file

Dynare understands dates in a mod file. Users can declare annual, quarterly, or monthly dates using the following syntax:

1990Y
1990Q3
1990M11

Behind the scene, Dynare’s preprocessor translates these expressions into instantiations of the MATLAB/Octave’s class dates described below. Basic operations can be performed on dates:

plus binary operator (+)

An integer scalar, interpreted as a number of periods, can be added to a date. For instance, if a = 1950Q1 then b = 1951Q2 and b = a + 5 are identical.

plus unary operator (+)

Increments a date by one period. +1950Q1 is identical to 1950Q2, ++++1950Q1 is identical to 1951Q1.

minus binary operator (-)

Has two functions: difference and subtraction. If the second argument is a date, calculates the difference between the first date and the secmond date (e.g. 1951Q2-1950Q1 is equal to 5). If the second argument is an integer X, subtracts X periods from the date (e.g. 1951Q2-2 is equal to 1950Q4).

minus unary operator (-)

Subtracts one period to a date. -1950Q1 is identical to 1949Q4. The unary minus operator is the reciprocal of the unary plus operator, +-1950Q1 is identical to 1950Q1.

colon operator (:)

Can be used to create a range of dates. For instance, r = 1950Q1:1951Q1 creates a dates object with five elements: 1950Q1, 1950Q2, 1950Q3, 1950Q4 and 1951Q1. By default the increment between each element is one period. This default can be changed using, for instance, the following instruction: 1950Q1:2:1951Q1 which will instantiate a dates object with three elements: 1950Q1, 1950Q3 and 1951Q1.

horzcat operator ([,])

Concatenates dates objects without removing repetitions. For instance [1950Q1, 1950Q2] is a dates object with two elements (1950Q1 and 1950Q2).

vertcat operator ([;])

Same as horzcat operator.

eq operator (equal, ==)

Tests if two dates objects are equal. +1950Q1==1950Q2 returns true, 1950Q1==1950Q2 returns false. If the compared objects have both n>1 elements, the eq operator returns a column vector, n by 1, of zeros and ones.

ne operator (not equal, ~=)

Tests if two dates objects are not equal. +1950Q1~= returns false while 1950Q1~=1950Q2 returns true. If the compared objects both have n>1 elements, the ne operator returns an n by 1 column vector of zeros and ones.

lt operator (less than, <)

Tests if a dates object preceeds another dates object. For instance, 1950Q1<1950Q3 returns true. If the compared objects have both n>1 elements, the lt operator returns a column vector, n by 1, of zeros and ones.

gt operator (greater than, >)

Tests if a dates object follows another dates object. For instance, 1950Q1>1950Q3 returns false. If the compared objects have both n>1 elements, the gt operator returns a column vector, n by 1, of zeros and ones.

le operator (less or equal, <=)

Tests if a dates object preceeds another dates object or is equal to this object. For instance, 1950Q1<=1950Q3 returns true. If the compared objects have both n>1 elements, the le operator returns a column vector, n by 1, of zeros and ones.

ge operator (greater or equal, >=)

Tests if a dates object follows another dates object or is equal to this object. For instance, 1950Q1>=1950Q3 returns false. If the compared objects have both n>1 elements, the ge operator returns a column vector, n by 1, of zeros and ones.

One can select an element, or some elements, in a dates object as he would extract some elements from a vector in MATLAB/Octave. Let a = 1950Q1:1951Q1 be a dates object, then a(1)==1950Q1 returns true, a(end)==1951Q1 returns true and a(end-1:end) selects the two last elements of a (by instantiating the dates object [1950Q4, 1951Q1]).

Remark: Dynare substitutes any occurrence of dates in the .mod file into an instantiation of the dates class regardless of the context. For instance, d = 1950Q1 will be translated as d = dates('1950Q1');. This automatic substitution can lead to a crash if a date is defined in a string. Typically, if the user wants to display a date:

disp('Initial period is 1950Q1');

Dynare will translate this as:

disp('Initial period is dates('1950Q1')');

which will lead to a crash because this expression is illegal in MATLAB. For this situation, Dynare provides the $ escape parameter. The following expression:

disp('Initial period is $1950Q1');

will be translated as:

disp('Initial period is 1950Q1');

in the generated MATLAB script.

The dates class

arg int freq: equal to 1, 4, or 12 (resp. for annual, quarterly, or monthly dates).
arg int ndat: the number of declared dates in the object.
arg int time: a ndat*2 array, the years are stored in the first column, the subperiods (1 for annual dates, 1-4 for quarterly dates, and 1-12 for monthly dates) are stored in the second column.

Each member is private, one can display the content of a member but cannot change its value directly. Note that it is not possible to mix frequencies in a dates object: all the elements must have common frequency.

The dates class has the following constructors:

Example

do1 = dates('1950Q1');
do2 = dates('1950Q2','1950Q3');
do3 = dates(do1,do2);
do4 = dates('Q',1950, 1);

A list of the available methods, by alphabetical order, is given below. Note that the MATLAB/Octave classes do not allow in place modifications: when a method is applied to an object a new object is instantiated. For instance, to apply the method multiplybytwo to an object X we write:

Y = X.multiplybytwo()

or equivalently:

Y = multiplybytwo(X)

the object X is left unchanged, and the object Y is a modified copy of X.

The dseries class

|br| The MATLAB/Octave dseries class handles time series data. As any MATLAB/Octave statements, this class can be used in a Dynare’s mod file. A dseries object has six members:

arg name: A nobs*1 cell of strings or a nobs*p character array, the names of the variables.
arg tex: A nobs*1 cell of strings or a nobs*p character array, the tex names of the variables.
arg dates dates: An object with nobs elements, the dates of the sample.
arg double data: A nobs by vobs array, the data.
arg ops: The history of operations on the variables.
arg tags: The user-defined tags on the variables.

data, name, tex are private members. The following constructors are available:

Example

Various ways to create a dseries object:

do1 = dseries(1999Q3);
do2 = dseries('filename.csv');
do3 = dseries([1; 2; 3], 1999Q3, {'var123'}, {'var_{123}'});

>> do1 = dseries(dates('1999Q3'));
>> do2 = dseries('filename.csv');
>> do3 = dseries([1; 2; 3], dates('1999Q3'), {'var123'}, {'var_{123}'});

One can easily create subsamples from a dseries object using the overloaded parenthesis operator. If ds is a dseries object with T observations and d is a dates object with S<T elements, such that \min(d) is not smaller than the date associated to the first observation in ds and \max(d) is not greater than the date associated to the last observation, then ds(d) instantiates a new dseries object containing the subsample defined by d.

A list of the available methods, by alphabetical order, is given below.

|br| Overloads the MATLAB/octave’s isempty function. Returns 1 if dseries object A is empty, 0 otherwise.