Debian Codegraph @

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

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