| 1 | package Statistics::Descriptive::Discrete; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | ### This module draws heavily from Statistics::Descriptive | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use AutoLoader; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use vars qw($VERSION $AUTOLOAD $DEBUG $Tolerance %autosubs); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | $VERSION = '0.12'; | |||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | $DEBUG = 0; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | #see Statistics::Descriptive documentation for use of $Tolerance | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | $Tolerance = 0.0; | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | #what subs can be autoloaded? | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | %autosubs = ( | |||||||||||||||||||||||||||||||||||||||||
| 19 | count => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 20 | mean => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 21 | geometric_mean=> undef, | ||||||||||||||||||||||||||||||||||||||||||
| 22 | harmonic_mean=>undef, | ||||||||||||||||||||||||||||||||||||||||||
| 23 | sum => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 24 | mode => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 25 | median => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | min => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | max => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 28 | mindex => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 29 | maxdex => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | standard_deviation => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | sample_range => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | variance => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | text => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | sub new | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 17 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 17 | my $proto = shift; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 17 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 17 | my $self = {}; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 17 | $self->{_permitted} = \%autosubs; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 17 | $self->{data} = (); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 17 | $self->{_dataindex} = (); #index of where each value first seen when adding data | |||||||||||||||||||||||||||||||||||||||||
| 45 | 17 | $self->{dirty} = 1; #is the data dirty? | |||||||||||||||||||||||||||||||||||||||||
| 46 | 17 | $self->{_index} = 0; #current index of number of data items added | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 17 | bless ($self,$class); | |||||||||||||||||||||||||||||||||||||||||
| 49 | 17 | ▶ | print __PACKAGE__,"->new(",join(',',@_),")\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 17 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | # Clear the stat object & erase all data | ||||||||||||||||||||||||||||||||||||||||||
| 54 | # Object will be ready to use as if new was called | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # Not sure this is more efficient than just creating a new object but | ||||||||||||||||||||||||||||||||||||||||||
| 56 | # maintained for compatability with Statistics::Descriptive | ||||||||||||||||||||||||||||||||||||||||||
| 57 | sub clear | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 8 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clear: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 8 | my %keys = %{ $self }; | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | #remove _permitted from the deletion list | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 8 | delete $keys{"_permitted"}; | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 8 | foreach my $key (keys %keys) | |||||||||||||||||||||||||||||||||||||||||
| 66 | { # Check each key in the object | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 132 | ▶ | print __PACKAGE__,"->clear, deleting $key\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 132 | delete $self->{$key}; # Delete any out of date cached key | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 8 | $self->{data} = (); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 8 | $self->{_dataindex} = (); | |||||||||||||||||||||||||||||||||||||||||
| 72 | 8 | $self->{dirty} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 8 | $self->{_index} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | sub add_data | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 21 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_data: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | #add data but don't compute ANY statistics yet | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 21 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 21 | ▶ | print __PACKAGE__,"->add_data(",join(',',@_),")\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | #get each element and add 0 to force it be a number | ||||||||||||||||||||||||||||||||||||||||||
| 83 | #that way, 0.000 and 0 are treated the same | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 21 | my $val = shift; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 21 | ▶ | while (defined $val) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 136 | $val += 0; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 136 | $self->{data}{$val}++; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 136 | ▶ | if (not exists $self->{_dataindex}{$val}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 74 | $self->{_dataindex}{$val} = $self->{_index}; | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 136 | $self->{_index}++; | |||||||||||||||||||||||||||||||||||||||||
| 93 | #set dirty flag so we know cached stats are invalid | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 136 | $self->{dirty}++; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 136 | $val = shift; #get next element | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | sub add_data_tuple | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 5 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_data_tuple: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | #add data but don't compute ANY statistics yet | ||||||||||||||||||||||||||||||||||||||||||
| 102 | #the data are pairs of values and occurrences | ||||||||||||||||||||||||||||||||||||||||||
| 103 | #e.g. 4,2 means 2 occurrences of the value 4 | ||||||||||||||||||||||||||||||||||||||||||
| 104 | #thanks to Bill Dueber for suggesting this | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 5 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 5 | ▶ | print __PACKAGE__,"->add_data_tuple(",join(',',@_),")\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | #we want an even number of arguments (tuples in the form (value, count)) | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 5 | ▶ | carp "argument list must have even number of elements" if @_ % 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | #get each element and add 0 to force it be a number | ||||||||||||||||||||||||||||||||||||||||||
| 113 | #that way, 0.000 and 0 are treated the same | ||||||||||||||||||||||||||||||||||||||||||
| 114 | #if $count is 0, then this will set the dirty flag but have no effect on | ||||||||||||||||||||||||||||||||||||||||||
| 115 | #the statistics | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 5 | my $val = shift; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 5 | my $count = shift; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 5 | ▶ | while (defined $count) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | { | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 18 | $val += 0; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 18 | $self->{data}{$val} += $count; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 18 | ▶ | if (not exists $self->{_dataindex}{$val}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 18 | $self->{_dataindex}{$val} = $self->{_index}; | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 18 | $self->{_index} += $count; | |||||||||||||||||||||||||||||||||||||||||
| 126 | #set dirty flag so we know cached stats are invalid | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 18 | $self->{dirty}++; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 18 | $val = shift; #get next element | |||||||||||||||||||||||||||||||||||||||||
| 129 | 18 | $count = shift; | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | sub _test_for_too_small_val | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 112 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_test_for_too_small_val: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 112 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 112 | my $val = shift; | |||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 112 | return (abs($val) <= $Statistics::Descriptive::Discrete::Tolerance); | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | sub _calc_harmonic_mean | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 24 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_calc_harmonic_mean: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 24 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 24 | my $count = shift; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 24 | my $datakeys = shift; #array ref | |||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 24 | my $hs = 0; | |||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 24 | foreach my $val ( @{$datakeys} ) | |||||||||||||||||||||||||||||||||||||||||
| 150 | { | ||||||||||||||||||||||||||||||||||||||||||
| 151 | ##Guarantee that there are no divide by zeros | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 91 | ▶ | if ($self->_test_for_too_small_val($val)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | { | ||||||||||||||||||||||||||||||||||||||||||
| 154 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | 88 | foreach (1..$self->{data}{$val}) | |||||||||||||||||||||||||||||||||||||||||
| 158 | { | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 199 | $hs += 1/$val; | |||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 21 | ▶ | if ($self->_test_for_too_small_val($hs)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | { | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 19 | return $count/$hs; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | sub _all_stats | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 28 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_all_stats: called CC=22 SCAR=30.9
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | #compute all the stats in one sub to save overhead of sub calls | ||||||||||||||||||||||||||||||||||||||||||
| 174 | #a little wasteful to do this if all we want is count or sum for example but | ||||||||||||||||||||||||||||||||||||||||||
| 175 | #I want to keep add_data as lean as possible since it gets called a lot | ||||||||||||||||||||||||||||||||||||||||||
| 176 | 28 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 28 | ▶ | print __PACKAGE__,"->_all_stats(",join(',',@_),")\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | #if data is empty, set all stats to undef and return | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 28 | ▶ | if (!$self->{data}) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | { | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 4 | foreach my $key (keys %{$self->{_permitted}}) | |||||||||||||||||||||||||||||||||||||||||
| 183 | { | ||||||||||||||||||||||||||||||||||||||||||
| 184 | 60 | $self->{$key} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 4 | $self->{count} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 187 | 4 | return; | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | #count = total number of data values we have | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 24 | my $count = 0; | |||||||||||||||||||||||||||||||||||||||||
| 192 | 24 | $count += $_ foreach (values %{$self->{data}}); | |||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | 24 | my @datakeys = keys %{$self->{data}}; | |||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | #initialize min, max, mode to an arbitrary value that's in the hash | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 24 | my $default = $datakeys[0]; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 24 | my $max = $default; | |||||||||||||||||||||||||||||||||||||||||
| 199 | 24 | my $min = $default; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 24 | my $mode = $default; | |||||||||||||||||||||||||||||||||||||||||
| 201 | 24 | my $moden = 0; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 24 | my $sum = 0; | |||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | #find min, max, sum, and mode | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 24 | foreach (@datakeys) | |||||||||||||||||||||||||||||||||||||||||
| 206 | { | ||||||||||||||||||||||||||||||||||||||||||
| 207 | 95 | my $n = $self->{data}{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 95 | $sum += $_ * $n; | |||||||||||||||||||||||||||||||||||||||||
| 209 | 95 | ▶ | $min = $_ if $_ < $min; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | 95 | ▶ | $max = $_ if $_ > $max; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | #only finds one mode but there could be more than one | ||||||||||||||||||||||||||||||||||||||||||
| 213 | #also, there might not be any mode (all the same frequency) | ||||||||||||||||||||||||||||||||||||||||||
| 214 | #todo: need to make this more robust | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 95 | ▶ | if ($n > $moden) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | { | ||||||||||||||||||||||||||||||||||||||||||
| 217 | 32 | $mode = $_; | |||||||||||||||||||||||||||||||||||||||||
| 218 | 32 | $moden = $n; | |||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | 24 | my $mindex = $self->{_dataindex}{$min}; | |||||||||||||||||||||||||||||||||||||||||
| 222 | 24 | my $maxdex = $self->{_dataindex}{$max}; | |||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | 24 | my $mean = $sum/$count; | |||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | 24 | my $stddev = 0; | |||||||||||||||||||||||||||||||||||||||||
| 227 | 24 | my $variance = 0; | |||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | 24 | ▶ | if ($count > 1) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | { | ||||||||||||||||||||||||||||||||||||||||||
| 231 | # Thanks to Peter Dienes for finding and fixing a round-off error | ||||||||||||||||||||||||||||||||||||||||||
| 232 | # in the following variance calculation | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 23 | foreach my $val (@datakeys) | |||||||||||||||||||||||||||||||||||||||||
| 235 | { | ||||||||||||||||||||||||||||||||||||||||||
| 236 | 94 | $stddev += $self->{data}{$val} * (($val - $mean) ** 2); | |||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 23 | $variance = $stddev / ($count - 1); | |||||||||||||||||||||||||||||||||||||||||
| 239 | 23 | $stddev = sqrt($variance); | |||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||
| 241 | 1 | else {$stddev = undef} | |||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | #find median, and do it without creating a list of the all the data points | ||||||||||||||||||||||||||||||||||||||||||
| 244 | #if n=count is odd and n=2k+1 then median = data(k+1) | ||||||||||||||||||||||||||||||||||||||||||
| 245 | #if n=count is even and n=2k, then median = (data(k) + data(k+1))/2 | ||||||||||||||||||||||||||||||||||||||||||
| 246 | 24 | my $odd = $count % 2; #odd or even number of points? | |||||||||||||||||||||||||||||||||||||||||
| 247 | 24 | my $even = !$odd; | |||||||||||||||||||||||||||||||||||||||||
| 248 | 24 | ▶ | my $k = $odd ? ($count-1)/2 : $count/2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 24 | my $median = undef; | |||||||||||||||||||||||||||||||||||||||||
| 250 | 24 | my $temp = 0; | |||||||||||||||||||||||||||||||||||||||||
| 251 | 24 | MEDIAN: foreach my $val (sort {$a <=> $b} (@datakeys)) | |||||||||||||||||||||||||||||||||||||||||
| 252 | { | ||||||||||||||||||||||||||||||||||||||||||
| 253 | 67 | foreach (1..$self->{data}{$val}) | |||||||||||||||||||||||||||||||||||||||||
| 254 | { | ||||||||||||||||||||||||||||||||||||||||||
| 255 | 124 | $temp++; | |||||||||||||||||||||||||||||||||||||||||
| 256 | 124 | ▶ | if (($temp == $k) && $even) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$temp == $k and $even
MC/DC100%2/2
$temp == $k and $even
$temp == $k
$even
Truth table100%3/3
A and B
A= $temp == $kB= $even
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | { | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 16 | $median += $val; | |||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 260 | elsif ($temp == $k+1) | ||||||||||||||||||||||||||||||||||||||||||
| 261 | { | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 24 | $median += $val; | |||||||||||||||||||||||||||||||||||||||||
| 263 | 24 | ▶ | $median /= 2 if $even; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 24 | last MEDIAN; | |||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | #compute geometric mean | ||||||||||||||||||||||||||||||||||||||||||
| 270 | 24 | my $gm = 1; | |||||||||||||||||||||||||||||||||||||||||
| 271 | 24 | my $exponent = 1/$count; | |||||||||||||||||||||||||||||||||||||||||
| 272 | 24 | foreach my $val (@datakeys) | |||||||||||||||||||||||||||||||||||||||||
| 273 | { | ||||||||||||||||||||||||||||||||||||||||||
| 274 | 92 | ▶ | if ($val < 0) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | { | ||||||||||||||||||||||||||||||||||||||||||
| 276 | 3 | $gm = undef; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 3 | last; | |||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 89 | foreach (1..$self->{data}{$val}) | |||||||||||||||||||||||||||||||||||||||||
| 280 | { | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 202 | $gm *= $val**$exponent; | |||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | #compute harmonic mean | ||||||||||||||||||||||||||||||||||||||||||
| 286 | 24 | my $harmonic_mean = scalar $self->_calc_harmonic_mean($count, \@datakeys); | |||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | 24 | ▶ | print __PACKAGE__,"count: $count, _index ",$self->{_index},"\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | 24 | $self->{count} = $count; | |||||||||||||||||||||||||||||||||||||||||
| 291 | 24 | $self->{sum} = $sum; | |||||||||||||||||||||||||||||||||||||||||
| 292 | 24 | $self->{standard_deviation} = $stddev; | |||||||||||||||||||||||||||||||||||||||||
| 293 | 24 | $self->{variance} = $variance; | |||||||||||||||||||||||||||||||||||||||||
| 294 | 24 | $self->{min} = $min; | |||||||||||||||||||||||||||||||||||||||||
| 295 | 24 | $self->{max} = $max; | |||||||||||||||||||||||||||||||||||||||||
| 296 | 24 | $self->{mindex} = $mindex; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 24 | $self->{maxdex} = $maxdex; | |||||||||||||||||||||||||||||||||||||||||
| 298 | 24 | $self->{sample_range} = $max - $min; #todo: does this require any bounds checking | |||||||||||||||||||||||||||||||||||||||||
| 299 | 24 | $self->{mean} = $mean; | |||||||||||||||||||||||||||||||||||||||||
| 300 | 24 | $self->{geometric_mean} = $gm; | |||||||||||||||||||||||||||||||||||||||||
| 301 | 24 | $self->{harmonic_mean} = $harmonic_mean; | |||||||||||||||||||||||||||||||||||||||||
| 302 | 24 | $self->{median} = $median; | |||||||||||||||||||||||||||||||||||||||||
| 303 | 24 | $self->{mode} = $mode; | |||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | #clear dirty flag so we don't needlessly recompute the statistics | ||||||||||||||||||||||||||||||||||||||||||
| 306 | 24 | $self->{dirty} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | sub set_text | ||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_text: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | $self->{text} = shift; | |||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | sub get_data | ||||||||||||||||||||||||||||||||||||||||||
| 316 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_data: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | #returns a list of the data in sorted order | ||||||||||||||||||||||||||||||||||||||||||
| 318 | #the list could be very big an this defeat the purpose of using this module | ||||||||||||||||||||||||||||||||||||||||||
| 319 | #use this only if you really need it | ||||||||||||||||||||||||||||||||||||||||||
| 320 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 321 | 1 | ▶ | print __PACKAGE__,"->get_data(",join(',',@_),")\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | 1 | my @data; | |||||||||||||||||||||||||||||||||||||||||
| 324 | 1 | foreach my $val (sort {$a <=> $b} (keys %{$self->{data}})) | |||||||||||||||||||||||||||||||||||||||||
| 325 | { | ||||||||||||||||||||||||||||||||||||||||||
| 326 | 5 | push @data, $val foreach (1..$self->{data}{$val}); | |||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||
| 328 | 1 | return @data; | |||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | # this is the previous frequency_distribution code | ||||||||||||||||||||||||||||||||||||||||||
| 332 | # redid this completely based on current implementation in | ||||||||||||||||||||||||||||||||||||||||||
| 333 | # Statistics::Descriptive | ||||||||||||||||||||||||||||||||||||||||||
| 334 | # sub frequency_distribution | ||||||||||||||||||||||||||||||||||||||||||
| 335 | # { | ||||||||||||||||||||||||||||||||||||||||||
| 336 | # #Compute frequency distribution (histogram), borrowed heavily from Statistics::Descriptive | ||||||||||||||||||||||||||||||||||||||||||
| 337 | # #Behavior is slightly different than Statistics::Descriptive | ||||||||||||||||||||||||||||||||||||||||||
| 338 | # #e.g. if partition is not specified, we use to set the number of partitions | ||||||||||||||||||||||||||||||||||||||||||
| 339 | # # if partition = 0, then we return the data hash WITHOUT binning it into equal bins | ||||||||||||||||||||||||||||||||||||||||||
| 340 | # # I often want to just see how many of each value I saw | ||||||||||||||||||||||||||||||||||||||||||
| 341 | # #Also, you can manually pass in the bin info (min bin, bin size, and number of partitions) | ||||||||||||||||||||||||||||||||||||||||||
| 342 | # #I don't cache the frequency data like Statistics::Descriptive does since it's not as expensive to compute | ||||||||||||||||||||||||||||||||||||||||||
| 343 | # #but I might add that later | ||||||||||||||||||||||||||||||||||||||||||
| 344 | # #todo: the minbin/binsize stuff is funky and not intuitive -- fix it | ||||||||||||||||||||||||||||||||||||||||||
| 345 | # my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 346 | # print __PACKAGE__,"->frequency_distribution(",join(',',@_),")\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | # my $partitions = shift; #how many partitions (bins)? | ||||||||||||||||||||||||||||||||||||||||||
| 349 | # my $minbin = shift; #upper bound of first bin | ||||||||||||||||||||||||||||||||||||||||||
| 350 | # my $binsize = shift; #how wide is each bin? | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | # #if partition == 0, then return the data hash | ||||||||||||||||||||||||||||||||||||||||||
| 353 | # if (not defined $partitions || ($partitions == 0)) | ||||||||||||||||||||||||||||||||||||||||||
| 354 | # { | ||||||||||||||||||||||||||||||||||||||||||
| 355 | # $self->{frequency_partitions} = 0; | ||||||||||||||||||||||||||||||||||||||||||
| 356 | # %{$self->{frequency}} = %{$self->{data}}; | ||||||||||||||||||||||||||||||||||||||||||
| 357 | # return %{$self->{frequency}}; | ||||||||||||||||||||||||||||||||||||||||||
| 358 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | # #otherwise, partition better be >= 1 | ||||||||||||||||||||||||||||||||||||||||||
| 361 | # return undef unless $partitions >= 1; | ||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||
| 363 | # $self->_all_stats() if $self->{dirty}; #recompute stats if dirty, (so we have count) | ||||||||||||||||||||||||||||||||||||||||||
| 364 | # return undef if $self->{count} < 2; #must have at least 2 values | ||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | # #set up the bins | ||||||||||||||||||||||||||||||||||||||||||
| 367 | # my ($interval, $iter, $max); | ||||||||||||||||||||||||||||||||||||||||||
| 368 | # if (defined $minbin && defined $binsize) | ||||||||||||||||||||||||||||||||||||||||||
| 369 | # { | ||||||||||||||||||||||||||||||||||||||||||
| 370 | # $iter = $minbin; | ||||||||||||||||||||||||||||||||||||||||||
| 371 | # $max = $minbin+$partitions*$binsize - $binsize; | ||||||||||||||||||||||||||||||||||||||||||
| 372 | # $interval = $binsize; | ||||||||||||||||||||||||||||||||||||||||||
| 373 | # $iter -= $interval; #so that loop that sets up bins works correctly | ||||||||||||||||||||||||||||||||||||||||||
| 374 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 375 | # else | ||||||||||||||||||||||||||||||||||||||||||
| 376 | # { | ||||||||||||||||||||||||||||||||||||||||||
| 377 | # $iter = $self->{min}; | ||||||||||||||||||||||||||||||||||||||||||
| 378 | # $max = $self->{max}; | ||||||||||||||||||||||||||||||||||||||||||
| 379 | # $interval = $self->{sample_range}/$partitions; | ||||||||||||||||||||||||||||||||||||||||||
| 380 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 381 | # my @k; | ||||||||||||||||||||||||||||||||||||||||||
| 382 | # my %bins; | ||||||||||||||||||||||||||||||||||||||||||
| 383 | # while (($iter += $interval) < $max) | ||||||||||||||||||||||||||||||||||||||||||
| 384 | # { | ||||||||||||||||||||||||||||||||||||||||||
| 385 | # $bins{$iter} = 0; | ||||||||||||||||||||||||||||||||||||||||||
| 386 | # push @k, $iter; | ||||||||||||||||||||||||||||||||||||||||||
| 387 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 388 | # $bins{$max} = 0; | ||||||||||||||||||||||||||||||||||||||||||
| 389 | # push @k, $max; | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | # VALUE: foreach my $val (keys %{$self->{data}}) | ||||||||||||||||||||||||||||||||||||||||||
| 392 | # { | ||||||||||||||||||||||||||||||||||||||||||
| 393 | # foreach my $k (@k) | ||||||||||||||||||||||||||||||||||||||||||
| 394 | # { | ||||||||||||||||||||||||||||||||||||||||||
| 395 | # if ($val <= $k) | ||||||||||||||||||||||||||||||||||||||||||
| 396 | # { | ||||||||||||||||||||||||||||||||||||||||||
| 397 | # $bins{$k} += $self->{data}{$val}; #how many of this value do we have? | ||||||||||||||||||||||||||||||||||||||||||
| 398 | # next VALUE; | ||||||||||||||||||||||||||||||||||||||||||
| 399 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 400 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 401 | # # } | ||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||
| 403 | # %{$self->{frequency}} = %bins; #save it for later in case I add caching | ||||||||||||||||||||||||||||||||||||||||||
| 404 | # $self->{frequency_partitions} = $partitions; #in case I add caching in the future | ||||||||||||||||||||||||||||||||||||||||||
| 405 | # return %{$self->{frequency}}; | ||||||||||||||||||||||||||||||||||||||||||
| 406 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | sub frequency_distribution_ref | ||||||||||||||||||||||||||||||||||||||||||
| 409 | 12 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
frequency_distribution_ref: called CC=17 SCAR=28.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 410 | 12 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 411 | 12 | my @k = (); | |||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||
| 413 | # If called with no parameters, return the cached hashref | ||||||||||||||||||||||||||||||||||||||||||
| 414 | # if we have one and data is not dirty | ||||||||||||||||||||||||||||||||||||||||||
| 415 | # This is implemented this way because that's how Statistics::Descriptive | ||||||||||||||||||||||||||||||||||||||||||
| 416 | # implements this. I don't like it. | ||||||||||||||||||||||||||||||||||||||||||
| 417 | 12 | ▶ | if ((!@_) && (! $self->{dirty}) && (defined $self->{_frequency})) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not @_ || $self->{"dirty"} and defined $self->{"_frequency"}
@_ || $self->{'dirty'}
MC/DC66%2/3
not @_ || $self->{"dirty"} and defined $self->{"_frequency"}
@_
$self->{'dirty'}
defined $self->{"_frequency"}
Truth table75%3/4
not(A || B) and C
A= @_B= $self->{'dirty'}C= defined $self->{"_frequency"}
| |||||||||||||||||||||||||||||||||||||||||||
| 418 | { | ||||||||||||||||||||||||||||||||||||||||||
| 419 | 3 | return $self->{_frequency}; | |||||||||||||||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | 9 | ▶ | $self->_all_stats() if $self->{dirty}; #recompute stats if dirty, (so we have count) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | # Must have at least two elements | ||||||||||||||||||||||||||||||||||||||||||
| 425 | 9 | ▶ | if ($self->count() < 2) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 426 | { | ||||||||||||||||||||||||||||||||||||||||||
| 427 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | 8 | my %bins; | |||||||||||||||||||||||||||||||||||||||||
| 431 | 8 | my $partitions = shift; | |||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | 8 | ▶ | if (ref($partitions) eq 'ARRAY') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 434 | { | ||||||||||||||||||||||||||||||||||||||||||
| 435 | 4 | @k = @{ $partitions }; | |||||||||||||||||||||||||||||||||||||||||
| 436 | 4 | ▶ | return undef unless @k; ##Empty array | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 437 | 4 | ▶ | if (@k > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 438 | ##Check for monotonicity | ||||||||||||||||||||||||||||||||||||||||||
| 439 | 4 | my $element = $k[0]; | |||||||||||||||||||||||||||||||||||||||||
| 440 | 4 | for my $next_elem (@k[1..$#k]) { | |||||||||||||||||||||||||||||||||||||||||
| 441 | 18 | ▶ | if ($element > $next_elem) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 442 | 0 | carp "Non monotonic array cannot be used as frequency bins!\n"; | |||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||||||||||||||||
| 445 | 18 | $element = $next_elem; | |||||||||||||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||||||||||||||
| 448 | 4 | %bins = map { $_ => 0 } @k; | |||||||||||||||||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||||||||||||||||
| 450 | else | ||||||||||||||||||||||||||||||||||||||||||
| 451 | { | ||||||||||||||||||||||||||||||||||||||||||
| 452 | 4 | ▶ | return undef unless (defined $partitions) && ($partitions >= 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $partitions and $partitions >= 1
MC/DC50%1/2
defined $partitions and $partitions >= 1
defined $partitions
$partitions >= 1
Truth table66%2/3
A and B
A= defined $partitionsB= $partitions >= 1
| |||||||||||||||||||||||||||||||||||||||||||
| 453 | 3 | my $interval = $self->sample_range() / $partitions; | |||||||||||||||||||||||||||||||||||||||||
| 454 | 3 | foreach my $idx (1 .. ($partitions-1)) | |||||||||||||||||||||||||||||||||||||||||
| 455 | { | ||||||||||||||||||||||||||||||||||||||||||
| 456 | 2 | push @k, ($self->min() + $idx * $interval); | |||||||||||||||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | 3 | $bins{$self->max()} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | 3 | push @k, $self->max(); | |||||||||||||||||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | ELEMENT: | ||||||||||||||||||||||||||||||||||||||||||
| 465 | 7 | foreach my $element (keys %{$self->{data}}) | |||||||||||||||||||||||||||||||||||||||||
| 466 | { | ||||||||||||||||||||||||||||||||||||||||||
| 467 | 45 | foreach my $limit (@k) | |||||||||||||||||||||||||||||||||||||||||
| 468 | { | ||||||||||||||||||||||||||||||||||||||||||
| 469 | 109 | ▶ | if ($element <= $limit) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 470 | { | ||||||||||||||||||||||||||||||||||||||||||
| 471 | 44 | $bins{$limit} += $self->{data}{$element}; | |||||||||||||||||||||||||||||||||||||||||
| 472 | 44 | next ELEMENT; | |||||||||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||
| 477 | 7 | $self->{_frequency} = \%bins; | |||||||||||||||||||||||||||||||||||||||||
| 478 | 7 | return $self->{_frequency}; | |||||||||||||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||
| 481 | 3 | ▶ | sub frequency_distribution { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
frequency_distribution: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 482 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||
| 484 | 3 | my $ret = $self->frequency_distribution_ref(@_); | |||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||
| 486 | 3 | ▶ | if (!defined($ret)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 487 | { | ||||||||||||||||||||||||||||||||||||||||||
| 488 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 489 | } | ||||||||||||||||||||||||||||||||||||||||||
| 490 | else | ||||||||||||||||||||||||||||||||||||||||||
| 491 | { | ||||||||||||||||||||||||||||||||||||||||||
| 492 | 3 | return %$ret; | |||||||||||||||||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | # return count of unique values in data if called in scalar context | ||||||||||||||||||||||||||||||||||||||||||
| 497 | # returns sorted array of unique data values if called in array context | ||||||||||||||||||||||||||||||||||||||||||
| 498 | # returns undef if no data | ||||||||||||||||||||||||||||||||||||||||||
| 499 | sub uniq | ||||||||||||||||||||||||||||||||||||||||||
| 500 | 8 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
uniq: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 501 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||
| 503 | 8 | ▶ | if (!$self->{data}) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 504 | { | ||||||||||||||||||||||||||||||||||||||||||
| 505 | 2 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||
| 508 | 6 | my @datakeys = sort {$a <=> $b} keys %{$self->{data}}; | |||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | 6 | ▶ | if (wantarray) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 511 | { | ||||||||||||||||||||||||||||||||||||||||||
| 512 | 2 | return @datakeys; | |||||||||||||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||||||||||||||
| 514 | else | ||||||||||||||||||||||||||||||||||||||||||
| 515 | { | ||||||||||||||||||||||||||||||||||||||||||
| 516 | 4 | my $uniq = @datakeys; | |||||||||||||||||||||||||||||||||||||||||
| 517 | 4 | return $uniq; | |||||||||||||||||||||||||||||||||||||||||
| 518 | } | ||||||||||||||||||||||||||||||||||||||||||
| 519 | } | ||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||
| 521 | 84 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=6 SCAR=18.4
| |||||||||||||||||||||||||||||||||||||||||||
| 522 | 84 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 523 | 84 | ▶ | my $type = ref($self) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 524 | or croak "$self is not an object"; | ||||||||||||||||||||||||||||||||||||||||||
| 525 | 84 | my $name = $AUTOLOAD; | |||||||||||||||||||||||||||||||||||||||||
| 526 | 84 | $name =~ s/.*://; ##Strip fully qualified-package portion | |||||||||||||||||||||||||||||||||||||||||
| 527 | 84 | ▶ | return if $name eq "DESTROY"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 528 | 67 | ▶ | unless (exists $self->{_permitted}{$name} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 529 | 0 | croak "Can't access `$name' field in class $type"; | |||||||||||||||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||
| 532 | 67 | ▶ | print __PACKAGE__,"->AUTOLOAD $name\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||
| 534 | #compute stats if necessary | ||||||||||||||||||||||||||||||||||||||||||
| 535 | 67 | ▶ | $self->_all_stats() if $self->{dirty}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 536 | 67 | return $self->{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||
| 539 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||||||
| 541 | __END__ | ||||||||||||||||||||||||||||||||||||||||||