| 1 | |||||||||||
| 2 | package Statistics::Basic::Mode; | ||||||||||
| 3 | |||||||||||
| 4 | 33 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 33 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 33 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 33 | ▶ | use Statistics::Basic; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 33 | ▶ | use Scalar::Util qw(blessed); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 33 | ▶ | use base 'Statistics::Basic::_OneVectorBase'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | use overload | ||||||||||
| 13 | 33 | ▶ | '""' => sub { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.4
Subroutine
__ANON__: called CC=3 SCAR=13.1
| |||||||||||
| 14 | 1 | ▶ | defined( my $q = $_[0]->query ) or return "n/a"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | 1 | ▶ | return $q if ref $q; # vectors interpolate themselves | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | 0 | $Statistics::Basic::fmt->format_number($_[0]->query, $Statistics::Basic::IPRES); | |||||||||
| 17 | }, | ||||||||||
| 18 | 0 | ▶ | '0+' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 19 | 0 | my $q = $_[0]->query; | |||||||||
| 20 | 0 | ▶ | croak "result is multimodal and cannot be used as a number" if ref $q; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 0 | $q; | |||||||||
| 22 | }, | ||||||||||
| 23 | 33 | fallback => 1; # tries to do what it would have done if this wasn't present. | |||||||||
| 24 | |||||||||||
| 25 | 4 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||
| 26 | 4 | my $class = shift; | |||||||||
| 27 | |||||||||||
| 28 | 4 | ▶ | warn "[new $class]\n" if $Statistics::Basic::DEBUG >= 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | |||||||||||
| 30 | 4 | my $this = bless {}, $class; | |||||||||
| 31 | 4 | ▶ | my $vector = eval { Statistics::Basic::Vector->new(@_) } or croak $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 4 | ▶ | my $c = $vector->_get_computer("mode"); return $c if defined $c; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | |||||||||||
| 34 | 4 | $this->{v} = $vector; | |||||||||
| 35 | |||||||||||
| 36 | 4 | $vector->_set_computer( mode => $this ); | |||||||||
| 37 | |||||||||||
| 38 | 4 | return $this; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 7 | ▶ | sub _recalc { | ||||||||
|
Subroutine
_recalc: called CC=7 SCAR=19.5
| |||||||||||
| 42 | 7 | my $this = shift; | |||||||||
| 43 | 7 | my $v = $this->{v}; | |||||||||
| 44 | 7 | my $cardinality = $v->query_size; | |||||||||
| 45 | |||||||||||
| 46 | 7 | delete $this->{recalc_needed}; | |||||||||
| 47 | 7 | delete $this->{_value}; | |||||||||
| 48 | 7 | ▶ | return unless $cardinality > 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 7 | ▶ | return unless $v->query_filled; # only applicable in certain circumstances | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | |||||||||||
| 51 | 7 | my %mode; | |||||||||
| 52 | 7 | my $max = 0; | |||||||||
| 53 | |||||||||||
| 54 | 7 | for my $val ($v->query) { | |||||||||
| 55 | 33 | ▶ | no warnings 'uninitialized'; ## no critic | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 56 | 51 | my $t = ++ $mode{$val}; | |||||||||
| 57 | 51 | ▶ | $max = $t if $t > $max; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | } | ||||||||||
| 59 | 7 | my @a = sort {$a<=>$b} grep { $mode{$_}==$max } keys %mode; | |||||||||
| 60 | |||||||||||
| 61 | 7 | ▶ | $this->{_value} = ( (@a == 1) ? $a[0] : Statistics::Basic::Vector->new(\@a) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | |||||||||||
| 63 | 7 | ▶ | warn "[recalc " . ref($this) . "] count of $this->{_value} = $max\n" if $Statistics::Basic::DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | |||||||||||
| 65 | 7 | return; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | sub is_multimodal { | ||||||||
|
Subroutine
is_multimodal: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 69 | 0 | my $this = shift; | |||||||||
| 70 | 0 | my $that = $this->query; | |||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | return (blessed($that) ? 1:0); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 33 | 1; | |||||||||