File Coverage

blib/lib/Math/Business/WMA.pm
Criterion Covered Total %
statement 39 41 95.1
branch 9 12 75.0
condition n/a
subroutine 7 9 77.7
pod 0 6 0.0
total 55 68 80.8


line stmt bran cond sub pod time code
1             package Math::Business::WMA;
2              
3 2     2   4003 use strict;
  2         8  
  2         45  
4 2     2   10 use warnings;
  2         5  
  2         50  
5 2     2   9 use Carp;
  2         5  
  2         961  
6              
7             1;
8              
9 0     0 0 0 sub tag { (shift)->[-1] }
10              
11 0     0 0 0 sub recommended { croak "no recommendation" }
12              
13             sub new {
14 5     5 0 218 my $class = shift;
15 5         22 my $this = bless [], $class;
16              
17 5         12 my $days = shift;
18 5 50       13 if( defined $days ) {
19 5         25 $this->set_days( $days );
20             }
21              
22 5         27 return $this;
23             }
24              
25             sub set_days {
26 5     5 0 9 my $this = shift;
27 5         7 my $arg = int(shift);
28              
29 5 50       13 croak "days must be a positive non-zero even integer" if $arg <= 0;
30 5         24 @$this = (
31             $arg,
32             ($arg*($arg+1))/2,
33             [], # the data (we actually need to store it, although we can avoid calculating much of it)
34             undef, # the sum of the data
35             undef, # the the last numerator
36             undef, # the WMA
37              
38             undef, # tag must be last
39             );
40              
41 5         17 $this->[-1] = "WMA($arg)";
42             }
43              
44             sub insert {
45 4044     4044 0 344004 my $this = shift;
46 4044         7583 my ($N, $D, $dat, $total_m, $numerator_m, $EMA) = @$this;
47              
48 4044 50       7217 croak "You must set the number of days before you try to insert" if not defined $N;
49              
50 4044         4739 my $old;
51 4044         7028 while( defined( my $P = shift ) ) {
52 4044         5783 push @$dat, $P;
53              
54 4044 100       6889 if( @$dat > $N ) {
    100          
55 4002         4955 $old = shift @$dat;
56              
57 4002         6293 $numerator_m = $numerator_m + $P*$N - $total_m;
58 4002         7716 $total_m = $total_m + $P - $old;
59              
60             } elsif( @$dat == $N ) {
61 5         14 $old = 1;
62 5         13 my $x = 1;
63              
64 5         9 $total_m = $numerator_m = 0;
65              
66 5         16 $numerator_m += $_ for map {$_*$x++} @$dat;
  42         92  
67 5         50 $total_m += $_ for @$dat;
68             }
69             }
70              
71 4044 100       13285 @$this = ($N, $D, $dat, $total_m, $numerator_m, (defined($old) ? $numerator_m/$D:undef));
72             }
73              
74             sub query {
75 4044     4044 0 8791 my $this = shift;
76              
77 4044         7734 return $this->[5];
78             }
79              
80             __END__