Descriptive/Full.pm

filter: Statementstmt 95.4% 400 / 419 Branchbran 82.5% 99 / 120 Conditioncond 78.2% 18 / 23 MC/DCmcdc 60.0% 9 / 15 Subroutinesub 95.5% 43 / 45 Podpod 100% 26 / 26 totaltotal 91.8% 595 / 648 CC 82 SCAR 16.3
CC
82
Cov
92%
frequency_distribution_ref
14.5
least_squares_fit
8.4
quantile
7.2
CRAP 5.1
« Descriptive/Sparse.pm ↑ Summary Descriptive/Smoother.pm »
1 package Statistics::Descriptive::Full;
2 11 $Statistics::Descriptive::Full::VERSION = '3.0801';
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Statistics::Descriptive::Smoother;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 ▶use vars qw($a $b %fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 ▶use parent qw(Statistics::Descriptive::Sparse);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 11 ▶use List::MoreUtils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 ## no critic (ProhibitExplicitReturnUndef)
18 ##Create a list of fields not to remove when data is updated
19 11 %fields = (
20 _permitted => undef, ##Place holder for the inherited key hash
21 data => undef, ##Our data
22 samples => undef, ##Number of samples for each value of the data set
23 presorted => undef, ##Flag to indicate the data is already sorted
24 _reserved => undef, ##Place holder for this lookup hash
25 );
26  
27 11 __PACKAGE__->_make_private_accessors(
28 [
29 qw(data samples frequency geometric_mean harmonic_mean
30 least_squares_fit median mode
31 skewness kurtosis median_absolute_deviation
32 )
33 ]
34 );
35 11 __PACKAGE__->_make_accessors( [qw(presorted _reserved _trimmed_mean_cache)] );
36  
37 sub _clear_fields
38 39 ▶{
Subroutine _clear_fields: called CC=1 SCAR=0.0
39 39 my $self = shift;
40  
41 # Empty array ref for holding data later!
42 39 $self->_data( [] );
43 39 $self->_samples( [] );
44 39 $self->_reserved( \%fields );
45 39 $self->presorted(0);
46 39 $self->_trimmed_mean_cache( +{} );
47  
48 39 return;
49 }
50  
51 ##Have to override the base method to add the data to the object
52 ##The proxy method from above is still valid
53 sub new
54 39 ▶{
Subroutine new: called CC=2 SCAR=7.0
Pod documented
55 39 my $proto = shift;
56 39 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
1380
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
57  
58 # Create my self re SUPER
59 39 my $self = $class->SUPER::new();
60 39 bless( $self, $class ); #Re-anneal the object
61 39 $self->_clear_fields();
62 39 return $self;
63 }
64  
65 sub _is_reserved
66 0 ▶{
Subroutine _is_reserved: not called CC=1 SCAR=6.9
67 0 my $self = shift;
68 0 my $field = shift;
69  
70 0 return exists( $self->_reserved->{$field} );
71 }
72  
73 sub _delete_all_cached_keys
74 4 ▶{
Subroutine _delete_all_cached_keys: called CC=2 SCAR=6.9
75 4 my $self = shift;
76  
77 4 my %keys = %{$self};
78  
79 # Remove reserved keys for this class from the deletion list
80 4 delete @keys{ keys %{ $self->_reserved } };
81 4 delete @keys{ keys %{ $self->_permitted } };
82 4 delete $keys{_trimmed_mean_cache};
83  
84 KEYS_LOOP:
85 4 foreach my $key ( keys %keys )
86 { # Check each key in the object
87 6 delete $self->{$key}; # Delete any out of date cached key
88 }
89 4 $self->{_trimmed_mean_cache} = {}; # just reset this one
90 4 return;
91 }
92  
93 ##Clear a stat. More efficient than destroying an object and calling
94 ##new.
95 sub clear
96 0 ▶{
Subroutine clear: not called CC=2 SCAR=17.9
Pod documented
97 0 my $self = shift; ##Myself
98 0 my $key;
99  
100 0 ▶ if ( !$self->count() )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->count) 0 0 0
101 {
102 0 return;
103 }
104  
105 0 $self->_delete_all_cached_keys();
106 0 $self->SUPER::clear();
107 0 $self->_clear_fields();
108 }
109  
110 sub add_data
111 39 ▶{
Subroutine add_data: called CC=5 SCAR=16.1
Pod documented
112 39 my $self = shift; ##Myself
113  
114 39 my $aref;
115  
116 39 ▶ if ( ref $_[0] eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 3 36 39
117 {
118 3 $aref = $_[0];
119 }
120 else
121 {
122 36 $aref = \@_;
123 }
124  
125 ##If we were given no data, we do nothing.
126 39 ▶ return 1 if ( !@{$aref} );
Branch: 1 branch
BranchTrueFalseTotal
unless @{$aref;} 0 39 39
127  
128 39 my $oldmean;
129 39 my ( $min, $max, $sum, $sumsq );
130 39 my $count = $self->count;
131  
132 # $count is modified lower down, but we need this flag after that
133 39 my $has_existing_data = $count;
134  
135 # Take care of appending to an existing data set
136 39 ▶ if ($has_existing_data)
Branch: 1 branch
BranchTrueFalseTotal
if ($has_existing_data) { } 4 35 39
137 {
138 4 $min = $self->min();
139 4 $max = $self->max();
140 4 $sum = $self->sum();
141 4 $sumsq = $self->sumsq();
142 }
143 else
144 {
145 35 $min = $aref->[0];
146 35 $max = $aref->[0];
147 35 $sum = 0;
148 35 $sumsq = 0;
149 }
150  
151 # need to allow for already having data
152 39 $sum += List::Util::sum(@$aref);
153 39 $sumsq += List::Util::sum( map { $_**2 } @$aref );
154 39 $max = List::Util::max( $max, @$aref );
155 39 $min = List::Util::min( $min, @$aref );
156 39 $count += scalar @$aref;
157 39 my $mean = $sum / $count;
158  
159 39 $self->min($min);
160 39 $self->max($max);
161 39 $self->sample_range( $max - $min );
162 39 $self->sum($sum);
163 39 $self->sumsq($sumsq);
164 39 $self->mean($mean);
165 39 $self->count($count);
166  
167 ##Variance isn't commonly enough
168 ##used to recompute every single data add, so just clear its cache.
169 39 $self->_variance(undef);
170  
171 39 push @{ $self->_data() }, @{$aref};
172  
173 # no need to clear keys if we are a newly populated object,
174 # and profiling shows it takes a long time when creating
175 # and populating many stats objects
176 39 ▶ if ($has_existing_data)
Branch: 1 branch
BranchTrueFalseTotal
if ($has_existing_data) 4 35 39
177 {
178 ##Clear the presorted flag
179 4 $self->presorted(0);
180 4 $self->_delete_all_cached_keys();
181 }
182  
183 39 return 1;
184 }
185  
186 sub add_data_with_samples
187 1 ▶{
Subroutine add_data_with_samples: called CC=2 SCAR=6.9
Pod documented
188 1 my ( $self, $aref_values ) = @_;
189  
190 1 ▶ return 1 if ( !@{$aref_values} );
Branch: 1 branch
BranchTrueFalseTotal
unless @{$aref_values;} 0 1 1
191  
192 1 my $aref_data = [ map { keys %$_ } @{$aref_values} ];
193 1 my $aref_samples = [ map { values %$_ } @{$aref_values} ];
194  
195 1 $self->add_data($aref_data);
196 1 push @{ $self->_samples() }, @{$aref_samples};
197  
198 1 return 1;
199 }
200  
201 sub get_data
202 18 ▶{
Subroutine get_data: called CC=1 SCAR=0.0
Pod documented
203 18 my $self = shift;
204 18 return @{ $self->_data() };
205 }
206  
207 sub get_data_without_outliers
208 5 ▶{
Subroutine get_data_without_outliers: called CC=4 SCAR=13.9
Pod documented
209 5 my $self = shift;
210  
211 5 ▶ if ( $self->count() < $Statistics::Descriptive::Min_samples_number )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->count < $Statistics::Descriptive::Min_samples_number) 1 4 5
212 {
213 1 carp(
214 "Need at least $Statistics::Descriptive::Min_samples_number samples\n"
215 );
216 1 return;
217 }
218  
219 4 ▶ if ( !defined $self->{_outlier_filter} )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_outlier_filter"}) 1 3 4
220 {
221 1 carp("Outliers filter not defined\n");
222 1 return;
223 }
224  
225 3 my $outlier_candidate_index = $self->_outlier_candidate_index;
226 3 my $possible_outlier = ( $self->_data() )->[$outlier_candidate_index];
227 3 my $is_outlier = $self->{_outlier_filter}->( $self, $possible_outlier );
228  
229 3 ▶ return $self->get_data unless $is_outlier;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_outlier 1 2 3
230  
231 # Removing the outlier from the dataset
232 my @good_indexes =
233 2 grep { $_ != $outlier_candidate_index } ( 0 .. $self->count() - 1 );
234  
235 2 my @data = $self->get_data;
236 2 my @filtered_data = @data[@good_indexes];
237 2 return @filtered_data;
238 }
239  
240 sub set_outlier_filter
241 6 ▶{
Subroutine set_outlier_filter: called CC=3 SCAR=11.0
Pod documented
242 6 my ( $self, $code_ref ) = @_;
243  
244 6 ▶ if ( !$code_ref || ref($code_ref) ne "CODE" )
Branch: 1 branch
BranchTrueFalseTotal
if (not $code_ref or ref $code_ref ne "CODE") 2 4 6
Condition100%3/3
not $code_ref or ref $code_ref ne "CODE"
l!l&&r!l&&!r
114
MC/DC100%2/2
not $code_ref or ref $code_ref ne "CODE"
not $code_ref ref $code_ref ne "CODE"
Truth table100%3/3
not(A) or B
A= not $code_refB= ref $code_ref ne "CODE"
ABresult
1X1
011
000
245 {
246 2 carp("Need to pass a code reference");
247 2 return;
248 }
249  
250 4 $self->{_outlier_filter} = $code_ref;
251 4 return 1;
252 }
253  
254 sub _outlier_candidate_index
255 3 ▶{
Subroutine _outlier_candidate_index: called CC=3 SCAR=11.0
256 3 my $self = shift;
257  
258 3 my $mean = $self->mean();
259 3 my $outlier_candidate_index = 0;
260 3 my $max_std_deviation = abs( ( $self->_data() )->[0] - $mean );
261 3 foreach my $idx ( 1 .. ( $self->count() - 1 ) )
262 {
263 21 my $curr_value = ( $self->_data() )->[$idx];
264 21 ▶ if ( $max_std_deviation < abs( $curr_value - $mean ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($max_std_deviation < abs $curr_value - $mean) 3 18 21
265 {
266 3 $outlier_candidate_index = $idx;
267 3 $max_std_deviation = abs( $curr_value - $mean );
268 }
269 }
270 3 return $outlier_candidate_index;
271 }
272  
273 sub set_smoother
274 2 ▶{
Subroutine set_smoother: called CC=1 SCAR=0.0
Pod documented
275 2 my ( $self, $args ) = @_;
276  
277 2 $args->{data} = $self->_data();
278 2 $args->{samples} = $self->_samples();
279  
280 2 $self->{_smoother} = Statistics::Descriptive::Smoother->instantiate($args);
281 }
282  
283 sub get_smoothed_data
284 2 ▶{
Subroutine get_smoothed_data: called CC=2 SCAR=6.9
Pod documented
285 2 my ( $self, $args ) = @_;
286  
287 2 ▶ if ( !defined $self->{_smoother} )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_smoother"}) 1 1 2
288 {
289 1 carp("Smoother object not defined\n");
290 1 return;
291 }
292 1 $self->{_smoother}->get_smoothed_data();
293 }
294  
295 sub maxdex
296 4 ▶{
Subroutine maxdex: called CC=3 SCAR=11.0
Pod documented
297 4 my $self = shift;
298  
299 4 ▶ return undef if !$self->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count 1 3 4
300 3 my $maxdex;
301  
302 3 ▶ if ( $self->presorted )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->presorted) { } 1 2 3
303 {
304 1 $maxdex = $self->count - 1;
305 }
306 else
307 {
308 2 my $max = $self->max;
309 2 ▶ $maxdex = List::MoreUtils::first_index { $_ == $max } $self->get_data;
Subroutine __ANON__: called CC=1 SCAR=0.0
310 }
311  
312 3 $self->{maxdex} = $maxdex;
313  
314 3 return $maxdex;
315 }
316  
317 sub mindex
318 4 ▶{
Subroutine mindex: called CC=3 SCAR=11.0
Pod documented
319 4 my $self = shift;
320  
321 4 ▶ return undef if !$self->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count 1 3 4
322  
323 #my $maxdex = $self->{maxdex};
324 #return $maxdex if defined $maxdex;
325 3 my $mindex;
326  
327 3 ▶ if ( $self->presorted )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->presorted) { } 1 2 3
328 {
329 1 $mindex = 0;
330 }
331 else
332 {
333 2 my $min = $self->min;
334 2 ▶ $mindex = List::MoreUtils::first_index { $_ == $min } $self->get_data;
Subroutine __ANON__: called CC=1 SCAR=0.0
335 }
336  
337 3 $self->{mindex} = $mindex;
338  
339 3 return $mindex;
340 }
341  
342 sub sort_data
343 28 ▶{
Subroutine sort_data: called CC=2 SCAR=6.9
Pod documented
344 28 my $self = shift;
345  
346 28 ▶ if ( !$self->presorted() )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->presorted) 12 16 28
347 {
348 ##Sort the data in descending order
349 12 $self->_data( [ sort { $a <=> $b } @{ $self->_data() } ] );
350 12 $self->presorted(1);
351 ##Fix the maxima and minima indices - no, this is unnecessary now we have methods
352 #$self->mindex(0);
353 #$self->maxdex($#{$self->_data()});
354 }
355  
356 28 return 1;
357 }
358  
359 sub percentile
360 4 ▶{
Subroutine percentile: called CC=5 SCAR=16.2
Pod documented
361 4 my $self = shift;
362 4 ▶ my $percentile = shift || 0;
Condition100%2/2
shift() || 0
l!l
31
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
363 ##Since we're returning a single value there's no real need
364 ##to cache this.
365  
366 ##If the requested percentile is less than the "percentile bin
367 ##size" then return undef. Check description of RFC 2330 in the
368 ##POD below.
369 4 my $count = $self->count();
370  
371 4 ▶ if ( ( !$count ) || ( $percentile < 100 / $count ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not $count or $percentile < 100 / $count) 2 2 4
Condition66%2/3
not $count or $percentile < 100 / $count
l!l&&r!l&&!r
202
MC/DC50%1/2
not $count or $percentile < 100 / $count
not $count $percentile < 100 / $count
Truth table66%2/3
not(A) or B
A= not $countB= $percentile < 100 / $count
ABresult
1X1
011
000
372 {
373 2 return; # allow for both scalar and list context
374 }
375  
376 2 $self->sort_data();
377 2 my $num = $count * $percentile / 100;
378 2 my $index = &POSIX::ceil($num) - 1;
379 2 my $val = $self->_data->[$index];
380 return wantarray
381 2 ▶ ? ( $val, $index )
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
382 : $val;
383 }
384  
385 sub _calc_new_median
386 7 ▶{
Subroutine _calc_new_median: called CC=2 SCAR=6.9
387 7 my $self = shift;
388 7 my $count = $self->count();
389  
390 ##Even or odd
391 7 ▶ if ( $count % 2 )
Branch: 1 branch
BranchTrueFalseTotal
if ($count % 2) { } 4 3 7
392 {
393 4 return $self->_data->[ ( $count - 1 ) / 2 ];
394 }
395 else
396 {
397 return (
398 (
399 3 $self->_data->[ ($count) / 2 ] +
400 $self->_data->[ ( $count - 2 ) / 2 ]
401 ) / 2
402 );
403 }
404 }
405  
406 sub median
407 17 ▶{
Subroutine median: called CC=3 SCAR=11.0
Pod documented
408 17 my $self = shift;
409  
410 17 ▶ return undef if !$self->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count 2 15 17
411  
412 ##Cached?
413 15 ▶ if ( !defined( $self->_median() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_median) 7 8 15
414 {
415 7 $self->sort_data();
416 7 $self->_median( $self->_calc_new_median() );
417 }
418 15 return $self->_median();
419 }
420  
421 sub quantile
422 18 ▶{
Subroutine quantile: called CC=7 SCAR=19.8
Pod documented
423 18 my ( $self, $QuantileNumber ) = @_;
424  
425 18 ▶ unless ( defined $QuantileNumber and $QuantileNumber =~ m/^0|1|2|3|4$/ )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $QuantileNumber and $QuantileNumber =~ /^0|1|2|3|4$/) 0 18 18
Condition33%1/3
defined $QuantileNumber and $QuantileNumber =~ /^0|1|2|3|4$/
!ll&&!rl&&r
0018
MC/DC0%0/2
defined $QuantileNumber and $QuantileNumber =~ /^0|1|2|3|4$/
defined $QuantileNumber $QuantileNumber =~ /^0|1|2|3|4$/
Truth table33%1/3
A and B
A= defined $QuantileNumberB= $QuantileNumber =~ /^0|1|2|3|4$/
ABresult
0X0
100
111
426 {
427 0 carp("Bad quartile type, must be 0, 1, 2, 3 or 4\n");
428 0 return;
429 }
430  
431 # check data count after the args are checked - should help debugging
432 18 ▶ return undef if !$self->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count 1 17 18
433  
434 17 $self->sort_data();
435  
436 17 ▶ return $self->_data->[0] if ( $QuantileNumber == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $QuantileNumber == 0 3 14 17
437  
438 14 my $count = $self->count();
439  
440 14 ▶ return $self->_data->[ $count - 1 ] if ( $QuantileNumber == 4 );
Branch: 1 branch
BranchTrueFalseTotal
if $QuantileNumber == 4 3 11 14
441  
442 11 my $K_quantile = ( ( $QuantileNumber / 4 ) * ( $count - 1 ) + 1 );
443 11 my $F_quantile = $K_quantile - POSIX::floor($K_quantile);
444 11 $K_quantile = POSIX::floor($K_quantile);
445  
446 # interpolation
447 11 my $aK_quantile = $self->_data->[ $K_quantile - 1 ];
448 11 ▶ return $aK_quantile if ( $F_quantile == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $F_quantile == 0 4 7 11
449 7 my $aKPlus_quantile = $self->_data->[$K_quantile];
450  
451 # Calcul quantile
452 7 my $quantile =
453 $aK_quantile + ( $F_quantile * ( $aKPlus_quantile - $aK_quantile ) );
454  
455 7 return $quantile;
456 }
457  
458 sub _real_calc_trimmed_mean
459 2 ▶{
Subroutine _real_calc_trimmed_mean: called CC=2 SCAR=6.9
460 2 my $self = shift;
461 2 my $lower = shift;
462 2 my $upper = shift;
463  
464 2 my $lower_trim = int( $self->count() * $lower );
465 2 my $upper_trim = int( $self->count() * $upper );
466 2 my ( $val, $oldmean ) = ( 0, 0 );
467 2 my ( $tm_count, $tm_mean, $index ) = ( 0, 0, $lower_trim );
468  
469 2 $self->sort_data();
470 2 ▶ while ( $index <= $self->count() - $upper_trim - 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($index <= $self->count - $upper_trim - 1) 17 2 19
471 {
472 17 $val = $self->_data()->[$index];
473 17 $oldmean = $tm_mean;
474 17 ++$index;
475 17 ++$tm_count;
476 17 $tm_mean += ( $val - $oldmean ) / $tm_count;
477 }
478  
479 2 return $tm_mean;
480 }
481  
482 sub trimmed_mean
483 4 ▶{
Subroutine trimmed_mean: called CC=4 SCAR=13.9
Pod documented
484 4 my $self = shift;
485 4 my ( $lower, $upper );
486  
487 #upper bound is in arg list or is same as lower
488 4 ▶ if ( @_ == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 2 2 4
489 {
490 2 ( $lower, $upper ) = ( $_[0], $_[0] );
491 }
492 else
493 {
494 2 ( $lower, $upper ) = ( $_[0], $_[1] );
495 }
496  
497 # check data count after the args
498 4 ▶ return undef if !$self->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count 1 3 4
499  
500 ##Cache
501 3 my $thistm = join ':', $lower, $upper;
502 3 my $cache = $self->_trimmed_mean_cache();
503 3 ▶ if ( !exists( $cache->{$thistm} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $cache->{$thistm}) 2 1 3
504 {
505 2 $cache->{$thistm} = $self->_real_calc_trimmed_mean( $lower, $upper );
506 }
507  
508 3 return $cache->{$thistm};
509 }
510  
511 sub _test_for_too_small_val
512 15 ▶{
Subroutine _test_for_too_small_val: called CC=1 SCAR=0.0
513 15 my $self = shift;
514 15 my $val = shift;
515  
516 15 return ( abs($val) <= $Statistics::Descriptive::Tolerance );
517 }
518  
519 sub _calc_harmonic_mean
520 5 ▶{
Subroutine _calc_harmonic_mean: called CC=4 SCAR=13.9
521 5 my $self = shift;
522  
523 5 my $hs = 0;
524  
525 5 foreach my $item ( @{ $self->_data() } )
526 {
527 ##Guarantee that there are no divide by zeros
528 11 ▶ if ( $self->_test_for_too_small_val($item) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_test_for_too_small_val($item)) 1 10 11
529 {
530 1 return;
531 }
532  
533 10 $hs += 1 / $item;
534 }
535  
536 4 ▶ if ( $self->_test_for_too_small_val($hs) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_test_for_too_small_val($hs)) 3 1 4
537 {
538 3 return;
539 }
540  
541 1 return $self->count() / $hs;
542 }
543  
544 sub harmonic_mean
545 5 ▶{
Subroutine harmonic_mean: called CC=2 SCAR=7.0
Pod documented
546 5 my $self = shift;
547  
548 5 ▶ if ( !defined( $self->_harmonic_mean() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_harmonic_mean) 5 0 5
549 {
550 5 $self->_harmonic_mean( scalar( $self->_calc_harmonic_mean() ) );
551 }
552  
553 5 return $self->_harmonic_mean();
554 }
555  
556 sub mode
557 5 ▶{
Subroutine mode: called CC=6 SCAR=17.9
Pod documented
558 5 my $self = shift;
559  
560 5 ▶ if ( !defined( $self->_mode() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_mode) 3 2 5
561 {
562 3 my $mode = 0;
563 3 my $occurances = 0;
564  
565 3 my %count;
566  
567 3 foreach my $item ( @{ $self->_data() } )
568 {
569 11 my $count = ++$count{$item};
570 11 ▶ if ( $count > $occurances )
Branch: 1 branch
BranchTrueFalseTotal
if ($count > $occurances) 4 7 11
571 {
572 4 $mode = $item;
573 4 $occurances = $count;
574 }
575 }
576  
577 $self->_mode(
578 3 ▶ ( $occurances > 1 )
Branch: 1 branch
BranchTrueFalseTotal
$occurances > 1 ? : 1 2 3
579 ? { exists => 1, mode => $mode }
580 : { exists => 0, }
581 );
582 }
583  
584 5 my $m = $self->_mode;
585  
586 5 ▶ return $m->{'exists'} ? $m->{mode} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$m->{'exists'} ? : 2 3 5
587 }
588  
589 sub geometric_mean
590 2 ▶{
Subroutine geometric_mean: called CC=5 SCAR=16.3
Pod documented
591 2 my $self = shift;
592  
593 2 ▶ return undef if !$self->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count 1 1 2
594  
595 1 ▶ if ( !defined( $self->_geometric_mean() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_geometric_mean) 1 0 1
596 {
597 1 my $gm = 1;
598 1 my $exponent = 1 / $self->count();
599  
600 1 for my $val ( @{ $self->_data() } )
601 {
602 3 ▶ if ( $val < 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($val < 0) 0 3 3
603 {
604 0 return undef;
605 }
606 3 $gm *= $val**$exponent;
607 }
608  
609 1 $self->_geometric_mean($gm);
610 }
611  
612 1 return $self->_geometric_mean();
613 }
614  
615 sub skewness
616 7 ▶{
Subroutine skewness: called CC=5 SCAR=16.1
Pod documented
617 7 my $self = shift;
618  
619 7 ▶ if ( !defined( $self->_skewness() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_skewness) 7 0 7
620 {
621 7 my $n = $self->count();
622 7 my $sd = $self->standard_deviation();
623  
624 7 my $skew;
625  
626 # skip if insufficient records
627 7 ▶ if ( $sd && $n > 2 )
Branch: 1 branch
BranchTrueFalseTotal
if ($sd and $n > 2) 5 2 7
Condition100%3/3
$sd and $n > 2
!ll&&!rl&&r
115
MC/DC100%2/2
$sd and $n > 2
$sd $n > 2
Truth table100%3/3
A and B
A= $sdB= $n > 2
ABresult
0X0
100
111
628 {
629  
630 5 my $mean = $self->mean();
631  
632 5 my $sum_pow3;
633 5 foreach my $rec ( $self->get_data )
634 {
635 83 $sum_pow3 += ( ( $rec - $mean ) / $sd )**3;
636 }
637  
638 5 my $correction = $n / ( ( $n - 1 ) * ( $n - 2 ) );
639  
640 5 $skew = $correction * $sum_pow3;
641 }
642  
643 7 $self->_skewness($skew);
644 }
645  
646 7 return $self->_skewness();
647 }
648  
649 sub kurtosis
650 7 ▶{
Subroutine kurtosis: called CC=5 SCAR=16.1
Pod documented
651 7 my $self = shift;
652  
653 7 ▶ if ( !defined( $self->_kurtosis() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_kurtosis) 7 0 7
654 {
655 7 my $kurt;
656  
657 7 my $n = $self->count();
658 7 my $sd = $self->standard_deviation();
659  
660 7 ▶ if ( $sd && $n > 3 )
Branch: 1 branch
BranchTrueFalseTotal
if ($sd and $n > 3) 5 2 7
Condition100%3/3
$sd and $n > 3
!ll&&!rl&&r
115
MC/DC100%2/2
$sd and $n > 3
$sd $n > 3
Truth table100%3/3
A and B
A= $sdB= $n > 3
ABresult
0X0
100
111
661 {
662  
663 5 my $mean = $self->mean();
664  
665 5 my $sum_pow4;
666 5 foreach my $rec ( $self->get_data )
667 {
668 83 $sum_pow4 += ( ( $rec - $mean ) / $sd )**4;
669 }
670  
671 5 my $correction1 =
672 ( $n * ( $n + 1 ) ) / ( ( $n - 1 ) * ( $n - 2 ) * ( $n - 3 ) );
673 5 my $correction2 =
674 ( 3 * ( $n - 1 )**2 ) / ( ( $n - 2 ) * ( $n - 3 ) );
675  
676 5 $kurt = ( $correction1 * $sum_pow4 ) - $correction2;
677 }
678  
679 7 $self->_kurtosis($kurt);
680 }
681  
682 7 return $self->_kurtosis();
683 }
684  
685 sub frequency_distribution_ref
686 6 ▶{
Subroutine frequency_distribution_ref: called CC=14 SCAR=26.7
Pod documented
687 6 my $self = shift;
688 6 my @k = ();
689  
690 # Must have at least two elements
691 6 ▶ if ( $self->count() < 2 )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->count < 2) 1 5 6
692 {
693 1 return undef;
694 }
695  
696 5 ▶ if ( ( !@_ ) && ( defined $self->_frequency() ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not @_ and defined $self->_frequency) 1 4 5
Condition66%2/3
not @_ and defined $self->_frequency
!ll&&!rl&&r
401
MC/DC50%1/2
not @_ and defined $self->_frequency
not @_ defined $self->_frequency
Truth table66%2/3
not(A) and B
A= not @_B= defined $self->_frequency
ABresult
0X0
100
111
697 {
698 1 return $self->_frequency();
699 }
700  
701 4 my %bins;
702 4 my $partitions = shift;
703  
704 4 ▶ if ( ref($partitions) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $partitions eq 'ARRAY') { } 2 2 4
705 {
706 2 @k = @{$partitions};
707 2 ▶ return undef unless @k; ##Empty array
Branch: 1 branch
BranchTrueFalseTotal
unless @k 0 2 2
708 2 ▶ if ( @k > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@k > 1) 2 0 2
709 {
710 ##Check for monotonicity
711 2 my $element = $k[0];
712 2 for my $next_elem ( @k[ 1 .. $#k ] )
713 {
714 8 ▶ if ( $element > $next_elem )
Branch: 1 branch
BranchTrueFalseTotal
if ($element > $next_elem) 0 8 8
715 {
716 0 carp
717 "Non monotonic array cannot be used as frequency bins!\n";
718 0 return undef;
719 }
720 8 $element = $next_elem;
721 }
722 }
723 2 %bins = map { $_ => 0 } @k;
724 }
725 else
726 {
727 2 ▶ return undef unless $partitions >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $partitions >= 1 0 2 2
728 2 my $interval = $self->sample_range() / $partitions;
729 2 foreach my $idx ( 1 .. ( $partitions - 1 ) )
730 {
731 20 push @k, ( $self->min() + $idx * $interval );
732 }
733  
734 2 $bins{ $self->max() } = 0;
735  
736 2 push @k, $self->max();
737 }
738  
739 ELEMENT:
740 4 foreach my $element ( @{ $self->_data() } )
741 {
742 129 foreach my $limit (@k)
743 {
744 1134 ▶ if ( $element <= $limit )
Branch: 1 branch
BranchTrueFalseTotal
if ($element <= $limit) 129 1005 1134
745 {
746 129 $bins{$limit}++;
747 129 next ELEMENT;
748 }
749 }
750 }
751  
752 4 return $self->_frequency( \%bins );
753 }
754  
755 sub frequency_distribution
756 5 ▶{
Subroutine frequency_distribution: called CC=2 SCAR=6.9
Pod documented
757 5 my $self = shift;
758  
759 5 my $ret = $self->frequency_distribution_ref(@_);
760  
761 5 ▶ if ( !defined($ret) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ret) { } 1 4 5
762 {
763 1 return undef;
764 }
765 else
766 {
767 4 return %$ret;
768 }
769 }
770  
771 sub least_squares_fit
772 3 ▶{
Subroutine least_squares_fit: called CC=8 SCAR=21.3
Pod documented
773 3 my $self = shift;
774 3 ▶ return () if $self->count() < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->count < 2 2 1 3
775  
776 ##Sigma sums
777 1 my ( $sigmaxy, $sigmax, $sigmaxx, $sigmayy, $sigmay ) =
778 ( 0, 0, 0, 0, $self->sum );
779 1 my ( $xvar, $yvar, $err );
780  
781 ##Work variables
782 1 my ( $iter, $y, $x, $denom ) = ( 0, 0, 0, 0 );
783 1 my $count = $self->count();
784 1 my @x;
785  
786 ##Outputs
787 1 my ( $m, $q, $r, $rms );
788  
789 1 ▶ if ( !defined $_[1] )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_[1]) { } 1 0 1
790 {
791 1 @x = 1 .. $self->count();
792 }
793 else
794 {
795 0 @x = @_;
796 0 ▶ if ( $self->count() != scalar @x )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->count != scalar @x) 0 0 0
797 {
798 0 carp "Range and domain are of unequal length.";
799 0 return ();
800 }
801 }
802 1 foreach my $x_val (@x)
803 {
804 4 $y = $self->_data->[$iter];
805 4 $sigmayy += $y * $y;
806 4 $sigmaxx += $x_val * $x_val;
807 4 $sigmaxy += $x_val * $y;
808 4 $sigmax += $x_val;
809 4 ++$iter;
810 }
811 1 $denom = $count * $sigmaxx - $sigmax * $sigmax;
812 return ()
813 1 ▶ unless abs($denom) > $Statistics::Descriptive::Tolerance;
Branch: 1 branch
BranchTrueFalseTotal
unless abs $denom > $Statistics::Descriptive::Tolerance 0 1 1
814  
815 1 $m = ( $count * $sigmaxy - $sigmax * $sigmay ) / $denom;
816 1 $q = ( $sigmaxx * $sigmay - $sigmax * $sigmaxy ) / $denom;
817  
818 1 $xvar = $sigmaxx - $sigmax * $sigmax / $count;
819 1 $yvar = $sigmayy - $sigmay * $sigmay / $count;
820  
821 1 $denom = sqrt( $xvar * $yvar );
822 1 ▶ return () unless ( abs($denom) > $Statistics::Descriptive::Tolerance );
Branch: 1 branch
BranchTrueFalseTotal
unless abs $denom > $Statistics::Descriptive::Tolerance 0 1 1
823 1 $r = ( $sigmaxy - $sigmax * $sigmay / $count ) / $denom;
824  
825 1 $iter = 0;
826 1 $rms = 0.0;
827 1 foreach (@x)
828 {
829 ##Error = Real y - calculated y
830 4 $err = $self->_data->[$iter] - ( $m * $_ + $q );
831 4 $rms += $err * $err;
832 4 ++$iter;
833 }
834  
835 1 $rms = sqrt( $rms / $count );
836  
837 1 $self->_least_squares_fit( [ $q, $m, $r, $rms ] );
838  
839 1 return @{ $self->_least_squares_fit() };
840 }
841  
842 sub median_absolute_deviation
843 1 ▶{
Subroutine median_absolute_deviation: called CC=2 SCAR=7.0
Pod documented
844 1 my ($self) = @_;
845  
846 1 ▶ if ( !defined( $self->_median_absolute_deviation() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_median_absolute_deviation) 1 0 1
847 {
848 1 my $stat = $self->new;
849 1 $stat->add_data( map { abs( $_ - $self->median ) } $self->get_data );
850 1 $self->_median_absolute_deviation( $stat->median );
851 }
852  
853 1 return $self->_median_absolute_deviation();
854 }
855  
856 sub summary
857 1 ▶{
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
858 1 my ($self) = @_;
859  
860 1 my $FMT = '%.5e';
861  
862 return
863 1 sprintf( "Min: $FMT\nMax: $FMT\nMean: $FMT\nMedian: $FMT\n"
864 . "1st quantile: $FMT\n3rd quantile: $FMT\n",
865 $self->min, $self->max, $self->mean, $self->median, $self->quantile(1),
866 $self->quantile(3), );
867  
868 }
869 11 1;
870  
871 __END__
« Descriptive/Sparse.pm ↑ Summary Descriptive/Smoother.pm »