~ubuntu-branches/debian/sid/octave-tisean/sid

« back to all changes in this revision

Viewing changes to inst/xzero.m

  • Committer: Package Import Robot
  • Author(s): Rafael Laboissiere
  • Date: 2017-08-14 12:53:47 UTC
  • Revision ID: package-import@ubuntu.com-20170814125347-ju5owr4dggr53a2n
Tags: upstream-0.2.3
ImportĀ upstreamĀ versionĀ 0.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## Copyright (C) 1996-2015 Piotr Held
 
2
##
 
3
## This file is part of Octave.
 
4
##
 
5
## Octave is free software; you can redistribute it and/or
 
6
## modify it under the terms of the GNU General Public
 
7
## License as published by the Free Software Foundation;
 
8
## either version 3 of the License, or (at your option) any
 
9
## later version.
 
10
##
 
11
## Octave is distributed in the hope that it will be useful,
 
12
## but WITHOUT ANY WARRANTY; without even the implied
 
13
## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 
14
## PURPOSE.  See the GNU General Public License for more
 
15
## details.
 
16
##
 
17
## You should have received a copy of the GNU General Public
 
18
## License along with Octave; see the file COPYING.  If not,
 
19
## see <http://www.gnu.org/licenses/>.
 
20
 
 
21
## -*- texinfo -*-
 
22
## @deftypefn{Function File} {output =} xzero (@var{X1}, @var{X2})
 
23
## @deftypefnx{Function File} {output =} xzero (@var{X1}, @var{X2}, @var{paramName}, @var{paramValue}, @dots{})
 
24
##
 
25
## Takes two data sets and fits a zeroth order model of data set 1 (@var{X1})
 
26
## to predict data set 2 (@var{X2}) - cross prediction. It then computes the
 
27
## error of the model. This is done by searching for all neighbors in @var{X1}
 
28
## of the points of set @var{X2} which should be forecasted and taking as their
 
29
## images the average of the images of the neighbors. The obtained forecast
 
30
## error is normalized to the variance of data set @var{X2}.
 
31
##
 
32
## @strong{Inputs}
 
33
##
 
34
## Both @var{X1} and @var{X2} must be present. They must be realvectors
 
35
## of the same length.
 
36
##
 
37
## @strong{Parameters}
 
38
##
 
39
## @table @var
 
40
## @item m
 
41
## Embedding dimension [default = 3].
 
42
## @item d
 
43
## Delay for embedding [default = 1].
 
44
## @item n
 
45
## The number of points for which the error should be calculated 
 
46
## [default = all].
 
47
## @item k
 
48
## Minimum number of neighbors for the fit [default = 30].
 
49
## @item r
 
50
## The neighborhood size to start with [default = 1e-3].
 
51
## @item f
 
52
## Factor by which to increase the neighborhood size if not
 
53
## enough neighbors were found [default = 1.2].
 
54
## @item s
 
55
## Steps to be forecast (@code{x2(n+steps) = av(x1(i+steps)}) [default = 1].
 
56
## @end table
 
57
##
 
58
## @strong{Output}
 
59
##
 
60
## Contains value of parameter '@var{s}' lines. Each line represents the
 
61
## forecast error divided by the standard deviation of the second data set 
 
62
## (@var{X2}). This second data set is the one being forecasted.
 
63
##
 
64
## @strong{Algorithms}
 
65
##
 
66
## The algorithms for this functions have been taken from the TISEAN package.
 
67
## @end deftypefn
 
68
 
 
69
## Author: Piotr Held <pjheld@gmail.com>.
 
70
## This function is based on xzero of TISEAN 3.0.1
 
71
## https://github.com/heggus/Tisean"
 
72
 
 
73
function output = xzero (X1, X2, varargin)
 
74
 
 
75
  # Initial input validation
 
76
  if (nargin < 2)
 
77
    print_usage;
 
78
  endif
 
79
 
 
80
  if ((isvector (X1) == false) || (isreal(X1) == false))
 
81
    error ('Octave:invalid-input-arg', "X1 must be a realvector");
 
82
  endif
 
83
 
 
84
  if ((isvector (X2) == false) || (isreal(X2) == false))
 
85
    error ('Octave:invalid-input-arg', "X2 must be a realvector");
 
86
  endif
 
87
 
 
88
  if (length (X1) != length (X2))
 
89
    error ('Octave:invalid-input-arg', "X1 and X2 must be of same length");
 
90
  endif
 
91
 
 
92
  # Default parameters
 
93
  embdim  = 3;
 
94
  delay   = 1;
 
95
  clength = length (X1);
 
96
  minn    = 30;
 
97
  eps0    = 1e-3;
 
98
  epsf    = 1.2;
 
99
  step    = 1;
 
100
 
 
101
  #### Parse the input
 
102
  p = inputParser ();
 
103
  p.FunctionName = "xzero";
 
104
 
 
105
  isPositiveIntScalar    = @(x) isreal(x) && isscalar (x) && ...
 
106
                             (x > 0) && (x-round(x) == 0);
 
107
  isPositiveScalar     = @(x) isreal(x) && isscalar (x) && (x > 0);
 
108
 
 
109
  p.addParamValue ("m", embdim, isPositiveIntScalar);
 
110
  p.addParamValue ("d", delay, isPositiveIntScalar);
 
111
  p.addParamValue ("n", clength, isPositiveIntScalar);
 
112
  p.addParamValue ("k", minn, isPositiveIntScalar);
 
113
  p.addParamValue ("r", eps0, isPositiveScalar);
 
114
  p.addParamValue ("f", epsf, isPositiveScalar);
 
115
  p.addParamValue ("s", step, isPositiveIntScalar);
 
116
 
 
117
  p.parse (varargin{:});
 
118
 
 
119
  # Assign input
 
120
  embdim   = p.Results.m;
 
121
  delay    = p.Results.d;
 
122
  clength  = p.Results.n;
 
123
  minn     = p.Results.k;
 
124
  eps0     = p.Results.r;
 
125
  epsset   = !ismember ("r", p.UsingDefaults);
 
126
  epsf     = p.Results.f;
 
127
  step     = p.Results.s;
 
128
 
 
129
  output  = __xzero__ (X1, X2, embdim, delay, clength, minn, eps0, epsset, ...
 
130
                       epsf, step);
 
131
endfunction
 
132
 
 
133
%!fail ("xzero(1)");
 
134
%!fail ("xzero('a')");
 
135
 
 
136
%!test
 
137
%! hen = henon(2000)(:,1);
 
138
%! hen1 = hen(1:1000);
 
139
%! hen2 = hen(1001:end);
 
140
%! res_tisean = [1 0.6438699;2 0.9101371;3 0.9752469;4 0.9600329;5 0.9788585;6 0.9937851;7 1.002654;8 0.9973579;9 1.00776;10 1.008823;11 1.016724;12 1.017996;13 1.011284;14 1.005963;15 1.008479;16 1.007647;17 1.009703;18 1.018097;19 1.008374;20 1.006889];
 
141
%! out = xzero (hen1,hen2,'m',4,'d',6,'s',20);
 
142
%! assert(out, res_tisean(:,2),-1e-6);