X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Feconometrics-1.0.8%2Fkernel_optimal_bandwidth.m;fp=octave_packages%2Feconometrics-1.0.8%2Fkernel_optimal_bandwidth.m;h=29d2da9bec85a9aad5208f2e2437ed924015e362;hp=0000000000000000000000000000000000000000;hb=c880e8788dfc484bf23ce13fa2787f2c6bca4863;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/econometrics-1.0.8/kernel_optimal_bandwidth.m b/octave_packages/econometrics-1.0.8/kernel_optimal_bandwidth.m new file mode 100644 index 0000000..29d2da9 --- /dev/null +++ b/octave_packages/econometrics-1.0.8/kernel_optimal_bandwidth.m @@ -0,0 +1,58 @@ +# Copyright (C) 2007 Michael Creel +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; If not, see . + +# kernel_optimal_bandwidth: find optimal bandwith doing leave-one-out cross validation +# inputs: +# * data: data matrix +# * depvar: column vector or empty (""). +# If empty, do kernel density, orherwise, kernel regression +# * kernel (optional, string) the kernel function to use +# output: +# * h: the optimal bandwidth + +function bandwidth = kernel_optimal_bandwidth(data, depvar, kernel) + + if (nargin < 2) error("kernel_optimal_bandwidth: 3 arguments required"); endif + if (nargin < 3) kernel = "__kernel_epanechnikov"; endif + + do_density = false; + if isempty(depvar) do_density = true; endif; + + # SA controls + ub = 3; + lb = -5; + nt = 1; + ns = 1; + rt = 0.05; + maxevals = 50; + neps = 5; + functol = 1e-2; + paramtol = 1e-3; + verbosity = 0; + minarg = 1; + sa_control = { lb, ub, nt, ns, rt, maxevals, neps, functol, paramtol, verbosity, 1}; + + # bfgs controls + bfgs_control = {10}; + + if do_density + bandwidth = samin("kernel_density_cvscore", {1, data, kernel}, sa_control); + bandwidth = bfgsmin("kernel_density_cvscore", {bandwidth, data, kernel}, bfgs_control); + else + bandwidth = samin("kernel_regression_cvscore", {1, data, depvar, kernel}, sa_control); + bandwidth = bfgsmin("kernel_regression_cvscore", {bandwidth, data, depvar, kernel}, bfgs_control); + endif + bandwidth = exp(bandwidth); +endfunction