1function [nvar,vartan,CovarFileNumber] = dsge_simulated_theoretical_covariance(SampleSize,M_,options_,oo_,type)
2% function [nvar,vartan,CovarFileNumber] = dsge_simulated_theoretical_covariance(SampleSize,M_,options_,oo_,type)
3% This function computes the posterior or prior distribution of the endogenous
4% variables second order moments.
5%
6% INPUTS
7%   SampleSize   [integer]       scalar, number of simulations.
8%   M_           [structure]     Dynare structure describing the model.
9%   options_     [structure]     Dynare structure defining global options.
10%   oo_          [structure]     Dynare structure where the results are saved.
11%   type         [string]        'prior' or 'posterior'
12%
13%
14% OUTPUTS
15%   nvar              [integer]  nvar is the number of stationary variables.
16%   vartan            [char]     array of characters (with nvar rows).
17%   CovarFileNumber   [integer]  scalar, number of prior or posterior data files (for covariance).
18
19% Copyright (C) 2007-2020 Dynare Team
20%
21% This file is part of Dynare.
22%
23% Dynare is free software: you can redistribute it and/or modify
24% it under the terms of the GNU General Public License as published by
25% the Free Software Foundation, either version 3 of the License, or
26% (at your option) any later version.
27%
28% Dynare is distributed in the hope that it will be useful,
29% but WITHOUT ANY WARRANTY; without even the implied warranty of
30% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31% GNU General Public License for more details.
32%
33% You should have received a copy of the GNU General Public License
34% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
35
36nodecomposition = 1;
37
38% Get informations about the _posterior_draws files.
39if strcmpi(type,'posterior')
40    NumberOfDrawsFiles = length(dir([M_.dname '/metropolis/' M_.fname '_' type '_draws*' ]));
41    posterior = 1;
42elseif strcmpi(type,'prior')
43    NumberOfDrawsFiles = length(dir([M_.dname '/prior/draws/' type '_draws*' ]));
44    CheckPath('prior/moments',M_.dname);
45    posterior = 0;
46else
47    disp('dsge_simulated_theoretical_covariance:: Unknown type!')
48    error();
49end
50
51%delete old stale files before creating new ones
52if posterior
53    delete_stale_file([M_.dname '/metropolis/' M_.fname '_Posterior2ndOrderMoments*'])
54else
55    delete_stale_file([M_.dname '/prior/moments/' M_.fname '_Prior2ndOrderMoments*'])
56end
57
58% Set varlist (vartan)
59if ~posterior
60    if isfield(options_,'varlist')
61        temp = options_.varlist;
62    end
63    options_.varlist = options_.prior_analysis_endo_var_list;
64end
65[ivar,vartan] = get_variables_list(options_,M_);
66if ~posterior
67    if exist('temp','var')
68        options_.varlist = temp;
69    end
70end
71nvar = length(ivar);
72
73% Set the size of the auto-correlation function to zero.
74nar = options_.ar;
75options_.ar = 0;
76
77% Number of lines in posterior data files.
78MaXNumberOfCovarLines = ceil(options_.MaxNumberOfBytes/(nvar*(nvar+1)/2)/8);
79
80if SampleSize<=MaXNumberOfCovarLines
81    Covariance_matrix = zeros(SampleSize,nvar*(nvar+1)/2);
82    NumberOfCovarFiles = 1;
83else
84    Covariance_matrix = zeros(MaXNumberOfCovarLines,nvar*(nvar+1)/2);
85    NumberOfLinesInTheLastCovarFile = mod(SampleSize,MaXNumberOfCovarLines);
86    NumberOfCovarFiles = ceil(SampleSize/MaXNumberOfCovarLines);
87end
88
89NumberOfCovarLines = rows(Covariance_matrix);
90CovarFileNumber = 1;
91
92% Compute 2nd order moments and save them in *_[Posterior, Prior]2ndOrderMoments* files
93linea = 0;
94for file = 1:NumberOfDrawsFiles
95    if posterior
96        temp=load([M_.dname '/metropolis/' M_.fname '_' type '_draws' num2str(file) ]);
97    else
98        temp=load([M_.dname '/prior/draws/' type '_draws' num2str(file) ]);
99    end
100    NumberOfDraws = rows(temp.pdraws);
101    isdrsaved = columns(temp.pdraws)-1;
102    for linee = 1:NumberOfDraws
103        linea = linea+1;
104        if isdrsaved
105            M_=set_parameters_locally(M_,temp.pdraws{linee,1});% Needed to update the covariance matrix of the state innovations.
106            dr = temp.pdraws{linee,2};
107        else
108            M_=set_parameters_locally(M_,temp.pdraws{linee,1});
109            [dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_);
110        end
111        tmp = th_autocovariances(dr,ivar,M_,options_,nodecomposition);
112        for i=1:nvar
113            for j=i:nvar
114                Covariance_matrix(linea,symmetric_matrix_index(i,j,nvar)) = tmp{1}(i,j);
115            end
116        end
117        if linea == NumberOfCovarLines
118            if posterior
119                save([ M_.dname '/metropolis/' M_.fname '_Posterior2ndOrderMoments' int2str(CovarFileNumber) '.mat' ],'Covariance_matrix');
120            else
121                save([ M_.dname '/prior/moments/' M_.fname '_Prior2ndOrderMoments' int2str(CovarFileNumber) '.mat' ],'Covariance_matrix');
122            end
123            CovarFileNumber = CovarFileNumber + 1;
124            linea = 0;
125            test = CovarFileNumber-NumberOfCovarFiles;
126            if ~test% Prepare the last round...
127                Covariance_matrix = zeros(NumberOfLinesInTheLastCovarFile,nvar*(nvar+1)/2);
128                NumberOfCovarLines = NumberOfLinesInTheLastCovarFile;
129            elseif test<0
130                Covariance_matrix = zeros(MaXNumberOfCovarLines,nvar*(nvar+1)/2);
131            else
132                clear('Covariance_matrix');
133            end
134        end
135    end
136end
137
138options_.ar = nar;
139