1
## Copyright (C) 2015 Piotr Held
2
## Copyright (C) 2015 Juan Pablo Carbajal
4
## This file is part of Octave.
6
## Octave is free software; you can redistribute it and/or
7
## modify it under the terms of the GNU General Public
8
## License as published by the Free Software Foundation;
9
## either version 3 of the License, or (at your option) any
12
## Octave is distributed in the hope that it will be useful,
13
## but WITHOUT ANY WARRANTY; without even the implied
14
## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
## PURPOSE. See the GNU General Public License for more
18
## You should have received a copy of the GNU General Public
19
## License along with Octave; see the file COPYING. If not,
20
## see <http://www.gnu.org/licenses/>.
23
## @deftypefn{Function File} {@var{output} =} timerev (@var{S})
24
## @deftypefnx{Function File} {@var{output} =} timerev (@var{S}, @var{delay})
26
## Calculates time reversal assymetry statistic.
28
## Accomplishes this using the following equation applied to each component
33
## $$\frac{\sum (y_n-y_{n-d})^3}{\sum (y_n-y_{n-d})^2}$$
52
## This function always assumes that each time series is along the longer
53
## dimension of matrix @var{S}. It also assumes that every dimension
54
## (counting along the shorter dimension) of @var{S} is considered a
55
## component of the time series.
57
## The delay for the statistic ('d' in the equation above) [default = 1].
62
## The output is the calculated time reversal asymmetry statistic. It is calculated
63
## for each component separately and is alligned with the components, so if the
64
## input's components were columns vectors the output will be a row vector and
67
## @strong{Algorithms}
69
## The algorithms for this functions have been taken from the TISEAN package.
72
## Author: Piotr Held <pjheld@gmail.com>.
73
## This function is based on timerev of TISEAN 3.0.1
74
## https://github.com/heggus/Tisean"
76
function output = timerev (S, delay);
79
if (nargin != 1 && nargin != 2)
83
if ((!ismatrix (S)) || (!isreal(S)))
84
error ('Octave:invalid-input-arg', "S is not a realmatrix");
87
# If no delay was given assign 1
92
# Verify delay is a positive integer
93
isPositiveInteger = @(x) isreal(x) && isscalar (x) && (x > 0) ...
95
if (!isPositiveInteger(delay))
96
error ("Octave:invalid-input-arg", "delay must be a positive integer");
99
# Verify the input series 'S' is at least as long as 'delay + 1'
100
if (delay >= length (S))
101
error ("Octave:invalid-input-arg", ["the length of the input series must ",...
102
"be at least as long as 'delay + 1'"]);
105
# Correct S to always have more rows than columns
107
if (rows (S) < columns (S))
113
idx = delay+1:rows(S);
114
t2 = sum ((S(idx,:) - S(idx-delay,:)).^2);
115
t3 = sum ((S(idx,:) - S(idx-delay,:)).^3);
118
# Transpose output if input components were row vectors and not column vectors
125
%% Test output against TISEAN program 'timerev'
126
%!assert (timerev (henon (1000)(:,1),4),0.313235879,-1e-6)
127
%!assert (timerev (henon (1000), 4), [0.313235879, 0.312556326], -1e-6)
128
%!assert (timerev (henon(1000).', 4), [0.313235879; 0.312556326], -1e-6)
130
%% Testing input validation
131
%!error <positive> timerev (1, 0);
132
%!error <long> timerev ([1 2], 2);
134
%% Test if default values load properly
135
%!assert (timerev (henon (100)), timerev (henon (100),1))