394 lines
26 KiB
HTML
394 lines
26 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
|
|||
|
"http://www.w3.org/TR/REC-html40/loose.dtd">
|
|||
|
<html>
|
|||
|
<head>
|
|||
|
<title>Description of metropolis</title>
|
|||
|
<meta name="keywords" content="metropolis">
|
|||
|
<meta name="description" content="stephane.adjemian@ens.fr [09-02-2005]">
|
|||
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|||
|
<meta name="generator" content="m2html © 2003 Guillaume Flandin">
|
|||
|
<meta name="robots" content="index, follow">
|
|||
|
<link type="text/css" rel="stylesheet" href="../m2html.css">
|
|||
|
</head>
|
|||
|
<body>
|
|||
|
<a name="_top"></a>
|
|||
|
<div><a href="../index.html">Home</a> > <a href="index.html">.</a> > metropolis.m</div>
|
|||
|
|
|||
|
<!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png"> Master index</a></td>
|
|||
|
<td align="right"><a href="index.html">Index for . <img alt=">" border="0" src="../right.png"></a></td></tr></table>-->
|
|||
|
|
|||
|
<h1>metropolis
|
|||
|
</h1>
|
|||
|
|
|||
|
<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
|
|||
|
<div class="box"><strong>stephane.adjemian@ens.fr [09-02-2005]</strong></div>
|
|||
|
|
|||
|
<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
|
|||
|
<div class="box"><strong>function metropolis(xparam1,vv,gend,data,rawdata,mh_bounds) </strong></div>
|
|||
|
|
|||
|
<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
|
|||
|
<div class="fragment"><pre class="comment"> stephane.adjemian@ens.fr [09-02-2005]</pre></div>
|
|||
|
|
|||
|
<!-- crossreference -->
|
|||
|
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
|
|||
|
This function calls:
|
|||
|
<ul style="list-style-image:url(../matlabicon.gif)">
|
|||
|
<li><a href="CheckPath.html" class="code" title="function DirectoryName = CheckPath(type)">CheckPath</a> 06-03-2005</li><li><a href="DsgeLikelihood.html" class="code" title="function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data)">DsgeLikelihood</a> stephane.adjemian@cepremap.cnrs.fr [09-07-2004]</li></ul>
|
|||
|
This function is called by:
|
|||
|
<ul style="list-style-image:url(../matlabicon.gif)">
|
|||
|
<li><a href="dynare_estimation.html" class="code" title="function dynare_estimation(var_list_)">dynare_estimation</a> </li></ul>
|
|||
|
<!-- crossreference -->
|
|||
|
|
|||
|
|
|||
|
<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
|
|||
|
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function metropolis(xparam1,vv,gend,data,rawdata,mh_bounds)</a>
|
|||
|
0002 <span class="comment">% stephane.adjemian@ens.fr [09-02-2005]</span>
|
|||
|
0003 <span class="keyword">global</span> M_ oo_ options_ bayestopt_ estim_params_
|
|||
|
0004
|
|||
|
0005 bayestopt_.penalty = 1e8;
|
|||
|
0006
|
|||
|
0007 MhDirectoryName = <a href="CheckPath.html" class="code" title="function DirectoryName = CheckPath(type)">CheckPath</a>(<span class="string">'metropolis'</span>);
|
|||
|
0008
|
|||
|
0009 nblck = options_.mh_nblck;
|
|||
|
0010 nruns = ones(nblck,1)*options_.mh_replic;
|
|||
|
0011 npar = length(xparam1);
|
|||
|
0012 MAX_nruns = ceil(options_.MaxNumberOfBytes/(npar+2)/8);
|
|||
|
0013 d = chol(vv);
|
|||
|
0014 options_.lik_algo = 1;
|
|||
|
0015 OpenOldFile = ones(nblck,1);
|
|||
|
0016
|
|||
|
0017 <span class="keyword">if</span> options_.load_mh_file == 0
|
|||
|
0018 <span class="comment">% Here we start a new metropolis-hastings, previous draws are not</span>
|
|||
|
0019 <span class="comment">% considered.</span>
|
|||
|
0020 <span class="keyword">if</span> nblck > 1
|
|||
|
0021 disp(<span class="string">'MH: Multiple chains mode.'</span>)
|
|||
|
0022 <span class="keyword">else</span>
|
|||
|
0023 disp(<span class="string">'MH: One Chain mode.'</span>)
|
|||
|
0024 <span class="keyword">end</span>
|
|||
|
0025 <span class="comment">% Delete old mh files...</span>
|
|||
|
0026 files = dir([ MhDirectoryName <span class="string">'/'</span> M_.fname <span class="string">'_mh*_blck*.mat'</span>]);
|
|||
|
0027 <span class="keyword">if</span> length(files)
|
|||
|
0028 delete([ MhDirectoryName <span class="string">'/'</span> M_.fname <span class="string">'_mh*_blck*.mat'</span>]);
|
|||
|
0029 disp(<span class="string">'MH: Old _mh files succesfully erased!'</span>)
|
|||
|
0030 <span class="keyword">end</span>
|
|||
|
0031 <span class="comment">% Initial values...</span>
|
|||
|
0032 <span class="keyword">if</span> nblck > 1<span class="comment">% Case 1: multiple chains</span>
|
|||
|
0033 disp(<span class="string">'MH: Searching for initial values...'</span>)
|
|||
|
0034 ix2 = zeros(nblck,npar);
|
|||
|
0035 ilogpo2 = zeros(nblck,1);
|
|||
|
0036 <span class="keyword">for</span> j=1:nblck
|
|||
|
0037 validate = 0;
|
|||
|
0038 init_iter = 0;
|
|||
|
0039 trial = 1;
|
|||
|
0040 <span class="keyword">while</span> validate == 0 & trial <= 10
|
|||
|
0041 candidate = options_.mh_init_scale*randn(1,npar)*d + transpose(xparam1);
|
|||
|
0042 <span class="keyword">if</span> all(candidate' > mh_bounds(:,1)) & all(candidate' < mh_bounds(:,2))
|
|||
|
0043 ix2(j,:) = candidate;
|
|||
|
0044 ilogpo2(j) = -<a href="DsgeLikelihood.html" class="code" title="function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data)">DsgeLikelihood</a>(ix2(j,:)',gend,data);
|
|||
|
0045 j = j+1;
|
|||
|
0046 validate = 1;
|
|||
|
0047 <span class="keyword">end</span>
|
|||
|
0048 init_iter = init_iter + 1;
|
|||
|
0049 <span class="keyword">if</span> init_iter > 100 & validate == 0
|
|||
|
0050 disp([<span class="string">'MH: I couldn''t get a valid initial value in 100 trials.'</span>])
|
|||
|
0051 disp([<span class="string">'MH: You should Reduce mh_init_scale...'</span>])
|
|||
|
0052 disp(sprintf(<span class="string">'MH: Parameter mh_init_scale is equal to %f.'</span>,options_.mh_init_scale))
|
|||
|
0053 options_.mh_init_scale = input(<span class="string">'MH: Enter a new value... '</span>);
|
|||
|
0054 trial = trial+1;
|
|||
|
0055 <span class="keyword">end</span>
|
|||
|
0056 <span class="keyword">end</span>
|
|||
|
0057 <span class="keyword">if</span> trial > 10 & ~validate
|
|||
|
0058 disp([<span class="string">'MH: I''m unable to find a starting value for block '</span> int2str(j)])
|
|||
|
0059 <span class="keyword">return</span>
|
|||
|
0060 <span class="keyword">end</span>
|
|||
|
0061 <span class="keyword">end</span>
|
|||
|
0062 disp(<span class="string">'MH: Initial values found!'</span>)
|
|||
|
0063 disp(<span class="string">' '</span>)
|
|||
|
0064 <span class="keyword">else</span><span class="comment">% Case 2: one chain (we start from the posterior mode)</span>
|
|||
|
0065 candidate = transpose(xparam1);
|
|||
|
0066 <span class="keyword">if</span> all(candidate' > mh_bounds(:,1)) & all(candidate' < mh_bounds(:,2))
|
|||
|
0067 ix2 = candidate;
|
|||
|
0068 ilogpo2 = -<a href="DsgeLikelihood.html" class="code" title="function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data)">DsgeLikelihood</a>(ix2',gend,data);
|
|||
|
0069 disp(<span class="string">'MH: Initialization at the posterior mode.'</span>)
|
|||
|
0070 disp(<span class="string">' '</span>)
|
|||
|
0071 <span class="keyword">else</span>
|
|||
|
0072 disp(<span class="string">'MH: Initialization failed...'</span>)
|
|||
|
0073 disp(<span class="string">'MH: The posterior mode lies outside the prior bounds.'</span>)
|
|||
|
0074 <span class="keyword">return</span>
|
|||
|
0075 <span class="keyword">end</span>
|
|||
|
0076 <span class="keyword">end</span>
|
|||
|
0077 fblck = 1;
|
|||
|
0078 fline = ones(nblck,1);
|
|||
|
0079 NewFile = ones(nblck,1);
|
|||
|
0080 <span class="comment">% Creation of the mh-history file:</span>
|
|||
|
0081 file = dir([MhDirectoryName <span class="string">'/'</span> M_.fname <span class="string">'_mh_history.mat'</span>]);
|
|||
|
0082 <span class="keyword">if</span> length(files)
|
|||
|
0083 delete([ MhDirectoryName <span class="string">'/'</span> M_.fname <span class="string">'_mh_history.mat'</span>]);
|
|||
|
0084 disp(<span class="string">'MH: Old mh_history file succesfully erased!'</span>)
|
|||
|
0085 <span class="keyword">end</span>
|
|||
|
0086 AnticipatedNumberOfFiles = floor(nruns(1)/MAX_nruns);
|
|||
|
0087 AnticipatedNumberOfLinesInTheLastFile = nruns(1) - AnticipatedNumberOfFiles*MAX_nruns;
|
|||
|
0088 record.Nblck = nblck;
|
|||
|
0089 record.MhDraws = zeros(1,3);
|
|||
|
0090 record.MhDraws(1,1) = nruns(1);
|
|||
|
0091 record.MhDraws(1,2) = AnticipatedNumberOfFiles+1;
|
|||
|
0092 record.MhDraws(1,3) = AnticipatedNumberOfLinesInTheLastFile;
|
|||
|
0093 record.AcceptationRates = zeros(1,nblck);
|
|||
|
0094 record.Seeds.Normal = randn(<span class="string">'state'</span>);
|
|||
|
0095 record.Seeds.Unifor = rand(<span class="string">'state'</span>);
|
|||
|
0096 record.InitialParameters = ix2;
|
|||
|
0097 record.InitialLogLiK = ilogpo2;
|
|||
|
0098 record.LastParameters = zeros(nblck,npar);
|
|||
|
0099 record.LastLogLiK = zeros(nblck,1);
|
|||
|
0100 record.LastFileNumber = AnticipatedNumberOfFiles+1;
|
|||
|
0101 record.LastLineNumber = AnticipatedNumberOfLinesInTheLastFile;
|
|||
|
0102 save([MhDirectoryName <span class="string">'/'</span> M_.fname <span class="string">'_mh_history'</span>],<span class="string">'record'</span>);
|
|||
|
0103 <span class="keyword">elseif</span> options_.load_mh_file == 1<span class="comment">% Here we consider previous mh files (previous mh did not crash).</span>
|
|||
|
0104 disp(<span class="string">'MH: I''m loading past metropolis-hastings simulations...'</span>)
|
|||
|
0105 file = dir([ MhDirectoryName <span class="string">'/'</span> M_.fname <span class="string">'_mh_history.mat'</span> ]);
|
|||
|
0106 files = dir([ MhDirectoryName <span class="string">'/'</span> M_.fname <span class="string">'_mh*.mat'</span>]);
|
|||
|
0107 <span class="keyword">if</span> ~length(files)
|
|||
|
0108 disp(<span class="string">'MH:: FAILURE! there is no MH file to load here!'</span>)
|
|||
|
0109 <span class="keyword">return</span>
|
|||
|
0110 <span class="keyword">end</span>
|
|||
|
0111 <span class="keyword">if</span> ~length(file)
|
|||
|
0112 disp(<span class="string">'MH:: FAILURE! there is no MH-history file!'</span>)
|
|||
|
0113 <span class="keyword">return</span>
|
|||
|
0114 <span class="keyword">else</span>
|
|||
|
0115 load([ MhDirectoryName <span class="string">'/'</span> M_.fname <span class="string">'_mh_history'</span>])
|
|||
|
0116 <span class="keyword">end</span>
|
|||
|
0117 past_number_of_blocks = record.Nblck;
|
|||
|
0118 <span class="keyword">if</span> past_number_of_blocks ~= nblck
|
|||
|
0119 disp(<span class="string">'MH:: The specified number of blocks doesn''t match with the previous number of blocks!'</span>)
|
|||
|
0120 disp([<span class="string">'MH:: You declared '</span> int2str(nblck) <span class="string">' blocks, but the previous number of blocks was '</span> int2str(past_number_of_blocks) <span class="string">'.'</span>])
|
|||
|
0121 disp([<span class="string">'MH:: I will run the Metropolis-Hastings with '</span> int2str(past_number_of_blocks) <span class="string">' blocks.'</span> ])
|
|||
|
0122 nblck = past_number_of_blocks;
|
|||
|
0123 options_.mh_nblck = nblck;
|
|||
|
0124 <span class="keyword">end</span>
|
|||
|
0125 <span class="comment">% I get the last line of the last mh-file for initialization</span>
|
|||
|
0126 <span class="comment">% of the new metropolis-hastings simulations:</span>
|
|||
|
0127 LastFileNumber = record.LastFileNumber;
|
|||
|
0128 LastLineNumber = record.LastLineNumber;
|
|||
|
0129 <span class="keyword">if</span> LastLineNumber < MAX_nruns
|
|||
|
0130 NewFile = ones(nblck,1)*LastFileNumber;
|
|||
|
0131 <span class="keyword">else</span>
|
|||
|
0132 NewFile = ones(nblck,1)*(LastFileNumber+1);
|
|||
|
0133 <span class="keyword">end</span>
|
|||
|
0134 ilogpo2 = record.LastLogLiK;
|
|||
|
0135 ix2 = record.LastParameters;
|
|||
|
0136 fblck = 1;
|
|||
|
0137 fline = ones(nblck,1)*(LastLineNumber+1);
|
|||
|
0138 NumberOfPreviousSimulations = sum(record.MhDraws(:,1),1);
|
|||
|
0139 record.MhDraws = [record.MhDraws;zeros(1,3)];
|
|||
|
0140 NumberOfDrawsWrittenInThePastLastFile = MAX_nruns - LastLineNumber;
|
|||
|
0141 NumberOfDrawsToBeSaved = nruns(1) - NumberOfDrawsWrittenInThePastLastFile;
|
|||
|
0142 AnticipatedNumberOfFiles = floor(NumberOfDrawsToBeSaved/MAX_nruns);
|
|||
|
0143 AnticipatedNumberOfLinesInTheLastFile = NumberOfDrawsToBeSaved - AnticipatedNumberOfFiles*MAX_nruns;
|
|||
|
0144 record.LastFileNumber = LastFileNumber + AnticipatedNumberOfFiles + 1;
|
|||
|
0145 record.LastLineNumber = AnticipatedNumberOfLinesInTheLastFile;
|
|||
|
0146 record.MhDraws(<span class="keyword">end</span>,1) = nruns(1);
|
|||
|
0147 record.MhDraws(<span class="keyword">end</span>,2) = AnticipatedNumberOfFiles+1;
|
|||
|
0148 record.MhDraws(<span class="keyword">end</span>,3) = AnticipatedNumberOfLinesInTheLastFile;
|
|||
|
0149 randn(<span class="string">'state'</span>,record.Seeds.Normal);
|
|||
|
0150 rand(<span class="string">'state'</span>,record.Seeds.Unifor);
|
|||
|
0151 save([MhDirectoryName <span class="string">'/'</span> M_.fname <span class="string">'_mh_history'</span>],<span class="string">'record'</span>);
|
|||
|
0152 disp([<span class="string">'MH: ... It''s done. I''ve loaded '</span> int2str(NumberOfPreviousSimulations) <span class="string">' simulations.'</span>])
|
|||
|
0153 disp(<span class="string">' '</span>)
|
|||
|
0154 <span class="keyword">elseif</span> options_.load_mh_file == -1<span class="comment">% The previous metropolis-hastings</span>
|
|||
|
0155 <span class="comment">% crashed before the end! I try to</span>
|
|||
|
0156 <span class="comment">% recover the saved draws...</span>
|
|||
|
0157 disp(<span class="string">'MH: Recover mode!'</span>)
|
|||
|
0158 disp(<span class="string">' '</span>)
|
|||
|
0159 file = dir([MhDirectoryName <span class="string">'/'</span> M_.fname <span class="string">'_mh_history.mat'</span>]);
|
|||
|
0160 <span class="keyword">if</span> ~length(file)
|
|||
|
0161 disp(<span class="string">'MH:: FAILURE! there is no MH-history file!'</span>)
|
|||
|
0162 <span class="keyword">return</span>
|
|||
|
0163 <span class="keyword">else</span>
|
|||
|
0164 load([ MhDirectoryName <span class="string">'/'</span> M_.fname <span class="string">'_mh_history'</span>])
|
|||
|
0165 <span class="keyword">end</span>
|
|||
|
0166 nblck = record.Nblck;
|
|||
|
0167 options_.mh_nblck = nblck;
|
|||
|
0168 <span class="keyword">if</span> size(record.MhDraws,1) == 1
|
|||
|
0169 OldMh = 0;<span class="comment">% The crashed metropolis was the first session.</span>
|
|||
|
0170 <span class="keyword">else</span>
|
|||
|
0171 OldMh = 1;<span class="comment">% The crashed metropolis wasn't the first session.</span>
|
|||
|
0172 <span class="keyword">end</span>
|
|||
|
0173 <span class="comment">% Default initialization:</span>
|
|||
|
0174 <span class="keyword">if</span> OldMh
|
|||
|
0175 ilogpo2 = record.LastLogLiK;
|
|||
|
0176 ix2 = record.LastParameters;
|
|||
|
0177 <span class="keyword">else</span>
|
|||
|
0178 ilogpo2 = record.InitialLogLiK;
|
|||
|
0179 ix2 = record.InitialParameters;
|
|||
|
0180 <span class="keyword">end</span>
|
|||
|
0181 <span class="comment">% Set "NewFile":</span>
|
|||
|
0182 <span class="keyword">if</span> OldMh
|
|||
|
0183 LastLineNumberInThePreviousMh = record.MhDraws(end-1,3);
|
|||
|
0184 LastFileNumberInThePreviousMh = sum(record.MhDraws(1:end-1,2),1);
|
|||
|
0185 <span class="keyword">if</span> LastLineNumberInThePreviousMh < MAX_nruns
|
|||
|
0186 NewFile = ones(nblck,1)*LastFileNumberInThePreviousMh;
|
|||
|
0187 <span class="keyword">else</span>
|
|||
|
0188 NewFile = ones(nblck,1)*(LastFileNumberInThePreviousMh+1);
|
|||
|
0189 <span class="keyword">end</span>
|
|||
|
0190 <span class="keyword">else</span>
|
|||
|
0191 NewFile = ones(nblck,1);
|
|||
|
0192 <span class="keyword">end</span>
|
|||
|
0193 <span class="comment">% Set fline (First line):</span>
|
|||
|
0194 <span class="keyword">if</span> OldMh
|
|||
|
0195 fline = ones(nblck,1)*(record.MhDraws(end-1,3)+1);
|
|||
|
0196 <span class="keyword">else</span>
|
|||
|
0197 fline = ones(nblck,1);
|
|||
|
0198 <span class="keyword">end</span>
|
|||
|
0199 <span class="comment">% Set fblck (First block):</span>
|
|||
|
0200 fblck = 1;
|
|||
|
0201 <span class="comment">% How many mh files should we have ?</span>
|
|||
|
0202 ExpectedNumberOfMhFilesPerBlock = sum(record.MhDraws(:,2),1);
|
|||
|
0203 ExpectedNumberOfMhFiles = ExpectedNumberOfMhFilesPerBlock*nblck;
|
|||
|
0204 <span class="comment">% I count the total number of saved mh files...</span>
|
|||
|
0205 AllMhFiles = dir([MhDirectoryName <span class="string">'/'</span> M_.fname <span class="string">'_mh*_blck*.mat'</span>]);
|
|||
|
0206 TotalNumberOfMhFiles = length(AllMhFiles);
|
|||
|
0207 <span class="comment">% I count the number of saved mh files per block</span>
|
|||
|
0208 NumberOfMhFilesPerBlock = zeros(nblck,1);
|
|||
|
0209 <span class="keyword">for</span> i = 1:nblck
|
|||
|
0210 BlckMhFiles = dir([ MhDirectoryName <span class="string">'/'</span> M_.fname <span class="string">'_mh*_blck'</span> int2str(i) <span class="string">'.mat'</span>]);
|
|||
|
0211 NumberOfMhFilesPerBlock(i) = length(BlckMhFiles);
|
|||
|
0212 <span class="keyword">end</span>
|
|||
|
0213 tmp = NumberOfMhFilesPerBlock(1); b = 1;
|
|||
|
0214 <span class="comment">% Is there a chain with less mh files than the first chain ?</span>
|
|||
|
0215 CrashedBlck = 1;
|
|||
|
0216 <span class="keyword">while</span> b <= nblck
|
|||
|
0217 <span class="keyword">if</span> NumberOfMhFilesPerBlock(b) < ExpectedNumberOfMhFilesPerBlock
|
|||
|
0218 CrashedBlck = b;<span class="comment">% YES!</span>
|
|||
|
0219 <span class="keyword">break</span>
|
|||
|
0220 <span class="keyword">end</span>
|
|||
|
0221 b = b+1;
|
|||
|
0222 <span class="keyword">end</span>
|
|||
|
0223 <span class="comment">% The new metropolis-hastings should start from (fblck=CrashedBlck)</span>
|
|||
|
0224 fblck = CrashedBlck;
|
|||
|
0225 <span class="comment">% How many mh-files are saved in this block ?</span>
|
|||
|
0226 NumberOfSavedMhFilesInTheCrashedBlck = <span class="keyword">...</span>
|
|||
|
0227 NumberOfMhFilesPerBlock(CrashedBlck);
|
|||
|
0228 <span class="comment">% How many mh-files were saved in this block during the last session</span>
|
|||
|
0229 <span class="comment">% (if there was a complete session before the crash) ?</span>
|
|||
|
0230 <span class="keyword">if</span> OldMh
|
|||
|
0231 ante = sum(record.MhDraws(1:end-1,2),1);
|
|||
|
0232 load([MhDirectoryName <span class="string">'/'</span> M_.fname <span class="string">'_mh'</span> int2str(ante) <span class="string">'_blck'</span> int2str(CrashedBlck) <span class="string">'.mat'</span>],<span class="string">'logpo2'</span>);
|
|||
|
0233 <span class="keyword">if</span> length(logpo2) == MAX_nruns
|
|||
|
0234 IsTheLastFileOfThePreviousMhFull = 1;
|
|||
|
0235 <span class="keyword">else</span>
|
|||
|
0236 IsTheLastFileOfThePreviousMhFull = 0;
|
|||
|
0237 <span class="keyword">end</span>
|
|||
|
0238 <span class="keyword">else</span>
|
|||
|
0239 ante = 0;<span class="comment">% Because the crashed session is the first one</span>
|
|||
|
0240 IsTheLastFileOfThePreviousMhFull = 1;
|
|||
|
0241 <span class="keyword">end</span>
|
|||
|
0242 <span class="keyword">if</span> ~IsTheLastFileOfThePreviousMhFull
|
|||
|
0243 MhFileExist = 1;
|
|||
|
0244 MhFileNumber = ante;
|
|||
|
0245 <span class="keyword">while</span> MhFileExist
|
|||
|
0246 MhFileNumber = MhFileNumber + 1;
|
|||
|
0247 <span class="keyword">if</span> ~exist([MhDirectoryName <span class="string">'/'</span> M_.fname <span class="string">'_mh'</span> int2str(MhFileNumber) <span class="string">'_blck'</span> int2str(CrashedBlck) <span class="string">'.mat'</span>])
|
|||
|
0248 MhFileExist = 0;
|
|||
|
0249 <span class="keyword">end</span>
|
|||
|
0250 <span class="keyword">end</span>
|
|||
|
0251 <span class="comment">% if MhFileNumber > ExpectedNumberOfMhFilesPerBlock % Peut tre dplacer plus haut...</span>
|
|||
|
0252 <span class="comment">% disp('MH : You are using the recover mode but the previous session');</span>
|
|||
|
0253 <span class="comment">% disp(' of the metropolis-hastings didn''t crash!')</span>
|
|||
|
0254 <span class="comment">% disp('MH : I stop and you shoud modify your mod file.')</span>
|
|||
|
0255 <span class="comment">% return</span>
|
|||
|
0256 <span class="comment">% else</span>
|
|||
|
0257 NumberOfCompletedMhFiles = (MhFileNumber-1)-ante;
|
|||
|
0258 <span class="comment">% How many runs were saved ?</span>
|
|||
|
0259 <span class="keyword">if</span> OldMh
|
|||
|
0260 reste = MAX_nruns-record.MhDraws(end-1,3);
|
|||
|
0261 <span class="keyword">else</span>
|
|||
|
0262 reste = 0
|
|||
|
0263 <span class="keyword">end</span>
|
|||
|
0264 NumberOfSavedDraws = MAX_nruns*(NumberOfCompletedMhFiles) + reste;
|
|||
|
0265 <span class="comment">% Here is the number of draws we still need to complete the block:</span>
|
|||
|
0266 nruns(CrashedBlck) = nruns(CrashedBlck)-NumberOfSavedDraws;
|
|||
|
0267 <span class="comment">% I initialize with the last saved mh file of the inccomplete</span>
|
|||
|
0268 <span class="comment">% block:</span>
|
|||
|
0269 load([MhDirectoryName <span class="string">'/'</span> M_.fname <span class="string">'_mh'</span> int2str(MhFileNumber-1) <span class="string">'_blck'</span> int2str(CrashedBlck) <span class="string">'.mat'</span>]);
|
|||
|
0270 ilogpo2(CrashedBlck) = logpo2(end);
|
|||
|
0271 ix2(CrashedBlck,:) = x2(<span class="keyword">end</span>,:);
|
|||
|
0272 NewFile(CrashedBlck) = MhFileNumber;
|
|||
|
0273 fline(CrashedBlck,1) = 1;
|
|||
|
0274 <span class="keyword">else</span>
|
|||
|
0275 <span class="comment">% creuser ce qui se passe dans ce cas !</span>
|
|||
|
0276 OpenOldFile(CrashedBlck) = 0;
|
|||
|
0277 disp(<span class="string">'Ok'</span>)
|
|||
|
0278 <span class="keyword">end</span>
|
|||
|
0279 <span class="keyword">end</span><span class="comment">% of (if options_.load_mh_file == {0,1 or -1})</span>
|
|||
|
0280 <span class="comment">%%%%</span>
|
|||
|
0281 <span class="comment">%%%% NOW i run the (nblck-fblck+1) metropolis-hastings chains</span>
|
|||
|
0282 <span class="comment">%%%%</span>
|
|||
|
0283 InitSizeArray = min([MAX_nruns*ones(nblck) nruns],[],2);
|
|||
|
0284 <span class="keyword">for</span> b = fblck:nblck
|
|||
|
0285 <span class="keyword">if</span> (options_.load_mh_file~=0) & (fline(b)>1) & OpenOldFile(b)
|
|||
|
0286 load([<span class="string">'./'</span> MhDirectoryName <span class="string">'/'</span> M_.fname <span class="string">'_mh'</span> int2str(NewFile(b)) <span class="keyword">...</span>
|
|||
|
0287 <span class="string">'_blck'</span> int2str(b) <span class="string">'.mat'</span>])
|
|||
|
0288 x2 = [x2;zeros(InitSizeArray(b)-fline(b)+1,npar)];
|
|||
|
0289 logpo2 = [logpo2;zeros(InitSizeArray(b)-fline(b)+1,1)];
|
|||
|
0290 OpenOldFile(b) = 0;
|
|||
|
0291 <span class="keyword">else</span>
|
|||
|
0292 x2 = zeros(InitSizeArray(b),npar);
|
|||
|
0293 logpo2 = zeros(InitSizeArray(b),1);
|
|||
|
0294 <span class="keyword">end</span>
|
|||
|
0295 hh = waitbar(0,[<span class="string">'Please wait... Metropolis-Hastings ('</span> int2str(b) <span class="string">'/'</span> int2str(nblck) <span class="string">')...'</span>]);
|
|||
|
0296 set(hh,<span class="string">'Name'</span>,<span class="string">'Metropolis-Hastings'</span>)
|
|||
|
0297 isux = 0;
|
|||
|
0298 irun = fline(b);
|
|||
|
0299 j = 1;
|
|||
|
0300 <span class="keyword">while</span> j <= nruns(b)
|
|||
|
0301 par = randn(1,npar)*d;
|
|||
|
0302 par = par.*bayestopt_.jscale' + ix2(b,:);
|
|||
|
0303 <span class="keyword">if</span> all(par'>mh_bounds(:,1)) & all(par'<mh_bounds(:,2))
|
|||
|
0304 logpost = -<a href="DsgeLikelihood.html" class="code" title="function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data)">DsgeLikelihood</a>(par',gend,data);
|
|||
|
0305 <span class="keyword">else</span>
|
|||
|
0306 logpost = -inf;
|
|||
|
0307 <span class="keyword">end</span>
|
|||
|
0308 <span class="keyword">if</span> (logpost > -inf) & (log(rand) < logpost-ilogpo2(b))
|
|||
|
0309 x2(irun,:) = par;
|
|||
|
0310 ix2(b,:) = par;
|
|||
|
0311 logpo2(irun) = logpost;
|
|||
|
0312 ilogpo2(b) = logpost;
|
|||
|
0313 isux = isux + 1;
|
|||
|
0314 <span class="keyword">else</span>
|
|||
|
0315 x2(irun,:) = ix2(b,:);
|
|||
|
0316 logpo2(irun) = ilogpo2(b);
|
|||
|
0317 <span class="keyword">end</span>
|
|||
|
0318 prtfrc = j/nruns(b);
|
|||
|
0319 waitbar(prtfrc,hh,[ <span class="string">'('</span> int2str(b) <span class="string">'/'</span> int2str(nblck) <span class="string">') '</span> sprintf(<span class="string">'%f done, acceptation rate %f'</span>,prtfrc,isux/j)]);
|
|||
|
0320 <span class="keyword">if</span> (irun == InitSizeArray(b)) | (j == nruns(b)) <span class="comment">% Now I save the simulations</span>
|
|||
|
0321 save([MhDirectoryName <span class="string">'/'</span> M_.fname <span class="string">'_mh'</span> int2str(NewFile(b)) <span class="string">'_blck'</span> int2str(b)],<span class="string">'x2'</span>,<span class="string">'logpo2'</span>);
|
|||
|
0322 InitSizeArray(b) = min(nruns(b)-j,MAX_nruns);
|
|||
|
0323 <span class="keyword">if</span> j == nruns(b) <span class="comment">% I record the last draw...</span>
|
|||
|
0324 record.LastParameters(b,:) = x2(<span class="keyword">end</span>,:);
|
|||
|
0325 record.LastLogLiK(b) = logpo2(end);
|
|||
|
0326 <span class="keyword">end</span>
|
|||
|
0327 <span class="keyword">if</span> InitSizeArray(b)
|
|||
|
0328 x2 = zeros(InitSizeArray(b),npar);
|
|||
|
0329 logpo2 = zeros(InitSizeArray(b),1);
|
|||
|
0330 NewFile(b) = NewFile(b) + 1;
|
|||
|
0331 irun = 0;
|
|||
|
0332 <span class="keyword">else</span> <span class="comment">% InitSizeArray is equal to zero because we are at the end of an mc chain.</span>
|
|||
|
0333 InitSizeArray(b) = min(nruns(b),MAX_nruns);
|
|||
|
0334 <span class="keyword">end</span>
|
|||
|
0335 <span class="keyword">end</span>
|
|||
|
0336 j=j+1;
|
|||
|
0337 irun = irun + 1;
|
|||
|
0338 <span class="keyword">end</span><span class="comment">% End of the simulations for one mh-block.</span>
|
|||
|
0339 record.AcceptationRates(b) = isux/j;
|
|||
|
0340 close(hh);
|
|||
|
0341 <span class="keyword">end</span><span class="comment">% End of the loop over the mh-blocks.</span>
|
|||
|
0342 record.Seeds.Normal = randn(<span class="string">'state'</span>);
|
|||
|
0343 record.Seeds.Unifor = rand(<span class="string">'state'</span>);
|
|||
|
0344 save([MhDirectoryName <span class="string">'/'</span> M_.fname <span class="string">'_mh_history'</span>],<span class="string">'record'</span>);
|
|||
|
0345 disp([<span class="string">'MH: Number of mh files : '</span> int2str(NewFile(1)) <span class="string">' per block.'</span>])
|
|||
|
0346 disp([<span class="string">'MH: Total number of generated files : '</span> int2str(NewFile(1)*nblck) <span class="string">'.'</span>])
|
|||
|
0347 disp([<span class="string">'MH: Total number of iterations : '</span> int2str((NewFile(1)-1)*MAX_nruns+irun-1) <span class="string">'.'</span>])
|
|||
|
0348 disp(<span class="string">' '</span>)</pre></div>
|
|||
|
<hr><address>Generated on Fri 16-Jun-2006 09:09:06 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> © 2003</address>
|
|||
|
</body>
|
|||
|
</html>
|