]> Creatis software - CreaPhase.git/blob - octave_packages/econometrics-1.0.8/kernel_regression_nodes.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / econometrics-1.0.8 / kernel_regression_nodes.m
1 # Copyright (C) 2006, 2007  Michael Creel <michael.creel@uab.es>
2 # under the terms of the GNU General Public License.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; If not, see <http://www.gnu.org/licenses/>.
16
17 # kernel_regression_nodes: for internal use by kernel_regression - does calculations on nodes
18
19 function z = kernel_regression_nodes(eval_points, data, do_cv, kernel, points_per_node, nslaves, debug)
20
21         if (nslaves > 0)
22                 global NEWORLD
23                 [info, myrank] = MPI_Comm_rank(NEWORLD);
24         else myrank = 0; # if not parallel then do all on master node
25         endif
26
27         if myrank == 0 # Do this if I'm master
28                 startblock = nslaves*points_per_node + 1;
29                 endblock = rows(eval_points);
30         else    # this is for the slaves
31                 startblock = myrank*points_per_node - points_per_node + 1;
32                 endblock = myrank*points_per_node;
33         endif
34
35         # the block of eval_points this node does
36         myeval = eval_points(startblock:endblock,:);
37         nn = rows(myeval);
38         n = rows(data);
39
40         y = data(:,1);
41         data = data(:,2:columns(data));
42         W = __kernel_weights(data, myeval, kernel);
43
44         # drop own weight for CV
45         if (do_cv) W = W - diag(diag(W)); endif
46
47         den = sum(W,2);
48         if !all(den)
49                 warning("kernel_regression: some evaluation points have no neighbors - increase the bandwidth");
50                 den = den + eps; # avoid divide by zero
51         endif
52
53         W = W ./ (repmat(den,1,n));
54         z = W*y;
55
56         if debug
57                 printf("z on node %d: \n", myrank);
58                 z'
59         endif
60 endfunction
61
62