blib/lib/Statistics/DEA.pm

filter: Statementstmt 100% 61 / 61 Branchbran 62.5% 15 / 24 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 12 / 12 Podpod 100% 7 / 7 totaltotal 86.8% 99 / 114 CC 15 SCAR 10.5
CC
15
Cov
87%
new
5.3
completeness
3.1
update
3.0
CRAP 2.8
↑ Summary
1 package Statistics::DEA;
2  
3 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 $VERSION = '0.04';
6  
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 # use warnings; requires 5.6.0
9  
10 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Statistics::DEA - Discontiguous Exponential Averaging
15  
16 =head1 SYNOPSIS
17  
18 use Statistics::DEA;
19  
20 my $dea = Statistics::DEA->new($alpha, $max_gap);
21  
22 while (($data, $time) = some_data_source(...)) {
23 ...
24 $dea->update($data, $time);
25 print $dea->average(), "\n";
26 print $dea->standard_deviation(), "\n";
27 print $dea->completeness($time), "\n";
28 ...
29 }
30  
31 =head1 DESCRIPTION
32  
33 The Statistics::DEA module can be used to compute exponentially
34 decaying averages and standard deviations even when the data has
35 gaps. The algorithm also avoids initial value bias and postgap bias.
36  
37 =head2 new
38  
39 my $dea = Statistics::DEA->new($alpha, $max_gap);
40  
41 Creates a new (potentially discontiguous) exponential average object.
42  
43 The I<$alpha> is the exponential decay of I<data>: from zero (inclusive)
44 to one (exclusive): the lower values cause the effect of data to decay
45 more quickly, the higher values cause the effect of data to decay more
46 slowly . Specifically, weights on older data decay exponentially with a
47 characteristic time of I<-1/log(alpha)> (I<log> being the natural logarithm).
48  
49 The I<$max_gap> is the maximum I<time> gap after which the data is
50 considered lost. If the time interval between updates is I<dt>,
51 using a I<$max_gap> of I<N*dt> will cause each update to fill in up to
52 I<N-1> of any preceeding skipped updates with the current data value.
53 Use a I<$max_gap> of I<dt> to prevent such filling.
54  
55 =head2 update
56  
57 $dea->update($data, $time);
58  
59 Update the average with new data at a particular point in time.
60  
61 The time parameter is how you can indicate gaps in the data;
62 if you don't have gaps in your data, just monotonously increase it,
63 for example C<$time++>.
64  
65 =head2 average
66  
67 my $avg = $dea->average();
68  
69 Return the current average.
70  
71 Functionally equivalent alias avg() is also available.
72  
73 =head2 standard_deviation
74  
75 my $std_dev = $dea->standard_deviation();
76  
77 Return the current standard deviation.
78  
79 Functionally equivalent alias std_dev() is also available.
80  
81 =head2 completeness
82  
83 my $completeness = $dea->completeness($time);
84  
85 Return the current I<completeness>: how well based the current average
86 and standard deviation are on actual data. Any time intervals between
87 updates greater than I<$max_gap> reduce this value. A series of updates
88 at time intervals of less than I<$max_gap> will gradually increase this
89 value from its initial, minimum, value of 0 to its maximum value of 1.
90  
91 The $time should represent the current time. It must be >= the time of
92 the last update.
93  
94 =head2 alpha
95  
96 my $alpha = $dea->alpha();
97  
98 Return the current exponential decay of data.
99  
100 $dea->alpha($alpha);
101  
102 Set the exponential decay of data.
103  
104 =head2 max_gap
105  
106 my $alpha = $dea->max_gap();
107  
108 Return the current maximum time gap.
109  
110 $dea->max_gap($max_gap);
111  
112 Set the maximum time gap.
113  
114 =head1 AUTHOR
115  
116 Jarkko Hietaniemi <jhi@iki.fi>
117  
118 =head1 LICENSE
119  
120 This library is free software; you can redistribute it and/or modify
121 it under the same terms as Perl itself.
122  
123 =head1 ACKNOWLEDGEMENT
124  
125 The idea and the code are from the September 1998 Doctor Dobb's
126 Journal Algorithm Alley article "Discontiguous Exponential Averaging"
127 by John C. Gunther, used with permission. JCG also provided valuable
128 feedback for the documentation -- and even fixed a bug in the code
129 without knowing Perl as such. This is just a Perlification of the
130 pseudocode in the article, all errors in transcription are solely mine.
131  
132 =cut
133  
134 6 ▶sub alpha {
Subroutine alpha: called CC=2 SCAR=6.9
Pod documented
135 6 my $dea = shift;
136 6 ▶ $dea->{alpha} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 3 6
137 6 $dea->_max_weight();
138 6 return $dea->{alpha};
139 }
140  
141 6 ▶sub max_gap {
Subroutine max_gap: called CC=2 SCAR=6.9
Pod documented
142 6 my $dea = shift;
143 6 ▶ $dea->{max_gap} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 3 6
144 6 $dea->_max_weight();
145 6 return $dea->{max_gap};
146 }
147  
148 12 ▶sub _max_weight { # Internal use only.
Subroutine _max_weight: called CC=3 SCAR=11.0
149 12 my $dea = shift;
150 12 ▶ return unless defined $dea->{alpha} && defined $dea->{max_gap};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dea->{"alpha"} and defined $dea->{"max_gap"} 3 9 12
Condition66%2/3
defined $dea->{"alpha"} and defined $dea->{"max_gap"}
!ll&&!rl&&r
039
MC/DC50%1/2
defined $dea->{"alpha"} and defined $dea->{"max_gap"}
defined $dea->{"alpha"} defined $dea->{"max_gap"}
Truth table66%2/3
A and B
A= defined $dea->{"alpha"}B= defined $dea->{"max_gap"}
ABresult
0X0
100
111
151 9 $dea->{max_weight} = 1 - $dea->{alpha} ** $dea->{max_gap};
152 }
153  
154 3 ▶sub new {
Subroutine new: called CC=5 SCAR=16.7
Pod documented
155 3 my $class = shift;
156 3 ▶ croak __PACKAGE__, "::new: need two arguments: alpha, max_gap"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 3 3
157 unless @_ == 2;
158 3 my ($alpha, $max_gap) = @_;
159 3 ▶ croak __PACKAGE__, "::new: Not 0 <= alpha $alpha < 1"
Branch: 1 branch
BranchTrueFalseTotal
unless 0 <= $alpha and $alpha < 1 0 3 3
Condition33%1/3
0 <= $alpha and $alpha < 1
!ll&&!rl&&r
003
MC/DC0%0/2
0 <= $alpha and $alpha < 1
0 <= $alpha $alpha < 1
Truth table33%1/3
A and B
A= 0 <= $alphaB= $alpha < 1
ABresult
0X0
100
111
160 unless 0 <= $alpha && $alpha < 1;
161 3 ▶ croak __PACKAGE__, "::new: Not max_gap $max_gap > 0"
Branch: 1 branch
BranchTrueFalseTotal
unless $max_gap > 0 0 3 3
162 unless $max_gap > 0;
163 3 my $dea = bless {}, $class;
164 3 $dea->{sum_of_weights} = 0;
165 3 $dea->{sum_of_data} = 0;
166 3 $dea->{sum_of_squared_data} = 0;
167 3 $dea->{previous_time} = -1e38; # about IEEE -Infinity
168 3 $dea->alpha($alpha);
169 3 $dea->max_gap($max_gap);
170 3 return $dea;
171 }
172  
173 3 ▶sub update {
Subroutine update: called CC=3 SCAR=11.1
Pod documented
174 3 my ($dea, $new_data, $time) = @_;
175 croak __PACKAGE__, "::update: Not previous_time $dea->{previous_time} < time $time"
176 3 ▶ unless $dea->{previous_time} < $time;
Branch: 1 branch
BranchTrueFalseTotal
unless $dea->{"previous_time"} < $time 0 3 3
177 my $weight_reduction_factor =
178 3 $dea->{alpha} ** ($time - $dea->{previous_time});
179 3 my $new_data_weight_a = 1 - $weight_reduction_factor;
180 3 my $new_data_weight_b = $dea->{max_weight};
181 3 ▶ my $new_data_weight =
Branch: 1 branch
BranchTrueFalseTotal
$new_data_weight_a < $new_data_weight_b ? : 0 3 3
182 $new_data_weight_a < $new_data_weight_b ?
183 $new_data_weight_a : $new_data_weight_b;
184 $dea->{sum_of_weights} =
185 $weight_reduction_factor * $dea->{sum_of_weights} +
186 3 $new_data_weight;
187 $dea->{sum_of_data} =
188 $weight_reduction_factor * $dea->{sum_of_data} +
189 3 $new_data_weight * $new_data;
190 $dea->{sum_of_squared_data} =
191 $weight_reduction_factor * $dea->{sum_of_squared_data} +
192 3 $new_data_weight * $new_data * $new_data;
193 3 $dea->{previous_time} = $time;
194 }
195  
196 4 ▶sub _average { # Internal use only.
Subroutine _average: called CC=1 SCAR=0.0
197 4 my $dea = shift;
198 4 return $dea->{sum_of_data} / $dea->{sum_of_weights};
199 }
200  
201 2 ▶sub average {
Subroutine average: called CC=2 SCAR=7.1
Pod documented
202 2 my $dea = shift;
203 croak __PACKAGE__, "::average: Not sum_of_weights > 0"
204 2 ▶ unless $dea->{sum_of_weights} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $dea->{"sum_of_weights"} > 0 0 2 2
205 2 return $dea->_average();
206 }
207  
208 3 *avg = \&average;
209  
210 2 ▶sub standard_deviation {
Subroutine standard_deviation: called CC=2 SCAR=7.0
Pod documented
211 2 my $dea = shift;
212 croak __PACKAGE__, "::standard_deviation: Not sum_of_weights > 0"
213 2 ▶ unless $dea->{sum_of_weights} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $dea->{"sum_of_weights"} > 0 0 2 2
214 2 my $average = $dea->_average();
215 return sqrt($dea->{sum_of_squared_data} / $dea->{sum_of_weights} -
216 2 $average * $average);
217 }
218  
219 3 *std_dev = \&standard_deviation;
220  
221 2 ▶sub completeness {
Subroutine completeness: called CC=3 SCAR=11.3
Pod documented
222 2 my $dea = shift;
223 2 ▶ croak __PACKAGE__, "::completeness: need one argument: time"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 2 2
224 unless @_ == 1;
225 2 my $time = shift;
226 croak __PACKAGE__, "::completeness: Not previous_time $dea->{previous_time} <= time $time"
227 2 ▶ unless $dea->{previous_time} <= $time;
Branch: 1 branch
BranchTrueFalseTotal
unless $dea->{"previous_time"} <= $time 0 2 2
228 return
229 $dea->{alpha} ** ($time - $dea->{previous_time}) *
230 2 $dea->{sum_of_weights};
231 }
232  
233 3 1;
↑ Summary