| 1 | package Statistics::Shannon; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use vars qw($VERSION @ISA); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Statistics::Frequency 0.04; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | @ISA = qw(Statistics::Frequency); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 4 | my $Napier = exp(1); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Statistics::Shannon - Shannon index | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | The object-oriented interface: | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | use Statistics::Shannon; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # The constructor is inherited from Statistics::Frequency. | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | my $pop = Statistics::Shannon->new(@data); | ||||||||||||||||||||||||||||
| 27 | my $pop = Statistics::Shannon->new(\@data); | ||||||||||||||||||||||||||||
| 28 | my $pop = Statistics::Shannon->new(\%data); | ||||||||||||||||||||||||||||
| 29 | my $pop = Statistics::Shannon->new($another); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # The Shannon index and the Shannon evenness. | ||||||||||||||||||||||||||||
| 32 | # The default base uses natural logarithm. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | print $pop->index, "\n"; | ||||||||||||||||||||||||||||
| 35 | print $pop->index($base), "\n"; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | print $pop->evenness, "\n"; | ||||||||||||||||||||||||||||
| 38 | print $pop->evenness($base), "\n"; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | The "anonymous" interface where the population data is not a | ||||||||||||||||||||||||||||
| 41 | Statistics::Frequency object but instead either an array reference, | ||||||||||||||||||||||||||||
| 42 | in which case the array elements are the frequencies, or a hash | ||||||||||||||||||||||||||||
| 43 | reference, in which keys the hash values are the frequencies. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | use Statistics::Shannon; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | print Statistics::Shannon::index([ data ]), "\n"; | ||||||||||||||||||||||||||||
| 48 | print Statistics::Shannon::index([ data ], $base), "\n"; | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | print Statistics::Shannon::index({ data }), "\n"; | ||||||||||||||||||||||||||||
| 51 | print Statistics::Shannon::index({ data }, $base), "\n"; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | print Statistics::Shannon::evenness([ data ]), "\n"; | ||||||||||||||||||||||||||||
| 54 | print Statistics::Shannon::evenness([ data ], $base), "\n"; | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | print Statistics::Shannon::evenness({ data }), "\n"; | ||||||||||||||||||||||||||||
| 57 | print Statistics::Shannon::evenness({ data }, $base), "\n"; | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | The rest of data manipulation interface inherited from | ||||||||||||||||||||||||||||
| 60 | Statistics::Frequency, see L<Statistics::Frequency>. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | $pop->add_data(@more_data); | ||||||||||||||||||||||||||||
| 63 | $pop->add_data(\@more_data); | ||||||||||||||||||||||||||||
| 64 | $pop->add_data(\%more_data); | ||||||||||||||||||||||||||||
| 65 | $pop->add_data($another); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | $pop->remove_data(@less_data); | ||||||||||||||||||||||||||||
| 68 | $pop->remove_data(\@less_data); | ||||||||||||||||||||||||||||
| 69 | $pop->remove_data(\%less_data); | ||||||||||||||||||||||||||||
| 70 | $pop->remove_data($another); | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | $pop->copy_data($another); | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | $pop->clear_data(); | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | Statistics::Shannon module can be used to compute the Shannon | ||||||||||||||||||||||||||||
| 79 | index of data, which is a variability measure of data. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | The index() and evenness() interfaces are the only genuine interfaces | ||||||||||||||||||||||||||||
| 82 | of this module, the constructor and the rest of the data manipulation | ||||||||||||||||||||||||||||
| 83 | interface is inherited from Statistics::Frequency. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | The Shannon index is also known as Shannon-Wiener index and | ||||||||||||||||||||||||||||
| 86 | as Shannon-Weaver index, especially when applied to biology | ||||||||||||||||||||||||||||
| 87 | and ecology and when talking about populations and biodiversity. | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head2 new | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | my $pop = Statistics::Shannon->new(@data); | ||||||||||||||||||||||||||||
| 92 | my $pop = Statistics::Shannon->new(\@data); | ||||||||||||||||||||||||||||
| 93 | my $pop = Statistics::Shannon->new(\%data); | ||||||||||||||||||||||||||||
| 94 | my $pop = Statistics::Shannon->new($another); | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Creates a new Shannon object from the initial data. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | The data may be either a list, a reference to an array or a reference | ||||||||||||||||||||||||||||
| 99 | to a hash. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =over 4 | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =item * | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | If the data is a list (or an array), the list elements are counted | ||||||||||||||||||||||||||||
| 106 | to find out their frequencies. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =item * | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | If the data is a reference to an array, the array elements are counted | ||||||||||||||||||||||||||||
| 111 | to find out their frequencies. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =item * | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | If the data is a reference to a hash, the hash keys are the data | ||||||||||||||||||||||||||||
| 116 | elements and the hash values are the data frequencies. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =item * | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | If the data is another Statistics::Shannon object, its | ||||||||||||||||||||||||||||
| 121 | frequencies are used. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =back | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =head2 index | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | $pop->index; | ||||||||||||||||||||||||||||
| 128 | $pop->index($base); | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Return the Shannon index of the data. The index is | ||||||||||||||||||||||||||||
| 131 | defined as | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | $Shannon = -sum($p{$e}*log($p{$e}) | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | where the $p{$e} is the proportional [0,1] frequency of the element $e. | ||||||||||||||||||||||||||||
| 136 | The log() is the natural logarithm: if you want to use some other base, | ||||||||||||||||||||||||||||
| 137 | specify the base. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head2 evenness | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Evenness measures how similar the frequencies are. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | $Evenness = $Shannon / log($NumberOfDifferentElements) | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | When all the frequencies are equal, evenness is one. Frequency | ||||||||||||||||||||||||||||
| 146 | imbalance increases the evenness value. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head2 add_data | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | $pop->add_data(@more_data); | ||||||||||||||||||||||||||||
| 151 | $pop->add_data(\@more_data); | ||||||||||||||||||||||||||||
| 152 | $pop->add_data(\%more_data); | ||||||||||||||||||||||||||||
| 153 | $pop->add_data($another); | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | Add more data to the object. The arguments are as in new(). | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head2 remove_data | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | $pop->remove_data(@less_data); | ||||||||||||||||||||||||||||
| 160 | $pop->remove_data(\@less_data); | ||||||||||||||||||||||||||||
| 161 | $pop->remove_data(\%less_data); | ||||||||||||||||||||||||||||
| 162 | $pop->remove_data($another); | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | Remove data from the object. The arguments are as in new(). | ||||||||||||||||||||||||||||
| 165 | The frequencies of data elements are gapped at zero. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head2 copy_data | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | $pop->clear_data($another); | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Copy all data from another object. The old data is discarded. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head2 clear_data | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | $pop->clear_data(); | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Remove all data from the object. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head1 ERRORS | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | The optional base given to index() and evenness() must naturally | ||||||||||||||||||||||||||||
| 182 | be greater than one. If not, an error like | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | index: base cannot be <= 1.0 | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | will be thrown. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Claude Elwood Shannon is known as the father of information theory: | ||||||||||||||||||||||||||||
| 191 | L<http://www-gap.dcs.st-and.ac.uk/~history/Mathematicians/Shannon.html> | ||||||||||||||||||||||||||||
| 192 | and L<http://www.bell-labs.com/news/2001/february/26/1.html> | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | For another variability index see | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | L<Statistics::Simpson> | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | For the data manipulation interface see (though the whole | ||||||||||||||||||||||||||||
| 199 | interface is documented here) | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | L<Statistics::Frequency> | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | Copyright (C) 2002-2016, Jarkko Hietaniemi <jhi@iki.fi> | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 208 | it under the same terms as Perl 5.18.2. | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =cut | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 14 | ▶ | sub index { | ||||||||||||||||||||||||||
|
Subroutine
index: called CC=15 SCAR=27.7
Pod
documented
| |||||||||||||||||||||||||||||
| 213 | 14 | my ($self, $base) = @_; | |||||||||||||||||||||||||||
| 214 | 14 | ▶ | if (@_ == 2 && $base <= 1.0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ == 2 and $base <= 1
MC/DC100%2/2
@_ == 2 and $base <= 1
@_ == 2
$base <= 1
Truth table100%3/3
A and B
A= @_ == 2B= $base <= 1
| |||||||||||||||||||||||||||||
| 215 | 1 | require Carp; | |||||||||||||||||||||||||||
| 216 | 1 | Carp::croak("index: base cannot be <= 1.0"); | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | 13 | ▶ | $base ||= $Napier; | ||||||||||||||||||||||||||
|
Condition66%2/3
$base ||= $Napier
MC/DC0%0/2
$base ||= $Napier
$base
$Napier
Truth table66%2/3
A ||= B
A= $baseB= $Napier
| |||||||||||||||||||||||||||||
| 219 | 13 | my $shannon = 0; | |||||||||||||||||||||||||||
| 220 | 13 | ▶ | if (ref $self eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 0 | $self = [ values %$self ]; | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | 13 | ▶ | if (ref $self eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | 2 | my $total; | |||||||||||||||||||||||||||
| 225 | 2 | for my $e (@$self) { | |||||||||||||||||||||||||||
| 226 | 6 | $total += $e; | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | 2 | for my $e (@$self) { | |||||||||||||||||||||||||||
| 229 | 6 | my $prop = $e / $total; | |||||||||||||||||||||||||||
| 230 | 6 | ▶ | next unless $prop; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 6 | $shannon += $prop * log($prop); | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | 2 | $shannon = -$shannon; | |||||||||||||||||||||||||||
| 234 | } else { | ||||||||||||||||||||||||||||
| 235 | 11 | ▶ | if (!exists $self->{shannon} || !defined $self->{shannon}->{$base}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $self->{"shannon"} and defined $self->{"shannon"}{$base}
MC/DC100%2/2
exists $self->{"shannon"} and defined $self->{"shannon"}{$base}
exists $self->{"shannon"}
defined $self->{"shannon"}{$base}
Truth table100%3/3
A and B
A= exists $self->{"shannon"}B= defined $self->{"shannon"}{$base}
| |||||||||||||||||||||||||||||
| 236 | 5 | my %prop = $self->proportional_frequencies; | |||||||||||||||||||||||||||
| 237 | 5 | for my $e (keys %prop) { | |||||||||||||||||||||||||||
| 238 | 10 | ▶ | next unless $prop{$e}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 10 | $shannon += $prop{$e} * log($prop{$e}); | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | 5 | ▶ | if (defined $shannon) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | 5 | $shannon = -$shannon; | |||||||||||||||||||||||||||
| 243 | 5 | $self->{shannon}->{$base} = $shannon; | |||||||||||||||||||||||||||
| 244 | 0 | ▶ | $self->_set_update_callback( sub { delete $_[0]->{shannon}->{$base} if exists $_[0]->{shannon} } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | 11 | $shannon = $self->{shannon}->{$base}; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | 13 | ▶ | return @_ == 2 ? $shannon / log($base) : $shannon; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 11 | ▶ | sub evenness { | ||||||||||||||||||||||||||
|
Subroutine
evenness: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 253 | 11 | my ($self, $base) = @_; | |||||||||||||||||||||||||||
| 254 | 11 | ▶ | if (@_ == 2 && $base <= 1.0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ == 2 and $base <= 1
MC/DC100%2/2
@_ == 2 and $base <= 1
@_ == 2
$base <= 1
Truth table100%3/3
A and B
A= @_ == 2B= $base <= 1
| |||||||||||||||||||||||||||||
| 255 | 1 | require Carp; | |||||||||||||||||||||||||||
| 256 | 1 | Carp::croak("evenness: base cannot be <= 1.0"); | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | 10 | ▶ | if (ref $self eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | 0 | $self = [ values %$self ]; | |||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | 10 | my $a = ref $self eq 'ARRAY'; | |||||||||||||||||||||||||||
| 262 | 10 | ▶ | my $S = $a ? @$self : $self->elements; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | 10 | ▶ | my $i = $S > 1 ? ( $a ? Statistics::Shannon::index($self) : $self->index ) : undef; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 264 | 10 | ▶ | my $E = $S > 1 ? ( @_ == 2 ? $i * log($base) / log($S) : $i / log($S) ) : undef; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 265 | 10 | ▶ | return defined $E && @_ == 2 ? $E / log($base) : $E; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $E and @_ == 2
MC/DC100%2/2
defined $E and @_ == 2
defined $E
@_ == 2
Truth table100%3/3
A and B
A= defined $EB= @_ == 2
| |||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 4 | 1; | |||||||||||||||||||||||||||