]> Creatis software - CreaPhase.git/blob - octave_packages/data-smoothing-1.3.0/rgdtsmcorewrap.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / data-smoothing-1.3.0 / rgdtsmcorewrap.m
1 ## Copyright (C) 2008 Jonathan Stickel <jonathan.stickel@nrel.gov>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {@var{cve} =} rgdtsmcorewrap (@var{log10lambda}, @var{x}, @var{y}, @var{d}, @var{mincell}, @var{options})
18 ## @deftypefnx {Function File} {@var{stdevdif} =} rgdtsmcorewrap (@var{log10lambda}, @var{x}, @var{y}, @var{d}, @var{mincell}, @var{options})
19 ##
20 ##  Wrapper function for rgdtsmcore in order to minimize over
21 ##  @var{lambda} w.r.t. cross-validation error OR the squared difference
22 ##  between the standard deviation of (@var{y}-@var{yhat}) and the given
23 ##  standard deviation.  This function is called from regdatasmooth.
24 ## @seealso{regdatasmooth}
25 ## @end deftypefn
26
27 function out = rgdtsmcorewrap (log10lambda, x, y, d, mincell, varargin)
28
29   if (nargin < 5)
30     print_usage;
31   endif
32
33   lambda = 10^(log10lambda);
34
35   if ( length(mincell) == 2 ) # using stdev to find optimal lambda
36     stdev = mincell{2};
37     yhat  = rgdtsmcore (x, y, d, lambda, varargin{:});
38
39     xhatprov = 0;
40     relative = 0;
41     for i = 1:length(varargin)
42       if strcmp(varargin{i},"relative")
43         relative = 1;
44       elseif strcmp(varargin{i},"xhat")
45         xhatprov = 1;
46         xhat = varargin{i+1};
47       endif
48     endfor
49
50     if (xhatprov)
51       idx = interp1(xhat,1:length(xhat),x,"nearest");
52       if relative
53         stdevd = std((y-yhat(idx))./y);
54       else
55         stdevd = std(y-yhat(idx));
56       endif
57     else
58       if (relative)
59         stdevd = std((y-yhat)./y);
60       else
61         stdevd = std(y-yhat);
62       endif
63     endif
64
65     out = (stdevd - stdev)^2;
66
67   else # use gcv to find optimal lambda
68     [yhat, out] = rgdtsmcore (x, y, d, lambda, varargin{:});
69   endif
70
71 endfunction