lib/Statistics/Descriptive/PDL/SampleWeighted.pm

filter: Statementstmt 97.5% 80 / 82 Branchbran 68.7% 11 / 16 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 85.7% 108 / 126 CC 12 SCAR 12.5
CC
12
Cov
89%
_percentile
7.8
_standard_deviation
3.1
_median
2.0
CRAP 3.5
« lib/Statistics/Descriptive/PDL/Weighted.pm ↑ Summary
1 package Statistics::Descriptive::PDL::SampleWeighted;
2
3 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 # avoid loading too much, especially into our name space
8 3 ▶use PDL::Lite '2.012';
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 # this is otherwise not loaded due to oddities with multiple loading of PDL::Lite
11 #*pdl = \&PDL::Core::pdl;
12
13 # We could inherit from PDL::Objects, but in this case we want
14 # to hide the piddle from the caller to avoid arbitrary changes
15 # being applied to it.
16
17 ## no critic (ProhibitExplicitReturnUndef)
18
19 3 our $VERSION = '0.17';
20
21 3 ▶use parent 'Statistics::Descriptive::PDL::Weighted';
Subroutine BEGIN: called CC=1 SCAR=0.0
22
23 3 my @cache_methods = qw /
24 median
25 standard_deviation skewness kurtosis
26 /;
27 3 __PACKAGE__->_make_caching_accessors( \@cache_methods );
28
29
30
31 15 ▶sub _wt_type{PDL::long}
Subroutine _wt_type: called CC=1 SCAR=0.0
32
33
34 4 ▶sub _standard_deviation {
Subroutine _standard_deviation: called CC=3 SCAR=11.2
35 4 my $self = shift;
36
37 4 my $data = $self->_get_piddle;
38
39 4 my $wts = $self->_get_weights_piddle;
40 4 my $n = $wts->sum;
41
42 4 ▶ return 0 if $n == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 1 0 4 4
43
44 4 my $var = ((($data ** 2) * $wts)->sum - $n * $self->mean ** 2);
45
46 4 ▶ return $var > 0 ? sqrt ($var / ($n - 1)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$var > 0 ? : 4 0 4
47 }
48
49
50 9 ▶sub _median {
Subroutine _median: called CC=2 SCAR=6.9
51 9 my $self = shift;
52
53 9 my $piddle = $self->_sort_piddle;
54 9 my $cumsum = $self->_get_cumsum_weight_vector;
55
56 9 my $target_wt = $self->sum_weights * 0.5;
57 # vsearch should be faster since it uses a binary search
58 9 my $idx = PDL->pdl($target_wt)->vsearch_insert_leftmost($cumsum);
59
60 # if the target weight is "on a boundary" between
61 # two values then we need to interpolate
62 9 ▶ my $median
Branch: 1 branch
BranchTrueFalseTotal
$target_wt == $cumsum->at($idx) ? : 7 2 9
63 = $target_wt == $cumsum->at($idx)
64 ? ($piddle->at($idx) + $piddle->at($idx+1)) / 2
65 : $piddle->at($idx);
66
67 9 return $median;
68 }
69
70
71 4 ▶sub _skewness {
Subroutine _skewness: called CC=2 SCAR=6.9
72 4 my $self = shift;
73
74 4 my $n = $self->sum_weights;
75 4 ▶ return undef if $n < 3;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 3 1 3 4
76
77 3 my $data = $self->_get_piddle;
78
79 # long winded approach
80 3 my $mean = $self->mean;
81 3 my $sd = $self->standard_deviation;
82 3 my $wts = $self->_get_weights_piddle;
83 3 my $sumpow3 = ($wts * ((($data - $mean) / $sd) ** 3))->sum;
84 # inplace seems not to be faster here.
85 # Possibly PDL is smart enough to do it by default
86 # in such cases
87 #my $sumpow3 = ($data - $mean)->inplace->divide($sd, 0)->pow(3)->mult($wts, 0)->sum;
88 3 my $correction = $n / ( ($n-1) * ($n-2) );
89 3 my $skew = $correction * $sumpow3;
90 3 return $skew;
91 }
92
93 4 ▶sub _kurtosis {
Subroutine _kurtosis: called CC=2 SCAR=6.9
94 4 my $self = shift;
95
96 4 my $n = $self->sum_weights;
97 4 ▶ return undef if $n <= 3;
Branch: 1 branch
BranchTrueFalseTotal
if $n <= 3 1 3 4
98
99 3 my $data = $self->_get_piddle;
100 3 my $mean = $self->mean;
101 3 my $sd = $self->standard_deviation;
102 3 my $wts = $self->_get_weights_piddle;
103
104 3 my $sumpow4 = ($wts * ((($data - $mean) / $sd) ** 4))->sum;
105
106 3 my $correction1 = ( $n * ($n+1) ) / ( ($n-1) * ($n-2) * ($n-3) );
107 3 my $correction2 = ( 3 * ($n-1) ** 2) / ( ($n-2) * ($n-3) );
108
109 3 return ( $correction1 * $sumpow4 ) - $correction2;
110 }
111
112 # crude memoisation - would be nice to use
113 # state but it has issues with lists on older perls
114 3 my %k_piddle_cache;
115
116 # Uses same basic algorithm as PDL::pctl.
117 25 ▶sub _percentile {
Subroutine _percentile: called CC=7 SCAR=20.6
118 25 my ($self, $p) = @_;
119
120 25 my $piddle = $self->_get_piddle;
121
122 return undef
123 25 ▶ if !defined $piddle or $piddle->isempty;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $piddle or $piddle->isempty 0 25 25
Condition33%1/3
not defined $piddle or $piddle->isempty
l!l&&r!l&&!r
0025
MC/DC0%0/2
not defined $piddle or $piddle->isempty
not defined $piddle $piddle->isempty
Truth table33%1/3
not(A) or B
A= not defined $piddleB= $piddle->isempty
ABresult
1X1
011
000
124
125 25 $self->_sort_piddle;
126
127 # possible slowdown here - users need to dedup before calling to avoid
128 # my $deduped = $self->_deduplicate;
129 # there is actually no need to dedup
130 25 my $deduped = $self;
131 25 $piddle = $deduped->_get_piddle;
132
133 # my $wt_piddle = $self->_get_weights_piddle;
134
135 25 my $cumsum = $deduped->_get_cumsum_weight_vector;
136 25 my $wt_sum = $deduped->sum_weights;
137
138 3 ▶ use POSIX qw /floor/;
Subroutine BEGIN: called CC=1 SCAR=0.0
139
140 25 my $target_wt = ($p / 100) * ($wt_sum - 1) + 1;
141 25 my $k = floor $target_wt;
142 25 my $d = $target_wt - $k;
143
144 25 ▶ my $idx = ($k_piddle_cache{$k} //= PDL->pdl(PDL::indx(), [$k]))->vsearch_insert_leftmost($cumsum)->at(0);
Condition66%2/3
$k_piddle_cache{$k} //= "PDL"->pdl(PDL::indx(), [$k])
l!l&&r!l&&!r
14110
MC/DC0%0/2
$k_piddle_cache{$k} //= "PDL"->pdl(PDL::indx(), [$k])
$k_piddle_cache{$k} "PDL"->pdl(PDL::indx(), [$k])
Truth table66%2/3
A //= B
A= $k_piddle_cache{$k}B= "PDL"->pdl(PDL::indx(), [$k])
ABresult
1X1
011
000
145
146 25 ▶ if (scalar keys %k_piddle_cache > 10000) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %k_piddle_cache > 10000) 0 25 25
147 # Reset if we get too many
148 # - could be more nuanced based on frequency
149 # but then we would have to track it
150 0 %k_piddle_cache = ();
151 }
152
153 # we need to interpolate if our target weight falls between two sets of weights
154 # e.g. target is 1.3, but the cumulative weights are [1,2] or [1,5]
155 25 my $fraction = $target_wt - ($cumsum->at($idx));
156 25 ▶ if ($fraction > 0 && $fraction < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fraction > 0 and $fraction < 1) 25 0 25
Condition33%1/3
$fraction > 0 and $fraction < 1
!ll&&!rl&&r
0025
MC/DC0%0/2
$fraction > 0 and $fraction < 1
$fraction > 0 $fraction < 1
Truth table33%1/3
A and B
A= $fraction > 0B= $fraction < 1
ABresult
0X0
100
111
157 25 my $lower_val = $piddle->at($idx);
158 25 my $upper_val = $piddle->at($idx+1);
159 25 my $val = $lower_val + $d * ($upper_val - $lower_val);
160 25 return $val;
161 }
162
163 0 return $piddle->at($idx);
164 }
165
166 # weight for each sample is 1
167 2 ▶sub _sum_sqr_sample_weights {
Subroutine _sum_sqr_sample_weights: called CC=1 SCAR=0.0
168 2 my $self = shift;
169 2 return $self->sum_weights;
170 }
171
172
173
174 3 1;
175
176 __END__
« lib/Statistics/Descriptive/PDL/Weighted.pm ↑ Summary