dsge_posterior_theoretical_covariance.m 3.3 KB
Newer Older
1
function [nvar,vartan,CovarFileNumber] = dsge_posterior_theoretical_covariance(SampleSize,M_,options_,oo_)
2
3
4
5
% This function estimates the posterior density of the endogenous
% variables second order moments. 
% 
% INPUTS 
6
7
8
9
%   SampleSize   [integer]
%   
%
%
10
11
12
13
% OUTPUTS 
%   None.
%
% SPECIAL REQUIREMENTS
14
15
16
17
18
19
20
21
%    Other matlab routines distributed with Dynare: set_stationary_variables_list.m
%                                                   CheckPath.m
%                                                   selec_posterior_draws.m                          
%                                                   set_parameters.m
%                                                   resol.m
%                                                   th_autocovariances.m    
%                                                   posterior_moments.m
%    
22
%  
assia's avatar
assia committed
23
% part of DYNARE, copyright Dynare Team (2007-2008)
24
% Gnu Public License.
assia's avatar
assia committed
25

26
27
type = 'posterior';
    
28
% Set varlist (vartan)
29
[ivar,vartan] = set_stationary_variables_list;
30
31
nvar = length(ivar);

32
33
34
% Set the size of the auto-correlation function to zero.
nar = options_.ar;
options_.ar = 0;    
35

36
37
38
39
40
% Get informations about the _posterior_draws files.
DrawsFiles = dir([M_.dname '/metropolis/' M_.fname '_' type '_draws*' ]);
NumberOfDrawsFiles = length(DrawsFiles);

% Number of lines in posterior data files.
41
42
MaXNumberOfCovarLines = ceil(options_.MaxNumberOfBytes/(nvar*(nvar+1)/2)/8);

43
if SampleSize<=MaXNumberOfCovarLines
44
45
46
47
    Covariance_matrix = zeros(NumberOfSimulations,nvar*(nvar+1)/2);
    NumberOfCovarFiles = 1;
else
    Covariance_matrix = zeros(MaXNumberOfCovarLines,nvar*(nvar+1)/2);
48
49
    NumberOfLinesInTheLastCovarFile = mod(SampleSize,MaXNumberOfCovarLines);
    NumberOfCovarFiles = ceil(SampleSize/MaXNumberOfCovarLines);
50
51
52
53
54
55
56
57
58
59
end

NumberOfCovarLines = rows(Covariance_matrix);
CovarFileNumber = 1;

% Compute 2nd order moments and save them in *_Posterior2ndOrderMoments* files
linea = 0;
for file = 1:NumberOfDrawsFiles
    load([MhDirectoryName '/' DrawsFiles(file).name]);
    NumberOfDraws = rows(pdraws);
60
    isdrsaved = cols(pdraws)-1;
61
62
    for linee = 1:NumberOfDraws
        linea = linea+1;
63
64
65
66
67
68
        if isdrsaved
            dr = pdraws{linee,2};
        else
            set_parameters(pdraws{linee,1});
            [dr,info] = resol(oo_.steady_state,0);
        end
69
        tmp = th_autocovariances(dr,ivar,M_,options_);
70
71
        for i=1:nvar
            for j=i:nvar
72
                Covariance_matrix(linea,symmetric_matrix_index(i,j,nvar)) = tmp{1}(i,j);
73
74
75
76
77
78
79
            end
        end
        if linea == NumberOfCovarLines
            save([fname '_Posterior2ndOrderMoments' int2str(CovarFileNumber)],'Covariance_matrix');
            CovarFileNumber = CovarFileNumber + 1;
            linea = 0;
            test = CovarFileNumber-NumberOfCovarFiles;
80
            if ~test% Prepare the last round...
81
82
                Covariance_matrix = zeros(NumberOfLinesInTheLastCovarFile,nvar*(nvar+1)/2);
                NumberOfCovarLines = NumberOfLinesInTheLastCovarFile;
83
84
                CovarFileNumber = CovarFileNumber - 1;
            elseif test<0;
85
86
                Covariance_matrix = zeros(MaXNumberOfCovarLines,nvar*(nvar+1)/2);
            else
87
                clear('Covariance_matrix');
88
89
90
91
92
            end
        end
    end
end

93
options_.ar = nar;
94
95
96
97
98


function r = rows(M)
    r = size(M,1);

99
function c = cols(M)
100
    c = size(M,2);