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