| 1 |
function [yactyrge,yactyre,yactqmyge,yactqmge,yactqge,yacteoqyge,yactqme] = fn_datana2(xdatae,q_m,vlistlog,vlistper,Byrqm,Eyrqm) |
| 2 |
% [yactyrge,yactyre,yactqmyge,yactqmge,yactqge,yacteoqyge,yactqme] = fn_datana2(xdatae,q_m,vlistlog,vlistper,Byrqm,Eyrqm) |
| 3 |
% |
| 4 |
% Generate prior period, period-this-year-to-period-last-year, and other growth rates. |
| 5 |
% For annual rates, works for both calendar and any annual years, depending on Byrqm and Eyrqm |
| 6 |
% |
| 7 |
% xdatae: all data (logged levels or interest rates/100, some of which may be NaN) with the first |
| 8 |
% 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. Optional. 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. Byqm(2) must be specified; in other |
| 15 |
% words, it must be not set to 0 as in, say, fn_dataext. |
| 16 |
% Eyrqm: [year period] -- end year and period. Optional. Eyqm(2) must be specified; in other words, it |
| 17 |
% must be not set to 0 as in, say, fn_dataext. |
| 18 |
% NOTE: if no inputs Byrqm and Eyrqm are specified, all growth rates begin at xdatae(1,1:2). |
| 19 |
%---------- |
| 20 |
% yactyrge: annual growth rates with dates in the first 2 columns. |
| 21 |
% yactyre: annual average logged level with dates in the 1st 2 columns. |
| 22 |
% yactqmyge: period-this-year-to-period-last-year annual growth rates with dates in the first 2 columns. |
| 23 |
% yactqmge: prior-period annualized growth rates with dates in the first 2 columns. |
| 24 |
% yactqge: if monthly data, prior-quarter annualized growth rate with dates in the first 2 columns; |
| 25 |
% if not, yactqge = NaN. |
| 26 |
% yacteoqyge: if monthly data, EOQ is last month of quarter. EOQ-this-year-to-EOQ-last-year annual growth rates with dates in first 2 columns. |
| 27 |
% If not monthly data, yacteoqyge = NaN. |
| 28 |
% Note that yacteoqyge(:,1:2) = yactqge(4:end,1:2). |
| 29 |
% yactqme: data (logged levels or interest rates/100) with dates in the first 2 columns. |
| 30 |
% Same as xdatae but with Brow:Erow. |
| 31 |
% |
| 32 |
% Tao Zha, April 2000. |
| 33 |
% See the old but useful function fore_mqy.m. |
| 34 |
% Added yactqge in February 2004. |
| 35 |
% Added yacteoqyge in March 2004. |
| 36 |
% |
| 37 |
% Copyright (C) 1997-2012 Tao Zha |
| 38 |
% |
| 39 |
% This free software: you can redistribute it and/or modify |
| 40 |
% it under the terms of the GNU General Public License as published by |
| 41 |
% the Free Software Foundation, either version 3 of the License, or |
| 42 |
% (at your option) any later version. |
| 43 |
% |
| 44 |
% It is distributed in the hope that it will be useful, |
| 45 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 46 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 47 |
% GNU General Public License for more details. |
| 48 |
% |
| 49 |
% If you did not received a copy of the GNU General Public License |
| 50 |
% with this software, see <http://www.gnu.org/licenses/>. |
| 51 |
% |
| 52 |
|
| 53 |
if size(xdatae,1)<2*q_m |
| 54 |
error('We need at least two years of xdatae to get annual rates. Check xdatae!!') |
| 55 |
end |
| 56 |
|
| 57 |
if nargin==4 |
| 58 |
Brow=1; Erow=length(xdatae(:,1)); |
| 59 |
nyr = floor((Erow-Brow+1)/q_m); |
| 60 |
yrsg = [xdatae(q_m+1,1):xdatae(q_m+1,1)+nyr-2]'; % for annual growth later on |
| 61 |
else |
| 62 |
if Byrqm(2)<1 | Eyrqm(2)<1 |
| 63 |
error('This function requires specifying both years and months (quarters) in Byrqm and Eyrqm') |
| 64 |
end |
| 65 |
|
| 66 |
Brow = min(find(xdatae(:,1)==Byrqm(1))); |
| 67 |
if isempty(Brow) |
| 68 |
error('Byrqm is outside the date range of xdatae(:,1:2)!') |
| 69 |
end |
| 70 |
nadt=Byrqm(2)-xdatae(Brow,2); |
| 71 |
if nadt<0 |
| 72 |
error('Byrqm is outside the date range indicated by xdatae(:,1:2)!') |
| 73 |
end |
| 74 |
Brow=Brow+nadt; |
| 75 |
% |
| 76 |
Erow = min(find(xdatae(:,1)==Eyrqm(1))); |
| 77 |
if isempty(Brow) |
| 78 |
error('Eyrqm is outside the date range of xdatae(:,1:2)!') |
| 79 |
end |
| 80 |
nadt2=Eyrqm(2)-xdatae(Erow,2); |
| 81 |
if nadt<0 |
| 82 |
error('Eyrqm is outside the date range indicated by xdatae(:,1:2)!') |
| 83 |
end |
| 84 |
Erow=Erow+nadt2; |
| 85 |
|
| 86 |
nyr = floor((Erow-Brow+1)/q_m); |
| 87 |
yrsg = [Byrqm(1)+1:Byrqm(1)+nyr-1]'; % for annual growth later on, which will |
| 88 |
% start at Byrqm(1) instead of Byrqm(1)+1 |
| 89 |
end |
| 90 |
% |
| 91 |
yactqme = xdatae(Brow:Erow,:); % with dates |
| 92 |
yactqm = yactqme(:,3:end); % only data |
| 93 |
|
| 94 |
%======== prior period change (annaluized rate) |
| 95 |
yactqmg = yactqm(2:end,:); % start at second period to get growth rate |
| 96 |
yactqmg(:,vlistlog) = (yactqm(2:end,vlistlog) - yactqm(1:end-1,vlistlog)) .* q_m; |
| 97 |
% monthly, 12*log(1+growth rate), annualized growth rate |
| 98 |
yactqmg(:,vlistlog) = 100*(exp(yactqmg(:,vlistlog))-1); |
| 99 |
yactqmg(:,vlistper) = 100*yactqmg(:,vlistper); |
| 100 |
yactqmge = [yactqme(2:end,1:2) yactqmg]; |
| 101 |
|
| 102 |
%======== change from the last year |
| 103 |
yactqmyg = yactqm(q_m+1:end,:); % start at the last-year period to get growth rate |
| 104 |
yactqmyg(:,vlistlog) = (yactqm(q_m+1:end,vlistlog) - yactqm(1:end-q_m,vlistlog)); |
| 105 |
yactqmyg(:,vlistlog) = 100*(exp(yactqmyg(:,vlistlog))-1); |
| 106 |
yactqmyg(:,vlistper) = 100*yactqmyg(:,vlistper); |
| 107 |
yactqmyge = [yactqme(q_m+1:end,1:2) yactqmyg]; |
| 108 |
|
| 109 |
%======== annual growth rates |
| 110 |
nvar = length(xdatae(1,3:end)); |
| 111 |
ygmts = yactqm(1:nyr*q_m,:); % converted to the multiplication of q_m |
| 112 |
ygmts1 = reshape(ygmts,q_m,nyr,nvar); |
| 113 |
ygmts2 = sum(ygmts1,1) ./ q_m; |
| 114 |
ygmts3 = reshape(ygmts2,nyr,nvar); % converted to annual average series |
| 115 |
% |
| 116 |
yactyrg = ygmts3(2:end,:); % start at the last-year period to get growth rate |
| 117 |
yactyrg(:,vlistlog) = ygmts3(2:end,vlistlog) - ygmts3(1:end-1,vlistlog); |
| 118 |
% annual rate: log(1+growth rate) |
| 119 |
yactyrg(:,vlistlog) = 100*(exp(yactyrg(:,vlistlog))-1); |
| 120 |
yactyrg(:,vlistper) = 100*yactyrg(:,vlistper); |
| 121 |
yactyrge = [yrsg zeros(nyr-1,1) yactyrg]; |
| 122 |
yrsg1=[yrsg(1)-1:yrsg(end)]'; |
| 123 |
yactyre = [yrsg1 zeros(nyr,1) ygmts3]; |
| 124 |
|
| 125 |
|
| 126 |
%======== Quarter-to-last quarter annualized growth rates. |
| 127 |
if (q_m==12) |
| 128 |
%=== Beginning row. |
| 129 |
mcnt = mod(xdatae(1,2),3); %Begining month. |
| 130 |
if (mcnt==0) |
| 131 |
QrowBin = 2; % Reset the beginning month to match a quarter. |
| 132 |
elseif (mcnt==1) |
| 133 |
QrowBin = 1; % Reset the beginning month to match a quarter. |
| 134 |
elseif (mcnt==2) |
| 135 |
QrowBin = 3; % Reset the beginning month to match a quarter. |
| 136 |
else |
| 137 |
error('.../fn_datana2.m: Number for months in the dates must be between 1 and 12 inclusive'); |
| 138 |
end |
| 139 |
Qcnt = (xdatae(QrowBin,2)+2)/3; |
| 140 |
if (xdatae(1,2)<11) % Up to October. |
| 141 |
QyearBin = xdatae(1,1); |
| 142 |
else % November and December. |
| 143 |
QyearBin = xdatae(1,1) + 1; |
| 144 |
end;%if |
| 145 |
%=== Ending row. |
| 146 |
QrowEnd = size(xdatae, 1) - mod(xdatae(end,2),3); % Reset the ending month to match a quarter. |
| 147 |
nqs = (QrowEnd - QrowBin + 1)/3; |
| 148 |
|
| 149 |
|
| 150 |
%=== Getting last month of the quarter (end of quarter: eoq). |
| 151 |
yacteoq = xdatae(QrowBin:QrowEnd,3:end); % Without dates. |
| 152 |
yacteoq1 = reshape(yacteoq, 3, nqs, nvar); |
| 153 |
yacteoq2 = zeros(1, nqs, nvar); |
| 154 |
yacteoq2(:,:,vlistper) = yacteoq1(3,:,vlistper); |
| 155 |
yacteoq2(:,:,vlistlog) = yacteoq1(3,:,vlistlog); |
| 156 |
yacteoq3 = reshape(yacteoq2, nqs, nvar); |
| 157 |
%=== EOQ-this-year-to-EOQ-last-year annual growth rates. |
| 158 |
yacteoqyg = yacteoq3(5:end,:); |
| 159 |
yacteoqyg(:,vlistlog) = 100*(exp(yacteoq3(5:end, vlistlog) - yacteoq3(1:end-4, vlistlog))-1); |
| 160 |
yacteoqyg(:,vlistper) = 100*yacteoqyg(:,vlistper); |
| 161 |
|
| 162 |
|
| 163 |
%=== Getting quarterly averages. |
| 164 |
yactq = xdatae(QrowBin:QrowEnd,3:end); % Without dates. |
| 165 |
nqs = (QrowEnd - QrowBin + 1)/3; |
| 166 |
yactq1 = reshape(yactq, 3, nqs, nvar); |
| 167 |
yactq2 = zeros(1, nqs, nvar); |
| 168 |
yactq2(:,:,vlistper) = sum(yactq1(:,:,vlistper), 1) ./ 3; |
| 169 |
yactq2(:,:,vlistlog) = sum(exp(yactq1(:,:,vlistlog)), 1) ./ 3; |
| 170 |
yactq3 = reshape(yactq2, nqs, nvar); |
| 171 |
%=== Quarterly (to prior-quarter) annualized growth rates. |
| 172 |
yactqg = yactq3(2:end,:); |
| 173 |
yactqg(:,vlistlog) = 100*((yactq3(2:end, vlistlog) ./ yactq3(1:end-1, vlistlog)).^4-1); |
| 174 |
yactqg(:,vlistper) = 100*yactqg(:,vlistper); |
| 175 |
|
| 176 |
|
| 177 |
%======= Adding quarterly dates. ======= |
| 178 |
qdates = zeros(nqs,2); |
| 179 |
for k=1:nqs |
| 180 |
qdates(k,1) = floor(QyearBin + (k+Qcnt-2)/4); |
| 181 |
tmpi = mod(k+Qcnt-1, 4); |
| 182 |
if (tmpi==0) |
| 183 |
qdates(k,2) = 4; |
| 184 |
else |
| 185 |
qdates(k,2) = tmpi; |
| 186 |
end |
| 187 |
end |
| 188 |
yacteoqyge = [qdates(5:end,:) yacteoqyg]; |
| 189 |
yactqge = [qdates(2:end,:) yactqg]; |
| 190 |
else |
| 191 |
yacteoqyge = NaN; |
| 192 |
yactqge = NaN; |
| 193 |
end |