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

« back to all changes in this revision

Viewing changes to inst/lfo_run.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} {@var{output} =} lfo_run (@var{S})
 
23
## @deftypefnx{Function File} {@var{output} =} lfo_run (@var{S}, @var{paramName}, @var{paramValue}, @dots{})
 
24
##
 
25
## This function depending on whether switch 'zeroth' is set produces either
 
26
## a local linear ansatz or a zeroth order ansatz for a possibly multivariate
 
27
## time series and iterates an artificial trajectory. The initial values for
 
28
## the trajectory are the last points of the original time series. 
 
29
## Thus it actually forecasts the time series.
 
30
##
 
31
## @strong{Input}
 
32
##
 
33
## @table @var
 
34
## @item S
 
35
## This function always assumes that each time series is along the longer 
 
36
## dimension of matrix @var{S}. It also assumes that every dimension 
 
37
## (counting along the shorter dimension) of @var{S} is considered a 
 
38
## component of the time series.
 
39
## @end table
 
40
##
 
41
## @strong{Parameters}
 
42
##
 
43
## @table @var
 
44
## @item m
 
45
## The embedding dimension used. It is synonymous to the second part 
 
46
## of flag '-m' from TISEAN. The first part of the TISEAN flag is omitted
 
47
## as all of the available components of @var{S} are analyzed
 
48
## [default = 1].
 
49
## @item d
 
50
## Delay used for the embedding [default = 1].
 
51
## @item L
 
52
## Number of iterations into the future, length of prediction [default = 1000].
 
53
## @item k
 
54
## Minimal number of neighbors for the fit [default = 30].
 
55
## @item r
 
56
## Neighborhood size to start with [default = 1e-3].
 
57
## @item f
 
58
## Factor to increase neighborhood size if not enough neighbors were
 
59
## found [default = 1.2].
 
60
## @end table
 
61
##
 
62
## @strong{Switch}
 
63
##
 
64
## @table @var
 
65
## @item zeroth
 
66
## Perform a zeroth order fit instead a local linear one. This is synonymous
 
67
## with flag '-0' from TISEAN.
 
68
## @end table
 
69
##
 
70
## @strong{Output}
 
71
##
 
72
## Components of the forecasted time series.
 
73
##
 
74
## @seealso{lfo_test, lfo_ar, lzo_run}
 
75
##
 
76
## @strong{Algorithms}
 
77
##
 
78
## The algorithms for this functions have been taken from the TISEAN package.
 
79
## @end deftypefn
 
80
 
 
81
## Author: Piotr Held <pjheld@gmail.com>.
 
82
## This function is based on lfo-run of TISEAN 3.0.1 
 
83
## https://github.com/heggus/Tisean"
 
84
 
 
85
function output = lfo_run (S, varargin)
 
86
 
 
87
  if (nargin < 1)
 
88
    print_usage;
 
89
  endif
 
90
 
 
91
  if ((ismatrix (S) == false) || (isreal(S) == false) || ...
 
92
       (isreal(S) == false))
 
93
    error ('Octave:invalid-input-arg', "S must be a realmatrix");
 
94
  endif
 
95
 
 
96
  # Default values
 
97
  embed   = 2;
 
98
  delay   = 1;
 
99
  flength = 1000;
 
100
  minn    = 30;
 
101
  eps0    = 1e-3;
 
102
  epsf    = 1.2;
 
103
 
 
104
  #### Parse the input
 
105
  p = inputParser ();
 
106
  p.FunctionName = "lfo_run";
 
107
 
 
108
  isPositiveIntScalar = @(x) isreal(x) && isscalar (x) && ...
 
109
                             (x > 0) && (x-round(x) == 0);
 
110
  isNonNegativeIntScalar = @(x) isPositiveIntScalar (x) || (x == 0);
 
111
  isPositiveScalar    = @(x) isreal(x) && isscalar (x) && (x > 0);
 
112
  isNonNegativeScalar = @(x) isPositiveScalar (x) || (x == 0);
 
113
 
 
114
  p.addParamValue ("m", embed, isPositiveIntScalar);
 
115
  p.addParamValue ("d", delay, isPositiveIntScalar);
 
116
  p.addParamValue ("l", flength, isPositiveIntScalar);
 
117
  p.addParamValue ("k", minn, isPositiveIntScalar);
 
118
  p.addParamValue ("r", eps0, isPositiveScalar);
 
119
  p.addParamValue ("f", epsf, isPositiveScalar);
 
120
  p.addSwitch ("zeroth");
 
121
 
 
122
  p.parse (varargin{:});
 
123
 
 
124
  # Assign inputs
 
125
  embed     = p.Results.m;
 
126
  delay     = p.Results.d;
 
127
  flength   = p.Results.l;
 
128
  minn      = p.Results.k;
 
129
  eps0      = p.Results.r;
 
130
  epsset    = !ismember ('r', p.UsingDefaults);
 
131
  epsf      = p.Results.f;
 
132
  do_zeroth = p.Results.zeroth;
 
133
 
 
134
  # Correct S to always have more rows than columns
 
135
  trnspsd = false;
 
136
  if (rows (S) < columns (S))
 
137
    S = S.';
 
138
    trnspsd = true;
 
139
  endif
 
140
 
 
141
  output = __lfo_run__ (S, embed, delay, flength, minn,
 
142
                        eps0, epsset, epsf, do_zeroth);
 
143
 
 
144
  if (trnspsd)
 
145
  output = output.';
 
146
  endif
 
147
endfunction
 
148
 
 
149
%!test
 
150
%! lfo_run_hen = [0.4178371 0.5916857;0.6124674 0.4178371;0.3080429 0.6124674;0.9938561 0.3080429;-0.3374249 0.9938561;0.7975559 -0.3374249;-0.1623264 0.7975559;0.4382906 -0.1623264;0.4370613 0.4382906;0.9818325 0.4370613;-0.3019938 0.9818325;0.8267984 -0.3019938;-0.2345942 0.8267984;0.5331624 -0.2345942;0.4509924 0.5331624];
 
151
%! res = lfo_run (henon (1000),'m',4,'d',6,'l',15);
 
152
%! assert (res, lfo_run_hen, 1e-5);
 
153
 
 
154
%!error <singular> lfo_run(henon(1000));
 
155
%!error <forecast failed> lfo_run(1:500,'m',1);
 
156
 
 
157
%% Check for neverending execution
 
158
%!error <too large> lfo_run(1:10, 'm',1);