| 1 | # Copyright 2009 Francesco Nidito. All rights reserved. | ||||||||||
| 2 | # | ||||||||||
| 3 | # This library is free software; you can redistribute it and/or | ||||||||||
| 4 | # modify it under the same terms as Perl itself. | ||||||||||
| 5 | |||||||||||
| 6 | package Statistics::OnLine; | ||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | use vars qw($VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | $VERSION = '0.02'; | |||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 14 | 1 | my $class = shift; | |||||||||
| 15 | 1 | return bless { | |||||||||
| 16 | _count => 0, | ||||||||||
| 17 | _mean => 0, | ||||||||||
| 18 | _M2 => 0, | ||||||||||
| 19 | _M3 => 0, | ||||||||||
| 20 | _M4 => 0, | ||||||||||
| 21 | version => $VERSION, | ||||||||||
| 22 | }, $class; | ||||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | 11 | ▶ | sub add_data { | ||||||||
|
Subroutine
add_data: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 26 | 11 | my $self = shift; | |||||||||
| 27 | 11 | foreach my $x (@_) { $self->_update_statistics($x); } | |||||||||
| 28 | 11 | return $self; | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 6 | ▶ | sub clean { | ||||||||
|
Subroutine
clean: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 32 | 6 | my ($self) = @_; | |||||||||
| 33 | 6 | foreach my $i (grep /^_/, keys %{$self} ){ $self->{$i} = 0; } | |||||||||
| 34 | 6 | return $self; | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | # fast algorithm to update all the statistics at once: | ||||||||||
| 38 | # http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Higher-order_statistics | ||||||||||
| 39 | 33 | ▶ | sub _update_statistics { | ||||||||
|
Subroutine
_update_statistics: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 33 | my ($self, $x) = @_; | |||||||||
| 41 | 33 | my ($mean, $M2, $M3, $M4) = (0, 0, 0, 0); | |||||||||
| 42 | |||||||||||
| 43 | 33 | $self->{_count}++; | |||||||||
| 44 | 33 | my $n = $self->{_count}; # shorter to write $n ;-) | |||||||||
| 45 | |||||||||||
| 46 | # $n**2 and $n**3 efficiently | ||||||||||
| 47 | 33 | my $n2 = $n*$n; | |||||||||
| 48 | 33 | my $n3 = $n2*$n; | |||||||||
| 49 | |||||||||||
| 50 | 33 | my $delta = $x - $self->{_mean}; | |||||||||
| 51 | |||||||||||
| 52 | # $delta**(2|3|4)... efficiently | ||||||||||
| 53 | 33 | my $delta2 = $delta*$delta; | |||||||||
| 54 | 33 | my $delta3 = $delta2*$delta; | |||||||||
| 55 | 33 | my $delta4 = $delta3*$delta; | |||||||||
| 56 | |||||||||||
| 57 | 33 | $mean = $self->{_mean} + $delta/$n; | |||||||||
| 58 | 33 | $M2 = $self->{_M2} + $delta2*($n - 1)/$n; | |||||||||
| 59 | 33 | $M3 = $self->{_M3} + $delta3*($n-1)*($n-2)/$n2 - 3*$delta*$self->{_M2}/$n; | |||||||||
| 60 | 33 | $M4 = $self->{_M4} + $delta4*($n-1)*($n2-3*$n+3)/$n3 + 6*$delta2*$self->{_M2}/$n2 - 4*$delta*$self->{_M3}/$n; | |||||||||
| 61 | |||||||||||
| 62 | 33 | $self->{_mean} = $mean; | |||||||||
| 63 | 33 | $self->{_M2} = $M2; | |||||||||
| 64 | 33 | $self->{_M3} = $M3; | |||||||||
| 65 | 33 | $self->{_M4} = $M4; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 2 | ▶ | sub count { | ||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 69 | 2 | return $_[0]->{_count}; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 1 | ▶ | sub mean { | ||||||||
|
Subroutine
mean: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||
| 73 | 1 | ▶ | die "too few elements to compute mean" if( $_[0]->{_count} == 0 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 1 | return $_[0]->{_mean}; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 0 | ▶ | sub variance { | ||||||||
|
Subroutine
variance: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 78 | 0 | ▶ | die "too few elements to compute variance" if( $_[0]->{_count} < 2 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 0 | return $_[0]->{_M2}/($_[0]->{_count} - 1); | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 0 | ▶ | sub variance_n { | ||||||||
|
Subroutine
variance_n: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 83 | 0 | ▶ | die "too few elements to compute variance_n" if( $_[0]->{_count} == 0 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | return $_[0]->{_M2}/$_[0]->{_count}; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 2 | ▶ | sub skewness { | ||||||||
|
Subroutine
skewness: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||
| 88 | 2 | ▶ | die "too few elements to compute skewness" if( $_[0]->{_count} == 0 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 2 | ▶ | die "variance is zero: cannot compute skewness" if( $_[0]->{_M2} == 0 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | |||||||||||
| 91 | 2 | return sqrt( $_[0]->{_count} )*$_[0]->{_M3}/( $_[0]->{_M2}**(3/2)); | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 2 | ▶ | sub kurtosis { | ||||||||
|
Subroutine
kurtosis: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||
| 95 | 2 | ▶ | die "too few elements to compute kurtosis" if( $_[0]->{_count} < 4 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 2 | ▶ | die "variance is zero: cannot compute kurtosis" if( $_[0]->{_M2} == 0 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | |||||||||||
| 98 | 2 | return $_[0]->{_count}*$_[0]->{_M4}/($_[0]->{_M2}*$_[0]->{_M2}) - 3; | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 1 | 1; | |||||||||
| 102 | |||||||||||
| 103 | __END__ | ||||||||||