Home > . > mh_optimal_bandwidth.m

mh_optimal_bandwidth

PURPOSE ^

% This function gives the optimal bandwidth parameter of a kernel estimator

SYNOPSIS ^

function optimal_bandwidth = mh_optimal_bandwidth(data,n,bandwidth,kernel_function)

DESCRIPTION ^

%  This function gives the optimal bandwidth parameter of a kernel estimator 
%  used to estimate a posterior univariate density from realisations of a 
%  Metropolis-Hastings algorithm.  
% 
%  * M. Skold and G.O. Roberts [2003], "Density estimation for the Metropolis-Hastings algorithm". 
%  * Silverman [1986], "Density estimation for statistics and data analysis". 
%
%  data            :: a vector with n elements.
%  bandwidth       :: a scalar equal to 0,-1 or -2. For a value different from 0,-1 or -2 the
%                     function will return optimal_bandwidth = bandwidth.
%  kernel_function :: 'gaussian','uniform','triangle','epanechnikov',
%                     'quartic','triweight','cosinus'.
%
%  stephane.adjemian@cepremap.cnrs.fr [07-15-2004] <-- [01/16/2004]

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function optimal_bandwidth = mh_optimal_bandwidth(data,n,bandwidth,kernel_function) 
0002 %%  This function gives the optimal bandwidth parameter of a kernel estimator
0003 %%  used to estimate a posterior univariate density from realisations of a
0004 %%  Metropolis-Hastings algorithm.
0005 %%
0006 %%  * M. Skold and G.O. Roberts [2003], "Density estimation for the Metropolis-Hastings algorithm".
0007 %%  * Silverman [1986], "Density estimation for statistics and data analysis".
0008 %%
0009 %%  data            :: a vector with n elements.
0010 %%  bandwidth       :: a scalar equal to 0,-1 or -2. For a value different from 0,-1 or -2 the
0011 %%                     function will return optimal_bandwidth = bandwidth.
0012 %%  kernel_function :: 'gaussian','uniform','triangle','epanechnikov',
0013 %%                     'quartic','triweight','cosinus'.
0014 %%
0015 %%  stephane.adjemian@cepremap.cnrs.fr [07-15-2004] <-- [01/16/2004]
0016 
0017 
0018 %% KERNEL SPECIFICATION...
0019 if strcmpi(kernel_function,'gaussian') 
0020     k    = inline('inv(sqrt(2*pi))*exp(-0.5*x.^2)'); 
0021     k2   = inline('inv(sqrt(2*pi))*(-exp(-0.5*x.^2)+(x.^2).*exp(-0.5*x.^2))'); % second derivate of the gaussian kernel
0022     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))'); % fourth derivate...
0023     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))'); % sixth derivate...
0024     mu02 = inv(2*sqrt(pi)); 
0025     mu21 = 1; 
0026 elseif strcmpi(kernel_function,'uniform') 
0027     k    = inline('0.5*(abs(x) <= 1)'); 
0028     mu02 = 0.5; 
0029     mu21 = 1/3; 
0030 elseif strcmpi(kernel_function,'triangle') 
0031     k    = inline('(1-abs(x)).*(abs(x) <= 1)'); 
0032     mu02 = 2/3; 
0033     mu21 = 1/6; 
0034 elseif strcmpi(kernel_function,'epanechnikov') 
0035     k    = inline('0.75*(1-x.^2).*(abs(x) <= 1)'); 
0036     mu02 = 3/5; 
0037     mu21 = 1/5;     
0038 elseif strcmpi(kernel_function,'quartic') 
0039     k    = inline('0.9375*((1-x.^2).^2).*(abs(x) <= 1)'); 
0040     mu02 = 15/21; 
0041     mu21 = 1/7; 
0042 elseif strcmpi(kernel_function,'triweight') 
0043     k    = inline('1.09375*((1-x.^2).^3).*(abs(x) <= 1)'); 
0044     k2   = inline('(105/4*(1-x.^2).*x.^2-105/16*(1-x.^2).^2).*(abs(x) <= 1)'); 
0045     k4   = inline('(-1575/4*x.^2+315/4).*(abs(x) <= 1)'); 
0046     k6   = inline('(-1575/2).*(abs(x) <= 1)'); 
0047     mu02 = 350/429; 
0048     mu21 = 1/9;     
0049 elseif strcmpi(kernel_function,'cosinus') 
0050     k    = inline('(pi/4)*cos((pi/2)*x).*(abs(x) <= 1)'); 
0051     k2   = inline('(-1/16*cos(pi*x/2)*pi^3).*(abs(x) <= 1)'); 
0052     k4   = inline('(1/64*cos(pi*x/2)*pi^5).*(abs(x) <= 1)'); 
0053     k6   = inline('(-1/256*cos(pi*x/2)*pi^7).*(abs(x) <= 1)'); 
0054     mu02 = (pi^2)/16; 
0055     mu21 = (pi^2-8)/pi^2;     
0056 else
0057     disp('mh_optimal_bandwidth :: ');
0058     error('This kernel function is not yet implemented in Dynare!');        
0059 end     
0060 
0061 
0062 %% OPTIMAL BANDWIDTH PARAMETER....
0063 if bandwidth == 0;  %  Rule of thumb bandwidth parameter (Silverman [1986] corrected by
0064                     %  Skold and Roberts [2003] for Metropolis-Hastings).
0065     sigma = std(data); 
0066     h = 2*sigma*(sqrt(pi)*mu02/(12*(mu21^2)*n))^(1/5); % Silverman's optimal bandwidth parameter.
0067     A = 0; 
0068     for i=1:n; 
0069         j = i; 
0070         while j<= n & data(j,1)==data(i,1); 
0071             j = j+1; 
0072         end;     
0073         A = A + 2*(j-i) - 1; 
0074     end; 
0075     A = A/n; 
0076     h = h*A^(1/5); % correction
0077 elseif bandwidth == -1;     % Adaptation of the Sheather and Jones [1991] plug-in estimation of the optimal bandwidth
0078                             % parameter for metropolis hastings algorithm.
0079     if strcmp(kernel_function,'uniform')      | ... 
0080        strcmp(kernel_function,'triangle')     | ... 
0081        strcmp(kernel_function,'epanechnikov') | ... 
0082        strcmp(kernel_function,'quartic'); 
0083        error('I can''t compute the optimal bandwidth with this kernel... Try the gaussian, triweight or cosinus kernels.'); 
0084     end;         
0085     sigma = std(data); 
0086     A = 0; 
0087     for i=1:n; 
0088         j = i; 
0089         while j<= n & data(j,1)==data(i,1); 
0090             j = j+1; 
0091         end;     
0092         A = A + 2*(j-i) - 1; 
0093     end; 
0094     A = A/n; 
0095     Itilda4 = 8*7*6*5/(((2*sigma)^9)*sqrt(pi)); 
0096     g3      = abs(2*A*k6(0)/(mu21*Itilda4*n))^(1/9); 
0097     Ihat3 = 0; 
0098     for i=1:n; 
0099         Ihat3 = Ihat3 + sum(k6((data(i,1)-data)/g3)); 
0100     end;     
0101     Ihat3 = -Ihat3/((n^2)*g3^7); 
0102     g2      = abs(2*A*k4(0)/(mu21*Ihat3*n))^(1/7); 
0103     Ihat2 = 0; 
0104     for i=1:n; 
0105         Ihat2 = Ihat2 + sum(k4((data(i)-data)/g2)); 
0106     end;     
0107     Ihat2 = Ihat2/((n^2)*g2^5); 
0108     h       = (A*mu02/(n*Ihat2*mu21^2))^(1/5);    % equation (22) in Skold and Roberts [2003] --> h_{MH}
0109 elseif bandwidth == -2;     % Bump killing... We construct local bandwith parameters in order to remove
0110                             % spurious bumps introduced by long rejecting periods.
0111     if strcmp(kernel_function,'uniform')      | ... 
0112        strcmp(kernel_function,'triangle')     | ... 
0113        strcmp(kernel_function,'epanechnikov') | ... 
0114        strcmp(kernel_function,'quartic'); 
0115         error('I can''t compute the optimal bandwidth with this kernel... Try the gaussian, triweight or cosinus kernels.'); 
0116     end;         
0117     sigma = std(data); 
0118     A = 0; 
0119     T = zeros(n,1); 
0120     for i=1:n; 
0121         j = i; 
0122         while j<= n & data(j,1)==data(i,1); 
0123             j = j+1; 
0124         end;     
0125         T(i) = (j-i); 
0126         A = A + 2*T(i) - 1; 
0127     end; 
0128     A = A/n; 
0129     Itilda4 = 8*7*6*5/(((2*sigma)^9)*sqrt(pi)); 
0130     g3      = abs(2*A*k6(0)/(mu21*Itilda4*n))^(1/9); 
0131     Ihat3 = 0; 
0132     for i=1:n; 
0133         Ihat3 = Ihat3 + sum(k6((data(i,1)-data)/g3)); 
0134     end;     
0135     Ihat3 = -Ihat3/((n^2)*g3^7); 
0136     g2      = abs(2*A*k4(0)/(mu21*Ihat3*n))^(1/7); 
0137     Ihat2 = 0; 
0138     for i=1:n; 
0139         Ihat2 = Ihat2 + sum(k4((data(i)-data)/g2)); 
0140     end;     
0141     Ihat2 = Ihat2/((n^2)*g2^5); 
0142     h = ((2*T-1)*mu02/(n*Ihat2*mu21^2)).^(1/5); % Note that h is a column vector (local banwidth parameters).
0143 elseif bandwidth > 0 
0144     h = bandwidth; 
0145 else
0146     disp('mh_optimal_bandwidth :: ');
0147     error('Parameter bandwidth must be a real parameter value or equal to 0,-1 or -2.'); 
0148 end
0149 
0150 optimal_bandwidth = h;

Generated on Fri 16-Jun-2006 09:09:06 by m2html © 2003