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

« back to all changes in this revision

Viewing changes to inst/c1.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} =} c1 (@var{S})
 
23
## @deftypefnx{Function File} {@var{output} =} c1 (@var{S}, @var{paramName}, @var{paramValue}, @dots{})
 
24
##
 
25
## Computers curves for the fixed mass computation of information dimension
 
26
## (mentioned in TISEAN 3.0.1 documentation).
 
27
##
 
28
## A logarithmic range of masses between 1/N and 1 is realised by varying the
 
29
## neighbour order k as well as the subsequence length n. For a given mass
 
30
## k/n, n is chosen as small is possible as long as k is not smaller than the
 
31
## value specified by parameter @var{k} .
 
32
##
 
33
## You will probably use the auxiliary functions c2d or c2t to process the
 
34
## output further. The formula used for the Gaussian kernel correlation sum
 
35
## does not apply to the information dimension. 
 
36
##
 
37
## @strong{Input}
 
38
##
 
39
## @table @var
 
40
## @item S
 
41
## This function always assumes that each time series is along the longer 
 
42
## dimension of matrix @var{S}. It also assumes that every dimension 
 
43
## (counting along the shorter dimension) of @var{S} is considered a 
 
44
## component of the time series.
 
45
## @end table
 
46
##
 
47
## @strong{Parameters}
 
48
##
 
49
## @table @var
 
50
## @item mindim
 
51
## The minimum embedding dimension [default = 1].
 
52
## @item maxdim
 
53
## The maximum embedding dimension [default = 10].
 
54
## @item d
 
55
## The delay used [default = 1].
 
56
## @item t
 
57
## Minimum time separation [default = 0].
 
58
## @item n
 
59
## The number of reference points. That number of points are selected at
 
60
## random from all time indices [default = 100].
 
61
## @item res
 
62
## Resolution, values per octave [default = 2].
 
63
## @item i
 
64
## Seed for the random numbers [use default seed].
 
65
## @item k
 
66
## Maximum number of neighbors [default = 100].
 
67
## @end table
 
68
##
 
69
## @strong{Switch}
 
70
##
 
71
## @table @var
 
72
## @item verbose
 
73
## Display information about current mass during execution.
 
74
## @end table
 
75
##
 
76
## @strong{Output}
 
77
##
 
78
## The output is a @var{maxdim} - @var{mindim} + 1 x 1 struct array with the
 
79
## following fields:
 
80
## @table @var
 
81
## @item dim
 
82
## The embedding dimension of the struct.
 
83
## @item c1
 
84
## A matrix with two collumns that contain the following data:
 
85
## @enumerate
 
86
## @item
 
87
## radius
 
88
## @item
 
89
## 'mass'
 
90
## @end enumerate
 
91
## @end table
 
92
##
 
93
## @seealso{demo c1, c2d, c2t}
 
94
##
 
95
## @strong{Algorithms}
 
96
##
 
97
## The algorithms for this functions have been taken from the TISEAN package.
 
98
## @end deftypefn
 
99
 
 
100
## Author: Piotr Held <pjheld@gmail.com>.
 
101
## This function is based on c1 of TISEAN 3.0.1 
 
102
## https://github.com/heggus/Tisean"
 
103
 
 
104
function output = c1 (S, varargin)
 
105
 
 
106
  if (nargin < 1)
 
107
    print_usage;
 
108
  endif
 
109
 
 
110
  if ((ismatrix (S) == false) || (isreal(S) == false) || ...
 
111
       (isreal(S) == false))
 
112
    error ('Octave:invalid-input-arg', "S must be a realmatrix");
 
113
  endif
 
114
 
 
115
  # Default values
 
116
  mindim     = 1;
 
117
  maxdim     = 10;
 
118
  delay      = 1;
 
119
  tmin       = 0;
 
120
  cmin       = 100;
 
121
  resolution = 2;
 
122
  seed       = 0;
 
123
  kmax       = 100;
 
124
 
 
125
  #### Parse the input
 
126
  p = inputParser ();
 
127
  p.FunctionName = "c1";
 
128
 
 
129
  isPositiveIntScalar    = @(x) isreal(x) && isscalar (x) && ...
 
130
                             (x > 0) && (x-round(x) == 0);
 
131
  isNonNegativeIntScalar = @(x) isPositiveIntScalar (x) || (x == 0);
 
132
  isNonNegativeScalar       = @(x) isreal(x) && isscalar (x) && (x >=0);
 
133
 
 
134
  p.addParamValue ("mindim", mindim, isPositiveIntScalar);
 
135
  p.addParamValue ("maxdim", maxdim, isPositiveIntScalar);
 
136
  p.addParamValue ("d", delay, isPositiveIntScalar);
 
137
  p.addParamValue ("t", tmin, isNonNegativeIntScalar);
 
138
  p.addParamValue ("n", cmin, isPositiveIntScalar);
 
139
  p.addParamValue ("res", resolution, isPositiveIntScalar);
 
140
  p.addParamValue ("i", seed, isNonNegativeScalar);
 
141
  p.addParamValue ("k", kmax, isPositiveIntScalar);
 
142
  p.addSwitch ("verbose");
 
143
 
 
144
  p.parse (varargin{:});
 
145
 
 
146
  # Assign input
 
147
  mindim     = p.Results.mindim;
 
148
  maxdim     = p.Results.maxdim;
 
149
  delay      = p.Results.d;
 
150
  tmin       = p.Results.t;
 
151
  cmin       = p.Results.n; 
 
152
  resolution = p.Results.res;
 
153
  seed       = p.Results.i;
 
154
  kmax       = p.Results.k;
 
155
  verbose    = p.Results.verbose;
 
156
 
 
157
  if (mindim > maxdim)
 
158
    warning ("Octave:tisean", ["Parameter 'mindim' is greater than ", ...
 
159
                               "'maxdim', setting 'mindim' = 'maxdim'"]);
 
160
    mindim = maxdim;
 
161
  endif
 
162
 
 
163
  # Correct S to always have more rows than columns
 
164
  trnspsd = false;
 
165
  if (rows (S) < columns (S))
 
166
    S = S.';
 
167
    trnspsd = true;
 
168
  endif
 
169
 
 
170
  output = __c1__ (S, mindim, maxdim, delay, tmin, cmin, resolution, seed,
 
171
                   kmax, verbose);
 
172
 
 
173
endfunction
 
174
 
 
175
%!demo
 
176
%! res   = c1 (henon (5000)(:,1), 'd', 1, 'maxdim', 6, 't',50, 'n', 500);
 
177
%! slope = c2d (res, 2);
 
178
%!
 
179
%! do_plot_c1  = @(x) semilogx (x{1}(:,1),x{1}(:,2),'g');
 
180
%! hold on
 
181
%! arrayfun (do_plot_c1, {slope.d});
 
182
%! plot ([5e-4 1],[1.2 1.2])
 
183
%! hold off
 
184
%! axis tight
 
185
%! ylim ([0 3]);
 
186
%! xlabel ("Epsilon")
 
187
%! ylabel ("Local slopes");
 
188
%! title ("Information dimension")
 
189
%!###############################################################
 
190
 
 
191
 
 
192
%% testing if it works with default parameters
 
193
%!test
 
194
%% res_c1 was generated by TISEAN 3.0.1
 
195
%! res_c1 =  [0.00204866193 0.000562021392;0.00458388403 0.00152773259;0.00732116727 0.00251880521;0.0114847319 0.00351527636;0.0178178754 0.00551304966;0.0229953099 0.00751305372;0.0337692834 0.0115151331;0.0432167873 0.0155181978;0.070036374 0.0225279164;0.0809115991 0.0315354168;0.105735436 0.045548249;0.137048692 0.0635655001;0.198512435 0.0905919373;0.249011219 0.127565667;0.336370319 0.180583045;0.417932093 0.255131334;0.533079803 0.361822665;0.760820627 0.510262668;1.17550445 0.726286411;0.00397582119 0.000562021392;0.00887203496 0.00152773259;0.0129663227 0.00251880521;0.0233632866 0.00451370422;0.0256882776 0.00551304966;0.0377443098 0.00851340499;0.0444159135 0.0115151331;0.0625304207 0.0165190343;0.0792045668 0.0225279164;0.112165064 0.0325363018;0.142160922 0.045548249;0.183529988 0.0645664856;0.247998312 0.0905919373;0.365789354 0.128554597;0.479705334 0.181571603;0.648174882 0.25644654;0.828185916 0.361822665;1.0652355 0.512893081;1.51780188 0.726286411];
 
196
%! clear __c1__
 
197
%! res = c1 (henon (1000), 'maxdim', 2);
 
198
%! res_mat = cell2mat({res.c1}.');
 
199
%% row 23 and 25 are excluded because TISEAN data was calculated using floats
 
200
%% this program uses doubles
 
201
%! good_idx = [1:22,24,26:38];
 
202
%! assert (res_mat(good_idx,:), res_c1(good_idx,:),-1e-5);
 
203
%% bad_idx are used as the idx of those that were further apart than the rest
 
204
%! bad_idx = setdiff (1:length(res_c1),good_idx);
 
205
%! assert (res_mat(bad_idx,:), res_c1(bad_idx,:),6e-3);
 
206
 
 
207
%% testing if works with other-than-default parameters
 
208
%!test
 
209
%! res_c1 = [0.0343293324 0.000628733949;0.0685995296 0.00170907611;0.104914345 0.00281779026;0.147767007 0.00393254356;0.187380955 0.00616745465;0.242883027 0.00840486214;0.320845127 0.0117625576;0.424331248 0.0162406135;0.5311203 0.0229629427;0.638983071 0.0319196843;0.77487731 0.0453561395;0.95007056 0.0643920898;1.13252032 0.0912670717;1.33960843 0.129390419;1.51084828 0.182906702;1.65632439 0.259117812;1.78787661 0.365813404;1.93386149 0.518235624;2.13333058 0.731626809;0.0578132086 0.000630145252;0.106152184 0.00171291234;0.163010269 0.0028241151;0.214579925 0.00394137064;0.280628532 0.00618129829;0.353775769 0.00842372701;0.448207438 0.0117889596;0.582583368 0.0162770674;0.693925321 0.0230144858;0.805182397 0.0319913328;0.948441863 0.0454579443;1.12319851 0.0645366237;1.28063464 0.0914719254;1.44319057 0.129558906;1.57120717 0.183243573;1.71812892 0.259117812;1.83539987 0.367163211;1.98069465 0.518235624;2.16835737 0.737046182;0.0653617978 0.000630145252;0.120350979 0.00171291234;0.182360902 0.0028241151;0.22783263 0.00394137064;0.300161123 0.00618129829;0.384833038 0.00842372701;0.497192621 0.0117889596;0.616096795 0.0162770674;0.750326335 0.0230144858;0.908238411 0.0331135131;1.03522205 0.0465802066;1.23226273 0.0656589493;1.37276983 0.0925942659;1.53525198 0.130750626;1.65205204 0.184261546;1.77773058 0.260474414;1.88386154 0.368523091;2.01883292 0.520948827;2.19400668 0.737046182];
 
210
%! clear __c1__
 
211
%! res = c1 (henon (1000), 'mindim', 8, 'd',2,'i',0.5,'t',50,'n',500);
 
212
%% row 1, 2, 22 are excluded because TISEAN data was calculated using floats
 
213
%% this program uses doubles
 
214
%! good_idx = [3:21,23:57];
 
215
%! assert (cell2mat({res.c1}.')(good_idx,:), res_c1(good_idx,:), 1e-5);
 
216
%% bad_idx are used as the idx of those that were further apart than the rest
 
217
%! bad_idx = setdiff (1:length(res_c1),good_idx);
 
218
%! assert (cell2mat({res.c1}.')(bad_idx,:), res_c1(bad_idx,:),-3e-3);
 
219
 
 
220
%% Test input validation
 
221
%% Promote warnings to error to not execute program
 
222
%!error <greater> warning("error", "Octave:tisean"); c1 ([1 2 3], 'mindim', 3, 'maxdim', 2);
 
223