2019-11-20 16:53:53 +01:00
|
|
|
function ds = surgibbs(ds, param_names, beta0, A, ndraws, discarddraws, thin, eqtags, model_name)
|
2020-09-24 13:33:03 +02:00
|
|
|
|
2017-07-12 16:43:12 +02:00
|
|
|
% Implements Gibbs Samipling for SUR
|
|
|
|
%
|
|
|
|
% INPUTS
|
2018-01-05 17:13:46 +01:00
|
|
|
% ds [dseries] data
|
2018-01-11 11:18:47 +01:00
|
|
|
% param_names [cellstr] list of parameters to estimate
|
|
|
|
% beta0 [vector] prior values (in order of param_names)
|
|
|
|
% A [matrix] prior distribution variance (in order of
|
|
|
|
% param_names)
|
2018-01-05 17:13:46 +01:00
|
|
|
% ndraws [int] number of draws
|
|
|
|
% discarddraws [int] number of draws to discard
|
2018-01-12 14:43:18 +01:00
|
|
|
% thin [int] if thin == N, save every Nth draw
|
2018-01-16 18:42:15 +01:00
|
|
|
% eqtags [cellstr] names of equation tags to estimate. If empty,
|
|
|
|
% estimate all equations
|
2019-02-28 11:49:50 +01:00
|
|
|
% model_name [string] name to use in oo_ and inc file
|
2017-07-12 16:43:12 +02:00
|
|
|
%
|
|
|
|
% OUTPUTS
|
|
|
|
% none
|
|
|
|
%
|
|
|
|
% SPECIAL REQUIREMENTS
|
2019-01-24 12:42:08 +01:00
|
|
|
% dynare must have been run with the option: json=compute
|
2020-09-24 13:33:03 +02:00
|
|
|
%
|
|
|
|
% REFERENCES
|
|
|
|
% - Ando, Tomohiro and Zellner, Arnold. 2010. Hierarchical Bayesian Analysis of the
|
|
|
|
% Seemingly Unrelated Regression and Simultaneous Equations Models Using a
|
|
|
|
% Combination of Direct Monte Carlo and Importance Sampling Techniques.
|
|
|
|
% Bayesian Analysis Volume 5, Number 1, pp. 65-96.
|
2017-07-12 16:43:12 +02:00
|
|
|
|
2022-04-13 13:15:19 +02:00
|
|
|
% Copyright © 2017-2021 Dynare Team
|
2017-07-12 16:43:12 +02:00
|
|
|
%
|
|
|
|
% 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
|
2021-06-09 17:33:48 +02:00
|
|
|
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
2017-07-12 16:43:12 +02:00
|
|
|
|
2019-01-14 17:57:36 +01:00
|
|
|
global M_ oo_ options_
|
2017-07-12 16:43:12 +02:00
|
|
|
|
2020-09-24 13:33:03 +02:00
|
|
|
%
|
|
|
|
% Check inputs
|
|
|
|
%
|
|
|
|
|
2019-02-28 11:49:50 +01:00
|
|
|
assert(nargin >= 5 && nargin <= 9, 'Incorrect number of arguments passed to surgibbs');
|
2018-01-11 11:18:47 +01:00
|
|
|
assert(isdseries(ds), 'The 1st argument must be a dseries');
|
|
|
|
assert(iscellstr(param_names), 'The 2nd argument must be a cellstr');
|
|
|
|
assert(isvector(beta0) && length(beta0) == length(param_names), ...
|
|
|
|
'The 3rd argument must be a vector with the same length as param_names and the same ');
|
|
|
|
if isrow(beta0)
|
|
|
|
beta0 = beta0';
|
|
|
|
end
|
|
|
|
assert(ismatrix(A) && all(all((A == A'))) && length(beta0) == size(A, 2), ...
|
|
|
|
'The 4th argument must be a symmetric matrix with the same dimension as beta0');
|
|
|
|
assert(isint(ndraws), 'The 5th argument must be an integer');
|
|
|
|
if nargin == 5
|
2018-01-05 17:13:46 +01:00
|
|
|
discarddraws = 0;
|
2018-01-11 11:18:47 +01:00
|
|
|
else
|
|
|
|
assert(isint(discarddraws), 'The 6th argument, if provided, must be an integer');
|
2018-01-05 17:13:46 +01:00
|
|
|
end
|
2018-01-12 14:43:18 +01:00
|
|
|
if nargin == 6
|
|
|
|
thin = 1;
|
|
|
|
else
|
|
|
|
assert(isint(thin), 'The 7th argument, if provided, must be an integer');
|
|
|
|
end
|
2017-07-12 16:43:12 +02:00
|
|
|
|
2019-02-28 11:49:50 +01:00
|
|
|
if nargin <= 8
|
|
|
|
if ~isfield(oo_, 'surgibbs')
|
|
|
|
model_name = 'surgibbs_model_number_1';
|
|
|
|
else
|
|
|
|
model_name = ['surgibbs_model_number_' num2str(length(fieldnames(oo_.surgibbs)) + 1)];
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if ~isvarname(model_name)
|
|
|
|
error('The 9th argument must be a valid string');
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-24 13:33:03 +02:00
|
|
|
%
|
|
|
|
% Estimation
|
|
|
|
%
|
|
|
|
|
2018-01-16 18:42:15 +01:00
|
|
|
if nargin == 8
|
2019-11-25 14:09:55 +01:00
|
|
|
[nobs, X, Y, m, lhssub, fp] = sur(ds, param_names, eqtags);
|
2018-01-16 18:42:15 +01:00
|
|
|
else
|
2019-11-25 14:09:55 +01:00
|
|
|
[nobs, X, Y, m, lhssub, fp] = sur(ds, param_names);
|
2018-01-16 18:42:15 +01:00
|
|
|
end
|
2019-01-11 16:00:20 +01:00
|
|
|
|
2019-11-20 16:53:53 +01:00
|
|
|
oo_.surgibbs.(model_name).dof = nobs;
|
|
|
|
|
2018-01-11 11:18:47 +01:00
|
|
|
beta = beta0;
|
2018-01-05 17:13:46 +01:00
|
|
|
A = inv(A);
|
2018-01-12 14:43:18 +01:00
|
|
|
thinidx = 1;
|
|
|
|
drawidx = 1;
|
2018-01-24 15:22:30 +01:00
|
|
|
nparams = length(param_names);
|
2019-02-28 11:49:50 +01:00
|
|
|
oo_.surgibbs.(model_name).betadraws = zeros(floor((ndraws-discarddraws)/thin), nparams);
|
2019-01-11 16:00:20 +01:00
|
|
|
if ~options_.noprint
|
2019-01-14 17:57:36 +01:00
|
|
|
disp('surgibbs: estimating, please wait...')
|
2019-01-11 16:00:20 +01:00
|
|
|
end
|
2019-12-10 17:03:22 +01:00
|
|
|
|
|
|
|
hh = dyn_waitbar(0,'Please wait. Gibbs sampler...');
|
|
|
|
set(hh,'Name','Surgibbs estimation.');
|
2020-05-19 17:39:14 +02:00
|
|
|
residdraws = zeros(floor((ndraws-discarddraws)/thin), nobs, m);
|
2017-07-12 16:43:12 +02:00
|
|
|
for i = 1:ndraws
|
2019-12-10 17:03:22 +01:00
|
|
|
if ~mod(i,10)
|
|
|
|
dyn_waitbar(i/ndraws,hh,'Please wait. Gibbs sampler...');
|
|
|
|
end
|
2018-01-05 17:13:46 +01:00
|
|
|
% Draw Omega, given X, Y, Beta
|
2017-07-12 16:43:12 +02:00
|
|
|
resid = reshape(Y - X*beta, nobs, m);
|
2018-01-05 17:13:46 +01:00
|
|
|
Omega = rand_inverse_wishart(m, nobs, chol(inv(resid'*resid/nobs)));
|
2017-07-12 16:43:12 +02:00
|
|
|
|
2018-01-05 17:13:46 +01:00
|
|
|
% Draw beta, given X, Y, Omega
|
2017-07-12 16:43:12 +02:00
|
|
|
tmp = kron(inv(Omega), eye(nobs));
|
|
|
|
tmp1 = X'*tmp*X;
|
|
|
|
Omegabar = inv(tmp1 + A);
|
2018-01-05 17:13:46 +01:00
|
|
|
betahat = tmp1\X'*tmp*Y;
|
|
|
|
betabar = Omegabar*(tmp1*betahat+A*beta0);
|
2018-01-11 11:18:47 +01:00
|
|
|
beta = rand_multivariate_normal(betabar', chol(Omegabar), nparams)';
|
2018-01-05 17:13:46 +01:00
|
|
|
if i > discarddraws
|
2018-01-12 14:43:18 +01:00
|
|
|
if thinidx == thin
|
2019-11-20 16:53:53 +01:00
|
|
|
oo_.surgibbs.(model_name).betadraws(drawidx, 1:nparams) = beta';
|
2020-05-19 17:39:14 +02:00
|
|
|
residdraws(drawidx, 1:nobs, 1:m) = resid;
|
2018-01-12 14:43:18 +01:00
|
|
|
thinidx = 1;
|
|
|
|
drawidx = drawidx + 1;
|
|
|
|
else
|
|
|
|
thinidx = thinidx + 1;
|
|
|
|
end
|
2018-01-05 17:13:46 +01:00
|
|
|
end
|
2017-07-12 18:12:26 +02:00
|
|
|
end
|
2019-12-10 17:03:22 +01:00
|
|
|
dyn_waitbar_close(hh);
|
2017-07-12 18:12:26 +02:00
|
|
|
|
2020-09-24 13:33:03 +02:00
|
|
|
%
|
|
|
|
% Save results.
|
|
|
|
%
|
|
|
|
|
2019-11-20 16:53:53 +01:00
|
|
|
oo_.surgibbs.(model_name).posterior.mean.beta = (sum(oo_.surgibbs.(model_name).betadraws)/rows(oo_.surgibbs.(model_name).betadraws))';
|
|
|
|
oo_.surgibbs.(model_name).posterior.variance.beta = cov(oo_.surgibbs.(model_name).betadraws);
|
|
|
|
|
|
|
|
% Yhat
|
2019-11-25 11:48:09 +01:00
|
|
|
oo_.surgibbs.(model_name).Yhat = X*oo_.surgibbs.(model_name).posterior.mean.beta;
|
2019-12-10 14:08:50 +01:00
|
|
|
oo_.surgibbs.(model_name).YhatOrig = oo_.surgibbs.(model_name).Yhat;
|
|
|
|
oo_.surgibbs.(model_name).Yobs = Y;
|
2019-11-20 16:53:53 +01:00
|
|
|
|
|
|
|
% Residuals
|
|
|
|
oo_.surgibbs.(model_name).resid = Y - oo_.surgibbs.(model_name).Yhat;
|
|
|
|
|
|
|
|
% Correct Yhat reported back to user
|
|
|
|
oo_.surgibbs.(model_name).Yhat = oo_.surgibbs.(model_name).Yhat + lhssub;
|
|
|
|
yhatname = [model_name '_FIT'];
|
|
|
|
ds.(yhatname) = dseries(oo_.surgibbs.(model_name).Yhat, fp, yhatname);
|
2019-02-26 16:56:20 +01:00
|
|
|
|
2019-11-20 16:53:53 +01:00
|
|
|
% Compute and save posterior densities.
|
|
|
|
for i=1:nparams
|
|
|
|
xx = oo_.surgibbs.(model_name).betadraws(:,i);
|
|
|
|
nn = length(xx);
|
|
|
|
bandwidth = mh_optimal_bandwidth(xx, nn, 0, 'gaussian');
|
|
|
|
[x, f] = kernel_density_estimate(xx, 512, nn, bandwidth, 'gaussian');
|
|
|
|
oo_.surgibbs.(model_name).posterior.density.(param_names{i}) = [x, f];
|
|
|
|
end
|
|
|
|
|
|
|
|
% Update model1s parameters with posterior mean.
|
|
|
|
oo_.surgibbs.(model_name).param_idxs = zeros(length(param_names), 1);
|
2019-02-26 16:56:20 +01:00
|
|
|
for i = 1:length(param_names)
|
2019-11-20 16:53:53 +01:00
|
|
|
if ~strcmp(param_names{i}, 'intercept')
|
|
|
|
oo_.surgibbs.(model_name).param_idxs(i) = find(strcmp(M_.param_names, param_names{i}));
|
|
|
|
M_.params(oo_.surgibbs.(model_name).param_idxs(i)) = oo_.surgibbs.(model_name).posterior.mean.beta(i);
|
|
|
|
end
|
2019-02-26 16:56:20 +01:00
|
|
|
end
|
2019-11-20 16:53:53 +01:00
|
|
|
oo_.surgibbs.(model_name).pnames = param_names;
|
|
|
|
oo_.surgibbs.(model_name).neqs = m;
|
|
|
|
|
|
|
|
% Estimate for sigma^2
|
|
|
|
SS_res = oo_.surgibbs.(model_name).resid'*oo_.surgibbs.(model_name).resid;
|
|
|
|
oo_.surgibbs.(model_name).s2 = SS_res/oo_.surgibbs.(model_name).dof;
|
|
|
|
|
2020-05-19 17:39:14 +02:00
|
|
|
% Set appropriate entries in Sigma_e
|
|
|
|
posterior_mean_resid = reshape((sum(residdraws))/rows(residdraws), nobs, m);
|
|
|
|
Sigma_e = posterior_mean_resid'*posterior_mean_resid/oo_.surgibbs.(model_name).dof;
|
|
|
|
|
2020-09-24 13:33:03 +02:00
|
|
|
% System R² value of McElroy (1977) - formula from Judge et al. (1986, p. 477)
|
|
|
|
%
|
|
|
|
% The R² is computed at the posterior mean of the estimated
|
|
|
|
% parameters. Maybe it would make more sense to compute a posterior
|
|
|
|
% distribution for this statistic…
|
2020-05-19 17:39:14 +02:00
|
|
|
oo_.surgibbs.(model_name).R2 = 1 - (oo_.surgibbs.(model_name).resid' * kron(inv(Sigma_e), eye(nobs)) * oo_.surgibbs.(model_name).resid) ...
|
2020-09-24 13:33:03 +02:00
|
|
|
/ (oo_.surgibbs.(model_name).Yobs' * kron(inv(Sigma_e), eye(nobs)-ones(nobs,nobs)/nobs) * oo_.surgibbs.(model_name).Yobs);
|
2017-07-13 13:41:40 +02:00
|
|
|
|
2019-02-26 14:32:19 +01:00
|
|
|
% Write .inc file
|
2019-11-20 16:53:53 +01:00
|
|
|
write_param_init_inc_file('surgibbs', model_name, oo_.surgibbs.(model_name).param_idxs, oo_.surgibbs.(model_name).posterior.mean.beta);
|
2019-02-26 14:32:19 +01:00
|
|
|
|
2020-09-24 13:33:03 +02:00
|
|
|
%
|
|
|
|
% Print Output
|
|
|
|
%
|
|
|
|
|
2019-01-11 16:00:20 +01:00
|
|
|
if ~options_.noprint
|
2019-11-25 14:09:55 +01:00
|
|
|
ttitle = 'Gibbs Sampling on SUR';
|
2019-11-20 16:53:53 +01:00
|
|
|
preamble = {['Model name: ' model_name], ...
|
|
|
|
sprintf('No. Equations: %d', oo_.surgibbs.(model_name).neqs), ...
|
|
|
|
sprintf('No. Independent Variables: %d', size(X, 2)), ...
|
|
|
|
sprintf('Observations: %d', oo_.surgibbs.(model_name).dof)};
|
2019-11-25 11:48:09 +01:00
|
|
|
|
2019-11-20 16:53:53 +01:00
|
|
|
afterward = {sprintf('s^2: %f', oo_.surgibbs.(model_name).s2), sprintf('R^2: %f', oo_.surgibbs.(model_name).R2)};
|
|
|
|
dyn_table(ttitle, preamble, afterward, param_names,...
|
|
|
|
{'Posterior mean', 'Posterior std.'}, 4,...
|
|
|
|
[oo_.surgibbs.(model_name).posterior.mean.beta, sqrt(diag(oo_.surgibbs.(model_name).posterior.variance.beta))]);
|
2019-01-11 16:00:20 +01:00
|
|
|
end
|
2017-07-17 17:47:29 +02:00
|
|
|
|
2020-09-24 13:33:03 +02:00
|
|
|
%
|
|
|
|
% Plot
|
|
|
|
%
|
|
|
|
|
2021-01-19 16:22:28 +01:00
|
|
|
% The histogram() function is not implemented in Octave and in MATLAB < R2014b
|
|
|
|
if ~options_.nograph && ~isoctave && ~matlab_ver_less_than('8.4')
|
2019-01-11 16:00:20 +01:00
|
|
|
figure
|
|
|
|
nrows = 5;
|
|
|
|
ncols = floor(nparams/nrows);
|
|
|
|
if mod(nparams, nrows) ~= 0
|
|
|
|
ncols = ncols + 1;
|
|
|
|
end
|
|
|
|
for j = 1:length(param_names)
|
|
|
|
subplot(nrows, ncols, j)
|
2019-02-28 11:49:50 +01:00
|
|
|
histogram(oo_.surgibbs.(model_name).betadraws(:, j))
|
|
|
|
hc = histcounts(oo_.surgibbs.(model_name).betadraws(:, j));
|
2019-11-20 16:53:53 +01:00
|
|
|
line([oo_.surgibbs.(model_name).posterior.mean.beta(j) oo_.surgibbs.(model_name).posterior.mean.beta(j)], [min(hc) max(hc)], 'Color', 'red');
|
2019-01-11 16:00:20 +01:00
|
|
|
title(param_names{j}, 'Interpreter', 'none')
|
|
|
|
end
|
2017-07-12 18:12:26 +02:00
|
|
|
end
|
|
|
|
end
|