1function [ya, yass, gya, gyass] = quarterly2annual(y,yss,GYTREND0,type,islog,aux)
2% function [ya, yass, gya, gyass] = quarterly2annual(y,yss,GYTREND0,type,islog,aux)
3% transforms quarterly (log-)level time series to annual level and growth rate
4% it accounts for stock/flow/deflator series.
5%
6% INPUTS
7% y        quarterly time series
8% yss      steady state of y
9% GYTREND0 growth rate of y
10% type     1 sum (default)
11%          2 average
12%          3 last period (Q4)
13%          4 geometric average
14%          5 annual price as quantity weighted average
15%          6 annual quantity from average price
16%          7 annual nominal from Q real and deflator
17%          8 annual ratio [e.g. trade balance to GDP]
18% islog    0 level (default)
19%          1 log-level
20%          2 growth rate Q frequency
21% aux      optional input used when type>4
22%
23%
24% OUTPUTS
25% ya       annual (log-)level
26% yass     annual steadystate (log-)level
27% gya      annual growth rate
28% gyass    annual growth rate steadystate
29
30% Copyright (C) 2017 Dynare Team
31%
32% This file is part of Dynare.
33%
34% Dynare is free software: you can redistribute it and/or modify
35% it under the terms of the GNU General Public License as published by
36% the Free Software Foundation, either version 3 of the License, or
37% (at your option) any later version.
38%
39% Dynare is distributed in the hope that it will be useful,
40% but WITHOUT ANY WARRANTY; without even the implied warranty of
41% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42% GNU General Public License for more details.
43%
44% You should have received a copy of the GNU General Public License
45% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
46
47if nargin ==0
48    disp('[ya, yass, gya, gyass] = quarterly2annual(y,yss,GYTREND0,type,islog);')
49    return
50end
51
52if nargin<4 || isempty(type)
53    type=1;
54end
55if nargin<5 || isempty(islog)
56    islog=0;
57end
58if isstruct(aux)
59    yaux=aux.y;
60    yauxss=aux.yss;
61    islogaux=aux.islog;
62    GYTREND0aux=aux.GYTREND0;
63    typeaux=aux.type;
64    if islogaux
65        yaux=exp(yaux+yauxss);
66        yauxss=exp(yauxss);
67        yaux=yaux-yauxss;
68    end
69elseif type > 4
70    error('TYPE>4 requires auxiliary variable!')
71end
72if islog == 2
73    % construct loglevel out of growth rate
74    y = cumsum(y);
75    yss=0;
76    islog=1;
77end
78if islog == 1
79    y=exp(y+yss);
80    yss=exp(yss);
81    y=y-yss;
82end
83switch type
84  case 1
85    yass = yss*(exp(-GYTREND0*3)+exp(-GYTREND0*2)+exp(-GYTREND0)+1);
86    tmp = lagged(y,3)*exp(-GYTREND0*3)+lagged(y,2)*exp(-GYTREND0*2)+lagged(y,1)*exp(-GYTREND0)+y; % annualized level
87    ya = tmp(4:4:end);
88  case 2
89    yass = yss*(exp(-GYTREND0*3)+exp(-GYTREND0*2)+exp(-GYTREND0)+1)/4;
90    tmp = (lagged(y,3)*exp(-GYTREND0*3)+lagged(y,2)*exp(-GYTREND0*2)+lagged(y,1)*exp(-GYTREND0)+y)/4; % annualized level
91    ya = tmp(4:4:end);
92  case 3
93    yass=yss;
94    tmp = y;
95    ya = tmp(4:4:end);
96  case 4
97    yass = yss*(exp(-GYTREND0*3/2));
98    tmp = (lagged(y+yss,3)*exp(-GYTREND0*3).*lagged(y+yss,2)*exp(-GYTREND0*2).*lagged(y+yss,1)*exp(-GYTREND0).*(y+yss)).^(1/4); % annualized level
99    tmp = tmp - yass;
100    ya = tmp(4:4:end);
101  case 5
102    % nominal series
103    yn = (y+yss).*(yaux+yauxss) - yss.*yauxss;
104    [yna, ynass] = quarterly2annual(yn,yss.*yauxss,GYTREND0+GYTREND0aux,typeaux,0,0);
105    % real series
106    [yra, yrass] = quarterly2annual(yaux,yauxss,GYTREND0aux,typeaux,0,0);
107    % deflator
108    yass = ynass/yrass;
109    ya = (yna+ynass)./(yra+yrass)-yass;
110  case 6
111    % nominal series
112    yn = (y+yss).*(yaux+yauxss) - yss.*yauxss;
113    [yna, ynass] = quarterly2annual(yn,yss.*yauxss,GYTREND0+GYTREND0aux,typeaux,0,0);
114    % deflator
115    [pa, pass] = quarterly2annual(yaux,yauxss,GYTREND0aux,2,0,0);
116    % real series
117    yass = ynass/pass;
118    ya = (yna+ynass)./(pa+pass)-yass;
119  case 7
120    % nominal series
121    yn = (y+yss).*(yaux+yauxss) - yss.*yauxss;
122    [ya, yass] = quarterly2annual(yn,yss.*yauxss,GYTREND0+GYTREND0aux,typeaux,0,0);
123    GYTREND0=GYTREND0+GYTREND0aux;
124
125    case 8
126        % numerator
127        yn = y;
128        [yna, ynass] = quarterly2annual(yn,yss,GYTREND0,typeaux(1),0,0);
129        % denominator
130        yd = yaux;
131        [yda, ydass] = quarterly2annual(yd,yauxss,GYTREND0aux,typeaux(2),0,0);
132        % ratio
133        yass = ynass/ydass;
134        ya = (yna+ynass)./(yda+ydass)-yass;
135        GYTREND0 = GYTREND0 - GYTREND0aux;
136
137  otherwise
138    error('Wrong type input')
139end
140
141% annual growth rate
142% gyass = GYTREND0*4; % this is for log diff
143gyass = exp(4*GYTREND0)-1; % this is for exact growth rate definition
144gya = (ya+yass)./(lagged(ya,1)+yass).*exp(4*GYTREND0)-1-gyass;
145
146if islog
147    ya=log(ya+yass);
148    yass=log(yass);
149    ya=ya-yass;
150end
151