0001 function optimal_bandwidth = mh_optimal_bandwidth(data,number_of_draws,bandwidth,kernel_function)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 if strcmpi(kernel_function,'gaussian')
0046
0047 k = inline('inv(sqrt(2*pi))*exp(-0.5*x.^2)');
0048
0049 k2 = inline('inv(sqrt(2*pi))*(-exp(-0.5*x.^2)+(x.^2).*exp(-0.5*x.^2))');
0050
0051 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))');
0052
0053 k6 = inline(['inv(sqrt(2*pi))*(-15*exp(-0.5*x.^2)+45*(x.^2).*exp(-' ...
0054 '0.5*x.^2)-15*(x.^4).*exp(-0.5*x.^2)+(x.^6).*exp(-0.5*x.^2))']);
0055 mu02 = inv(2*sqrt(pi));
0056 mu21 = 1;
0057 elseif strcmpi(kernel_function,'uniform')
0058 k = inline('0.5*(abs(x) <= 1)');
0059 mu02 = 0.5;
0060 mu21 = 1/3;
0061 elseif strcmpi(kernel_function,'triangle')
0062 k = inline('(1-abs(x)).*(abs(x) <= 1)');
0063 mu02 = 2/3;
0064 mu21 = 1/6;
0065 elseif strcmpi(kernel_function,'epanechnikov')
0066 k = inline('0.75*(1-x.^2).*(abs(x) <= 1)');
0067 mu02 = 3/5;
0068 mu21 = 1/5;
0069 elseif strcmpi(kernel_function,'quartic')
0070 k = inline('0.9375*((1-x.^2).^2).*(abs(x) <= 1)');
0071 mu02 = 15/21;
0072 mu21 = 1/7;
0073 elseif strcmpi(kernel_function,'triweight')
0074 k = inline('1.09375*((1-x.^2).^3).*(abs(x) <= 1)');
0075 k2 = inline('(105/4*(1-x.^2).*x.^2-105/16*(1-x.^2).^2).*(abs(x) <= 1)');
0076 k4 = inline('(-1575/4*x.^2+315/4).*(abs(x) <= 1)');
0077 k6 = inline('(-1575/2).*(abs(x) <= 1)');
0078 mu02 = 350/429;
0079 mu21 = 1/9;
0080 elseif strcmpi(kernel_function,'cosinus')
0081 k = inline('(pi/4)*cos((pi/2)*x).*(abs(x) <= 1)');
0082 k2 = inline('(-1/16*cos(pi*x/2)*pi^3).*(abs(x) <= 1)');
0083 k4 = inline('(1/64*cos(pi*x/2)*pi^5).*(abs(x) <= 1)');
0084 k6 = inline('(-1/256*cos(pi*x/2)*pi^7).*(abs(x) <= 1)');
0085 mu02 = (pi^2)/16;
0086 mu21 = (pi^2-8)/pi^2;
0087 else
0088 disp('mh_optimal_bandwidth:: ');
0089 error('This kernel function is not yet implemented in Dynare!');
0090 end
0091
0092
0093
0094 if bandwidth==0 || bandwidth==-1
0095 correction = correction_for_repeated_draws(data,number_of_draws);
0096 else
0097 correction = 0;
0098 end
0099
0100 sigma = std(data);
0101
0102 if bandwidth == 0;
0103 h = 2*sigma*(sqrt(pi)*mu02/(12*(mu21^2)*number_of_draws))^(1/5);
0104 h = h*correction^(1/5);
0105 elseif bandwidth == -1;
0106 if strcmp(kernel_function,'uniform') || ...
0107 strcmp(kernel_function,'triangle') || ...
0108 strcmp(kernel_function,'epanechnikov') || ...
0109 strcmp(kernel_function,'quartic')
0110 error(['I can''t compute the optimal bandwidth with this kernel...' ...
0111 'Try the gaussian, triweight or cosinus kernels.']);
0112 end
0113 Itilda4 = 8*7*6*5/(((2*sigma)^9)*sqrt(pi));
0114 g3 = abs(2*correction*k6(0)/(mu21*Itilda4*number_of_draws))^(1/9);
0115 Ihat3 = 0;
0116 for i=1:number_of_draws
0117 Ihat3 = Ihat3 + sum(k6((data(i,1)-data)/g3));
0118 end
0119 Ihat3 = - Ihat3/((number_of_draws^2)*g3^7);
0120 g2 = abs(2*correction*k4(0)/(mu21*Ihat3*number_of_draws))^(1/7);
0121 Ihat2 = 0;
0122 for i=1:number_of_draws
0123 Ihat2 = Ihat2 + sum(k4((data(i)-data)/g2));
0124 end
0125 Ihat2 = Ihat2/((number_of_draws^2)*g2^5);
0126 h = (correction*mu02/(number_of_draws*Ihat2*mu21^2))^(1/5);
0127 elseif bandwidth == -2;
0128
0129 if strcmp(kernel_function,'uniform') || ...
0130 strcmp(kernel_function,'triangle') || ...
0131 strcmp(kernel_function,'epanechnikov') || ...
0132 strcmp(kernel_function,'quartic')
0133 error(['I can''t compute the optimal bandwidth with this kernel...' ...
0134 'Try the gaussian, triweight or cosinus kernels.']);
0135 end
0136 T = zeros(n,1);
0137 for i=1:n
0138 j = i;
0139 while j<= n && (data(j,1)-data(i,1))<2*eps;
0140 j = j+1;
0141 end
0142 T(i) = (j-i);
0143 correction = correction + 2*T(i) - 1;
0144 end
0145 correction = correction/number_of_draws;
0146 Itilda4 = 8*7*6*5/(((2*sigma)^9)*sqrt(pi));
0147 g3 = abs(2*correction*k6(0)/(mu21*Itilda4*correction))^(1/9);
0148 Ihat3 = 0;
0149 for i=1:number_of_draws
0150 Ihat3 = Ihat3 + sum(k6((data(i,1)-data)/g3));
0151 end
0152 Ihat3 = -Ihat3/((n^2)*g3^7);
0153 g2 = abs(2*correction*k4(0)/(mu21*Ihat3*n))^(1/7);
0154 Ihat2 = 0;
0155 for i=1:number_of_draws;
0156 Ihat2 = Ihat2 + sum(k4((data(i)-data)/g2));
0157 end
0158 Ihat2 = Ihat2/((number_of_draws^2)*g2^5);
0159 h = ((2*T-1)*mu02/(number_of_draws*Ihat2*mu21^2)).^(1/5);
0160 else
0161 disp('mh_optimal_bandwidth:: ');
0162 error('Parameter bandwidth must be equal to 0, -1 or -2.');
0163 end
0164
0165 optimal_bandwidth = h;
0166 return,
0167
0168
0169 function correction = correction_for_repeated_draws(draws,n)
0170 correction = 0;
0171 for i=1:n
0172 j = i;
0173 while j<=n && ( draws(j,1) - draws(i,1) )<2*eps;
0174 j = j+1;
0175 end
0176 correction = correction + 2*(j-i) - 1;
0177 end
0178 correction = correction/n;