| 1 |
function [xdraw,mphv,sm,timeminutes] = a0impsmp(xinput) |
| 2 |
% [xdraw,mphv,sm,timeminutes] = a0impsmp(xinput) |
| 3 |
% Export the simulated pdfs of draws of only selected parameters |
| 4 |
% with importance sampling techniques |
| 5 |
% |
| 6 |
% xinput{1}: nfp -- total number of free parameters |
| 7 |
% xinput{2}: nvar -- number of variables |
| 8 |
% xinput{3}: xhat -- ML estimate of free parameters in A0 |
| 9 |
% xinput{4}: hess1 -- Hessian of -logLH for importance sampling |
| 10 |
% xinput{5}:Indxv -- index for selected variables of interest; normall first 2 are of our interest |
| 11 |
% to select variables, always check idmat0 to make sure. |
| 12 |
% When Indxv=[], xdraw is empty as well. When IndxGgraph=1, it plots |
| 13 |
% (1) pdf of 1st v for every buffer, (2) scattered plot of 1st and 2nd for every buffer, |
| 14 |
% (3) pdf of 1st v for all sequences; (4) scattered plot of 3rd and 4th for all sequences |
| 15 |
% (5) scattered plot of 1st and 2nd for al sequences. |
| 16 |
% xinput{6}: imndraws=nstarts*ndraws2 |
| 17 |
% xinput{7}: a0indx -- index number for non-zero elements in A0 |
| 18 |
% xinput{8}: tdf -- degrees of freedom for t-distribution |
| 19 |
% xinput{9}: nbuffer -- interval for printing, plotting, and saving |
| 20 |
% xinput{10}: Sbd -- nvar-by-nvar S{1}, ..., S{n} -- kind of covariance matrix for each simultaneous equation |
| 21 |
% Already divided by "fss." |
| 22 |
% xinput{11}: scf -- reduction scale factor for Metropolis jumping kernel |
| 23 |
% xinput{12}: Hsr1 -- square root of covariance matrix for free elements in A0 (nfp-by-nfp) |
| 24 |
% for importance sampling. |
| 25 |
% xinput{13}: fss -- effective sample size == nSample-lags+# of dummy observations |
| 26 |
%------------------ |
| 27 |
% xdraw: imndraws-by-length(Indxv) matrix of draws; |
| 28 |
% empty if Indxv is empty. |
| 29 |
% timeminutes: minutes used for this simulation |
| 30 |
% mphv: imndraws-by-1 vector of unscaled weights |
| 31 |
% sm: sum of the weights |
| 32 |
% |
| 33 |
% Written by Tao Zha 1999 |
| 34 |
% |
| 35 |
% Copyright (C) 1997-2012 Tao Zha |
| 36 |
% |
| 37 |
% This free software: you can redistribute it and/or modify |
| 38 |
% it under the terms of the GNU General Public License as published by |
| 39 |
% the Free Software Foundation, either version 3 of the License, or |
| 40 |
% (at your option) any later version. |
| 41 |
% |
| 42 |
% It is distributed in the hope that it will be useful, |
| 43 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 44 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 45 |
% GNU General Public License for more details. |
| 46 |
% |
| 47 |
% If you did not received a copy of the GNU General Public License |
| 48 |
% with this software, see <http://www.gnu.org/licenses/>. |
| 49 |
% |
| 50 |
|
| 51 |
nfp=xinput{1}; nvar=xinput{2}; xhat=xinput{3}; hess1=xinput{4}; Indxv=xinput{5}; |
| 52 |
imndraws=xinput{6}; a0indx=xinput{7}; tdf=xinput{8}; nbuffer=xinput{9}; |
| 53 |
Sbd=xinput{10}; scf=xinput{11}; Hsr1=xinput{12}; fss=xinput{13}; |
| 54 |
|
| 55 |
|
| 56 |
sm = 0.0; |
| 57 |
mphv=zeros(imndraws,1); |
| 58 |
if isempty(Indxv) |
| 59 |
xdraw=[]; |
| 60 |
else |
| 61 |
xdraw=zeros(imndraws,length(Indxv)); %<<>> draws of selected variables |
| 62 |
end |
| 63 |
|
| 64 |
tic |
| 65 |
for draws=1:imndraws |
| 66 |
% |
| 67 |
if ~mod(draws,nbuffer) |
| 68 |
draws |
| 69 |
% fwriteid = fopen('outA0.bin','a'); |
| 70 |
% count = fwrite(fwriteid,A0hatw,'double'); |
| 71 |
% status = fclose('all'); |
| 72 |
end |
| 73 |
|
| 74 |
|
| 75 |
%** draw free elements Avh in A0 and hyperparameters from t-dist |
| 76 |
Avhz1 = Hsr1\randn(nfp,1); % normal draws |
| 77 |
csq=randn(tdf,1); |
| 78 |
csq=sum(csq .* csq); |
| 79 |
Avhz = xhat+Avhz1/sqrt(csq/tdf); % Robert, p.382 |
| 80 |
|
| 81 |
% *** compute weights *** |
| 82 |
% * t-dist density, having taken log |
| 83 |
tdhz = -0.5*(length(Avhz)+tdf)*log(1+(Avhz-xhat)'*(hess1*(Avhz-xhat))/tdf); |
| 84 |
% * actual density, having taken log |
| 85 |
hAvhz = a0asfun(Avhz,Sbd,fss,nvar,a0indx); |
| 86 |
hAvhz = -hAvhz; % converted to logLH |
| 87 |
|
| 88 |
% * mph: m prim hat in Kloek & van Dijk, p.5 |
| 89 |
mph = exp(hAvhz-tdhz-scf); % scf: scaling factor to prevent overflow |
| 90 |
mphv(draws) = mph; |
| 91 |
|
| 92 |
sm=sm+mph; |
| 93 |
% * matrix of draws for selected variables |
| 94 |
if ~isempty(Indxv) |
| 95 |
xdraw(draws,:) = Avhz(Indxv)'; |
| 96 |
end |
| 97 |
% |
| 98 |
end |
| 99 |
timeminutes = toc/60 |