| 1 | package Statistics::Descriptive::Sparse; | ||||||||||||||||||||
| 2 | 11 | $Statistics::Descriptive::Sparse::VERSION = '3.0801'; | |||||||||||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 11 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 11 | ▶ | use vars qw(%fields); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 11 | ▶ | use Carp qw/ carp /; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 11 | require Statistics::Descriptive; | |||||||||||||||||||
| 9 | 11 | ▶ | use Statistics::Descriptive::Smoother (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | ## no critic (ProhibitExplicitReturnUndef) | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | sub _make_accessors | ||||||||||||||||||||
| 14 | 33 | ▶ | { | ||||||||||||||||||
|
Subroutine
_make_accessors: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 15 | 33 | my ( $pkg, $methods ) = @_; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | ## no critic | ||||||||||||||||||||
| 18 | 11 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | ## use critic | ||||||||||||||||||||
| 20 | 33 | foreach my $method (@$methods) | |||||||||||||||||||
| 21 | { | ||||||||||||||||||||
| 22 | 143 | *{ $pkg . "::" . $method } = do | |||||||||||||||||||
| 23 | 143 | { | |||||||||||||||||||
| 24 | 143 | my $m = $method; | |||||||||||||||||||
| 25 | 799 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 26 | 799 | my $self = shift; | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 799 | ▶ | if (@_) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | { | ||||||||||||||||||||
| 30 | 445 | $self->{$m} = shift; | |||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | 799 | return $self->{$m}; | |||||||||||||||||||
| 33 | 143 | }; | |||||||||||||||||||
| 34 | }; | ||||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 33 | return; | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | sub _make_private_accessors | ||||||||||||||||||||
| 41 | 22 | ▶ | { | ||||||||||||||||||
|
Subroutine
_make_private_accessors: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 42 | 22 | my ( $pkg, $methods ) = @_; | |||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | ## no critic | ||||||||||||||||||||
| 45 | 11 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 46 | ## use critic | ||||||||||||||||||||
| 47 | 22 | foreach my $method (@$methods) | |||||||||||||||||||
| 48 | { | ||||||||||||||||||||
| 49 | 132 | *{ $pkg . "::_" . $method } = do | |||||||||||||||||||
| 50 | 132 | { | |||||||||||||||||||
| 51 | 132 | my $m = $method; | |||||||||||||||||||
| 52 | 461 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 53 | 461 | my $self = shift; | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 461 | ▶ | if (@_) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | { | ||||||||||||||||||||
| 57 | 173 | $self->{$m} = shift; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | 461 | return $self->{$m}; | |||||||||||||||||||
| 60 | 132 | }; | |||||||||||||||||||
| 61 | }; | ||||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 22 | return; | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | ##Define the fields to be used as methods | ||||||||||||||||||||
| 68 | 11 | %fields = ( | |||||||||||||||||||
| 69 | count => 0, | ||||||||||||||||||||
| 70 | mean => undef, | ||||||||||||||||||||
| 71 | sum => undef, | ||||||||||||||||||||
| 72 | sumsq => undef, | ||||||||||||||||||||
| 73 | min => undef, | ||||||||||||||||||||
| 74 | max => undef, | ||||||||||||||||||||
| 75 | mindex => undef, | ||||||||||||||||||||
| 76 | maxdex => undef, | ||||||||||||||||||||
| 77 | sample_range => undef, | ||||||||||||||||||||
| 78 | variance => undef, | ||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 11 | __PACKAGE__->_make_accessors( [ grep { $_ ne "variance" } keys(%fields) ] ); | |||||||||||||||||||
| 82 | 11 | __PACKAGE__->_make_accessors( ["_permitted"] ); | |||||||||||||||||||
| 83 | 11 | __PACKAGE__->_make_private_accessors( ["variance"] ); | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | sub new | ||||||||||||||||||||
| 86 | 39 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 87 | 39 | my $proto = shift; | |||||||||||||||||||
| 88 | 39 | ▶ | 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
| |||||||||||||||||||||
| 89 | 39 | my $self = { %fields, }; | |||||||||||||||||||
| 90 | 39 | bless( $self, $class ); | |||||||||||||||||||
| 91 | 39 | $self->_permitted( \%fields ); | |||||||||||||||||||
| 92 | 39 | return $self; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | sub _is_permitted | ||||||||||||||||||||
| 96 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
_is_permitted: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 97 | 0 | my $self = shift; | |||||||||||||||||||
| 98 | 0 | my $key = shift; | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 0 | return exists( $self->_permitted()->{$key} ); | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | sub add_data | ||||||||||||||||||||
| 104 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
add_data: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||
| 105 | 0 | my $self = shift; ##Myself | |||||||||||||||||||
| 106 | 0 | my $oldmean; | |||||||||||||||||||
| 107 | 0 | my ( $min, $mindex, $max, $maxdex, $sum, $sumsq, $count ); | |||||||||||||||||||
| 108 | 0 | my $aref; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 0 | ▶ | if ( ref $_[0] eq 'ARRAY' ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | { | ||||||||||||||||||||
| 112 | 0 | $aref = $_[0]; | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | else | ||||||||||||||||||||
| 115 | { | ||||||||||||||||||||
| 116 | 0 | $aref = \@_; | |||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | ##If we were given no data, we do nothing. | ||||||||||||||||||||
| 120 | 0 | ▶ | return 1 if ( !@{$aref} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | ##Take care of appending to an existing data set | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 0 | ▶ | if ( !defined( $min = $self->min() ) ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | { | ||||||||||||||||||||
| 126 | 0 | $min = $aref->[ $mindex = 0 ]; | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | else | ||||||||||||||||||||
| 129 | { | ||||||||||||||||||||
| 130 | 0 | $mindex = $self->mindex(); | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 0 | ▶ | if ( !defined( $max = $self->max() ) ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 134 | { | ||||||||||||||||||||
| 135 | 0 | $max = $aref->[ $maxdex = 0 ]; | |||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | else | ||||||||||||||||||||
| 138 | { | ||||||||||||||||||||
| 139 | 0 | $maxdex = $self->maxdex(); | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 0 | $sum = $self->sum(); | |||||||||||||||||||
| 143 | 0 | $sumsq = $self->sumsq(); | |||||||||||||||||||
| 144 | 0 | $count = $self->count(); | |||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | ##Calculate new mean, sumsq, min and max; | ||||||||||||||||||||
| 147 | 0 | foreach ( @{$aref} ) | |||||||||||||||||||
| 148 | { | ||||||||||||||||||||
| 149 | 0 | $sum += $_; | |||||||||||||||||||
| 150 | 0 | $sumsq += $_**2; | |||||||||||||||||||
| 151 | 0 | ++$count; | |||||||||||||||||||
| 152 | 0 | ▶ | if ( $_ >= $max ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 153 | { | ||||||||||||||||||||
| 154 | 0 | $max = $_; | |||||||||||||||||||
| 155 | 0 | $maxdex = $count - 1; | |||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | 0 | ▶ | if ( $_ <= $min ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | { | ||||||||||||||||||||
| 159 | 0 | $min = $_; | |||||||||||||||||||
| 160 | 0 | $mindex = $count - 1; | |||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 0 | $self->min($min); | |||||||||||||||||||
| 165 | 0 | $self->mindex($mindex); | |||||||||||||||||||
| 166 | 0 | $self->max($max); | |||||||||||||||||||
| 167 | 0 | $self->maxdex($maxdex); | |||||||||||||||||||
| 168 | 0 | $self->sample_range( $max - $min ); | |||||||||||||||||||
| 169 | 0 | $self->sum($sum); | |||||||||||||||||||
| 170 | 0 | $self->sumsq($sumsq); | |||||||||||||||||||
| 171 | 0 | $self->mean( $sum / $count ); | |||||||||||||||||||
| 172 | 0 | $self->count($count); | |||||||||||||||||||
| 173 | ##indicator the value is not cached. Variance isn't commonly enough | ||||||||||||||||||||
| 174 | ##used to recompute every single data add. | ||||||||||||||||||||
| 175 | 0 | $self->_variance(undef); | |||||||||||||||||||
| 176 | 0 | return 1; | |||||||||||||||||||
| 177 | } | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | sub standard_deviation | ||||||||||||||||||||
| 180 | 21 | ▶ | { | ||||||||||||||||||
|
Subroutine
standard_deviation: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 181 | 21 | my $self = shift; ##Myself | |||||||||||||||||||
| 182 | 21 | ▶ | return undef if ( !$self->count() ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 183 | 17 | return sqrt( $self->variance() ); | |||||||||||||||||||
| 184 | } | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | ##Return variance; if needed, compute and cache it. | ||||||||||||||||||||
| 187 | sub variance | ||||||||||||||||||||
| 188 | 19 | ▶ | { | ||||||||||||||||||
|
Subroutine
variance: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 189 | 19 | my $self = shift; ##Myself | |||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | 19 | my $count = $self->count(); | |||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | 19 | ▶ | return undef if !$count; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | 18 | ▶ | return 0 if $count == 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 17 | ▶ | if ( !defined( $self->_variance() ) ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 198 | { | ||||||||||||||||||||
| 199 | 8 | my $variance = ( $self->sumsq() - $count * $self->mean()**2 ); | |||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | # Sometimes due to rounding errors we get a number below 0. | ||||||||||||||||||||
| 202 | # This makes sure this is handled as gracefully as possible. | ||||||||||||||||||||
| 203 | # | ||||||||||||||||||||
| 204 | # See: | ||||||||||||||||||||
| 205 | # | ||||||||||||||||||||
| 206 | # https://rt.cpan.org/Public/Bug/Display.html?id=46026 | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | 8 | ▶ | $variance = $variance < 0 ? 0 : $variance / ( $count - 1 ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | 8 | $self->_variance($variance); | |||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | # Return now to avoid re-entering this sub | ||||||||||||||||||||
| 213 | # (and therefore save time when many objects are used). | ||||||||||||||||||||
| 214 | 8 | return $variance; | |||||||||||||||||||
| 215 | } | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | 9 | return $self->_variance(); | |||||||||||||||||||
| 218 | } | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | ##Clear a stat. More efficient than destroying an object and calling | ||||||||||||||||||||
| 221 | ##new. | ||||||||||||||||||||
| 222 | sub clear | ||||||||||||||||||||
| 223 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
clear: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 224 | 0 | my $self = shift; ##Myself | |||||||||||||||||||
| 225 | 0 | my $key; | |||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | 0 | ▶ | return if ( !$self->count() ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 228 | 0 | ▶ | while ( my ( $field, $value ) = each %fields ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 229 | { # could use a slice assignment here | ||||||||||||||||||||
| 230 | 0 | $self->{$field} = $value; | |||||||||||||||||||
| 231 | } | ||||||||||||||||||||
| 232 | } | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | 11 | 1; | |||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | __END__ | ||||||||||||||||||||