Home > matlab > @dynSeries > dynSeries.m

dynSeries

PURPOSE ^

@info:

SYNOPSIS ^

function ts = dynSeries(a,b,c,d)

DESCRIPTION ^

@info:
! @deftypefn {Function File} {@var{ts} =} dynSeries (@var{a},@var{b},@var{c},@var{d})
! @anchor{dynSeries}
! @sp 1
! Constructor for the Dynare time series class.
! @sp 2
! @strong{Inputs}
! @sp 1
! @table @ @var
! @item a
! T*1 vector or T*N matrix of data.
! @item b
! Initial date. For Quaterly, Monthly or Weekly data, b must be a string. For yearly data or if the frequence is not
! defined b must be an integer.
! @item c
! N*q array of characters. Names of the N time series.
! @item d
! N*p array of characters. TeX names of the N time series.
! @end table
! @sp 1
! @strong{Outputs}
! @sp 1
! @table @ @var
! @item ts
! Dynare time series object.
! @end table
! @sp 1
! @strong{Properties}
! @sp 1
! The constructor defines the following properties:
! @sp 1
! @table @ @var
! @item data
! Array of doubles (nobs*vobs).
! @item nobs
! Scalar integer, the number of observations.
! @item vobs
! Scalar integer, the number of variables.
! @item name
! Array of chars (nvobs*n), names of the variables.
! @item tex
! Array of chars (nvobs*n), tex names of the variables.
! @item freq
! Scalar integer, the frequency of the time series. @var{freq} is equal to 1 if data are on a yearly basis or if
! frequency is unspecified. @var{freq} is equal to 4 if data are on a quaterly basis. @var{freq} is equal to
! 12 if data are on a monthly basis. @var{freq} is equal to 52 if data are on a weekly basis.
! @item time
! Array of integers (nobs*2). The first column defines the years associated to each observation. The second column,
! depending on the frequency, indicates the week, month or quarter numbers. For yearly data or unspecified frequency
! the second column is filled by ones.
! @item init
! Row vector of integers (1*2) indicating the year and the week, month or quarter of the first observation. @var{init}
! is the first row of @var{time}.
! @item last
! Row vector of integers (1*2) indicating the year and the week, month or quarter of the last observation. @var{init}
! is the first row of @var{time}.
! @end table
! @sp 1
! @strong{This function is called by:}
! @sp 2
! @strong{This function calls:}
! @ref{@@dynTime/dynTime}, @ref{@@dynTime/setTime}, @ref{@@dynTime/setFreq} 
!
! @end deftypefn
@eod:

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function ts = dynSeries(a,b,c,d)
0002 
0003 %@info:
0004 %! @deftypefn {Function File} {@var{ts} =} dynSeries (@var{a},@var{b},@var{c},@var{d})
0005 %! @anchor{dynSeries}
0006 %! @sp 1
0007 %! Constructor for the Dynare time series class.
0008 %! @sp 2
0009 %! @strong{Inputs}
0010 %! @sp 1
0011 %! @table @ @var
0012 %! @item a
0013 %! T*1 vector or T*N matrix of data.
0014 %! @item b
0015 %! Initial date. For Quaterly, Monthly or Weekly data, b must be a string. For yearly data or if the frequence is not
0016 %! defined b must be an integer.
0017 %! @item c
0018 %! N*q array of characters. Names of the N time series.
0019 %! @item d
0020 %! N*p array of characters. TeX names of the N time series.
0021 %! @end table
0022 %! @sp 1
0023 %! @strong{Outputs}
0024 %! @sp 1
0025 %! @table @ @var
0026 %! @item ts
0027 %! Dynare time series object.
0028 %! @end table
0029 %! @sp 1
0030 %! @strong{Properties}
0031 %! @sp 1
0032 %! The constructor defines the following properties:
0033 %! @sp 1
0034 %! @table @ @var
0035 %! @item data
0036 %! Array of doubles (nobs*vobs).
0037 %! @item nobs
0038 %! Scalar integer, the number of observations.
0039 %! @item vobs
0040 %! Scalar integer, the number of variables.
0041 %! @item name
0042 %! Array of chars (nvobs*n), names of the variables.
0043 %! @item tex
0044 %! Array of chars (nvobs*n), tex names of the variables.
0045 %! @item freq
0046 %! Scalar integer, the frequency of the time series. @var{freq} is equal to 1 if data are on a yearly basis or if
0047 %! frequency is unspecified. @var{freq} is equal to 4 if data are on a quaterly basis. @var{freq} is equal to
0048 %! 12 if data are on a monthly basis. @var{freq} is equal to 52 if data are on a weekly basis.
0049 %! @item time
0050 %! Array of integers (nobs*2). The first column defines the years associated to each observation. The second column,
0051 %! depending on the frequency, indicates the week, month or quarter numbers. For yearly data or unspecified frequency
0052 %! the second column is filled by ones.
0053 %! @item init
0054 %! Row vector of integers (1*2) indicating the year and the week, month or quarter of the first observation. @var{init}
0055 %! is the first row of @var{time}.
0056 %! @item last
0057 %! Row vector of integers (1*2) indicating the year and the week, month or quarter of the last observation. @var{init}
0058 %! is the first row of @var{time}.
0059 %! @end table
0060 %! @sp 1
0061 %! @strong{This function is called by:}
0062 %! @sp 2
0063 %! @strong{This function calls:}
0064 %! @ref{@@dynTime/dynTime}, @ref{@@dynTime/setTime}, @ref{@@dynTime/setFreq}
0065 %!
0066 %! @end deftypefn
0067 %@eod:
0068 
0069 % Copyright (C) 2011 Dynare Team
0070 %
0071 % This file is part of Dynare.
0072 %
0073 % Dynare is free software: you can redistribute it and/or modify
0074 % it under the terms of the GNU General Public License as published by
0075 % the Free Software Foundation, either version 3 of the License, or
0076 % (at your option) any later version.
0077 %
0078 % Dynare is distributed in the hope that it will be useful,
0079 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0080 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0081 % GNU General Public License for more details.
0082 %
0083 % You should have received a copy of the GNU General Public License
0084 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0085 
0086 % AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
0087 
0088 ts = struct;
0089 
0090 ts.data = [];
0091 ts.nobs = 0;
0092 ts.vobs = 0;
0093 ts.name = [];
0094 ts.tex  = [];
0095 ts.freq = [];
0096 ts.Time = dynTime();
0097 
0098 ts = class(ts,'dynSeries');
0099 
0100 switch nargin
0101   case 0
0102     return
0103   case {2,4}
0104     if nargin==2
0105         c = [];
0106         d = [];
0107     end
0108     % Get data, number of observations and number of variables.
0109     ts.data = a;
0110     ts.nobs = size(a,1);
0111     ts.vobs = size(a,2);
0112     % Get the first date and set the frequency.
0113     if ~isempty(b)
0114         if ischar(b)% Weekly, Monthly or Quaterly data.
0115             quaterly = findstr('Q',b);
0116             monthly  = findstr('M',b);
0117             weekly   = findstr('W',b);
0118             if ~isempty(quaterly)
0119                 ts.freq = 4;
0120             end
0121             if ~isempty(monthly)
0122                 ts.freq = 12;
0123             end
0124             if ~isempty(weekly)
0125                 ts.freq = 52;
0126             end
0127             if isempty(quaterly) && isempty(monthly) && isempty(weekly)
0128                 error('dynSeries:: Using a string as a second input argument, I can only handle weekly (W), monthly (M) or quaterly (Q) data!');
0129             end
0130         else% If b is not a string then yearly data are assumed.
0131             ts.freq = 1;
0132         end
0133         ts.Time = ts.Time.setFreq(ts.freq);
0134         ts.Time = ts.Time.setTime(dynDate(b):dynDate(b)+ts.nobs);
0135     else% If b is empty.
0136         ts.freq = 1;
0137         ts.Time = ts.Time.setFreq(1);
0138         ts.Time = ts.Time.setTime([transpose(1:ts.nobs) ones(ts.nobs,1)]);
0139     end
0140     % Get the names of the variables.
0141     if ~isempty(c)
0142         if ts.vobs==size(c,1)
0143             ts.name = c;
0144         else
0145             error('dynSeries:: The number of declared names does not match the number of variables!')
0146         end
0147     else
0148         for i=1:ts.vobs
0149             ts.name = char(ts.name,'--NA--');
0150         end
0151     end
0152     if ~isempty(d)
0153         if ts.vobs==size(d,1)
0154             ts.tex = d;
0155         else
0156             error('dynSeries:: The number of declared tex names does not match the number of variables!')
0157         end
0158     else
0159         for i=1:ts.vobs
0160             ts.tex = char(ts.tex,'--NA--');
0161         end
0162     end
0163   otherwise
0164     error('dynSeries:: Can''t instantiate the class, wrong calling sequence!')
0165 end
0166 
0167 
0168 %@test:1
0169 %$ addpath ../matlab
0170 %$ % Define a data set.
0171 %$ A = transpose(1:10);
0172 %$
0173 %$ % Define initial date
0174 %$ B1 = 1950;
0175 %$ B2 = '1950Q2';
0176 %$ B3 = '1950M10';
0177 %$ B4 = '1950W50';
0178 %$
0179 %$ % Define expected results.
0180 %$ e1.Time = transpose([1950 1951 1952 1953 1954 1955 1956 1957 1958 1959]);
0181 %$ e1.freq = 1;
0182 %$ e2.Time = char('1950Q2','1950Q3','1950Q4','1951Q1','1951Q2','1951Q3','1951Q4','1952Q1','1952Q2','1952Q3');
0183 %$ e2.freq = 4;
0184 %$ e3.Time = char('1950M10','1950M11','1950M12','1951M1','1951M2','1951M3','1951M4','1951M5','1951M6','1951M7');
0185 %$ e3.freq = 12;
0186 %$ e4.Time = char('1950W50','1950W51','1950W52','1951W1','1951W2','1951W3','1951W4','1951W5','1951W6','1951W7');
0187 %$ e4.freq = 52;
0188 %$
0189 %$ % Call the tested routine.
0190 %$ ts1 = dynSeries(A,B1);
0191 %$ ts2 = dynSeries(A,B2);
0192 %$ ts3 = dynSeries(A,B3);
0193 %$ ts4 = dynSeries(A,B4);
0194 %$
0195 %$ % Check the results.
0196 %$ t(1) = dyn_assert(getTime(ts1),e1.Time);
0197 %$ t(2) = dyn_assert(getTime(ts2),e2.Time);
0198 %$ t(3) = dyn_assert(getTime(ts3),e3.Time);
0199 %$ t(4) = dyn_assert(getTime(ts4),e4.Time);
0200 %$ t(5) = dyn_assert(ts1.freq,e1.freq);
0201 %$ t(6) = dyn_assert(ts2.freq,e2.freq);
0202 %$ t(7) = dyn_assert(ts3.freq,e3.freq);
0203 %$ t(8) = dyn_assert(ts4.freq,e4.freq);
0204 %$ T = all(t);
0205 %@eof:1

Generated on Fri 18-May-2012 02:41:00 by m2html © 2005