diff --git a/matlab/DsgeLikelihood_hh.m b/matlab/DsgeLikelihood_hh.m index e68370f11..9f6cbd2ca 100644 --- a/matlab/DsgeLikelihood_hh.m +++ b/matlab/DsgeLikelihood_hh.m @@ -266,4 +266,6 @@ end lnprior = priordens(xparam1,bayestopt_.pshape,bayestopt_.p6,bayestopt_.p7,bayestopt_.p3,bayestopt_.p4); fval = (likelihood-lnprior); options_.kalman_algo = kalman_algo; -llik=[-lnprior; lik(start:end)]; +lik=lik(start:end,:); +llik=[-lnprior; lik(:)]; +% llik=[-lnprior; lik(start:end)]; diff --git a/matlab/ReshapeMatFiles.m b/matlab/ReshapeMatFiles.m index d15371dfb..ad44331c7 100644 --- a/matlab/ReshapeMatFiles.m +++ b/matlab/ReshapeMatFiles.m @@ -115,7 +115,7 @@ switch TYPEarray eval(['STOCK_' CAPtype ' = zeros(NumberOfPeriodsPerTYPEfiles,TYPEsize(2),TYPEsize(3),B);']) for f2 = 1:NumberOfTYPEfiles load([MhDirectoryName M_.fname '_' type int2str(f2) '.mat']); - eval(['STOCK_' CAPtype '(:,:,:,idx+1:idx+size(stock_' type ',4))=stock_' ... + eval(['STOCK_' CAPtype '(:,:,1:+size(stock_' type ',3),idx+1:idx+size(stock_' type ',4))=stock_' ... type '(jdx+1:jdx+NumberOfPeriodsPerTYPEfiles,:,:,:);']) eval(['idx = idx + size(stock_' type ',4);']) end diff --git a/matlab/kalman/likelihood/univariate_diffuse_kalman_filter.m b/matlab/kalman/likelihood/univariate_diffuse_kalman_filter.m index 03d74c1f3..a7fac2699 100644 --- a/matlab/kalman/likelihood/univariate_diffuse_kalman_filter.m +++ b/matlab/kalman/likelihood/univariate_diffuse_kalman_filter.m @@ -1,4 +1,4 @@ -function [LIK, lik] = univariate_diffuse_kalman_filter(T,R,Q,H,Pinf,Pstar,Y,start,Z,kalman_tol,riccati_tol,data_index,number_of_observations,no_more_missing_observations) +function [LIK, llik] = univariate_diffuse_kalman_filter(T,R,Q,H,Pinf,Pstar,Y,start,Z,kalman_tol,riccati_tol,data_index,number_of_observations,no_more_missing_observations) % Computes the likelihood of a stationnary state space model (univariate approach). % % INPUTS @@ -52,6 +52,7 @@ a = zeros(mm,1); QQ = R*Q*transpose(R); t = 0; lik = zeros(smpl,1); +llik=zeros(smpl,pp); notsteady = 1; crit = 1.e-6; newRank = rank(Pinf,crit); @@ -74,10 +75,12 @@ while newRank && (tkalman_tol - lik(t) = lik(t) + log(Fstar) + prediction_error* ... + llik(t,i) = log(Fstar) + prediction_error* ... prediction_error/Fstar + l2pi; + lik(t) = lik(t) + llik(t,i); a = a + Kstar*(prediction_error/Fstar); Pstar = Pstar - Kstar*(Kstar'/Fstar); end @@ -116,8 +119,9 @@ while notsteady && (t kalman_tol a = a + Ki*(prediction_error/Fi); Pstar = Pstar - Ki*(Ki'/Fi); - lik(t) = lik(t) + log(Fi) + prediction_error*prediction_error/Fi ... + llik(t,i) = log(Fi) + prediction_error*prediction_error/Fi ... + l2pi; + lik(t) = lik(t) + llik(t,i); end end a = T*a; @@ -138,13 +142,15 @@ while t < smpl Ki = Pstar*Zi'; a = a + Ki*prediction_error/Fi; Pstar = Pstar - Ki*Ki'/Fi; - lik(t) = lik(t) + log(Fi) + prediction_error*prediction_error/Fi ... + llik(t,i) = log(Fi) + prediction_error*prediction_error/Fi ... + l2pi; + lik(t) = lik(t) + llik(t,i); end end a = T*a; end lik = lik/2; +llik = llik/2; -LIK = sum(lik(start:end)); \ No newline at end of file +LIK = sum(lik(start:end)); diff --git a/matlab/kalman/likelihood/univariate_diffuse_kalman_filter_corr.m b/matlab/kalman/likelihood/univariate_diffuse_kalman_filter_corr.m index 7c403a603..21230302c 100644 --- a/matlab/kalman/likelihood/univariate_diffuse_kalman_filter_corr.m +++ b/matlab/kalman/likelihood/univariate_diffuse_kalman_filter_corr.m @@ -1,4 +1,4 @@ -function [LIK, lik] = univariate_diffuse_kalman_filter_corr(T,R,Q,H,Pinf,Pstar,Y,start,Z,kalman_tol,riccati_tol,data_index,number_of_observations,no_more_missing_observations) +function [LIK, llik] = univariate_diffuse_kalman_filter_corr(T,R,Q,H,Pinf,Pstar,Y,start,Z,kalman_tol,riccati_tol,data_index,number_of_observations,no_more_missing_observations) % Computes the likelihood of a stationnary state space model (univariate % approach with correlated errors). % @@ -52,6 +52,7 @@ a = zeros(mm,1); QQ = R*Q*transpose(R); t = 0; lik = zeros(smpl,1); +llik = zeros(smpl,pp); notsteady = 1; crit = 1.e-6; newRank = rank(Pinf,crit); @@ -104,7 +105,8 @@ while newRank && (tkalman_tol)==0 & rank(Pinf,crit)==0); @@ -125,8 +127,9 @@ while newRank && (tkalman_tol - lik(t) = lik(t) + log(Fstar) + prediction_error* ... + llik(t,i) = log(Fstar) + prediction_error* ... prediction_error/Fstar + l2pi; + lik(t) = lik(t) + llik(t,i); a = a + Kstar*prediction_error/Fstar; Pstar = Pstar - Kstar*Kstar'/Fstar; end @@ -165,8 +168,9 @@ while notsteady && (t kalman_tol - lik(t) = lik(t) + log(Fi) + prediction_error*prediction_error/Fi ... + llik(t,i) = log(Fi) + prediction_error*prediction_error/Fi ... + l2pi; + lik(t) = lik(t) + llik(t,i); Ki = sum(PP(:,[MF(i) mm+i]),2)/Fi; a = a + Ki*prediction_error; PP = PP - (Ki*Fi)*transpose(Ki); @@ -111,8 +113,9 @@ while t < smpl Ki = ( PPPP(:,mf(i)) + PPPP(:,mm+i) )/Fi; a = a + Ki*prediction_error; PPPP = PPPP - (Fi*Ki)*transpose(Ki); - lik(t) = lik(t) + log(Fi) + prediction_error*prediction_error/Fi ... + llik(t,i) = log(Fi) + prediction_error*prediction_error/Fi ... + l2pi; + lik(t) = lik(t) + llik(t,i); end end a(1:mm) = T*a(1:mm); @@ -120,5 +123,6 @@ while t < smpl end lik = lik/2; +llik = llik/2; LIK = sum(lik(start:end)); \ No newline at end of file diff --git a/matlab/mr_gstep.m b/matlab/mr_gstep.m index 56d178d65..2f5196a90 100644 --- a/matlab/mr_gstep.m +++ b/matlab/mr_gstep.m @@ -27,7 +27,8 @@ n=size(x,1); if init, gstep_ = options_.gstep; - h1=max(abs(x),sqrt(gstep_)*ones(n,1))*eps^(1/4); +% h1=max(abs(x),sqrt(gstep_)*ones(n,1))*eps^(1/4); + h1=options_.gradient_epsilon*ones(n,1); return end if nargin<4, @@ -71,7 +72,8 @@ while i(2*htol)) & icount<10 & ic==0, +% while (abs(dx(it))<0.5*htol | abs(dx(it))>(2*htol)) & icount<10 & ic==0, + while (abs(dx(it))<0.5*htol) & icount<10 & ic==0, %while abs(dx(it))<0.5*htol & icount< 10 & ic==0, icount=icount+1; if abs(dx(it)) ~= 0, @@ -86,10 +88,10 @@ while i(2*htol), - h1(i)= htol/abs(dx(it))*h1(i); - xh1(i)=x(i)+h1(i); - end +% if abs(dx(it))>(2*htol), +% h1(i)= htol/abs(dx(it))*h1(i); +% xh1(i)=x(i)+h1(i); +% end try fx = feval(func,xh1,varargin{:}); catch diff --git a/matlab/mr_hessian.m b/matlab/mr_hessian.m index a652b3e86..2c6e4b731 100644 --- a/matlab/mr_hessian.m +++ b/matlab/mr_hessian.m @@ -50,7 +50,8 @@ if init, htol = 1.e-4; %h1=max(abs(x),gstep_*ones(n,1))*eps^(1/3); %h1=max(abs(x),sqrt(gstep_)*ones(n,1))*eps^(1/6); - h1=max(abs(x),sqrt(gstep_)*ones(n,1))*eps^(1/4); +% h1=max(abs(x),sqrt(gstep_)*ones(n,1))*eps^(1/4); + h1=options_.gradient_epsilon*ones(n,1); return, end func = str2func(func); @@ -102,7 +103,8 @@ while i(2*htol)) & icount<10 & ic==0, +% while (abs(dx(it))<0.5*htol | abs(dx(it))>(2*htol)) & icount<10 & ic==0, + while (abs(dx(it))<0.5*htol) & icount<10 & ic==0, %while abs(dx(it))<0.5*htol & icount< 10 & ic==0, icount=icount+1; %if abs(dx(it)) ~= 0, @@ -127,21 +129,21 @@ while i(2*htol), - h1(i)= htol/abs(dx(it))*h1(i); - xh1(i)=x(i)+h1(i); - try - [fx, ffx]=feval(func,xh1,varargin{:}); - catch - fx=1.e8; - end - while (fx-f0)==0, - h1(i)= h1(i)*2; - xh1(i)=x(i)+h1(i); - [fx, ffx]=feval(func,xh1,varargin{:}); - ic=1; - end - end +% if abs(dx(it))>(2*htol), +% h1(i)= htol/abs(dx(it))*h1(i); +% xh1(i)=x(i)+h1(i); +% try +% [fx, ffx]=feval(func,xh1,varargin{:}); +% catch +% fx=1.e8; +% end +% while (fx-f0)==0, +% h1(i)= h1(i)*2; +% xh1(i)=x(i)+h1(i); +% [fx, ffx]=feval(func,xh1,varargin{:}); +% ic=1; +% end +% end it=it+1; dx(it)=(fx-f0); h0(it)=h1(i); @@ -171,7 +173,7 @@ while igtol & check==0 & jit1, %(fval0(icount)-fval), @@ -116,7 +117,7 @@ while norm(gg)>gtol & check==0 & jitgtol & check==0 & jitgtol & check==0 & jitgtol & check==0 & jitgtol & check==0 & jit