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

« back to all changes in this revision

Viewing changes to inst/d2.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{values}, @var{pars}] =} d2 (@var{S})
 
23
## @deftypefnx{Function File} {[@var{values}, @var{pars}] =} d2 (@var{S}, @var{paramName}, @var{paramValue}, @dots{})
 
24
##
 
25
## This program estimates the correlation sum, the correlation dimension and
 
26
## the correlation entropy of a given, possibly multivariate, data set. It uses
 
27
## the box assisted search algorithm and is quite fast as long as one is not
 
28
## interested in large length scales. All length scales are computed
 
29
## simultaneously and the current center and epsilon are written every 2 min
 
30
## (real time, not cpu time) or every set number of center value increases.
 
31
## It is possible to set a maximum number of pairs. If this number is
 
32
## reached for a given length scale, the length scale will no longer be treated
 
33
## for the rest of the estimate.
 
34
##
 
35
## @strong{Input}
 
36
##
 
37
## @table @var
 
38
## @item S
 
39
## This function always assumes that each time series is along the longer 
 
40
## dimension of matrix @var{S}. It also assumes that every dimension 
 
41
## (counting along the shorter dimension) of @var{S} is considered a 
 
42
## component of the time series.
 
43
## @end table
 
44
##
 
45
## @strong{Parameters}
 
46
##
 
47
## @table @var
 
48
## @item m
 
49
## The maximum embedding dimension [default = 10].
 
50
## @item d
 
51
## The delay used [default = 1].
 
52
## @item t
 
53
## Theiler window [default = 0].
 
54
## @item rlow
 
55
## Minimum length scale [default = 1e-3].
 
56
## @item rhigh
 
57
## Maximum length scale [default = 1].
 
58
## @item eps_no
 
59
## Number of length scale values [default = 100].
 
60
## @item n
 
61
## Maximum number of pairs to be used (value 0 means all possible pairs)
 
62
## [default = 1000].
 
63
## @item p
 
64
## This parameter determines after how many iterations (center points)
 
65
## should the program pause and write out how many center points have been
 
66
## treated so far and the current epsilon. If @var{plot_corr} or
 
67
## @var{plot_slopes} or @var{plot_entrop} is set then during the pause a plot
 
68
## of the current state of @var{c2}, @var{d2} or @var{h2} (respectively)
 
69
## is produced. Regardless of the value of this parameter the program will
 
70
## pause every two minutes [default = only pause every 2 minutes].
 
71
## @end table
 
72
##
 
73
## @strong{Switches}
 
74
##
 
75
## @table @var
 
76
## @item normalized
 
77
## When this switch is set the program uses data normalized to [0,1] for all
 
78
## components.
 
79
## @item plot_corr
 
80
## If this switch is set then whenever the execution is paused (the frequency
 
81
## can be set with parameter @var{p}) the most recent correlation sums are
 
82
## plotted. The color used for them is blue.
 
83
## @item plot_slopes
 
84
## Same as @var{plot_corr} except the plotted values are the local slopes.
 
85
## They are plotted in red.
 
86
## @item plot_entrop
 
87
## Same as @var{plot_corr} except the correlation entropies are plotted.
 
88
## They are plotted in green.
 
89
## @end table
 
90
##
 
91
## @strong{Output}
 
92
##
 
93
## @table @var
 
94
## @item values
 
95
## This is a struct array that contains the following fields:
 
96
## @itemize @bullet
 
97
## @item
 
98
## dim - the dimension of the data
 
99
## @item
 
100
## c2 - the first column is the epsilon and the second the correlation sums for
 
101
## a particular embedding dimension
 
102
## @item
 
103
## d2 - the first column is the epsilon and the second the local slopes of
 
104
## the logarithm of the corrlation sum
 
105
## @item
 
106
## h2 - the first column is the epsilon and the second the correlation
 
107
## entropies
 
108
## @end itemize
 
109
## @item pars
 
110
## This is a struct. It contains the following fields:
 
111
## @itemize @bullet
 
112
## @item
 
113
## treated - the number of center points treated
 
114
## @item
 
115
## eps - the maximum epsilon used
 
116
## @end itemize
 
117
## @end table
 
118
##
 
119
## @seealso{demo d2, av_d2, c2t, c2g}
 
120
##
 
121
## @strong{Algorithms}
 
122
##
 
123
## The algorithms for this functions have been taken from the TISEAN package.
 
124
## @end deftypefn
 
125
 
 
126
## Author: Piotr Held <pjheld@gmail.com>.
 
127
## This function is based on d2 of TISEAN 3.0.1 
 
128
## https://github.com/heggus/Tisean"
 
129
 
 
130
function [values, pars] = d2 (S, varargin)
 
131
 
 
132
  if (nargin < 1 || nargout > 2)
 
133
    print_usage;
 
134
  endif
 
135
 
 
136
  if ((ismatrix (S) == false) || (isreal(S) == false) || ...
 
137
       (isreal(S) == false))
 
138
    error ('Octave:invalid-input-arg', "S must be a realmatrix");
 
139
  endif
 
140
 
 
141
  # Default values
 
142
  embed          = 10;
 
143
  delay          = 1;
 
144
  mindist        = 0;
 
145
  epsmin         = 1e-3;
 
146
  epsmax         = 1.0;
 
147
  howoften       = 100;
 
148
  maxfound       = 1000;
 
149
  iterator_pause = length (S);
 
150
 
 
151
  #### Parse the input
 
152
  p = inputParser ();
 
153
  p.FunctionName = "d2";
 
154
 
 
155
  isPositiveIntScalar    = @(x) isreal(x) && isscalar (x) && ...
 
156
                             (x > 0) && (x-round(x) == 0);
 
157
  isNonNegativeIntScalar = @(x) isPositiveIntScalar (x) || (x == 0);
 
158
  isPositiveScalar       = @(x) isreal(x) && isscalar (x) && (x > 0);
 
159
 
 
160
  p.addParamValue ("m", embed, isPositiveIntScalar);
 
161
  p.addParamValue ("d", delay, isPositiveIntScalar);
 
162
  p.addParamValue ("t", mindist, isNonNegativeIntScalar);
 
163
  p.addParamValue ("rlow", epsmin, isPositiveScalar);
 
164
  p.addParamValue ("rhigh", epsmax, isPositiveScalar);
 
165
  p.addParamValue ("eps_no", howoften, isPositiveIntScalar);
 
166
  p.addParamValue ("n", maxfound, isNonNegativeIntScalar);
 
167
  p.addParamValue ("p", iterator_pause, isPositiveIntScalar);
 
168
  p.addSwitch ("normalized");
 
169
  p.addSwitch ("plot_corr");
 
170
  p.addSwitch ("plot_slopes");
 
171
  p.addSwitch ("plot_entrop");
 
172
 
 
173
  p.parse (varargin{:});
 
174
 
 
175
  # Assign inputs
 
176
  embed          = p.Results.m;
 
177
  delay          = p.Results.d;
 
178
  mindist        = p.Results.t;
 
179
  epsmin         = p.Results.rlow;
 
180
  eps_min_set    = !ismember ("rlow", p.UsingDefaults);
 
181
  epsmax         = p.Results.rhigh;
 
182
  eps_max_set    = !ismember ("rhigh", p.UsingDefaults);
 
183
  howoften       = p.Results.eps_no;
 
184
  maxfound       = p.Results.n;
 
185
  rescale_set    = p.Results.normalized;
 
186
  iterator_pause = p.Results.p;
 
187
  plot_corr      = p.Results.plot_corr;
 
188
  plot_slopes    = p.Results.plot_slopes;
 
189
  plot_entrop    = p.Results.plot_entrop;
 
190
 
 
191
  # Input validation
 
192
  # Check if the delay and embedding dimensions are too large
 
193
  if ((length (S)-(embed-1)*delay) <= 0)
 
194
    error ("Octave:invalid-input-arg","Embedding dimension and delay are too \
 
195
large, the delay vector would be longer than the whole series.");
 
196
  endif
 
197
 
 
198
  # Check if rlow is smaller than rhigh
 
199
  if (epsmin >= epsmax)
 
200
    warning ("Octave:tisean", "'rlow' is greater or equal to 'rhigh'");
 
201
  endif
 
202
 
 
203
  # Correct S to always have more rows than columns
 
204
  trnspsd = false;
 
205
  if (rows (S) < columns (S))
 
206
    S = S.';
 
207
    trnspsd = true;
 
208
  endif
 
209
 
 
210
  [values, vars] = __d2__ (S, embed, delay, mindist, epsmin, eps_min_set,
 
211
                           epsmax, eps_max_set, howoften, maxfound,
 
212
                           rescale_set,iterator_pause);
 
213
 
 
214
  # Pause calculations and if flags are set, plot current state of the output.
 
215
  calc_paused = false;
 
216
  while (isfield (vars, "counter") ...
 
217
         && vars.counter < (length (S) - (embed-1)*delay))
 
218
 
 
219
    calc_paused   = true;
 
220
 
 
221
    printf ("\n");
 
222
    treated = vars.treated
 
223
    epsilon = vars.eps
 
224
    fflush (stdout);
 
225
 
 
226
    figure_no = 1;
 
227
    if (plot_corr)
 
228
      h             = figure (figure_no);
 
229
      figure_no    += 1;
 
230
      do_plot_corr  = @(x) loglog (x{1}(:,1),x{1}(:,2),'b');
 
231
      clf (h)
 
232
      hold on
 
233
      arrayfun (do_plot_corr, {values.c2});
 
234
      hold off
 
235
      xlabel ("Epsilon")
 
236
      ylabel ("Correlation sums")
 
237
      drawnow ()
 
238
    endif
 
239
    if (plot_slopes)
 
240
      h             = figure (figure_no);
 
241
      figure_no    += 1;
 
242
      do_plot_slope = @(x) semilogx (x{1}(:,1),x{1}(:,2),'r');
 
243
      clf (h)
 
244
      hold on
 
245
      arrayfun (do_plot_slope, {values.d2});
 
246
      hold off
 
247
      xlabel ("Epsilon")
 
248
      ylabel ("Local slopes")
 
249
      drawnow ()
 
250
    endif
 
251
    if (plot_entrop)
 
252
      h               =figure (figure_no);
 
253
      figure_no      += 1;
 
254
      do_plot_entrop  = @(x) semilogx (x{1}(:,1),x{1}(:,2),'g');
 
255
      clf (h)
 
256
      hold on
 
257
      arrayfun (do_plot_entrop, {values.h2});
 
258
      hold off
 
259
      xlabel ("Epsilon")
 
260
      ylabel ("Correlation entropies");
 
261
      drawnow ()
 
262
    endif
 
263
 
 
264
    # Continue on with computation
 
265
    [values, vars] = __d2__ (S, embed, delay, mindist, vars.EPSMIN,
 
266
                             eps_min_set, vars.EPSMAX, eps_max_set, howoften,
 
267
                             maxfound, rescale_set, iterator_pause,
 
268
                             vars.counter, vars.found, vars.norm, vars.boxc1,
 
269
                             vars.box, vars.list, vars.listc1, vars.imin,
 
270
                             vars.EPSMAX1);
 
271
  endwhile
 
272
 
 
273
  if (calc_paused)
 
274
    printf ("\n");
 
275
    fflush (stdout);
 
276
  endif
 
277
 
 
278
  pars = vars;
 
279
 
 
280
endfunction
 
281
 
 
282
%!demo
 
283
%! vals = d2 (henon (1000), 'd', 1, 'm', 5, 't',50);
 
284
%! 
 
285
%! subplot (2,3,1)
 
286
%! do_plot_corr  = @(x) loglog (x{1}(:,1),x{1}(:,2),'b');
 
287
%! hold on
 
288
%! arrayfun (do_plot_corr, {vals.c2});
 
289
%! hold off
 
290
%! axis tight
 
291
%! xlabel ("Epsilon")
 
292
%! ylabel ("Correlation sums")
 
293
%! title ("c2");
 
294
%!
 
295
%! subplot (2,3,4)
 
296
%! do_plot_entrop  = @(x) semilogx (x{1}(:,1),x{1}(:,2),'g');
 
297
%! hold on
 
298
%! arrayfun (do_plot_entrop, {vals.h2});
 
299
%! hold off
 
300
%! axis tight
 
301
%! xlabel ("Epsilon")
 
302
%! ylabel ("Correlation entropies");
 
303
%! title ("h2")
 
304
%!
 
305
%! subplot (2,3,[2 3 5 6])
 
306
%! do_plot_slope = @(x) semilogx (x{1}(:,1),x{1}(:,2),'r');
 
307
%! hold on
 
308
%! arrayfun (do_plot_slope, {vals.d2});
 
309
%! hold off
 
310
%! axis tight
 
311
%! xlabel ("Epsilon")
 
312
%! ylabel ("Local slopes")
 
313
%! title ("d2");
 
314
%!###############################################################
 
315
 
 
316
%!shared res, pars
 
317
%! [res, pars] = d2 (henon (100)(:,1),'m',2,'eps_no',20,'t',50);
 
318
 
 
319
%!test
 
320
%! res_d2_stat = [98 1.777433e+00];
 
321
%! assert ([pars.treated pars.eps], res_d2_stat, -1e-6);
 
322
 
 
323
%% test d2 field of output
 
324
%!test
 
325
%! res_d2_d2 = [1.777433 0.30648;1.235659 0.5606802;0.859021 0.7008824;0.5971852 0.8064865;0.4151589 0.9044852;0.2886154 0.9689651;0.2006434 0.8540874;0.1394858 0.8112826;0.09696954 0.8935407;0.06741253 0.9143867;0.0468647 0.856615;0.03258 0.8627251;0.02264938 1.503286;0.01574569 1.447036;0.01094629 0.7216412;0.007609782 0.6137634;0.005290265 1.29276;0.003677754 1.405042;1.777433 0.645521;1.235659 1.002253;0.859021 1.056272;0.5971852 1.026064;0.4151589 1.402018;0.2886154 1.565816;0.2006434 0.6950658;0.1394858 1.238292;0.09696954 0.9580286;0.06741253 1.75781;0.0468647 1.043798;0.03258 2.628165;0.02264938 1.405042;0.01574569 1.115245];
 
326
%! assert (cell2mat({res.d2}.'), res_d2_d2, -1e-6);
 
327
 
 
328
%% test h2 field of output
 
329
%!test
 
330
%! res_d2_h2 = [2.556748 -0;1.777433 0.1114257;1.235659 0.31527;0.859021 0.5700871;0.5971852 0.8632982;0.4151589 1.192138;0.2886154 1.544421;0.2006434 1.854938;0.1394858 2.149893;0.09696954 2.474754;0.06741253 2.807194;0.0468647 3.11863;0.03258 3.432288;0.02264938 3.978832;0.01574569 4.504925;0.01094629 4.767289;0.007609782 4.990433;0.005290265 5.460436;0.003677754 5.971262;2.556748 0;1.777433 0.1232638;1.235659 0.2838046;0.859021 0.4130123;0.5971852 0.4928431;0.4151589 0.6737291;0.2886154 0.890724;0.2006434 0.8329091;0.1394858 0.9881553;0.09696954 1.011601;0.06741253 1.318241;0.0468647 1.386294;0.03258 2.028148;0.02264938 1.99243;0.01574569 1.871802];
 
331
%! assert (cell2mat({res.h2}.'), res_d2_h2, -1e-6);
 
332
 
 
333
%% test c2 field of output
 
334
%!test
 
335
%! res_d2_c2 =  [2.556748 1;1.777433 0.8945578;1.235659 0.7295918;0.859021 0.5654762;0.5971852 0.4217687;0.4151589 0.3035714;0.2886154 0.2134354;0.2006434 0.1564626;0.1394858 0.1164966;0.09696954 0.08418367;0.06741253 0.06037415;0.0468647 0.04421769;0.03258 0.03231293;0.02264938 0.01870748;0.01574569 0.01105442;0.01094629 0.008503401;0.007609782 0.006802721;0.005290265 0.004251701;0.003677754 0.00255102;0.002556748 0;2.556748 1;1.777433 0.7908163;1.235659 0.5493197;0.859021 0.3741497;0.5971852 0.2576531;0.4151589 0.1547619;0.2886154 0.08758503;0.2006434 0.06802721;0.1394858 0.04336735;0.09696954 0.03061224;0.06741253 0.01615646;0.0468647 0.01105442;0.03258 0.004251701;0.02264938 0.00255102;0.01574569 0.00170068;0.01094629 0;0.007609782 0;0.005290265 0;0.003677754 0;0.002556748 0];
 
336
%! assert (cell2mat({res.c2}.'), res_d2_c2, -1e-6);
 
337
 
 
338
%% Test input validation
 
339
%!error <too large> d2 (1:5);
 
340
%% Promote warnings to error to not execute program
 
341
%!error <greater> warning("error", "Octave:tisean"); ...
 
342
%!                d2 (henon (1000), 'rlow', 4, 'rhigh', 1);