From be1e0f1b8a27f5fc0224665e65f6a491a4de75d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Scylla=29?= Date: Fri, 25 Jun 2010 14:58:53 +0200 Subject: [PATCH] Fixed bug for mode_compute=6 in Octave. Added an option to replace waitbar in matlab by textbar (as in Octave). The user has to set options_.console_mode before the estimation command. Cosmetic changes in gmhmaxlik (mode_compute=6). --- matlab/dynare_estimation_1.m | 8 +- matlab/global_initialization.m | 2 + matlab/gmhmaxlik.m | 107 +++++++++++++++--- matlab/independent_metropolis_hastings_core.m | 19 ++-- matlab/random_walk_metropolis_hastings_core.m | 23 ++-- 5 files changed, 122 insertions(+), 37 deletions(-) diff --git a/matlab/dynare_estimation_1.m b/matlab/dynare_estimation_1.m index 31d2da49a..c73ed8eb1 100644 --- a/matlab/dynare_estimation_1.m +++ b/matlab/dynare_estimation_1.m @@ -516,8 +516,12 @@ if options_.mode_compute > 0 & options_.posterior_mode_estimation end options_.mh_jscale = Scale; mouvement = max(max(abs(PostVar-OldPostVar))); - disp(['Change in the covariance matrix = ' num2str(mouvement) '.']) - disp(['Mode improvement = ' num2str(abs(OldMode-fval))]) + disp(' ') + disp('========================================================== ') + disp([' Change in the covariance matrix = ' num2str(mouvement) '.']) + disp([' Mode improvement = ' num2str(abs(OldMode-fval))]) + disp([' New value of jscale = ' num2str(Scale)]) + disp('========================================================== ') OldMode = fval; else OldPostVar = PostVar; diff --git a/matlab/global_initialization.m b/matlab/global_initialization.m index 1d5c34ce8..01c2f3b90 100644 --- a/matlab/global_initialization.m +++ b/matlab/global_initialization.m @@ -30,6 +30,8 @@ function global_initialization() global oo_ M_ options_ +options_.console_mode = 0; + options_.terminal_condition = 0; options_.rplottype = 0; options_.smpl = 0; diff --git a/matlab/gmhmaxlik.m b/matlab/gmhmaxlik.m index 230284324..65cbe8520 100644 --- a/matlab/gmhmaxlik.m +++ b/matlab/gmhmaxlik.m @@ -90,8 +90,13 @@ CovJump = VarCov; ModePar = xparam1; %% [1] I tune the scale parameter. -hh = waitbar(0,'Tuning of the scale parameter...'); -set(hh,'Name','Tuning of the scale parameter.') +if exist('OCTAVE_VERSION') || options_.console_mode + diary off; + disp(' '); +else + hh = waitbar(0,'Tuning of the scale parameter...'); + set(hh,'Name','Tuning of the scale parameter.') +end j = 1; jj = 1; isux = 0; jsux = 0; test = 0; ix2 = ModePar;% initial condition! @@ -121,7 +126,17 @@ while j<=MaxNumberOfTuningSimulations jsux = jsux + 1; end% ... otherwise I don't move. prtfrc = j/MaxNumberOfTuningSimulations; - waitbar(prtfrc,hh,sprintf('Acceptation rates: %f [%f]',isux/j,jsux/jj)); + if exist('OCTAVE_VERSION') || options_.console_mode + if mod(j, 10)==0 + if exist('OCTAVE_VERSION') + printf('Tuning of the scale parameter (%f%% done):: Acceptance rates: %f [%f]\r',prtfrc*100,isux/j,jsux/jj); + else + fprintf(' Tuning of the scale parameter (%f \b%% done):: Acceptance rates: %f [%f]\r',prtfrc*100,isux/j,jsux/jj); + end + end + else + waitbar(prtfrc,hh,sprintf('Acceptance rates: %f [%f]',isux/j,jsux/jj)); + end if j/500 == round(j/500) test1 = jsux/jj; cfactor = test1/AcceptanceTarget; @@ -141,11 +156,18 @@ while j<=MaxNumberOfTuningSimulations j = j+1; jj = jj + 1; end -close(hh); -iScale +if exist('OCTAVE_VERSION') || options_.console_mode + diary on; +else + close(hh); +end %% [2] One block metropolis, I update the covariance matrix of the jumping distribution -hh = waitbar(0,'Metropolis-Hastings...'); -set(hh,'Name','Looking for the posterior covariance...') +if exist('OCTAVE_VERSION') || options_.console_mode + diary off; +else + hh = waitbar(0,'Metropolis-Hastings...'); + set(hh,'Name','Estimation of the posterior covariance...') +end j = 1; isux = 0; ilogpo2 = - feval(ObjFun,ix2,varargin{:}); @@ -168,7 +190,17 @@ while j<= NumberOfIterations jsux = jsux + 1; end% ... otherwise I don't move. prtfrc = j/NumberOfIterations; - waitbar(prtfrc,hh,sprintf('%f done, acceptation rate %f',prtfrc,isux/j)); + if exist('OCTAVE_VERSION') || options_.console_mode + if mod(j, 10)==0 + if exist('OCTAVE_VERSION') + printf('Estimation of the posterior covariance (%f%% done):: Acceptance rates: %f\r',prtfrc,isux/j); + else + fprintf(' Estimation of the posterior covariance (%f \b%% done):: Acceptance rates: %f \r',prtfrc*100,isux/j); + end + end + else + waitbar(prtfrc,hh,sprintf('Acceptance rate: %f',isux/j)); + end % I update the covariance matrix and the mean: oldMeanPar = MeanPar; MeanPar = oldMeanPar + (1/j)*(ix2-oldMeanPar); @@ -176,15 +208,23 @@ while j<= NumberOfIterations (1/j)*(ix2*ix2' - CovJump - oldMeanPar*oldMeanPar'); j = j+1; end -close(hh); +if exist('OCTAVE_VERSION') || options_.console_mode + diary on; +else + close(hh); +end PostVar = CovJump; PostMean = MeanPar; %% [3 & 4] I tune the scale parameter (with the new covariance matrix) if %% this is the last call to the routine, and I climb the hill (without %% updating the covariance matrix)... if strcmpi(info,'LastCall') - hh = waitbar(0,'Tuning of the scale parameter...'); - set(hh,'Name','Tuning of the scale parameter.') + if exist('OCTAVE_VERSION') || options_.console_mode + diary off; + else + hh = waitbar(0,'Tuning of the scale parameter...'); + set(hh,'Name','Tuning of the scale parameter.') + end j = 1; jj = 1; isux = 0; jsux = 0; test = 0; @@ -209,7 +249,17 @@ if strcmpi(info,'LastCall') jsux = jsux + 1; end% ... otherwise I don't move. prtfrc = j/MaxNumberOfTuningSimulations; - waitbar(prtfrc,hh,sprintf('Acceptation rates: %f [%f]',isux/j,jsux/jj)); + if exist('OCTAVE_VERSION') || options_.console_mode + if mod(j, 10)==0 + if exist('OCTAVE_VERSION') + printf('Tuning of the scale parameter (%f%% done):: Acceptance rates: %f [%f]\r',prtfrc*100,isux/j,jsux/jj); + else + fprintf(' Tuning of the scale parameter (%f \b%% done):: Acceptance rates: %f [%f] \r',prtfrc*100,isux/j,jsux/jj); + end + end + else + waitbar(prtfrc,hh,sprintf('Acceptance rates: %f [%f]',isux/j,jsux/jj)); + end if j/1000 == round(j/1000) test1 = jsux/jj; cfactor = test1/AcceptanceTarget; @@ -225,16 +275,23 @@ if strcmpi(info,'LastCall') j = j+1; jj = jj + 1; end - close(hh); + if exist('OCTAVE_VERSION') || options_.console_mode + diary on; + else + close(hh); + end Scale = iScale; - iScale %% %% Now I climb the hill %% climb = 1; if climb - hh = waitbar(0,' '); - set(hh,'Name','Now I am climbing the hill...') + if exist('OCTAVE_VERSION') || options_.console_mode + diary off; + else + hh = waitbar(0,' '); + set(hh,'Name','Now I am climbing the hill...') + end j = 1; jj = 1; jsux = 0; test = 0; @@ -251,7 +308,17 @@ if strcmpi(info,'LastCall') jsux = jsux + 1; end% otherwise I don't move... prtfrc = j/MaxNumberOfClimbingSimulations; - waitbar(prtfrc,hh,sprintf('%f Jumps / MaxStepSize %f',jsux,sqrt(max(diag(iScale*CovJump))))); + if exist('OCTAVE_VERSION') || options_.console_mode + if mod(j, 10)==0 + if exist('OCTAVE_VERSION') + printf('Now I am climbing the hill (%f%% done):: %f Jumps / MaxStepSize %f\r',prtfrc*100,jsux,sqrt(max(diag(iScale*CovJump)))); + else + fprintf(' Now I am climbing the hill (%f \b%% done):: %f Jumps / MaxStepSize %f \r',prtfrc*100,jsux,sqrt(max(diag(iScale*CovJump)))); + end + end + else + waitbar(prtfrc,hh,sprintf('%f Jumps / MaxStepSize %f',jsux,sqrt(max(diag(iScale*CovJump))))); + end if j/200 == round(j/200) if jsux<=1 test = test+1; @@ -271,7 +338,11 @@ if strcmpi(info,'LastCall') j = j+1; jj = jj + 1; end - close(hh); + if exist('OCTAVE_VERSION') || options_.console_mode + diary on; + else + close(hh); + end end%climb else Scale = iScale; diff --git a/matlab/independent_metropolis_hastings_core.m b/matlab/independent_metropolis_hastings_core.m index e0bc9f41b..21bd59f99 100644 --- a/matlab/independent_metropolis_hastings_core.m +++ b/matlab/independent_metropolis_hastings_core.m @@ -102,8 +102,9 @@ for b = fblck:nblck, x2 = zeros(InitSizeArray(b),npar); logpo2 = zeros(InitSizeArray(b),1); end - if exist('OCTAVE_VERSION') - diary off; + if exist('OCTAVE_VERSION') || options_.console_mode + diary off + disp(' ') elseif whoiam % keyboard; waitbarString = ['Please wait... Metropolis-Hastings (' int2str(b) '/' int2str(options_.mh_nblck) ')...']; @@ -149,9 +150,13 @@ for b = fblck:nblck, logpo2(irun) = ilogpo2(b); end prtfrc = j/nruns(b); - if exist('OCTAVE_VERSION') - if mod(j, 10) == 0, - printf('MH: Computing Metropolis-Hastings (chain %d/%d): %3.f%% done, acception rate: %3.f%%\r', b, nblck, 100 * prtfrc, 100 * isux / j); + if exist('OCTAVE_VERSION') || options_.console_mode + if mod(j, 10) == 0 + if exist('OCTAVE_VERSION') + printf('MH: Computing Metropolis-Hastings (chain %d/%d): %3.f%% done, acception rate: %3.f%%\r', b, nblck, 100 * prtfrc, 100 * isux / j); + else + fprintf(' MH: Computing Metropolis-Hastings (chain %d/%d): %3.f \b%% done, acception rate: %3.f \b%%\r', b, nblck, 100 * prtfrc, 100 * isux / j); + end end if mod(j,50)==0 & whoiam, % keyboard; @@ -212,9 +217,9 @@ for b = fblck:nblck, irun = irun + 1; end% End of the simulations for one mh-block. record.AcceptationRates(b) = isux/j; - if exist('OCTAVE_VERSION') + if exist('OCTAVE_VERSION') || options_.console_mode printf('\n'); - diary on; + diary on elseif ~whoiam close(hh); end diff --git a/matlab/random_walk_metropolis_hastings_core.m b/matlab/random_walk_metropolis_hastings_core.m index a3f811fbc..b16d827d3 100644 --- a/matlab/random_walk_metropolis_hastings_core.m +++ b/matlab/random_walk_metropolis_hastings_core.m @@ -133,8 +133,9 @@ for b = fblck:nblck, x2 = zeros(InitSizeArray(b),npar); logpo2 = zeros(InitSizeArray(b),1); end - if exist('OCTAVE_VERSION') - diary off; + if exist('OCTAVE_VERSION') || options_.console_mode + diary off + disp(' ') elseif whoiam % keyboard; waitbarString = ['Please wait... Metropolis-Hastings (' int2str(b) '/' int2str(options_.mh_nblck) ')...']; @@ -177,11 +178,15 @@ for b = fblck:nblck, logpo2(irun) = ilogpo2(b); end prtfrc = j/nruns(b); - if exist('OCTAVE_VERSION') - if mod(j, 10) == 0, - printf('MH: Computing Metropolis-Hastings (chain %d/%d): %3.f%% done, acception rate: %3.f%%\r', b, nblck, 100 * prtfrc, 100 * isux / j); + if exist('OCTAVE_VERSION') || options_.console_mode + if mod(j, 10) == 0 + if exist('OCTAVE_VERSION') + printf('MH: Computing Metropolis-Hastings (chain %d/%d): %3.f%% done, acception rate: %3.f%%\r', b, nblck, 100 * prtfrc, 100 * isux / j); + else + fprintf(' MH: Computing Metropolis-Hastings (chain %d/%d): %3.f \b%% done, acceptance rate: %3.f \b%%\r', b, nblck, 100 * prtfrc, 100 * isux / j); + end end - if mod(j,50)==0 & whoiam, + if mod(j,50)==0 & whoiam % keyboard; waitbarString = [ '(' int2str(b) '/' int2str(options_.mh_nblck) '), ' sprintf('accept. %3.f%%%%', 100 * isux/j)]; fMessageStatus(prtfrc,whoiam,waitbarString, '', options_.parallel(ThisMatlab), MasterName, DyMo) @@ -240,7 +245,7 @@ for b = fblck:nblck, irun = irun + 1; end% End of the simulations for one mh-block. record.AcceptationRates(b) = isux/j; - if exist('OCTAVE_VERSION') + if exist('OCTAVE_VERSION') || options_.console_mode printf('\n'); diary on; elseif ~whoiam @@ -255,6 +260,4 @@ end% End of the loop over the mh-blocks. myoutput.record = record; myoutput.irun = irun; myoutput.NewFile = NewFile; -myoutput.OutputFileName = OutputFileName; - - +myoutput.OutputFileName = OutputFileName; \ No newline at end of file