Diff of /inst/kernel_density.m [508d22] .. [eb5224] Maximize Restore

  Switch to unified view

a/inst/kernel_density.m b/inst/kernel_density.m
1
# Copyright (C) 2006 Michael Creel <michael.creel@uab.es>
1
## Copyright (C) 2006 Michael Creel <michael.creel@uab.es>
2
#
2
##
3
# This program is free software; you can redistribute it and/or modify
3
## This program is free software; you can redistribute it and/or modify it under
4
# it under the terms of the GNU General Public License as published by
4
## the terms of the GNU General Public License as published by the Free Software
5
# the Free Software Foundation; either version 2 of the License, or
5
## Foundation; either version 3 of the License, or (at your option) any later
6
# (at your option) any later version.
6
## version.
7
#
7
##
8
# This program is distributed in the hope that it will be useful,
8
## This program is distributed in the hope that it will be useful, but WITHOUT
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11
# GNU General Public License for more details.
11
## details.
12
#
12
##
13
# You should have received a copy of the GNU General Public License
13
## You should have received a copy of the GNU General Public License along with
14
# along with this program; If not, see <http://www.gnu.org/licenses/>.
14
## this program; if not, see <http://www.gnu.org/licenses/>.
15
15
16
# kernel_density: multivariate kernel density estimator
16
## kernel_density: multivariate kernel density estimator
17
#
17
##
18
# usage:
18
## usage:
19
#     dens = kernel_density(eval_points, data, bandwidth)
19
##       dens = kernel_density(eval_points, data, bandwidth)
20
#
20
##
21
# inputs:
21
## inputs:
22
# eval_points: PxK matrix of points at which to calculate the density
22
##       eval_points: PxK matrix of points at which to calculate the density
23
#     data: NxK matrix of data points
23
##       data: NxK matrix of data points
24
# bandwidth: positive scalar, the smoothing parameter. The fit
24
##       bandwidth: positive scalar, the smoothing parameter. The fit
25
#         is more smooth as the bandwidth increases.
25
##               is more smooth as the bandwidth increases.
26
# kernel (optional): string. Name of the kernel function. Default is
26
##       kernel (optional): string. Name of the kernel function. Default is
27
#     Gaussian kernel.
27
##               Gaussian kernel.
28
# prewhiten bool (optional): default false. If true, rotate data
28
##       prewhiten bool (optional): default false. If true, rotate data
29
#         using Choleski decomposition of inverse of covariance,
29
##               using Choleski decomposition of inverse of covariance,
30
#     to approximate independence after the transformation, which
30
##               to approximate independence after the transformation, which
31
#     makes a product kernel a reasonable choice.
31
##               makes a product kernel a reasonable choice.
32
# do_cv: bool (optional). default false. If true, calculate leave-1-out
32
##       do_cv: bool (optional). default false. If true, calculate leave-1-out
33
#      density for cross validation
33
##                density for cross validation
34
# computenodes: int (optional, default 0).
34
##       computenodes: int (optional, default 0).
35
#     Number of compute nodes for parallel evaluation
35
##               Number of compute nodes for parallel evaluation
36
# debug: bool (optional, default false). show results on compute nodes if doing
36
##       debug: bool (optional, default false). show results on compute nodes if doing
37
#     a parallel run
37
##               a parallel run
38
# outputs:
38
## outputs:
39
# dens: Px1 vector: the fitted density value at each of the P evaluation points.
39
##       dens: Px1 vector: the fitted density value at each of the P evaluation points.
40
#
40
##
41
# References:
41
## References:
42
# Wand, M.P. and Jones, M.C. (1995), 'Kernel smoothing'.
42
## Wand, M.P. and Jones, M.C. (1995), 'Kernel smoothing'.
43
# http://www.xplore-stat.de/ebooks/scripts/spm/html/spmhtmlframe73.html
43
## http://www.xplore-stat.de/ebooks/scripts/spm/html/spmhtmlframe73.html
44
44
45
function z = kernel_density(eval_points, data, bandwidth, kernel, prewhiten, do_cv, computenodes, debug)
45
function z = kernel_density(eval_points, data, bandwidth, kernel, prewhiten, do_cv, computenodes, debug)
46
46
47
    if nargin < 2; error("kernel_density: at least 2 arguments are required"); endif
47
    if nargin < 2; error("kernel_density: at least 2 arguments are required"); endif
48
48