| 1 | package Statistics::Basic::Variance; | ||||||||||
| 2 | |||||||||||
| 3 | 33 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 33 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 33 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 33 | ▶ | use base 'Statistics::Basic::_OneVectorBase'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 45 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||
| 10 | 45 | my $class = shift; | |||||||||
| 11 | |||||||||||
| 12 | 45 | ▶ | warn "[new $class]\n" if $Statistics::Basic::DEBUG >= 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 13 | |||||||||||
| 14 | 45 | my $this = bless {}, $class; | |||||||||
| 15 | 45 | ▶ | my $vector = eval { Statistics::Basic::Vector->new(@_) } or croak $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | 45 | ▶ | my $c = $vector->_get_computer("variance"); return $c if defined $c; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | |||||||||||
| 18 | 44 | $this->{v} = $vector; | |||||||||
| 19 | 44 | ▶ | $this->{m} = eval { Statistics::Basic::Mean->new($vector) } or croak $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | |||||||||||
| 21 | 44 | $vector->_set_computer( variance => $this ); | |||||||||
| 22 | |||||||||||
| 23 | 44 | return $this; | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | 64 | ▶ | sub _recalc { | ||||||||
|
Subroutine
_recalc: called CC=8 SCAR=21.4
| |||||||||||
| 27 | 64 | my $this = shift; | |||||||||
| 28 | 64 | my $first = shift; | |||||||||
| 29 | |||||||||||
| 30 | 64 | delete $this->{recalc_needed}; | |||||||||
| 31 | 64 | delete $this->{_value}; | |||||||||
| 32 | |||||||||||
| 33 | 64 | my $mean = $this->{m}->query; | |||||||||
| 34 | 64 | ▶ | return unless defined $mean; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | |||||||||||
| 36 | 64 | my $v = $this->{v}; | |||||||||
| 37 | 64 | my $cardinality = $v->query_size; | |||||||||
| 38 | 64 | ▶ | $cardinality -- if $Statistics::Basic::UNBIAS; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 64 | ▶ | return unless $cardinality > 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | |||||||||||
| 41 | 64 | ▶ | if( $Statistics::Basic::DEBUG >= 2 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 0 | warn "[recalc " . ref($this) . "] ( $_ - $mean ) ** 2\n" for $v->query; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 33 | ▶ | my $sum = 0; { no warnings 'uninitialized'; ## no critic | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 46 | 64 | $sum += ( $_ - $mean ) ** 2 for $v->query; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 64 | $this->{_value} = ($sum / $cardinality); | |||||||||
| 50 | |||||||||||
| 51 | 64 | ▶ | warn "[recalc " . ref($this) . "] ($sum/$cardinality) = $this->{_value}\n" if $Statistics::Basic::DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | |||||||||||
| 53 | 64 | return; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 1 | ▶ | sub query_mean { return $_[0]->{m} } | ||||||||
|
Subroutine
query_mean: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 57 | |||||||||||
| 58 | 33 | 1; | |||||||||