1
## Copyright (C) 1996-2015 Piotr Held
3
## This file is part of Octave.
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
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
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/>.
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{})
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.
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.
45
## @strong{Parameters}
49
## The maximum embedding dimension [default = 10].
51
## The delay used [default = 1].
53
## Theiler window [default = 0].
55
## Minimum length scale [default = 1e-3].
57
## Maximum length scale [default = 1].
59
## Number of length scale values [default = 100].
61
## Maximum number of pairs to be used (value 0 means all possible pairs)
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].
77
## When this switch is set the program uses data normalized to [0,1] for all
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.
84
## Same as @var{plot_corr} except the plotted values are the local slopes.
85
## They are plotted in red.
87
## Same as @var{plot_corr} except the correlation entropies are plotted.
88
## They are plotted in green.
95
## This is a struct array that contains the following fields:
98
## dim - the dimension of the data
100
## c2 - the first column is the epsilon and the second the correlation sums for
101
## a particular embedding dimension
103
## d2 - the first column is the epsilon and the second the local slopes of
104
## the logarithm of the corrlation sum
106
## h2 - the first column is the epsilon and the second the correlation
110
## This is a struct. It contains the following fields:
113
## treated - the number of center points treated
115
## eps - the maximum epsilon used
119
## @seealso{demo d2, av_d2, c2t, c2g}
121
## @strong{Algorithms}
123
## The algorithms for this functions have been taken from the TISEAN package.
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"
130
function [values, pars] = d2 (S, varargin)
132
if (nargin < 1 || nargout > 2)
136
if ((ismatrix (S) == false) || (isreal(S) == false) || ...
137
(isreal(S) == false))
138
error ('Octave:invalid-input-arg', "S must be a realmatrix");
149
iterator_pause = length (S);
153
p.FunctionName = "d2";
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);
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");
173
p.parse (varargin{:});
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;
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.");
198
# Check if rlow is smaller than rhigh
199
if (epsmin >= epsmax)
200
warning ("Octave:tisean", "'rlow' is greater or equal to 'rhigh'");
203
# Correct S to always have more rows than columns
205
if (rows (S) < columns (S))
210
[values, vars] = __d2__ (S, embed, delay, mindist, epsmin, eps_min_set,
211
epsmax, eps_max_set, howoften, maxfound,
212
rescale_set,iterator_pause);
214
# Pause calculations and if flags are set, plot current state of the output.
216
while (isfield (vars, "counter") ...
217
&& vars.counter < (length (S) - (embed-1)*delay))
222
treated = vars.treated
228
h = figure (figure_no);
230
do_plot_corr = @(x) loglog (x{1}(:,1),x{1}(:,2),'b');
233
arrayfun (do_plot_corr, {values.c2});
236
ylabel ("Correlation sums")
240
h = figure (figure_no);
242
do_plot_slope = @(x) semilogx (x{1}(:,1),x{1}(:,2),'r');
245
arrayfun (do_plot_slope, {values.d2});
248
ylabel ("Local slopes")
252
h =figure (figure_no);
254
do_plot_entrop = @(x) semilogx (x{1}(:,1),x{1}(:,2),'g');
257
arrayfun (do_plot_entrop, {values.h2});
260
ylabel ("Correlation entropies");
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,
283
%! vals = d2 (henon (1000), 'd', 1, 'm', 5, 't',50);
286
%! do_plot_corr = @(x) loglog (x{1}(:,1),x{1}(:,2),'b');
288
%! arrayfun (do_plot_corr, {vals.c2});
291
%! xlabel ("Epsilon")
292
%! ylabel ("Correlation sums")
296
%! do_plot_entrop = @(x) semilogx (x{1}(:,1),x{1}(:,2),'g');
298
%! arrayfun (do_plot_entrop, {vals.h2});
301
%! xlabel ("Epsilon")
302
%! ylabel ("Correlation entropies");
305
%! subplot (2,3,[2 3 5 6])
306
%! do_plot_slope = @(x) semilogx (x{1}(:,1),x{1}(:,2),'r');
308
%! arrayfun (do_plot_slope, {vals.d2});
311
%! xlabel ("Epsilon")
312
%! ylabel ("Local slopes")
314
%!###############################################################
317
%! [res, pars] = d2 (henon (100)(:,1),'m',2,'eps_no',20,'t',50);
320
%! res_d2_stat = [98 1.777433e+00];
321
%! assert ([pars.treated pars.eps], res_d2_stat, -1e-6);
323
%% test d2 field of output
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);
328
%% test h2 field of output
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);
333
%% test c2 field of output
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);
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);