« blib/lib/Statistics/Descriptive/PDL.pm
↑ Summary
lib/Statistics/Descriptive/PDL/SampleWeighted.pm »
| 1 | package Statistics::Descriptive::PDL::Weighted; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # avoid loading too much, especially into our name space | ||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use PDL::Lite '2.012'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # We could inherit from PDL::Objects, but in this case we want | ||||||||||||||||||||||||||||
| 11 | # to hide the piddle from the caller to avoid arbitrary changes | ||||||||||||||||||||||||||||
| 12 | # being applied to it. | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | ## no critic (ProhibitExplicitReturnUndef) | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 5 | our $VERSION = '0.17'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use parent 'Statistics::Descriptive::PDL'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 5 | my @cache_methods = qw / | |||||||||||||||||||||||||||
| 21 | count sum mode median | ||||||||||||||||||||||||||||
| 22 | mean standard_deviation skewness kurtosis | ||||||||||||||||||||||||||||
| 23 | geometric_mean harmonic_mean | ||||||||||||||||||||||||||||
| 24 | sum_weights sum_sqr_weights | ||||||||||||||||||||||||||||
| 25 | /; | ||||||||||||||||||||||||||||
| 26 | 5 | __PACKAGE__->_make_caching_accessors( \@cache_methods ); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 41 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 41 | my $proto = shift; | |||||||||||||||||||||||||||
| 31 | 41 | ▶ | my $data_type = shift // PDL::double(); | ||||||||||||||||||||||||||
|
Condition66%2/3
shift() // PDL::double()
MC/DC0%0/2
shift() // PDL::double()
shift()
PDL::double()
Truth table66%2/3
A // B
A= shift()B= PDL::double()
| |||||||||||||||||||||||||||||
| 32 | 41 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||
|
Condition66%2/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 41 | my $self = { | |||||||||||||||||||||||||||
| 35 | piddle => undef, | ||||||||||||||||||||||||||||
| 36 | weights_piddle => undef, | ||||||||||||||||||||||||||||
| 37 | data_type => $data_type, | ||||||||||||||||||||||||||||
| 38 | }; | ||||||||||||||||||||||||||||
| 39 | 41 | bless $self, $class; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 41 | return $self; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 32 | ▶ | sub _wt_type{PDL::double} | ||||||||||||||||||||||||||
|
Subroutine
_wt_type: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 47 | ▶ | sub add_data { | ||||||||||||||||||||||||||
|
Subroutine
add_data: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 47 | my ($self, $data, $weights) = @_; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 47 | my ($data_piddle, $weights_piddle); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 47 | my $data_from_hash; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 47 | ▶ | if (ref $data eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 9 | $data_piddle = PDL->pdl ($self->{data_type}, [keys %$data])->flat; | |||||||||||||||||||||||||||
| 56 | 9 | $weights_piddle = PDL->pdl ($self->_wt_type, [values %$data])->flat; | |||||||||||||||||||||||||||
| 57 | 9 | $data_from_hash = 1; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||||||||||
| 60 | 38 | $data_piddle = PDL->pdl ($self->{data_type}, $data)->flat; | |||||||||||||||||||||||||||
| 61 | 38 | $weights_piddle = PDL->pdl ($self->_wt_type, $weights)->flat; | |||||||||||||||||||||||||||
| 62 | 38 | ▶ | die "data and weight vectors not of same length" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | if scalar $data_piddle->nelem != $weights_piddle->nelem; | ||||||||||||||||||||||||||||
| 64 | 37 | ▶ | die "Cannot pass zero or negative weights" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | if PDL::any($weights_piddle <= 0); | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 44 | ▶ | return if !$data_piddle->nelem; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 44 | my $has_existing_data = $self->count; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # Take care of appending to an existing data set | ||||||||||||||||||||||||||||
| 73 | 44 | ▶ | if ($has_existing_data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 12 | my $d_piddle = $self->_get_piddle; | |||||||||||||||||||||||||||
| 75 | 12 | $d_piddle = $d_piddle->append ($data_piddle); | |||||||||||||||||||||||||||
| 76 | 12 | $self->_set_piddle ($d_piddle); | |||||||||||||||||||||||||||
| 77 | 12 | my $w_piddle = $self->_get_weights_piddle; | |||||||||||||||||||||||||||
| 78 | 12 | $w_piddle = $w_piddle->append ($weights_piddle); | |||||||||||||||||||||||||||
| 79 | 12 | $self->_set_weights_piddle ($w_piddle); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 12 | delete $self->{sorted}; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | else { | ||||||||||||||||||||||||||||
| 84 | 32 | $self->_set_piddle ($data_piddle); | |||||||||||||||||||||||||||
| 85 | 32 | $self->_set_weights_piddle ($weights_piddle); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # need to clear late because count is cached | ||||||||||||||||||||||||||||
| 89 | 44 | $self->clear_cache; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # somewhat awkward but needs to be set after clearing the cache | ||||||||||||||||||||||||||||
| 92 | 44 | ▶ | if ($data_from_hash && !$has_existing_data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$data_from_hash and not $has_existing_data
MC/DC100%2/2
$data_from_hash and not $has_existing_data
$data_from_hash
not $has_existing_data
Truth table100%3/3
A and not(B)
A= $data_from_hashB= not $has_existing_data
| |||||||||||||||||||||||||||||
| 93 | 5 | $self->values_are_unique(1); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 44 | return $self->count; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | ▶ | sub get_data { | ||||||||||||||||||||||||||
|
Subroutine
get_data: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 101 | 1 | my $piddle = $self->_get_piddle; | |||||||||||||||||||||||||||
| 102 | 1 | my $wts_piddle = $self->_get_weights_piddle; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 1 | ▶ | my $data = defined $piddle ? $piddle->unpdl : []; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 1 | ▶ | my $wts = defined $wts_piddle ? $wts_piddle->unpdl : []; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 1 | ▶ | return wantarray ? ($data, $wts) : [$data, $wts]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 3 | ▶ | sub get_data_as_hash { | ||||||||||||||||||||||||||
|
Subroutine
get_data_as_hash: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 3 | my $piddle = $self->_get_piddle; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 3 | ▶ | return wantarray ? () : {} | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 116 | if !defined $piddle; | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 3 | my $deduped = $self->_deduplicate; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 3 | my $data = $deduped->_get_piddle->unpdl; | |||||||||||||||||||||||||||
| 121 | 3 | my $wts = $deduped->_get_weights_piddle->unpdl; | |||||||||||||||||||||||||||
| 122 | 3 | my %h; | |||||||||||||||||||||||||||
| 123 | 3 | @h{@$data} = @$wts; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 3 | ▶ | return wantarray ? (%h) : \%h; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 26 | ▶ | sub values_are_unique { | ||||||||||||||||||||||||||
|
Subroutine
values_are_unique: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 130 | 26 | my $self = shift; | |||||||||||||||||||||||||||
| 131 | 26 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 14 | my $flag = shift; | |||||||||||||||||||||||||||
| 133 | 14 | $self->{_cache}{deduplicated} = !!$flag; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 26 | return $self->{_cache}{deduplicated}; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 72 | ▶ | sub _set_weights_piddle { | ||||||||||||||||||||||||||
|
Subroutine
_set_weights_piddle: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 139 | 72 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 140 | 72 | $self->{weights_piddle} = PDL->pdl ($data); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 245 | ▶ | sub _get_weights_piddle { | ||||||||||||||||||||||||||
|
Subroutine
_get_weights_piddle: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 144 | 245 | my $self = shift; | |||||||||||||||||||||||||||
| 145 | 245 | return $self->{weights_piddle}; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 44 | ▶ | sub _count { | ||||||||||||||||||||||||||
|
Subroutine
_count: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 149 | 44 | my $self = shift; | |||||||||||||||||||||||||||
| 150 | 44 | my $piddle = $self->_get_weights_piddle; | |||||||||||||||||||||||||||
| 151 | 44 | ▶ | return undef if !defined $piddle; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 44 | return $piddle->sum; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 3 | ▶ | sub _sum { | ||||||||||||||||||||||||||
|
Subroutine
_sum: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 156 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 157 | 3 | return ($self->_get_piddle * $self->_get_weights_piddle)->sum; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 34 | ▶ | sub _sum_weights { | ||||||||||||||||||||||||||
|
Subroutine
_sum_weights: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 161 | 34 | my $self = shift; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 34 | return $self->_get_weights_piddle->sum; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 3 | ▶ | sub _sum_sqr_weights { | ||||||||||||||||||||||||||
|
Subroutine
_sum_sqr_weights: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 167 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 168 | 3 | return $self->_get_weights_piddle->power(2)->sum; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | #sub min_weight { | ||||||||||||||||||||||||||||
| 172 | # my $self = shift; | ||||||||||||||||||||||||||||
| 173 | # return $self->_get_weights_piddle->min; | ||||||||||||||||||||||||||||
| 174 | #} | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 25 | ▶ | sub _mean { | ||||||||||||||||||||||||||
|
Subroutine
_mean: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 178 | 25 | my $self = shift; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 25 | my $data = $self->_get_piddle; | |||||||||||||||||||||||||||
| 181 | 25 | my $wts = $self->_get_weights_piddle; | |||||||||||||||||||||||||||
| 182 | 25 | return ($data * $wts)->sum / $wts->sum; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 22 | ▶ | sub _standard_deviation { | ||||||||||||||||||||||||||
|
Subroutine
_standard_deviation: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 187 | 22 | my $self = shift; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 22 | my $data = $self->_get_piddle; | |||||||||||||||||||||||||||
| 190 | 22 | my $sd; | |||||||||||||||||||||||||||
| 191 | 22 | my $n = $data->nelem; | |||||||||||||||||||||||||||
| 192 | 22 | ▶ | if ($n > 1) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 193 | # long winded approach | ||||||||||||||||||||||||||||
| 194 | 21 | my $wts = $self->_get_weights_piddle; | |||||||||||||||||||||||||||
| 195 | 21 | my $mean = $self->mean; | |||||||||||||||||||||||||||
| 196 | 21 | my $sumsqr = ($wts * (($data - $mean) ** 2))->sum; | |||||||||||||||||||||||||||
| 197 | 21 | my $var = $sumsqr / $self->sum_weights; | |||||||||||||||||||||||||||
| 198 | 21 | $sd = sqrt $var; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | elsif ($n == 1){ | ||||||||||||||||||||||||||||
| 201 | 1 | $sd = 0; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 22 | return $sd; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 5 | ▶ | sub variance { | ||||||||||||||||||||||||||
|
Subroutine
variance: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 208 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 209 | 5 | my $sd = $self->standard_deviation; | |||||||||||||||||||||||||||
| 210 | 5 | ▶ | return defined $sd ? $sd ** 2 : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 10 | ▶ | sub _median { | ||||||||||||||||||||||||||
|
Subroutine
_median: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 214 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 10 | my $data = $self->_sort_piddle; | |||||||||||||||||||||||||||
| 217 | 10 | my $cumsum = $self->_get_cumsum_weight_vector; | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 10 | my $target_wt = $self->sum_weights * 0.5; | |||||||||||||||||||||||||||
| 220 | # vsearch should be faster since it uses a binary search | ||||||||||||||||||||||||||||
| 221 | 10 | my $idx = PDL->pdl($target_wt)->vsearch_insert_leftmost($cumsum->reshape); | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 10 | return $data->at($idx); | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 55 | ▶ | sub _sort_piddle { | ||||||||||||||||||||||||||
|
Subroutine
_sort_piddle: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 227 | 55 | my $self = shift; | |||||||||||||||||||||||||||
| 228 | 55 | my $data = $self->_get_piddle; | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 55 | ▶ | return undef if !defined $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 55 | ▶ | return $data if $self->{sorted}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 22 | my $wts = $self->_get_weights_piddle; | |||||||||||||||||||||||||||
| 235 | 22 | my $s = $data->qsorti->reshape; | |||||||||||||||||||||||||||
| 236 | 22 | my $sorted_data = $data->slice($s); | |||||||||||||||||||||||||||
| 237 | 22 | my $sorted_wts = $wts->slice($s); | |||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 22 | $self->_set_piddle($sorted_data); | |||||||||||||||||||||||||||
| 240 | 22 | $self->_set_weights_piddle($sorted_wts); | |||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 22 | $self->{sorted} = 1; | |||||||||||||||||||||||||||
| 243 | 22 | delete $self->{_cache}{cumsum_weight_vector}; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 22 | return $sorted_data; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | # de-duplicate if needed, aggregating weights | ||||||||||||||||||||||||||||
| 249 | # there should be a sumover or which approach that will work better | ||||||||||||||||||||||||||||
| 250 | # maybe yvals related | ||||||||||||||||||||||||||||
| 251 | 10 | ▶ | sub _deduplicate { | ||||||||||||||||||||||||||
|
Subroutine
_deduplicate: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 252 | 10 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 253 | 10 | my $piddle = $self->_get_piddle; | |||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | return undef | ||||||||||||||||||||||||||||
| 256 | 10 | ▶ | if !defined $piddle; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 10 | ▶ | return $self | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | if $self->values_are_unique; | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 9 | my $unique = $piddle->uniq; | |||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 9 | ▶ | if ($unique->nelem == $piddle->nelem) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 3 | $self->values_are_unique(1); | |||||||||||||||||||||||||||
| 265 | 3 | return $self | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 6 | ▶ | if (!$self->{sorted}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 3 | $unique = $unique->qsort; | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 6 | $piddle = $self->_sort_piddle; | |||||||||||||||||||||||||||
| 273 | 6 | my $wts_piddle = $self->_get_weights_piddle; | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | # could use a map with a hash, but this avoids | ||||||||||||||||||||||||||||
| 276 | # stringification and loss of precision | ||||||||||||||||||||||||||||
| 277 | # (not that that should cause too many issues for most data) | ||||||||||||||||||||||||||||
| 278 | # Should try to reduce looping when there are | ||||||||||||||||||||||||||||
| 279 | # not many dups in large data sets | ||||||||||||||||||||||||||||
| 280 | 6 | my $j = 0; # index into deduplicated piddle | |||||||||||||||||||||||||||
| 281 | 6 | my $last_val = $piddle->at(0); | |||||||||||||||||||||||||||
| 282 | 6 | my @wts; | |||||||||||||||||||||||||||
| 283 | 6 | foreach my $i (0..$piddle->nelem-1) { | |||||||||||||||||||||||||||
| 284 | 204 | my $val = $piddle->at($i); | |||||||||||||||||||||||||||
| 285 | 204 | ▶ | if ($val != $last_val) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 168 | $j++; | |||||||||||||||||||||||||||
| 287 | 168 | $last_val = $val; | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | 204 | $wts[$j] += $wts_piddle->at($i); | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 6 | ▶ | if ($args{inplace}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | 2 | $self->_set_piddle($unique); | |||||||||||||||||||||||||||
| 294 | 2 | $self->_set_weights_piddle(\@wts); | |||||||||||||||||||||||||||
| 295 | 2 | $self->clear_cache; | |||||||||||||||||||||||||||
| 296 | 2 | $self->values_are_unique (1); | |||||||||||||||||||||||||||
| 297 | 2 | return $self; | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | 4 | my $new = $self->new($self->{data_type}); | |||||||||||||||||||||||||||
| 301 | 4 | $new->_set_piddle($unique); | |||||||||||||||||||||||||||
| 302 | 4 | $new->_set_weights_piddle(\@wts); | |||||||||||||||||||||||||||
| 303 | 4 | $new->values_are_unique (1); | |||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 4 | return $new; | |||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 49 | ▶ | sub _get_cumsum_weight_vector { | ||||||||||||||||||||||||||
|
Subroutine
_get_cumsum_weight_vector: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 309 | 49 | my $self = shift; | |||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | return $self->{_cache}{cumsum_weight_vector} | ||||||||||||||||||||||||||||
| 312 | 49 | ▶ | if defined $self->{_cache}{cumsum_weight_vector}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 313 | return $self->{_cache}{cumsum_weight_vector} | ||||||||||||||||||||||||||||
| 314 | 19 | = $self->_get_weights_piddle->cumusumover->reshape; | |||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 13 | ▶ | sub _skewness { | ||||||||||||||||||||||||||
|
Subroutine
_skewness: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 318 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 13 | my $data = $self->_get_piddle; | |||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | # long winded approach | ||||||||||||||||||||||||||||
| 323 | 13 | my $mean = $self->mean; | |||||||||||||||||||||||||||
| 324 | 13 | my $sd = $self->standard_deviation; | |||||||||||||||||||||||||||
| 325 | 13 | my $wts = $self->_get_weights_piddle; | |||||||||||||||||||||||||||
| 326 | 13 | my $sumpow3 = ($wts * ((($data - $mean) / $sd) ** 3))->sum; | |||||||||||||||||||||||||||
| 327 | 13 | my $skew = $sumpow3 / $self->sum_weights; | |||||||||||||||||||||||||||
| 328 | 13 | return $skew; | |||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 13 | ▶ | sub _kurtosis { | ||||||||||||||||||||||||||
|
Subroutine
_kurtosis: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 332 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 13 | my $data = $self->_get_piddle; | |||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | # long winded approach | ||||||||||||||||||||||||||||
| 337 | 13 | my $mean = $self->mean; | |||||||||||||||||||||||||||
| 338 | 13 | my $sd = $self->standard_deviation; | |||||||||||||||||||||||||||
| 339 | 13 | my $wts = $self->_get_weights_piddle; | |||||||||||||||||||||||||||
| 340 | 13 | my $sumpow4 = ($wts * ((($data - $mean) / $sd) ** 4))->sum; | |||||||||||||||||||||||||||
| 341 | 13 | my $kurt = $sumpow4 / $self->sum_weights - 3; | |||||||||||||||||||||||||||
| 342 | 13 | return $kurt; | |||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 6 | ▶ | sub _harmonic_mean { | ||||||||||||||||||||||||||
|
Subroutine
_harmonic_mean: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 347 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 6 | my $data = $self->_get_piddle; | |||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | # not sure about this... | ||||||||||||||||||||||||||||
| 352 | 6 | ▶ | return undef if $data->which->nelem != $data->nelem; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 5 | my $wts = $self->_get_weights_piddle; | |||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 5 | my $hs = ((1 / $data) * $wts)->sum; | |||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 5 | ▶ | return $hs ? $self->count / $hs : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | 5 | ▶ | sub _geometric_mean { | ||||||||||||||||||||||||||
|
Subroutine
_geometric_mean: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 362 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 5 | my $data = $self->_get_piddle; | |||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | # should add a sorted status check, as we can use vsearch in such cases | ||||||||||||||||||||||||||||
| 367 | 5 | ▶ | return undef if $data->where($data < 0)->nelem; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | 5 | my $wts = $self->_get_weights_piddle; | |||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | # formula from https://en.wikipedia.org/wiki/Weighted_geometric_mean | ||||||||||||||||||||||||||||
| 372 | 5 | return exp (($data->log * $wts)->sum / $wts->sum); | |||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 4 | ▶ | sub _mode { | ||||||||||||||||||||||||||
|
Subroutine
_mode: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 376 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | # de-duplicate and aggregate weights if needed | ||||||||||||||||||||||||||||
| 379 | 4 | my $deduped = $self->_deduplicate; | |||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | 4 | my $data = $deduped->_get_piddle; | |||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 4 | my $wts = $deduped->_get_weights_piddle; | |||||||||||||||||||||||||||
| 385 | 4 | my $mode = $data->at($wts->maximum_ind); | |||||||||||||||||||||||||||
| 386 | 4 | ▶ | if ($mode > $data->max) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 387 | # PDL returns strange numbers when distributions are flat | ||||||||||||||||||||||||||||
| 388 | 0 | $mode = undef; | |||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | 4 | return $mode; | |||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | # need to convert $p to fraction, or perhaps die if it is between 0 and 1 | ||||||||||||||||||||||||||||
| 395 | 5 | ▶ | sub _percentile { | ||||||||||||||||||||||||||
|
Subroutine
_percentile: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 396 | 5 | my ($self, $p) = @_; | |||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 5 | my $data = $self->_get_piddle; | |||||||||||||||||||||||||||
| 399 | return undef | ||||||||||||||||||||||||||||
| 400 | 5 | ▶ | if !defined $data or $data->isempty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $data or $data->isempty
MC/DC0%0/2
not defined $data or $data->isempty
not defined $data
$data->isempty
Truth table33%1/3
not(A) or B
A= not defined $dataB= $data->isempty
| |||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | 5 | $data = $self->_sort_piddle; | |||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 5 | my $cumsum = $self->_get_cumsum_weight_vector; | |||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | 5 | my $target_wt = $self->sum_weights * ($p / 100); | |||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | 5 | my $idx = PDL->pdl($target_wt)->vsearch_insert_leftmost($cumsum->reshape); | |||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | 5 | return $data->at($idx); | |||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | 5 | 1; | |||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | __END__ | ||||||||||||||||||||||||||||