blib/lib/Statistics/Descriptive/PDL.pm

filter: Statementstmt 91.1% 174 / 191 Branchbran 71.4% 50 / 70 Conditioncond 51.6% 16 / 31 MC/DCmcdc 25.0% 5 / 20 Subroutinesub 97.5% 40 / 41 Podpod 100% 14 / 14 totaltotal 81.4% 299 / 367 CC 47 SCAR 13.7
CC
47
Cov
82%
percentile
10.3
add_data
6.3
__ANON__
6.0
CRAP 3.9
↑ Summary lib/Statistics/Descriptive/PDL/Weighted.pm »
1 package Statistics::Descriptive::PDL;
2
3 ## no critic (ProhibitExplicitReturnUndef)
4
5 8 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Scalar::Util qw /blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use POSIX qw /fmod/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 # avoid loading too much, especially into our name space
12 8 ▶use PDL::Lite '2.012';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 # try to keep running if PDL::Stats is not installed
14 8 eval 'require PDL::Stats::Basic';
15 8 ▶my $has_PDL_stats_basic = $@ ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 8 8
16 #$has_PDL_stats_basic = 0;
17
18 # We could inherit from PDL::Objects, but in this case we want
19 # to hide the piddle from the caller to avoid arbitrary changes
20 # being applied to it.
21
22 8 our $VERSION = '0.17';
23
24 8 our $Tolerance = 0.0; # for compatibility with Stats::Descr, but not used here
25
26 8 my @cache_methods = qw /
27 count sum mode median
28 mean standard_deviation skewness kurtosis
29 geometric_mean harmonic_mean
30 max min sample_range
31 iqr
32 sum_sqr_sample_weights
33 /;
34 8 __PACKAGE__->_make_caching_accessors( \@cache_methods );
35
36 16 ▶sub _make_caching_accessors {
Subroutine _make_caching_accessors: called CC=2 SCAR=6.9
37 16 my ( $pkg, $methods ) = @_;
38
39 ## no critic
40 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 ## use critic
42 16 foreach my $method (@$methods)
43 {
44 192 *{ $pkg . "::" . $method } = do
45 192 {
46 192 my $m = $method;
47 683 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
48 683 my $self = shift;
49 return $self->{_cache}{$method}
50 683 ▶ if defined $self->{_cache}{$method};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_cache"}{$method} 234 449 683
51
52 449 my $piddle = $self->_get_piddle;
53 return undef
54 449 ▶ if !defined $piddle or $piddle->isempty;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $piddle or $piddle->isempty 122 327 449
Condition66%2/3
not defined $piddle or $piddle->isempty
l!l&&r!l&&!r
1220327
MC/DC50%1/2
not defined $piddle or $piddle->isempty
not defined $piddle $piddle->isempty
Truth table66%2/3
not(A) or B
A= not defined $piddleB= $piddle->isempty
ABresult
1X1
011
000
55
56 327 my $call_meth = "_$method";
57 327 my $val = $self->$call_meth;
58
59 327 ▶ if (blessed $val and $val->isa('PDL')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($val) and $val->isa("PDL")) 246 81 327
Condition66%2/3
builtin::blessed($val) and $val->isa("PDL")
!ll&&!rl&&r
810246
MC/DC50%1/2
builtin::blessed($val) and $val->isa("PDL")
builtin::blessed($val) $val->isa("PDL")
Truth table66%2/3
A and B
A= builtin::blessed($val)B= $val->isa("PDL")
ABresult
0X0
100
111
60 246 $val = $val->sclr;
61 }
62 327 return $self->{_cache}{$method} = $val;
63 192 };
64 };
65 }
66
67 16 return;
68 }
69
70 29 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
71 29 my $proto = shift;
72 29 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0290
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
73
74 29 my $self = {piddle => undef};
75 29 bless $self, $class;
76
77 29 return $self;
78 }
79
80 5 ▶sub available_stats {
Subroutine available_stats: called CC=2 SCAR=7.1
Pod documented
81 5 my $self = shift;
82 5 my @methods = sort (@cache_methods, qw /percentile variance/);
83 5 ▶ return wantarray ? @methods : \@methods;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 0 5
84 }
85
86 29 ▶sub add_data {
Subroutine add_data: called CC=6 SCAR=18.3
Pod documented
87 29 my $self = shift;
88 29 my $data;
89
90 # have we been passed an ndarray?
91 29 ▶ if (blessed $_[0] && $_[0]->isa('PDL')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_[0]) and $_[0]->isa('PDL')) { } 0 29 29
Condition33%1/3
builtin::blessed($_[0]) and $_[0]->isa("PDL")
!ll&&!rl&&r
2900
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->isa("PDL")
builtin::blessed($_[0]) $_[0]->isa("PDL")
Truth table33%1/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("PDL")
ABresult
0X0
100
111
92 0 $data = $_[0];
93 }
94 else {
95 29 ▶ $data
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 8 21 29
96 = ref ($_[0]) eq 'ARRAY'
97 ? $_[0]
98 : \@_;
99 29 ▶ return if !scalar @$data;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$data 0 29 29
100 }
101
102 29 my $piddle;
103 29 my $has_existing_data = $self->count;
104
105 29 $self->clear_cache;
106
107 # Take care of appending to an existing data set
108 29 ▶ if ($has_existing_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_existing_data) { } 4 25 29
109 4 $piddle = $self->_get_piddle;
110 4 $piddle = $piddle->append (PDL->pdl ($data)->flat);
111 4 $self->_set_piddle ($piddle);
112 }
113 else {
114 25 $self->_set_piddle (PDL->pdl($data)->flat);
115 }
116
117 29 return $self->count;
118 }
119
120 1 ▶sub get_data {
Subroutine get_data: called CC=3 SCAR=11.4
Pod documented
121 1 my $self = shift;
122 1 my $piddle = $self->_get_piddle;
123
124 1 ▶ my $data = defined $piddle ? $piddle->unpdl : [];
Branch: 1 branch
BranchTrueFalseTotal
defined $piddle ? : 1 0 1
125
126 1 ▶ return wantarray ? @$data : $data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
127 }
128
129 2 ▶sub get_data_as_hash {
Subroutine get_data_as_hash: called CC=3 SCAR=11.8
Pod documented
130 2 my $self = shift;
131
132 2 my $piddle = $self->_get_piddle;
133 2 ▶ if (defined $piddle) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $piddle) 2 0 2
134 2 require Statistics::Descriptive::PDL::SampleWeighted;
135 2 my $wtd_obj = Statistics::Descriptive::PDL::SampleWeighted->new;
136 2 my $wts_piddle = PDL->ones ($piddle->dims);
137 2 $wtd_obj->add_data ($piddle->copy, $wts_piddle);
138 2 return $wtd_obj->get_data_as_hash;
139 }
140
141 0 ▶ return wantarray ? () : {};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
142 }
143
144 0 ▶sub values_are_unique {}
Subroutine values_are_unique: not called CC=1 SCAR=0.0
Pod documented
145
146 # flatten $data if multidimensional
147 101 ▶sub _set_piddle {
Subroutine _set_piddle: called CC=1 SCAR=0.0
148 101 my ($self, $data) = @_;
149 101 $self->{piddle} = PDL->pdl ($data);
150 }
151
152 906 ▶sub _get_piddle {
Subroutine _get_piddle: called CC=1 SCAR=0.0
153 906 my $self = shift;
154 906 return $self->{piddle};
155 }
156
157 75 ▶sub clear_cache {
Subroutine clear_cache: called CC=1 SCAR=0.0
Pod documented
158 75 my $self = shift;
159 75 delete $self->{_cache};
160 75 return;
161 }
162
163 29 ▶sub _count {
Subroutine _count: called CC=1 SCAR=0.0
164 29 my $self = shift;
165 29 return $self->_get_piddle->nelem;
166 }
167
168 2 ▶sub _sum {
Subroutine _sum: called CC=1 SCAR=0.0
169 2 my $self = shift;
170 2 return $self->_get_piddle->sum;
171 }
172
173 2 ▶sub sum_weights {
Subroutine sum_weights: called CC=1 SCAR=0.0
Pod documented
174 2 my $self = shift;
175 2 return $self->count;
176 }
177
178 4 ▶sub sum_sqr_weights {
Subroutine sum_sqr_weights: called CC=1 SCAR=0.0
Pod documented
179 4 my $self = shift;
180 4 return $self->count;
181 }
182
183 3 ▶sub _sum_sqr_sample_weights {
Subroutine _sum_sqr_sample_weights: called CC=1 SCAR=0.0
184 3 my $self = shift;
185 3 return $self->sum_sqr_weights;
186 }
187
188 10 ▶sub _min {
Subroutine _min: called CC=1 SCAR=0.0
189 10 my $self = shift;
190 10 return $self->_get_piddle->min;
191 }
192
193 10 ▶sub _max {
Subroutine _max: called CC=1 SCAR=0.0
194 10 my $self = shift;
195 10 return $self->_get_piddle->max;
196 }
197
198 6 ▶sub _mean {
Subroutine _mean: called CC=1 SCAR=0.0
199 6 my $self = shift;
200 6 return $self->_get_piddle->average;
201 }
202
203
204 5 ▶sub sd {return $_[0]->standard_deviation}
Subroutine sd: called CC=1 SCAR=0.0
Pod documented
205 1 ▶sub stdev {return $_[0]->standard_deviation}
Subroutine stdev: called CC=1 SCAR=0.0
Pod documented
206
207 8 ▶sub _standard_deviation {
Subroutine _standard_deviation: called CC=5 SCAR=17.6
208 8 my $self = shift;
209
210 8 my $piddle = $self->_get_piddle;
211
212 8 my $sd;
213 8 my $n = $piddle->nelem;
214 8 ▶ if ($n > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n > 1) { } 7 1 8
elsif ($n == 1) { } 1 0 1
215 7 ▶ if ($has_PDL_stats_basic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_PDL_stats_basic) { } 7 0 7
216 7 $sd = $piddle->stdv_unbiased;
217 }
218 else {
219 0 my $var = (($piddle ** 2)->sum - $n * $self->mean ** 2);
220 0 ▶ $sd = $var > 0 ? sqrt ($var / ($n - 1)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$var > 0 ? : 0 0 0
221 }
222 }
223 elsif ($n == 1){
224 1 $sd = 0;
225 }
226 8 return $sd;
227 }
228
229 5 ▶sub variance {
Subroutine variance: called CC=2 SCAR=6.9
Pod documented
230 5 my $self = shift;
231 5 my $sd = $self->standard_deviation;
232 5 ▶ return defined $sd ? $sd ** 2 : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $sd ? : 3 2 5
233 }
234
235 10 ▶sub _median {
Subroutine _median: called CC=1 SCAR=0.0
236 10 my $self = shift;
237 10 return $self->_get_piddle->median;
238 }
239
240
241 9 ▶sub _skewness {
Subroutine _skewness: called CC=3 SCAR=13.6
242 9 my $self = shift;
243
244 9 my $piddle = $self->_get_piddle;
245
246 9 my $n = $piddle->nelem;
247
248 9 ▶ return undef if $n < 3;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 3 1 8 9
249
250 8 ▶ return $piddle->skew_unbiased
Branch: 1 branch
BranchTrueFalseTotal
if $has_PDL_stats_basic 8 0 8
251 if $has_PDL_stats_basic;
252
253 # do it ourselves
254 0 my $mean = $self->mean;
255 0 my $sd = $self->standard_deviation;
256 0 my $sumpow3 = ((($piddle - $mean) / $sd) ** 3)->sum;
257 0 my $correction = $n / ( ($n-1) * ($n-2) );
258 0 my $skew = $correction * $sumpow3;
259
260 0 return $skew;
261 }
262
263 9 ▶sub _kurtosis {
Subroutine _kurtosis: called CC=3 SCAR=14.2
264 9 my $self = shift;
265 9 my $piddle = $self->_get_piddle;
266
267 9 my $n = $piddle->nelem;
268
269 9 ▶ return undef if $n < 4;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 4 1 8 9
270
271 8 ▶ return $piddle->kurt_unbiased
Branch: 1 branch
BranchTrueFalseTotal
if $has_PDL_stats_basic 8 0 8
272 if $has_PDL_stats_basic;
273
274 # do it ourselves
275 0 my $mean = $self->mean;
276 0 my $sd = $self->standard_deviation;
277 0 my $sumpow4 = ((($piddle - $mean) / $sd) ** 4)->sum;
278
279 0 my $correction1 = ( $n * ($n+1) ) / ( ($n-1) * ($n-2) * ($n-3) );
280 0 my $correction2 = ( 3 * ($n-1) ** 2) / ( ($n-2) * ($n-3) );
281
282 0 my $kurt = ( $correction1 * $sumpow4 ) - $correction2;
283 0 return $kurt;
284 }
285
286 5 ▶sub _sample_range {
Subroutine _sample_range: called CC=3 SCAR=11.4
287 5 my $self = shift;
288 5 ▶ my $min = $self->min // return undef;
Condition50%1/2
$self->min // (return undef)
l!l
50
MC/DC0%0/1
$self->min // (return undef)
$self->min
Truth table50%1/2
A
A= $self->min
Aresult
11
00
289 5 ▶ my $max = $self->max // return undef;
Condition50%1/2
$self->max // (return undef)
l!l
50
MC/DC0%0/1
$self->max // (return undef)
$self->max
Truth table50%1/2
A
A= $self->max
Aresult
11
00
290 5 return $max - $min;
291 }
292
293
294 5 ▶sub _harmonic_mean {
Subroutine _harmonic_mean: called CC=3 SCAR=11.0
295 5 my $self = shift;
296 5 my $piddle = $self->_get_piddle;
297
298 5 ▶ return undef if $piddle->which->nelem != $piddle->nelem;
Branch: 1 branch
BranchTrueFalseTotal
if $piddle->which->nelem != $piddle->nelem 1 4 5
299
300 4 my $hs = (1 / $piddle)->sum;
301
302 4 ▶ return $hs ? $self->count / $hs : undef;
Branch: 1 branch
BranchTrueFalseTotal
$hs ? : 3 1 4
303 }
304
305 4 ▶sub _geometric_mean {
Subroutine _geometric_mean: called CC=3 SCAR=11.1
306 4 my $self = shift;
307 4 my $piddle = $self->_get_piddle;
308
309 4 my $count = $self->count;
310
311 4 ▶ return undef if !$count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 0 4 4
312 4 ▶ return undef if $piddle->where($piddle < 0)->nelem;
Branch: 1 branch
BranchTrueFalseTotal
if $piddle->where($piddle < 0)->nelem 0 4 4
313
314 4 my $exponent = 1 / $self->count();
315 4 my $powered = $piddle ** $exponent;
316
317 4 my $gm = $powered->dprodover;
318 }
319
320 6 ▶sub _mode {
Subroutine _mode: called CC=4 SCAR=14.0
321 6 my $self = shift;
322 6 my $piddle = $self->_get_piddle;
323
324 6 my $count = $piddle->nelem;
325 6 my $unique = $piddle->uniq;
326
327 6 ▶ return undef if $unique->nelem == $count or $unique->nelem == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $unique->nelem == $count or $unique->nelem == 1 3 3 6
Condition66%2/3
$unique->nelem == $count or $unique->nelem == 1
l!l&&r!l&&!r
303
MC/DC50%1/2
$unique->nelem == $count or $unique->nelem == 1
$unique->nelem == $count $unique->nelem == 1
Truth table66%2/3
A or B
A= $unique->nelem == $countB= $unique->nelem == 1
ABresult
1X1
011
000
328
329 #if (!($count % $unique->nelem)) {
330 # # might have equal numbers of each value
331 # # need to check for this, but for now return undef
332 # return undef;
333 #}
334
335 3 my $mode = $piddle->mode;
336
337 # bodge to handle odd values
338 3 ▶ return undef if !$piddle->in($mode)->max;
Branch: 1 branch
BranchTrueFalseTotal
unless $piddle->in($mode)->max 0 3 3
339
340 3 return $mode;
341 }
342
343 1 ▶sub percentiles {
Subroutine percentiles: called CC=3 SCAR=11.6
Pod documented
344 1 my ($self, @percentiles) = @_;
345
346 1 my $piddle = $self->_get_piddle;
347
348 return
349 1 ▶ if !defined $piddle || $piddle->nelem == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $piddle or $piddle->nelem == 0 0 1 1
Condition33%1/3
not defined $piddle or $piddle->nelem == 0
l!l&&r!l&&!r
001
MC/DC0%0/2
not defined $piddle or $piddle->nelem == 0
not defined $piddle $piddle->nelem == 0
Truth table33%1/3
not(A) or B
A= not defined $piddleB= $piddle->nelem == 0
ABresult
1X1
011
000
350
351 1 my @vals = map {$self->percentile($_)} @percentiles;
352
353 1 return @vals;
354 }
355
356
357 # caching wrapper
358 # need to convert $p to fraction, or perhaps die if it is between 0 and 1
359 # hard-coded cache percentiles not ideal
360 80 ▶sub percentile {
Subroutine percentile: called CC=10 SCAR=23.3
Pod documented
361 80 my ($self, $p) = @_;
362 80 my $piddle = $self->_get_piddle;
363
364 return undef
365 80 ▶ if !defined $piddle || $piddle->nelem == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $piddle or $piddle->nelem == 0 4 76 80
Condition66%2/3
not defined $piddle or $piddle->nelem == 0
l!l&&r!l&&!r
4076
MC/DC50%1/2
not defined $piddle or $piddle->nelem == 0
not defined $piddle $piddle->nelem == 0
Truth table66%2/3
not(A) or B
A= not defined $piddleB= $piddle->nelem == 0
ABresult
1X1
011
000
366
367 76 ▶ return $self->median
Branch: 1 branch
BranchTrueFalseTotal
if $p == 50 16 60 76
368 if $p == 50;
369
370 60 ▶ die "Percentile $p outside range 0..100"
Branch: 1 branch
BranchTrueFalseTotal
if $p < 0 or $p > 100 0 60 60
Condition33%1/3
$p < 0 or $p > 100
l!l&&r!l&&!r
0060
MC/DC0%0/2
$p < 0 or $p > 100
$p < 0 $p > 100
Truth table33%1/3
A or B
A= $p < 0B= $p > 100
ABresult
1X1
011
000
371 if $p < 0 or $p > 100;
372
373 # allow for other number formats like '005'
374 # needed for cache
375 60 $p += 0;
376
377 return $self->{_cache}{percentile}{$p}
378 60 ▶ if defined $self->{_cache}{percentile}{$p};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_cache"}{"percentile"}{$p} 12 48 60
379
380 48 my $pctl = $self->_percentile($p);
381
382 48 ▶ if (blessed $pctl && $pctl->isa('PDL')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($pctl) and $pctl->isa("PDL")) 18 30 48
Condition66%2/3
builtin::blessed($pctl) and $pctl->isa("PDL")
!ll&&!rl&&r
30018
MC/DC50%1/2
builtin::blessed($pctl) and $pctl->isa("PDL")
builtin::blessed($pctl) $pctl->isa("PDL")
Truth table66%2/3
A and B
A= builtin::blessed($pctl)B= $pctl->isa("PDL")
ABresult
0X0
100
111
383 18 $pctl = $pctl->sclr;
384 }
385
386 48 ▶ if (int ($p) == $p) {
Branch: 1 branch
BranchTrueFalseTotal
if (int $p == $p) 46 2 48
387 46 $self->{_cache}{percentile}{$p} = $pctl;
388 }
389
390 48 return $pctl;
391 }
392
393 18 ▶sub _percentile {
Subroutine _percentile: called CC=1 SCAR=0.0
394 18 my ($self, $p) = @_;
395
396 18 return $self->_get_piddle->pct($p / 100);
397 }
398
399
400
401 6 ▶sub _iqr {
Subroutine _iqr: called CC=1 SCAR=0.0
402 6 my $self = shift;
403 6 $self->percentile(75) - $self->percentile(25);
404 }
405
406
407 8 1;
408
409 __END__
↑ Summary lib/Statistics/Descriptive/PDL/Weighted.pm »