File Coverage

blib/lib/Statistics/Descriptive/Smoother/Weightedexponential.pm
Criterion Covered Total %
statement 48 48 100.0
branch 2 2 100.0
condition 1 2 50.0
subroutine 9 9 100.0
pod 1 1 100.0
total 61 62 98.3


line stmt bran cond sub pod time code
1             package Statistics::Descriptive::Smoother::Weightedexponential;
2             $Statistics::Descriptive::Smoother::Weightedexponential::VERSION = '3.0800';
3 2     2   12 use strict;
  2         4  
  2         52  
4 2     2   21 use warnings;
  2         4  
  2         51  
5              
6 2     2   9 use Carp qw/ carp /;
  2         3  
  2         81  
7 2     2   390 use parent 'Statistics::Descriptive::Smoother';
  2         246  
  2         9  
8              
9             sub _new
10             {
11 5     5   10 my ( $class, $args ) = @_;
12              
13 5 100       9 if ( scalar @{ $args->{data} } != scalar @{ $args->{samples} } )
  5         9  
  5         14  
14             {
15 1         191 carp("Number of data values and samples need to be the same\n");
16 1         33 return;
17             }
18              
19 4   50     18 return bless $args || {}, $class;
20             }
21              
22             # The name of the variables used in the code refers to the explanation in the pod
23             sub get_smoothed_data
24             {
25 3     3 1 13 my ($self) = @_;
26              
27 3         6 my ( @smoothed_values, @Wts );
28              
29             # W(0) = N(0)
30 3         3 push @Wts, @{ $self->{samples} }[0];
  3         12  
31              
32             # S(0) = X(0)
33 3         5 push @smoothed_values, @{ $self->{data} }[0];
  3         5  
34 3         9 my $C = $self->get_smoothing_coeff();
35              
36 3         10 foreach my $idx ( 1 .. ( $self->{count} - 1 ) )
37             {
38 27         36 my $Xt = $self->{data}->[$idx];
39 27         36 my $Nt = $self->{samples}->[$idx];
40 27         33 my $St_1 = $smoothed_values[-1];
41 27         39 my $Wt_1 = $Wts[-1];
42              
43 27         37 push @Wts, $self->_get_Wt( $Wt_1, $Nt );
44              
45 27         39 my $coeff_a = $self->_get_coeff_A( $Wt_1, $Nt );
46 27         39 my $coeff_b = $self->_get_coeff_B( $Wt_1, $Nt );
47              
48 27         44 my $smoothed_value =
49             ( $St_1 * $coeff_a + $Xt * $coeff_b ) / ( $coeff_a + $coeff_b );
50 27         48 push @smoothed_values, $smoothed_value;
51             }
52 3         13 return @smoothed_values;
53             }
54              
55             sub _get_Wt
56             {
57 27     27   45 my ( $self, $Wt_1, $Nt ) = @_;
58              
59 27         44 my $C = $self->get_smoothing_coeff();
60 27         41 my $coeff_a = $self->_get_coeff_A( $Wt_1, $Nt );
61 27         41 my $coeff_b = $self->_get_coeff_B( $Wt_1, $Nt );
62              
63 27         60 return ( ( $Wt_1 * $coeff_a + $Nt * $coeff_b ) / ( $coeff_a + $coeff_b ) );
64             }
65              
66             sub _get_coeff_A
67             {
68 54     54   88 my ( $self, $Wt_1, $Nt ) = @_;
69              
70 54         84 my $C = $self->get_smoothing_coeff();
71 54         125 return $C * ( $Wt_1 / ( $Wt_1 + $Nt ) );
72             }
73              
74             sub _get_coeff_B
75             {
76 54     54   88 my ( $self, $Wt_1, $Nt ) = @_;
77              
78 54         84 my $C = $self->get_smoothing_coeff();
79 54         94 return ( 1 - $C ) * ( $Nt / ( $Nt + $Wt_1 ) );
80             }
81              
82             1;
83              
84             __END__