blib/lib/Statistics/Sequences/Turns.pm

filter: Statementstmt 93.1% 68 / 73 Branchbran 73.6% 28 / 38 Conditioncond 78.5% 11 / 14 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 93.7% 15 / 16 Podpod 100% 9 / 9 totaltotal 86.1% 137 / 159 CC 26 SCAR 16.4
CC
26
Cov
86%
observed
9.1
z_value
7.2
_set_data
6.0
CRAP 5.2
↑ Summary
1 package Statistics::Sequences::Turns;
2 3 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use base qw(Statistics::Sequences);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 $Statistics::Sequences::Turns::VERSION = '0.13';
8 3 ▶use Statistics::Zed 0.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 =pod
11
12 =head1 NAME
13
14 Statistics::Sequences::Turns - Kendall's turning-points test - of peaks and troughs in a numerical sequence
15
16 =head1 VERSION
17
18 This is documentation for B<Version 0.13> of Statistics::Sequences::Turns.
19
20 =head1 SYNOPSIS
21
22 use strict;
23 use Statistics::Sequences::Turns 0.13;
24 my $turns = Statistics::Sequences::Turns->new();
25 $turns->load([2, 0, 8.5, 5, 3, 5.01, 2, 2, 3]); # numbers; or give as "data => $aref" with each stat call
26 my $val = $turns->observed(); # or descriptive methods: expected(), variance(), obsdev() and stdev()
27 $val = $turns->z_value(); # # or in list context get both z- and p-value
28 $val = $turns->p_value(); # as above, assume data are loaded
29 my $href = $turns->stats_hash(values => [qw/observed p_value/], ccorr => 1); # incl. any other stat-method
30 $turns->dump(values => [qw/observed expected p_value/], ccorr => 1, flag => 1, precision_s => 3, precision_p => 7);
31 # prints: observed = 11.000, expected = 10.900, p_value = 0.5700167
32
33 =head1 DESCRIPTION
34
35 Implements Kendall's (1973) "turning point test" of sudden changes as peaks and troughs in the values of a numerical sequence. It is sometimes described as a test of "cyclicity", and often used as a test of randomness. Kendall (1973) introduced this as a test of ups and downs relative to linear progressions in a sequence (ahead of describing tests based on autocorrelation and Fourier analysis).
36
37 Specifically, for a sequence of numerical data (interval or ordinal) of size I<N>, a count of turns is incremented if the value on trial I<i>, for all I<i> greater than zero and less than I<N>, is, with respect to its immediate neighbours (the values on I<i> - 1 and I<i> + 1), greater than both neighbours (a peak) or less than both neighbours (a trough). The difference of this observed number from the mean expected number of turns for a randomly generated sequence, taken as a unit of the standard deviation, gives a I<Z>-score for assessing the "randomness" of the sequence, i.e., the absence of a factor systematically affecting the frequency of peaks/troughs, given that, for turns, there is "a fairly rapid tendency of the distribution to normality" (Kendall 1973, p. 24).
38
39 With these local fluctuations tested regardless of their spacing and magnitude, the test does not indicate if the changes actually cycle between highs and lows, if they are more or less balanced in magnitude, or if any cycling is periodic; only if oscillation in general is more common than linear progression.
40
41 =head1 METHODS
42
43 =head2 new
44
45 $turns = Statistics::Sequences::Turns->new();
46
47 Returns a new Turns object. Expects/accepts no arguments but the classname.
48
49 =head2 load
50
51 $turns->load(@data);
52 $turns->load(\@data);
53 $turns->load('foodat' => \@data); # labelled whatever
54
55 Loads data anonymously or by name - see L<load|Statistics::Data/load, load_data> in the Statistics::Data manpage for details on the various ways data can be loaded and then retrieved (more than shown here). Data must be numerical (ordinal, interval type). All elements must be numerical of the method croaks.
56
57 =cut
58
59 2 ▶sub load {
Subroutine load: called CC=2 SCAR=7.0
Pod documented
60 2 my $self = shift;
61 2 $self->SUPER::load(@_);
62 2 ▶ croak __PACKAGE__, '::load All data must be numerical for turns statistics'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->all_numeric($self->access("index", -1)) 0 2 2
63 if !$self->all_numeric( $self->access( index => -1 ) );
64 2 return 1;
65 }
66
67 =head2 add, read, unload
68
69 See L<Statistics::Data> for these additional operations on data that have been loaded.
70
71 =head2 observed
72
73 $v = $turns->observed(); # use anonymously loaded data
74 $v = $turns->observed(name => 'mysequence'); # ... or by "name" given on loading
75 $v = $turns->observed(data => \@data); # ... or just give the data now
76
77 Returns observed number of turns. This is the number of peaks and troughs, starting the count from index 1 of the sequence (a flat array), checking if both its immediate left/right (or past/future) neighbours are lesser than it (a peak) or greater than it (a trough). Wherever the values in successive indices in the sequence are equal, they are treated as a single observation/datum - so the following:
78
79 0 0 1 1 0 1 1 1 0 1
80
81 is counted up for turns as
82
83 0 1 0 1 0 1
84 * * * *
85
86 This shows four turns - two peaks (0 1 0) and two troughs (1 0 1).
87
88 Returns 0 if the given list of is empty, or the number of its elements is less than 3.
89
90 =cut
91
92 8 ▶sub observed {
Subroutine observed: called CC=9 SCAR=22.1
Pod documented
93 8 my $self = shift;
94 8 ▶ my $args = ref $_[0] ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 5 3 8
95 8 my $data = _set_data( $self, $args );
96 8 my $trials = scalar @{$data};
97 8 ▶ return 0 if not $trials or $trials < 3;
Branch: 1 branch
BranchTrueFalseTotal
if not $trials or $trials < 3 0 8 8
Condition33%1/3
not $trials or $trials < 3
l!l&&r!l&&!r
008
MC/DC0%0/2
not $trials or $trials < 3
not $trials $trials < 3
Truth table33%1/3
not(A) or B
A= not $trialsB= $trials < 3
ABresult
1X1
011
000
98 8 my ( $count, $i ) = (0);
99 8 ▶ for ( $i = 1 ; $i < $trials - 1 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $trials - 1) 276 8 284
100 276 ▶ if ( ( $data->[ $i - 1 ] > $data->[$i] )
Branch: 2 branches
BranchTrueFalseTotal
if ($$data[$i - 1] > $data->[$i] and $$data[$i + 1] > $data->[$i]) { } 88 188 276
elsif ($$data[$i - 1] < $data->[$i] and $$data[$i + 1] < $data->[$i]) { } 88 148 236
Condition100%3/3
$$data[$i - 1] > $data->[$i] and $$data[$i + 1] > $data->[$i]
!ll&&!rl&&r
1404888
MC/DC100%2/2
$$data[$i - 1] > $data->[$i] and $$data[$i + 1] > $data->[$i]
$$data[$i - 1] > $data->[$i] $$data[$i + 1] > $data->[$i]
Truth table100%3/3
A and B
A= $$data[$i - 1] > $data->[$i]B= $$data[$i + 1] > $data->[$i]
ABresult
0X0
100
111
101 && ( $data->[ $i + 1 ] > $data->[$i] ) )
102 { # trough at $i
103 88 $count++;
104 }
105 ▶ elsif (( $data->[ $i - 1 ] < $data->[$i] )
Condition100%3/3
$$data[$i - 1] < $data->[$i] and $$data[$i + 1] < $data->[$i]
!ll&&!rl&&r
485288
MC/DC100%2/2
$$data[$i - 1] < $data->[$i] and $$data[$i + 1] < $data->[$i]
$$data[$i - 1] < $data->[$i] $$data[$i + 1] < $data->[$i]
Truth table100%3/3
A and B
A= $$data[$i - 1] < $data->[$i]B= $$data[$i + 1] < $data->[$i]
ABresult
0X0
100
111
106 && ( $data->[ $i + 1 ] < $data->[$i] ) )
107 { # peak at $i
108 88 $count++;
109 }
110 }
111 8 return $count;
112 }
113
114 =head2 expected
115
116 $v = $turns->expected(); # use loaded data; or specify by "name"
117 $v = $turns->expected(data => \@data); # use these data
118 $v = $turns->expected(trials => POS_INT); # don't use actual data; calculate from this number of trials
119
120 Returns the expected number of turns, which is set by I<N> the number of trials/observations/sample-size ...:
121
122 =for html <p>&nbsp;&nbsp;<i>E[T]</i> = 2 / 3 (<i>N</i> &ndash; 2)
123
124 or, equivalently (in some sources),
125
126 =for html <p>&nbsp;&nbsp;<i>E[T]</i> = ( 2<i>N</i> &ndash; 4 ) / 3
127
128 =cut
129
130 7 ▶sub expected {
Subroutine expected: called CC=3 SCAR=11.0
Pod documented
131 7 my $self = shift;
132 7 ▶ my $args = ref $_[0] ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 7 7
133 my $trials =
134 defined $args->{'trials'}
135 ? $args->{'trials'}
136 7 ▶ : scalar( @{ _set_data( $self, $args ) } );
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'trials'} ? : 4 3 7
137 7 return 2 / 3 * ( $trials - 2 );
138
139 #return (2 * $trials - 4) / 3;
140 }
141
142 =head2 variance
143
144 $v = $turns->variance(); # use loaded data; or specify by "name"
145 $v = $turns->variance(data => \@data); # use these data
146 $v = $turns->variance(trials => POS_INT); # don't use actual data; calculate from this number of trials
147
148 Returns the expected variance in the number of turns for the given length of data I<N>.
149
150 =for html <p>&nbsp;&nbsp;<i>V[T]</i> = (16<i>N</i> &ndash; 29 ) / 90
151
152 =cut
153
154 8 ▶sub variance {
Subroutine variance: called CC=3 SCAR=11.0
Pod documented
155 8 my $self = shift;
156 8 ▶ my $args = ref $_[0] ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 8 8
157 my $trials =
158 defined $args->{'trials'}
159 ? $args->{'trials'}
160 8 ▶ : scalar( @{ _set_data( $self, $args ) } );
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'trials'} ? : 4 4 8
161 8 return ( 16 * $trials - 29 ) / 90;
162 }
163
164 =head2 obsdev
165
166 $v = $turns->obsdev(); # use data already loaded - anonymously; or specify its "name"
167 $v = $turns->obsdev(data => \@data); # use these data
168
169 Returns the observed deviation from expectation for the loaded/given sequence: observed I<less> expected turn-count (I<O> - I<E>). Alias of C<observed_deviation> is supported.
170
171 =cut
172
173 1 ▶sub obsdev {
Subroutine obsdev: called CC=1 SCAR=0.0
Pod documented
174 1 return observed(@_) - expected(@_);
175 }
176 3 *observed_deviation = \&obsdev;
177
178 =head2 stdev
179
180 $v = $turns->stdev(); # use data already loaded - anonymously; or specify its "name"
181 $v = $turns->stdev(data => \@data);
182
183 Returns square-root of the variance. Aliases C<standard_deviation> and C<stddev> (common in other Statistics modules) are supported.
184
185 =cut
186
187 2 ▶sub stdev {
Subroutine stdev: called CC=1 SCAR=0.0
Pod documented
188 2 return sqrt variance(@_);
189 }
190 3 *standard_deviation = \&stdev;
191 3 *stddev = \&stdev;
192
193 =head2 z_value
194
195 $z = $turns->z_value(ccorr => 1); # use data already loaded - anonymously; or specify its "name"
196 $z = $turns->z_value(data => $aref, ccorr => BOOL);
197 ($z, $p) = $turns->z_value(data => $aref, ccorr => BOOL, tails => 2); # same but wanting an array, get the p-value too
198
199 Returns the deviation ratio, or I<Z>-score, taking the turncount expected from that observed and dividing by the root variance, by default with a continuity correction in the numerator. Called in list context, returns the I<Z>-score with its normal distribution, two-tailed I<p>-value.
200
201 The data to test can already have been L<load|load>ed, or sent directly as an aref keyed as B<data>.
202
203 Optional named arguments B<tails> (1 or 2), B<ccorr> (Boolean for the continuity-correction), B<precision_s> (for the statistic, i.e., I<Z>-score) and B<precision_p> (for the I<p>-value).
204
205 The method can all be called with "sufficient" data: giving, instead of actual data, the B<observed> number of turns, and the number of B<trials>, the latter being sufficient to compute the expected number of turns and its variance.
206
207 Alias C<z_score> is supported.
208
209 =cut
210
211 4 ▶sub z_value {
Subroutine z_value: called CC=7 SCAR=19.7
Pod documented
212 4 my $self = shift;
213 4 ▶ my $args = ref $_[0] ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 3 4
214 4 my $data = _set_data( $self, $args );
215 my $trials =
216 4 ▶ defined $args->{'trials'} ? $args->{'trials'} : scalar @{$data};
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'trials'} ? : 0 4 4
217 4 my $zed = Statistics::Zed->new();
218 my ( $zval, $pval ) = $zed->zscore(
219 observed => defined $args->{'observed'}
220 ? $args->{'observed'}
221 : $self->observed($args),
222 expected => $self->expected( trials => $trials ),
223 variance => $self->variance( trials => $trials ),
224 ccorr => defined $args->{'ccorr'} ? $args->{'ccorr'} : 1,
225 tails => $args->{'tails'} || 2,
226 precision_s => $args->{'precision_s'},
227 4 ▶ precision_p => $args->{'precision_p'},
Branch: 2 branches
BranchTrueFalseTotal
defined $args->{'observed'} ? : 0 4 4
defined $args->{'ccorr'} ? : 2 2 4
Condition50%1/2
$args->{'tails'} || 2
l!l
04
MC/DC0%0/1
$args->{'tails'} || 2
$args->{'tails'}
Truth table50%1/2
A
A= $args->{'tails'}
Aresult
11
00
228 );
229 4 ▶ return wantarray ? ( $zval, $pval ) : $zval;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 3 4
230 }
231 3 *z_score = \&z_value;
232
233 =head2 p_value
234
235 $p = $turns->p_value(); # using loaded data and default args
236 $p = $turns->p_value(ccorr => BOOL, tails => 1|2); # normal-approximation based on loaded data
237 $p = $turns->p_value(data => $aref, ccorr => BOOL, tails => 2); # using given data
238
239 Returns the normal distribution I<p>-value for the deviation ratio (I<Z>-score) of the observed number of turns, 2-tailed and continuity-correct by default (or set B<tails> => 1 and B<ccorr> => 0, respectively). Other arguments are as for L<z_value|Statistics::Sequences::Turns/z_value>.
240
241 =cut
242
243 1 ▶sub p_value {
Subroutine p_value: called CC=1 SCAR=0.0
Pod documented
244 1 return ( z_value(@_) )[1];
245 }
246
247 =head2 stats_hash
248
249 $href = $turns->stats_hash(values => [qw/observed expected variance z_value p_value/], ccorr => 1);
250
251 Returns a hashref for the counts and stats as specified in its "values" argument, and with any options for calculating them. See L<stats_hash|Statistics::Sequences/stats_hash> in the Statistics::Sequences manpage for details/options. If calling via a "turns" object, the option "stat => 'turns'" is not needed (unlike when using the parent Statistics::Sequences object).
252
253 =head2 dump
254
255 $turns->dump(flag => BOOL, verbose => BOOL, format => 'table|labline|csv');
256
257 Print test results to STDOUT. See L<dump|Statistics::Sequences/dump> in the Statistics::Sequences manpage for details/options.
258
259 =cut
260
261 0 ▶sub dump {
Subroutine dump: not called CC=2 SCAR=17.9
Pod documented
262 0 my $self = shift;
263 0 ▶ my $args = ref $_[0] ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
264 0 $args->{'stat'} = 'turns';
265 0 $self->SUPER::dump($args);
266 0 return $self;
267 }
268
269 19 ▶sub _set_data { # Get data via Statistics::Date
Subroutine _set_data: called CC=6 SCAR=18.0
270 # Remove equivalent successors: e.g., strip 2nd 2 from (3, 2, 2, 7, 2):
271 19 my $self = shift;
272 19 ▶ my $args = ref $_[0] ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 19 0 19
273 19 my $data = $self->access($args)
274 ; # have been already checked to be numerical if previously load()'ed
275 19 ▶ ref $data or croak __PACKAGE__, '::Data for counting up turns are needed';
Branch: 1 branch
BranchTrueFalseTotal
ref $data or croak(__PACKAGE__, "::Data for counting up turns are needed") 19 0 19
276 19 my @data_u = ();
277 19 for my $i ( 0 .. ( scalar @{$data} - 1 ) ) {
278 731 ▶ push @data_u, $data->[$i]
Branch: 1 branch
BranchTrueFalseTotal
if not scalar @data_u or $data->[$i] != $data_u[-1] 711 20 731
Condition100%3/3
not scalar @data_u or $data->[$i] != $data_u[-1]
l!l&&r!l&&!r
1969220
MC/DC100%2/2
not scalar @data_u or $data->[$i] != $data_u[-1]
not scalar @data_u $data->[$i] != $data_u[-1]
Truth table100%3/3
not(A) or B
A= not scalar @data_uB= $data->[$i] != $data_u[-1]
ABresult
1X1
011
000
279 if not scalar @data_u
280 or $data->[$i] != $data_u[-1];
281 }
282 19 return \@data_u;
283 }
284
285 __END__
↑ Summary