From 24a91ab6bc804754a2aa44f586b7c97358fba8c6 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Tue, 25 Aug 2015 09:24:30 +0200 Subject: [PATCH 1/2] Replace inline by anonymous functions Inline will be removed from future Matlab versions --- matlab/gsa/log_trans_.m | 2 +- matlab/mh_optimal_bandwidth.m | 39 +++++++++++++++++------------------ 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/matlab/gsa/log_trans_.m b/matlab/gsa/log_trans_.m index 2f48c7156..da34bd480 100644 --- a/matlab/gsa/log_trans_.m +++ b/matlab/gsa/log_trans_.m @@ -26,7 +26,7 @@ end if nargin==1, xdir0=''; end -f=inline('gsa_skewness(log(y+lam))','lam','y'); +f=@(lam,y)gsa_skewness(log(y+lam)); isig=1; if ~(max(y0)<0 | min(y0)>0) if gsa_skewness(y0)<0, diff --git a/matlab/mh_optimal_bandwidth.m b/matlab/mh_optimal_bandwidth.m index 78128c62c..7355fddbd 100644 --- a/matlab/mh_optimal_bandwidth.m +++ b/matlab/mh_optimal_bandwidth.m @@ -21,10 +21,10 @@ function optimal_bandwidth = mh_optimal_bandwidth(data,number_of_draws,bandwidth % none. % % REFERENCES: -% [1] M. Skold and G.O. Roberts [2003], "Density estimation for the Metropolis-Hastings algorithm". +% [1] M. Skoeld and G.O. Roberts [2003], "Density estimation for the Metropolis-Hastings algorithm". % [2] Silverman [1986], "Density estimation for statistics and data analysis". -% Copyright (C) 2004-2011 Dynare Team +% Copyright (C) 2004-2015 Dynare Team % % This file is part of Dynare. % @@ -44,44 +44,43 @@ function optimal_bandwidth = mh_optimal_bandwidth(data,number_of_draws,bandwidth %% Kernel specifications. if strcmpi(kernel_function,'gaussian') % Kernel definition - k = inline('inv(sqrt(2*pi))*exp(-0.5*x.^2)'); + k = @(x)inv(sqrt(2*pi))*exp(-0.5*x.^2); % Second derivate of the kernel function - k2 = inline('inv(sqrt(2*pi))*(-exp(-0.5*x.^2)+(x.^2).*exp(-0.5*x.^2))'); + k2 = @(x)inv(sqrt(2*pi))*(-exp(-0.5*x.^2)+(x.^2).*exp(-0.5*x.^2)); % Fourth derivate of the kernel function - k4 = inline('inv(sqrt(2*pi))*(3*exp(-0.5*x.^2)-6*(x.^2).*exp(-0.5*x.^2)+(x.^4).*exp(-0.5*x.^2))'); + k4 = @(x)inv(sqrt(2*pi))*(3*exp(-0.5*x.^2)-6*(x.^2).*exp(-0.5*x.^2)+(x.^4).*exp(-0.5*x.^2)); % Sixth derivate of the kernel function - k6 = inline(['inv(sqrt(2*pi))*(-15*exp(-0.5*x.^2)+45*(x.^2).*exp(-' ... - '0.5*x.^2)-15*(x.^4).*exp(-0.5*x.^2)+(x.^6).*exp(-0.5*x.^2))']); + k6 = @(x)inv(sqrt(2*pi))*(-15*exp(-0.5*x.^2)+45*(x.^2).*exp(-0.5*x.^2)-15*(x.^4).*exp(-0.5*x.^2)+(x.^6).*exp(-0.5*x.^2)); mu02 = inv(2*sqrt(pi)); mu21 = 1; elseif strcmpi(kernel_function,'uniform') - k = inline('0.5*(abs(x) <= 1)'); + k = @(x)0.5*(abs(x) <= 1); mu02 = 0.5; mu21 = 1/3; elseif strcmpi(kernel_function,'triangle') - k = inline('(1-abs(x)).*(abs(x) <= 1)'); + k = @(x)(1-abs(x)).*(abs(x) <= 1); mu02 = 2/3; mu21 = 1/6; elseif strcmpi(kernel_function,'epanechnikov') - k = inline('0.75*(1-x.^2).*(abs(x) <= 1)'); + k = @(x)0.75*(1-x.^2).*(abs(x) <= 1); mu02 = 3/5; mu21 = 1/5; elseif strcmpi(kernel_function,'quartic') - k = inline('0.9375*((1-x.^2).^2).*(abs(x) <= 1)'); + k = @(x)0.9375*((1-x.^2).^2).*(abs(x) <= 1); mu02 = 15/21; mu21 = 1/7; elseif strcmpi(kernel_function,'triweight') - k = inline('1.09375*((1-x.^2).^3).*(abs(x) <= 1)'); - k2 = inline('(105/4*(1-x.^2).*x.^2-105/16*(1-x.^2).^2).*(abs(x) <= 1)'); - k4 = inline('(-1575/4*x.^2+315/4).*(abs(x) <= 1)'); - k6 = inline('(-1575/2).*(abs(x) <= 1)'); + k = @(x)1.09375*((1-x.^2).^3).*(abs(x) <= 1); + k2 = @(x)(105/4*(1-x.^2).*x.^2-105/16*(1-x.^2).^2).*(abs(x) <= 1); + k4 = @(x)(-1575/4*x.^2+315/4).*(abs(x) <= 1); + k6 = @(x)(-1575/2).*(abs(x) <= 1); mu02 = 350/429; mu21 = 1/9; elseif strcmpi(kernel_function,'cosinus') - k = inline('(pi/4)*cos((pi/2)*x).*(abs(x) <= 1)'); - k2 = inline('(-1/16*cos(pi*x/2)*pi^3).*(abs(x) <= 1)'); - k4 = inline('(1/64*cos(pi*x/2)*pi^5).*(abs(x) <= 1)'); - k6 = inline('(-1/256*cos(pi*x/2)*pi^7).*(abs(x) <= 1)'); + k = @(x)(pi/4)*cos((pi/2)*x).*(abs(x) <= 1); + k2 = @(x)(-1/16*cos(pi*x/2)*pi^3).*(abs(x) <= 1); + k4 = @(x)(1/64*cos(pi*x/2)*pi^5).*(abs(x) <= 1); + k6 = @(x)(-1/256*cos(pi*x/2)*pi^7).*(abs(x) <= 1); mu02 = (pi^2)/16; mu21 = (pi^2-8)/pi^2; else @@ -163,7 +162,7 @@ else end optimal_bandwidth = h; -return, +return function correction = correction_for_repeated_draws(draws,n) From 26437e1c329903354dd16ae411980fb67ead73c0 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Tue, 25 Aug 2015 09:45:39 +0200 Subject: [PATCH 2/2] Replace eval by indirect addressing of fields. --- matlab/GetPosteriorParametersStatistics.m | 24 +++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/matlab/GetPosteriorParametersStatistics.m b/matlab/GetPosteriorParametersStatistics.m index 84e9ee1a9..f9bfedbd0 100644 --- a/matlab/GetPosteriorParametersStatistics.m +++ b/matlab/GetPosteriorParametersStatistics.m @@ -377,18 +377,18 @@ fclose(fid); function oo = Filloo(oo,name,type,postmean,hpdinterval,postmedian,postvar,postdecile,density) -eval(['oo.posterior_mean.' type '.' name ' = postmean;']); -eval(['oo.posterior_hpdinf.' type '.' name ' = hpdinterval(1);']); -eval(['oo.posterior_hpdsup.' type '.' name ' = hpdinterval(2);']); -eval(['oo.posterior_median.' type '.' name ' = postmedian;']); -eval(['oo.posterior_variance.' type '.' name ' = postvar;']); -eval(['oo.posterior_std.' type '.' name ' = sqrt(postvar);']); -eval(['oo.posterior_deciles.' type '.' name ' = postdecile;']); -eval(['oo.posterior_density.' type '.' name ' = density;']); +oo.posterior_mean.(type).(name) = postmean; +oo.posterior_hpdinf.(type).(name) = hpdinterval(1); +oo.posterior_hpdsup.(type).(name) = hpdinterval(2); +oo.posterior_median.(type).(name) = postmedian; +oo.posterior_variance.(type).(name) = postvar; +oo.posterior_std.(type).(name) = sqrt(postvar); +oo.posterior_deciles.(type).(name) = postdecile; +oo.posterior_density.(type).(name) = density; function [post_mean,hpd_interval,post_var] = Extractoo(oo,name,type) hpd_interval = zeros(2,1); -eval(['post_mean = oo.posterior_mean.' type '.' name ';']); -eval(['hpd_interval(1) = oo.posterior_hpdinf.' type '.' name ';']); -eval(['hpd_interval(2) = oo.posterior_hpdsup.' type '.' name ';']); -eval(['post_var = oo.posterior_variance.' type '.' name ';']); \ No newline at end of file +post_mean = oo.posterior_mean.(type).(name); +hpd_interval(1) = oo.posterior_hpdinf.(type).(name); +hpd_interval(2) = oo.posterior_hpdsup.(type).(name); +post_var = oo.posterior_variance.(type).(name); \ No newline at end of file