File: kernel_optimal_bandwidth.m

package info (click to toggle)
octave-econometrics 1%3A1.1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 296 kB
  • sloc: cpp: 32; makefile: 9
file content (58 lines) | stat: -rw-r--r-- 2,047 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
## Copyright (C) 2007 Michael Creel <michael.creel@uab.es>
##
## 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 3 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 <http://www.gnu.org/licenses/>.

## 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