File Coverage

blib/lib/Statistics/Welford.pm
Criterion Covered Total %
statement 38 38 100.0
branch 6 10 60.0
condition n/a
subroutine 10 10 100.0
pod 8 8 100.0
total 62 66 93.9


line stmt bran cond sub pod time code
1             package Statistics::Welford;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Statistics::Welford - Standard statistics using Welford's algorithm
8              
9             =head1 SYNOPSIS
10              
11             my $stat = Statistics::Welford->new;
12              
13             while (1) {
14             $stat->add(rand);
15             ...
16             }
17              
18             print $stat->mean;
19              
20             =head1 DESCRIPTION
21              
22             Standard statistics using Welford's algorithm
23              
24             =head1 METHODS
25              
26             =cut
27              
28 1     1   882 use strict;
  1         2  
  1         45  
29 1     1   7 use warnings;
  1         2  
  1         498  
30              
31             our $VERSION = '0.02';
32              
33             =pod
34              
35             =head2 new
36              
37             my $stat = Statistics::Welford->new;
38              
39             The C constructor lets you create a new B object.
40              
41             =cut
42              
43             sub new {
44 1     1 1 408 my $class = shift;
45 1         4 my $self = bless { @_ }, $class;
46 1         5 $self->{n} = 0;
47 1         7 return $self;
48             }
49              
50             =pod
51              
52             =head2 add
53              
54             Add an entry to the statistics base
55              
56             =cut
57              
58             sub add {
59 10     10 1 1941 my ($self, $x) = @_;
60              
61 10         11 $self->{n}++;
62 10 100       25 if ($self->{n} == 1) {
63 1         1 $self->{old_m} = $x;
64 1         2 $self->{new_m} = $x;
65 1         2 $self->{min} = $x;
66 1         2 $self->{max} = $x;
67 1         1 $self->{old_s} = 0.0;
68 1         6 return $self;
69             }
70 9         21 $self->{new_m} = $self->{old_m} + ($x - $self->{old_m})/$self->{n};
71 9         23 $self->{new_s} = $self->{old_s} + ($x - $self->{old_m})*($x - $self->{new_m});
72 9 50       18 $self->{min} = $x if $x < $self->{min};
73 9 50       20 $self->{max} = $x if $x > $self->{max};
74              
75             # set up for next iteration
76 9         11 $self->{old_m} = $self->{new_m};
77 9         12 $self->{old_s} = $self->{new_s};
78 9         25 return $self;
79             }
80              
81             =head2 n
82              
83             Returns the number of entries to the statistics base
84              
85             =cut
86              
87             sub n {
88 1     1 1 206 my $self = shift;
89 1         6 return $self->{n};
90             }
91              
92             =head2 min
93              
94             Returns the minimum number in the statistics base
95              
96             =cut
97              
98             sub min {
99 1     1 1 2 my $self = shift;
100 1         19 return $self->{min};
101             }
102              
103             =head2 max
104              
105             Returns the maximum number in the statistics base
106              
107             =cut
108              
109             sub max {
110 1     1 1 2 my $self = shift;
111 1         4 return $self->{max};
112             }
113              
114             =head2 mean
115              
116             Returns the mean value
117              
118             =cut
119              
120             sub mean {
121 1     1 1 1 my $self = shift;
122 1 50       7 return $self->{n} > 0 ? $self->{new_m} : 0.0;
123             }
124              
125             =head2 variance
126              
127             Returns the variance value
128              
129             =cut
130              
131             sub variance {
132 2     2 1 2 my $self = shift;
133 2 50       21 return $self->{n} > 1 ? $self->{new_s}/($self->{n} - 1) : 0.0;
134             }
135              
136             =head2 standard_deviation
137              
138             Returns the standard deviation value
139              
140             =cut
141              
142             sub standard_deviation {
143 1     1 1 1 my $self = shift;
144 1         3 return sqrt( $self->variance );
145             }
146              
147             1;
148              
149             =pod
150              
151             =head1 AUTHOR
152              
153             Kaare Rasmussen .
154              
155             =head1 COPYRIGHT
156              
157             Copyright (C) 2010, Kaare Rasmussen
158              
159             This module is free software; you can redistribute it or modify it
160             under the same terms as Perl itself.
161              
162             =cut