Debian Codegraph @

contrib/ms-sbvar/TZcode/MatlabFiles/datana.m

raw
1 function [yactyrge,yactyre,yactqmyge,yactqmge,yactqme] = datana(xdatae,q_m,vlistlog,vlistper,Byrqm,Eyrqm)
2 % [yactyrge,yactyre,yactqmyge,yactqmge,yactqme] = datana(Byrqm,Eyrqm,xdatae,q_m,vlistlog,vlistper)
3 %
4 % Generate prior period, period-to-period-in-last-year, and annual growth rates
5 % For annual rates, works for both calendar and any annual years, depending on Byrqm and Eyrqm
6 % If monthly data, we haven't got time to get quarterly growth rates yet.
7 %
8 % xdatae: all data (some of which may be NaN) with the first 2 columns indicating years and periods.
9 % q_m: quarter or month period
10 % vlistlog: sublist for logged variables
11 % vlistper: sublists for percent variables
12 % Byrqm: [year quarter(month)]: beginning year and period. If Byqm(2)~=1, we don't get
13 % calendar annual rates. In other words, the first column of yactyge (which
14 % indicates years) does not mean calendar years
15 % Eyrqm: [year period]: end year and period
16 % NOTE: if no inputs Byrqm and Eyrqm are specified, all growth rates begin at xdatae(1,1:2).
17 %----------
18 % yactyrge: annual growth rates with dates in the first 2 columns.
19 % yactyre: annual average logged level with dates in the 1st 2 columns.
20 % yactqmyge: period-to-period-in-last-year annual growth rates with dates in the first 2 columns.
21 % yactqmge: prior-period annualized growth rates with dates in the first 2 columns.
22 % yactqme: logged data with dates in the first 2 columns.
23 %
24 % Tao Zha, April 2000
25 %
26 % Copyright (C) 1997-2012 Tao Zha
27 %
28 % This free software: you can redistribute it and/or modify
29 % it under the terms of the GNU General Public License as published by
30 % the Free Software Foundation, either version 3 of the License, or
31 % (at your option) any later version.
32 %
33 % It is distributed in the hope that it will be useful,
34 % but WITHOUT ANY WARRANTY; without even the implied warranty of
35 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 % GNU General Public License for more details.
37 %
38 % If you did not received a copy of the GNU General Public License
39 % with this software, see <http://www.gnu.org/licenses/>.
40 %
41
42
43 if size(xdatae,1)<2*q_m
44 error('We need at least two years of xdatae to get annual rates. Check xdatae!!')
45 end
46
47 if nargin==4
48 Brow=1; Erow=length(xdatae(:,1));
49 nyr = floor((Erow-Brow+1)/q_m);
50 yrsg = [xdatae(q_m+1,1):xdatae(q_m+1,1)+nyr-2]'; % for annual growth later on
51 else
52 if Byrqm(2)<1 | Eyrqm(2)<1
53 error('This function requires both years and months (quarters) in Byrqm and Eyrqm')
54 end
55
56 Brow = min(find(xdatae(:,1)==Byrqm(1)));
57 if isempty(Brow)
58 error('Byrqm is outside the date range of xdatae(:,1:2)!')
59 end
60 nadt=Byrqm(2)-xdatae(Brow,2);
61 if nadt<0
62 error('Byrqm is outside the date range indicated by xdatae(:,1:2)!')
63 end
64 Brow=Brow+nadt;
65 %
66 Erow = min(find(xdatae(:,1)==Eyrqm(1)));
67 if isempty(Brow)
68 error('Eyrqm is outside the date range of xdatae(:,1:2)!')
69 end
70 nadt2=Eyrqm(2)-xdatae(Erow,2);
71 if nadt<0
72 error('Eyrqm is outside the date range indicated by xdatae(:,1:2)!')
73 end
74 Erow=Erow+nadt2;
75
76 nyr = floor((Erow-Brow+1)/q_m);
77 yrsg = [Byrqm(1)+1:Byrqm(1)+nyr-1]'; % for annual growth later on, which will
78 % start at Byrqm(1) instead of Byrqm(1)+1
79 end
80 %
81 yactqme = xdatae(Brow:Erow,:); % with dates
82 yactqm = yactqme(:,3:end); % only data
83
84 %======== prior period change (annaluized rate)
85 yactqmg = yactqm(2:end,:); % start at second period to get growth rate
86 yactqmg(:,vlistlog) = (yactqm(2:end,vlistlog) - yactqm(1:end-1,vlistlog)) .* q_m;
87 % monthly, 12*log(1+growth rate), annualized growth rate
88 yactqmg(:,vlistlog) = 100*(exp(yactqmg(:,vlistlog))-1);
89 yactqmg(:,vlistper) = 100*yactqmg(:,vlistper);
90 yactqmge = [yactqme(2:end,1:2) yactqmg];
91
92 %======== change from the last year
93 yactqmyg = yactqm(q_m+1:end,:); % start at the last-year period to get growth rate
94 yactqmyg(:,vlistlog) = (yactqm(q_m+1:end,vlistlog) - yactqm(1:end-q_m,vlistlog));
95 yactqmyg(:,vlistlog) = 100*(exp(yactqmyg(:,vlistlog))-1);
96 yactqmyg(:,vlistper) = 100*yactqmyg(:,vlistper);
97 yactqmyge = [yactqme(q_m+1:end,1:2) yactqmyg];
98
99 %======== annual growth rates
100 nvar = length(xdatae(1,3:end));
101 ygmts = yactqm(1:nyr*q_m,:); % converted to the multiplication of q_m
102 ygmts1 = reshape(ygmts,q_m,nyr,nvar);
103 ygmts2 = sum(ygmts1,1) ./ q_m;
104 ygmts3 = reshape(ygmts2,nyr,nvar); % converted to annual average series
105 %
106 yactyrg = ygmts3(2:end,:); % start at the last-year period to get growth rate
107 yactyrg(:,vlistlog) = ygmts3(2:end,vlistlog) - ygmts3(1:end-1,vlistlog);
108 % annual rate: log(1+growth rate)
109 yactyrg(:,vlistlog) = 100*(exp(yactyrg(:,vlistlog))-1);
110 yactyrg(:,vlistper) = 100*yactyrg(:,vlistper);
111 yactyrge = [yrsg zeros(nyr-1,1) yactyrg];
112 yrsg1=[yrsg(1)-1:yrsg(end)]';
113 yactyre = [yrsg1 zeros(nyr,1) ygmts3];