use
vars
qw($VERSION @ISA $AUTOLOAD)
;
@ISA
=
qw (Statistics::Descriptive::Full);
$VERSION
=
'1.1'
;
my
%confidence_interval
=
(
"significance"
=>
undef
,
"alpha"
=>
undef
,
"df"
=>
undef
,
"standard_error"
=>
undef
,
"t_value"
=>
undef
,
"t_statistic"
=>
undef
,
"t_prob"
=>
undef
,
"delta"
=>
undef
,
"upper_clm"
=>
undef
,
"lower_clm"
=>
undef
,
"valid"
=>
undef
);
sub
new{
my
$proto
=
shift
;
my
$class
=
ref
(
$proto
) ||
$proto
;
my
$self
=
$class
->SUPER::new();
my
%confidence
=
%confidence_interval
;
$self
->{confidence}=\
%confidence
;
bless
(
$self
,
$class
);
return
$self
;
}
sub
compute_confidence_interval{
my
$self
=
shift
;
croak
"sample size must be >1 to compute the confidence interval \n"
if
(
$self
->count()<=1);
$self
->{
'significance'
}=95
if
(!
defined
(
$self
->{
'significance'
}));
$self
->{df}=
$self
->count()-1;
$self
->{alpha}=(100-
$self
->{significance})/2;
$self
->{alpha}/=100;
$self
->{standard_error}=
$self
->standard_deviation()/
sqrt
(
$self
->count());
$self
->{t_value}=
abs
tdistr(
$self
->{df},
$self
->{alpha});
$self
->{delta}=
$self
->{t_value}
*$self
->{standard_error};
$self
->{upper_clm}=
$self
->mean() +
$self
->{delta};
$self
->{lower_clm}=
$self
->mean() -
$self
->{delta};
$self
->{t_statistic}=
$self
->{standard_error}
?(
$self
->mean()/
$self
->{standard_error}):0;
$self
->{t_prob}=1-
abs
(tprob(
$self
->{df},-1
*$self
->{t_statistic})-tprob(
$self
->{df},
$self
->{t_statistic})) ;
$self
->{valid}=1;
return
1;
}
sub
add_data{
my
$self
=
shift
;
my
$aref
;
if
(
ref
$_
[0] eq
'ARRAY'
) {
$aref
=
$_
[0];
}
else
{
$aref
= \
@_
;
}
my
$significance
=
$self
->{
'significance'
}
if
(
defined
(
$self
->{
'significance'
}));
$self
->SUPER::add_data(
$aref
);
$self
->{
'significance'
}=
$significance
;
$self
->compute_confidence_interval()
if
((
defined
(
$self
->{count}))&&(
$self
->{count}>1)) ;
return
1;
}
sub
set_significance{
my
$self
=
shift
;
my
$significance
=
shift
;
$self
->{
'significance'
}=
$significance
if
((
$significance
>0)&&(
$significance
<100));
$self
->compute_confidence_interval()
if
((
defined
(
$self
->{count}))&&(
$self
->{count}>1));
return
1;
}
sub
print_confidence_interval{
my
$self
=
shift
;
print
"mean:"
,
$self
->mean(),
"\n"
;
print
"variance:"
,
$self
->variance(),
"\n"
;
my
$confidence
=\
%confidence_interval
;
foreach
my
$k
(
keys
%$confidence
)
{
print
"$k:"
,
$self
->{
$k
},
" \n"
;
}
return
1;
}
sub
output_confidence_interval{
my
$self
=
shift
;
croak
"sample size must be >1 to compute the confidence interval\n"
if
(
$self
->{valid}!=1);
my
$title
=
shift
;
print
"Summary from the observed values of the sample $title:\n"
;
print
"\tsample size= "
,
$self
->count(),
" , degree of freedom="
,
$self
->df(),
"\n"
;
print
"\tmean="
,
$self
->mean(),
" , variance="
,
$self
->variance(),
"\n"
;
print
"\tstandard deviation="
,
$self
->standard_deviation(),
" , standard error="
,
$self
->standard_error(),
"\n"
;
print
"\t the estimate of the mean is "
,
$self
->mean(),
" +/- "
,
$self
->delta(),
"\n\t"
,
" or ("
,
$self
->lower_clm(),
" to "
,
$self
->upper_clm,
" ) with "
,
$self
->significance,
" % of confidence\n"
;
print
"\t t-statistic=T="
,
$self
->t_statistic(),
" , Prob >|T|="
,
$self
->t_prob(),
"\n"
;
}
sub
AUTOLOAD{
my
$self
=
shift
;
my
$type
=
ref
(
$self
)
or croak
"$self is not an object"
;
my
$name
=
$AUTOLOAD
;
$self
->{_confidence}=\
%confidence_interval
;
$name
=~ s/.*://;
return
if
$name
eq
"DESTROY"
;
if
(
exists
$self
->{_permitted}->{
$name
} ) {
return
$self
->{
$name
};
}
elsif
(
exists
$self
->{
'_confidence'
}->{
$name
})
{
return
$self
->{
$name
};
}
else
{
croak
"Can't access `$name' field in class $type"
;
}
}
1;
use
vars
qw($VERSION $AUTOLOAD @ISA)
;
@ISA
=
qw (Statistics::PointEstimation);
$VERSION='1.1';
my
%fields
=
(
"count"
=>
undef
,
"mean"
=>
undef
,
"variance"
=>
undef
,
"standard_deviation"
=>
undef
,
"significance"
=>
undef
,
"alpha"
=>
undef
,
"df"
=>
undef
,
"standard_error"
=>
undef
,
"t_value"
=>
undef
,
"t_statistic"
=>
undef
,
"t_prob"
=>
undef
,
"delta"
=>
undef
,
"upper_clm"
=>
undef
,
"lower_clm"
=>
undef
,
"valid"
=>
undef
);
sub
new{
my
$proto
=
shift
;
my
$class
=
ref
(
$proto
) ||
$proto
;
my
$self
= {
%fields
};
bless
(
$self
,
$class
);
return
$self
;
}
sub
add_data{
croak
"the add_data() method is not supported in Statistics::PointEstimation::Sufficient\n"
;
}
sub
load_data{
my
$self
=
shift
;
my
(
$count
,
$mean
,
$variance
)=
@_
;
$self
->{count}=
$count
;
$self
->{mean}=
$mean
;
$self
->{variance}=
$variance
;
$self
->{standard_deviation}=
sqrt
(
$variance
);
$self
->compute_confidence_interval()
if
(
$self
->count()>1) ;
return
;
}
sub
AUTOLOAD{
my
$self
=
shift
;
my
$type
=
ref
(
$self
)
or croak
"$self is not an object"
;
$self
->{_confidence}=\
%fields
;
my
$name
=
$AUTOLOAD
;
$name
=~ s/.*://;
return
if
$name
eq
"DESTROY"
;
if
(
exists
$self
->{_confidence}->{
$name
})
{
return
$self
->{
$name
};
}
else
{
croak
"Can't access `$name' field in class $type"
;
}
}
1;