DsgeVarLikelihood.m 11 KB
Newer Older
1
function [fval,grad,hess,exit_flag,info,PHI,SIGMAu,iXX,prior] = DsgeVarLikelihood(xparam1,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults)
2
3
4
% Evaluates the posterior kernel of the bvar-dsge model.
%
% INPUTS
5
6
%   o xparam1       [double]     Vector of model's parameters.
%   o gend          [integer]    Number of observations (without conditionning observations for the lags).
7
8
%
% OUTPUTS
9
10
11
12
13
14
%   o fval          [double]     Value of the posterior kernel at xparam1.
%   o cost_flag     [integer]    Zero if the function returns a penalty, one otherwise.
%   o info          [integer]    Vector of informations about the penalty.
%   o PHI           [double]     Stacked BVAR-DSGE autoregressive matrices (at the mode associated to xparam1).
%   o SIGMAu        [double]     Covariance matrix of the BVAR-DSGE (at the mode associated to xparam1).
%   o iXX           [double]     inv(X'X).
15
%   o prior         [double]     a matlab structure describing the dsge-var prior.
16
17
18
19
%
% SPECIAL REQUIREMENTS
%   None.

Sébastien Villemot's avatar
Sébastien Villemot committed
20
% Copyright (C) 2006-2012 Dynare Team
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.

37
% Declaration of the persistent variables.
38
39
40
persistent dsge_prior_weight_idx

penalty = BayesInfo.penalty;
41

42
43
grad=[];
hess=[];
44
45
46
47
48
49
exit_flag = [];
info = [];
PHI = [];
SIGMAu = [];
iXX = [];
prior = [];
50

51
52
53
54
55
56
57
58
59
60
61
% Initialization of of the index for parameter dsge_prior_weight in Model.params.
if isempty(dsge_prior_weight_idx)
    dsge_prior_weight_idx = strmatch('dsge_prior_weight',Model.param_names);
end

% Get the number of estimated (dsge) parameters.
ns = EstimatedParameters.nvx + ...
     EstimatedParameters.nvn + ...
     EstimatedParameters.ncx + ...
     EstimatedParameters.ncn;
nx = ns + EstimatedParameters.np;
62

63
64
% Get the number of observed variables in the VAR model.
NumberOfObservedVariables = DynareDataset.info.nvobs;
65

66
67
68
69
% Get the number of lags in the VAR model.
NumberOfLags = DynareOptions.dsge_varlag;

% Get the number of parameters in the VAR model.
70
NumberOfParameters = NumberOfObservedVariables*NumberOfLags ;
71
if ~DynareOptions.noconstant
72
73
74
    NumberOfParameters = NumberOfParameters + 1;
end

75
% Get empirical second order moments for the observed variables.
76
77
78
79
80
mYY = evalin('base', 'mYY');
mYX = evalin('base', 'mYX');
mXY = evalin('base', 'mXY');
mXX = evalin('base', 'mXX');

81
% Initialize some of the output arguments.
82
fval = [];
83
exit_flag = 1;
84

85
86
87
88
89
% Return, with endogenous penalty, if some dsge-parameters are smaller than the lower bound of the prior domain.
if DynareOptions.mode_compute ~= 1 && any(xparam1 < BayesInfo.lb)
    k = find(xparam1 < BayesInfo.lb);
    fval = penalty+sum((BayesInfo.lb(k)-xparam1(k)).^2);
    exit_flag = 0;
90
91
92
93
    info = 41;
    return;
end

94
95
96
97
98
% Return, with endogenous penalty, if some dsge-parameters are greater than the upper bound of the prior domain.
if DynareOptions.mode_compute ~= 1 && any(xparam1 > BayesInfo.ub)
    k = find(xparam1 > BayesInfo.ub);
    fval = penalty+sum((xparam1(k)-BayesInfo.ub(k)).^2);
    exit_flag = 0;
99
100
101
102
    info = 42;
    return;
end

103
104
105
106
% Get the variance of each structural innovation.
Q = Model.Sigma_e;
for i=1:EstimatedParameters.nvx
    k = EstimatedParameters.var_exo(i,1);
107
108
    Q(k,k) = xparam1(i)*xparam1(i);
end
109
110
111
112
113
offset = EstimatedParameters.nvx;

% Check that the user does not estimate measurment errors.
% TODO Check that the user does not declare non estimated measurement errors...
if EstimatedParameters.nvn
114
    disp('DsgeVarLikelihood :: Measurement errors are not implemented!')
115
    return
116
117
118
119
120
end

% Check that the user does not estimate off diagonal elements in the covariance matrix of the structural innovation.
% TODO Check that Q is a diagonal matrix...
if EstimatedParameters.ncx
121
122
123
124
    disp('DsgeVarLikelihood :: Correlated structural innovations are not implemented!')
    return
end

125
126
127
128
129
130
% Update Model.params and Model.Sigma_e.
Model.params(EstimatedParameters.param_vals(:,1)) = xparam1(offset+1:end);
Model.Sigma_e = Q;

% Get the weight of the dsge prior.
dsge_prior_weight = Model.params(dsge_prior_weight_idx);
131

132
133
134
135
% Is the dsge prior proper?
if dsge_prior_weight<(NumberOfParameters+NumberOfObservedVariables)/DynareDataset.info.ntobs;
    fval = penalty+abs(DynareDataset.info.ntobs*dsge_prior_weight-(NumberOfParameters+NumberOfObservedVariables));
    exit_flag = 0;
136
    info = 51;
137
    return
138
139
140
141
142
end

%------------------------------------------------------------------------------
% 2. call model setup & reduction program
%------------------------------------------------------------------------------
143

144
145
146
147
148
% Solve the Dsge model and get the matrices of the reduced form solution. T and R are the matrices of the
% state equation
[T,R,SteadyState,info,Model,DynareOptions,DynareResults] = dynare_resolve(Model,DynareOptions,DynareResults,'restrict');

% Return, with endogenous penalty when possible, if dynare_resolve issues an error code (defined in resol).
149
if info(1) == 1 || info(1) == 2 || info(1) == 5
150
151
152
    fval = penalty+1;
    info = info(1);
    exit_flag = 0;
153
154
    return
elseif info(1) == 3 || info(1) == 4 || info(1) == 19 || info(1) == 20 || info(1) == 21
155
156
157
    fval = penalty+info(2);
    info = info(1);
    exit_flag = 0;
158
159
160
    return
end

161
162
163
164
% Define the mean/steady state vector.
if ~DynareOptions.noconstant
    if DynareOptions.loglinear
        constant = transpose(log(SteadyState(BayesInfo.mfys)));
165
    else
166
167
        constant = transpose(SteadyState(BayesInfo.mfys));
    end
168
169
170
else
    constant = zeros(1,NumberOfObservedVariables);
end
171
172
173
174

% Dsge-VAR with deterministic trends is not implemented
if BayesInfo.with_trend == 1
    error('DsgeVarLikelihood :: Linear trend is not yet implemented!')
175
176
177
178
179
end

%------------------------------------------------------------------------------
% 3. theoretical moments (second order)
%------------------------------------------------------------------------------
180
181
182
183

% Compute the theoretical second order moments
tmp0 = lyapunov_symm(T,R*Q*R',DynareOptions.qz_criterium,DynareOptions.lyapunov_complex_threshold);
mf  = BayesInfo.mf1;
184
185

% Get the non centered second order moments
186
TheoreticalAutoCovarianceOfTheObservedVariables = zeros(NumberOfObservedVariables,NumberOfObservedVariables,NumberOfLags+1);
187
188
TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1) = tmp0(mf,mf)+constant'*constant;
for lag = 1:NumberOfLags
189
    tmp0 = T*tmp0;
190
    TheoreticalAutoCovarianceOfTheObservedVariables(:,:,lag+1) = tmp0(mf,mf) + constant'*constant;
191
end
192

193
194
195
% Build the theoretical "covariance" between Y and X
GYX = zeros(NumberOfObservedVariables,NumberOfParameters);
for i=1:NumberOfLags
196
    GYX(:,(i-1)*NumberOfObservedVariables+1:i*NumberOfObservedVariables) = TheoreticalAutoCovarianceOfTheObservedVariables(:,:,i+1);
197
end
198
if ~DynareOptions.noconstant
199
200
    GYX(:,end) = constant';
end
201

202
% Build the theoretical "covariance" between X and X
203
GXX = kron(eye(NumberOfLags), TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1));
204
for i = 1:NumberOfLags-1
205
    tmp1 = diag(ones(NumberOfLags-i,1),i);
206
207
208
209
210
    tmp2 = diag(ones(NumberOfLags-i,1),-i);
    GXX = GXX + kron(tmp1,TheoreticalAutoCovarianceOfTheObservedVariables(:,:,i+1));
    GXX = GXX + kron(tmp2,TheoreticalAutoCovarianceOfTheObservedVariables(:,:,i+1)');
end

211
if ~DynareOptions.noconstant
212
213
214
215
216
217
218
219
220
221
    % Add one row and one column to GXX
    GXX = [GXX , kron(ones(NumberOfLags,1),constant') ; [  kron(ones(1,NumberOfLags),constant) , 1] ];
end

GYY = TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1);

assignin('base','GYY',GYY);
assignin('base','GXX',GXX);
assignin('base','GYX',GYX);

222
223
224
225
if ~isinf(dsge_prior_weight)% Evaluation of the likelihood of the dsge-var model when the dsge prior weight is finite.
    tmp0 = dsge_prior_weight*DynareDataset.info.ntobs*TheoreticalAutoCovarianceOfTheObservedVariables(:,:,1) + mYY ;
    tmp1 = dsge_prior_weight*DynareDataset.info.ntobs*GYX + mYX;
    tmp2 = inv(dsge_prior_weight*DynareDataset.info.ntobs*GXX+mXX);
226
227
228
229
    SIGMAu = tmp0 - tmp1*tmp2*tmp1'; clear('tmp0');
    if ~ispd(SIGMAu)
        v = diag(SIGMAu);
        k = find(v<0);
230
        fval = penalty + sum(v(k).^2);
231
        info = 52;
232
        exit_flag = 0;
233
234
        return;
    end
235
    SIGMAu = SIGMAu / (DynareDataset.info.ntobs*(1+dsge_prior_weight));
236
    PHI = tmp2*tmp1'; clear('tmp1');
237
    prodlng1 = sum(gammaln(.5*((1+dsge_prior_weight)*DynareDataset.info.ntobs- ...
238
239
                               NumberOfObservedVariables*NumberOfLags ...
                               +1-(1:NumberOfObservedVariables)')));
240
    prodlng2 = sum(gammaln(.5*(dsge_prior_weight*DynareDataset.info.ntobs- ...
241
                               NumberOfObservedVariables*NumberOfLags ...
242
243
244
245
246
247
248
249
                               +1-(1:NumberOfObservedVariables)')));
    lik = .5*NumberOfObservedVariables*log(det(dsge_prior_weight*DynareDataset.info.ntobs*GXX+mXX)) ...
          + .5*((dsge_prior_weight+1)*DynareDataset.info.ntobs-NumberOfParameters)*log(det((dsge_prior_weight+1)*DynareDataset.info.ntobs*SIGMAu)) ...
          - .5*NumberOfObservedVariables*log(det(dsge_prior_weight*DynareDataset.info.ntobs*GXX)) ...
          - .5*(dsge_prior_weight*DynareDataset.info.ntobs-NumberOfParameters)*log(det(dsge_prior_weight*DynareDataset.info.ntobs*(GYY-GYX*inv(GXX)*GYX'))) ...
          + .5*NumberOfObservedVariables*DynareDataset.info.ntobs*log(2*pi)  ...
          - .5*log(2)*NumberOfObservedVariables*((dsge_prior_weight+1)*DynareDataset.info.ntobs-NumberOfParameters) ...
          + .5*log(2)*NumberOfObservedVariables*(dsge_prior_weight*DynareDataset.info.ntobs-NumberOfParameters) ...
250
          - prodlng1 + prodlng2;
251
else% Evaluation of the likelihood of the dsge-var model when the dsge prior weight is infinite.
252
253
254
    iGXX = inv(GXX);
    SIGMAu = GYY - GYX*iGXX*transpose(GYX);
    PHI = iGXX*transpose(GYX);
255
256
    lik = DynareDataset.info.ntobs * ( log(det(SIGMAu)) + NumberOfObservedVariables*log(2*pi) +  ...
                   trace(inv(SIGMAu)*(mYY - transpose(mYX*PHI) - mYX*PHI + transpose(PHI)*mXX*PHI)/DynareDataset.info.ntobs));
257
    lik = .5*lik;% Minus likelihood
258
end
259

260
261
% Add the (logged) prior density for the dsge-parameters.
lnprior = priordens(xparam1,BayesInfo.pshape,BayesInfo.p6,BayesInfo.p7,BayesInfo.p3,BayesInfo.p4);
262
263
fval = (lik-lnprior);

264
if (nargout == 8)
265
266
267
268
269
270
271
    if isinf(dsge_prior_weight)
        iXX = iGXX;
    else
        iXX = tmp2;
    end
end

272
if (nargout==9)
273
274
275
276
277
    if isinf(dsge_prior_weight)
        iXX = iGXX;
    else
        iXX = tmp2;
    end
278
    iGXX = inv(GXX);
279
280
    prior.SIGMAstar = GYY - GYX*iGXX*GYX';
    prior.PHIstar = iGXX*transpose(GYX);
281
    prior.ArtificialSampleSize = fix(dsge_prior_weight*DynareDataset.info.ntobs);
282
283
    prior.DF = prior.ArtificialSampleSize - NumberOfParameters - NumberOfObservedVariables;
    prior.iGXX = iGXX;
284
end