File Coverage

blib/lib/Statistics/PointEstimation.pm
Criterion Covered Total %
statement 90 116 77.5
branch 13 34 38.2
condition 4 15 26.6
subroutine 17 21 80.9
pod 0 6 0.0
total 124 192 64.5


line stmt bran cond sub pod time code
1             package Statistics::PointEstimation;
2 1     1   644 use strict;
  1         3  
  1         55  
3 1     1   8 use Carp;
  1         3  
  1         171  
4 1     1   6 use vars qw($VERSION @ISA $AUTOLOAD);
  1         5  
  1         84  
5 1     1   966 use Statistics::Distributions qw(chisqrdistr tdistr fdistr udistr uprob chisqrprob tprob fprob);
  1         14324  
  1         159  
6 1     1   1415 use Statistics::Descriptive;
  1         19116  
  1         35  
7 1     1   11 use POSIX;
  1         2  
  1         9  
8              
9              
10             @ISA= qw (Statistics::Descriptive::Full);
11             $VERSION = '1.1';
12             my %confidence_interval= #data related to confidence interval
13             (
14              
15             "significance" => undef,
16             "alpha" => undef,
17             "df" =>undef,
18             "standard_error" => undef,
19             "t_value" =>undef,
20             "t_statistic" =>undef,
21             "t_prob" =>undef,
22             "delta" =>undef,
23             "upper_clm" => undef,
24             "lower_clm" =>undef,
25             "valid" =>undef
26             );
27              
28              
29            
30             sub new{
31 500     500 0 27158 my $proto = shift;
32 500   33     1844 my $class = ref($proto) || $proto;
33 500         4700 my $self = $class->SUPER::new();
34 500         37777 my %confidence=%confidence_interval;
35 500         1325 $self->{confidence}=\%confidence;
36 500         926 bless ($self, $class);
37 500         1129 return $self;
38             }
39              
40             sub compute_confidence_interval{
41 500     500 0 715 my $self=shift;
42 500 50       1151 croak "sample size must be >1 to compute the confidence interval \n" if($self->count()<=1);
43 500 50       3219 $self->{'significance'}=95 if (!defined($self->{'significance'}));
44 500         1215 $self->{df}=$self->count()-1;
45 500         2809 $self->{alpha}=(100-$self->{significance})/2;
46 500         779 $self->{alpha}/=100;
47 500         1145 $self->{standard_error}=$self->standard_deviation()/sqrt($self->count());
48 500         16596 $self->{t_value}=abs tdistr($self->{df},$self->{alpha});
49 500         23146 $self->{delta}=$self->{t_value}*$self->{standard_error};
50              
51 500         1191 $self->{upper_clm}=$self->mean() +$self->{delta};
52 500         3140 $self->{lower_clm}=$self->mean() -$self->{delta};
53 500 50       3227 $self->{t_statistic}=$self->{standard_error}
54             ?($self->mean()/$self->{standard_error}):0;
55 500         3340 $self->{t_prob}=1- abs (tprob($self->{df},-1*$self->{t_statistic})-tprob($self->{df},$self->{t_statistic})) ;
56 500         55417 $self->{valid}=1;
57 500         743 return 1;
58              
59             }
60             sub add_data{
61 500     500 0 1771 my $self = shift;
62 500         493 my $aref;
63              
64 500 50       914 if (ref $_[0] eq 'ARRAY') {
65 0         0 $aref = $_[0];
66             }
67             else {
68 500         823 $aref = \@_;
69             }
70 500 50       1448 my $significance=$self->{'significance'} if (defined($self->{'significance'}));
71 500         1427 $self->SUPER::add_data($aref);
72 500         43024 $self->{'significance'}=$significance;
73 500 50 33     2800 $self->compute_confidence_interval() if ((defined($self->{count}))&&($self->{count}>1)) ;
74              
75 500         1480 return 1;
76              
77             }
78             sub set_significance{ # set the significance level. usually 90, 95 or 99
79 500     500 0 1521 my $self=shift;
80 500         919 my $significance=shift;
81 500 50 33     2570 $self->{'significance'}=$significance if (($significance>0)&&($significance<100));
82 500 50 33     2083 $self->compute_confidence_interval() if((defined($self->{count}))&&($self->{count}>1));
83 500         1027 return 1;
84              
85             }
86              
87             sub print_confidence_interval{
88 1     1 0 5 my $self=shift;
89 1         4 print "mean:",$self->mean(),"\n";
90 1         18 print "variance:",$self->variance(),"\n";
91 1         20 my $confidence=\%confidence_interval;
92              
93 1         6 foreach my $k ( keys %$confidence)
94             {
95 11         44 print "$k:", $self->{$k}," \n";
96             }
97 1         11 return 1;
98              
99             }
100              
101             sub output_confidence_interval{
102 1     1 0 7 my $self=shift;
103 1 50       4 croak "sample size must be >1 to compute the confidence interval\n" if($self->{valid}!=1);
104 1         2 my $title=shift;
105 1         32 print "Summary from the observed values of the sample $title:\n";
106 1         5 print "\tsample size= ", $self->count()," , degree of freedom=", $self->df(), "\n";
107 1         6 print "\tmean=", $self->mean()," , variance=", $self->variance(),"\n";
108 1         27 print "\tstandard deviation=", $self->standard_deviation()," , standard error=", $self->standard_error(),"\n";
109 1         5 print "\t the estimate of the mean is ", $self->mean()," +/- ",$self->delta(),"\n\t",
110             " or (",$self->lower_clm()," to ",$self->upper_clm," ) with ",$self->significance," % of confidence\n";
111 1         11 print "\t t-statistic=T=",$self->t_statistic()," , Prob >|T|=",$self->t_prob(),"\n";
112             }
113              
114             sub AUTOLOAD{
115 998     998   4848 my $self = shift;
116 998 50       2252 my $type = ref($self)
117             or croak "$self is not an object";
118 998         1277 my $name = $AUTOLOAD;
119 998         1883 $self->{_confidence}=\%confidence_interval;
120 998         3349 $name =~ s/.*://;
121 998 50       2022 return if $name eq "DESTROY";
122 998 50       3289 if (exists $self->{_permitted}->{$name} ) {
    50          
123 0         0 return $self->{$name};
124             }
125             elsif(exists $self->{'_confidence'}->{$name})
126             {
127 998         2773 return $self->{$name};
128             }
129             else
130             {
131 0           croak "Can't access `$name' field in class $type";
132             }
133             }
134             1;
135              
136             package Statistics::PointEstimation::Sufficient;
137 1     1   4702 use strict;
  1         3  
  1         40  
138 1     1   16 use Carp;
  1         2  
  1         90  
139 1     1   6 use vars qw($VERSION $AUTOLOAD @ISA);
  1         2  
  1         99  
140 1     1   6 use POSIX;
  1         3  
  1         8  
141             @ISA=qw (Statistics::PointEstimation);
142             $VERSION='1.1';
143             my %fields= #data related to confidence interval
144             (
145             "count"=>undef,
146             "mean" =>undef,
147             "variance" => undef,
148             "standard_deviation" =>undef,
149             "significance" => undef,
150             "alpha" => undef,
151             "df" =>undef,
152             "standard_error" => undef,
153             "t_value" =>undef,
154             "t_statistic" =>undef,
155             "t_prob" =>undef,
156             "delta" =>undef,
157             "upper_clm" => undef,
158             "lower_clm" =>undef,
159             "valid" =>undef
160             );
161              
162             sub new{
163 0     0     my $proto = shift;
164 0   0       my $class = ref($proto) || $proto;
165 0           my $self = {%fields};
166 0           bless ($self, $class);
167 0           return $self;
168             }
169             sub add_data{
170              
171 0     0     croak "the add_data() method is not supported in Statistics::PointEstimation::Sufficient\n";
172              
173             }
174             sub load_data{
175 0     0     my $self=shift;
176 0           my ($count,$mean,$variance)=@_;
177 0           $self->{count}=$count;
178 0           $self->{mean}=$mean;
179 0           $self->{variance}=$variance;
180 0           $self->{standard_deviation}=sqrt($variance);
181 0 0         $self->compute_confidence_interval() if ($self->count()>1) ;
182 0           return;
183              
184             }
185              
186             sub AUTOLOAD{
187 0     0     my $self = shift;
188 0 0         my $type = ref($self)
189             or croak "$self is not an object";
190 0           $self->{_confidence}=\%fields;
191 0           my $name = $AUTOLOAD;
192 0           $name =~ s/.*://;
193 0 0         return if $name eq "DESTROY";
194              
195 0 0         if(exists $self->{_confidence}->{$name})
196             {
197 0           return $self->{$name};
198             }
199             else
200             {
201 0           croak "Can't access `$name' field in class $type";
202             }
203             }
204             1;
205            
206              
207             __END__