00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifdef __GNUG__
00020 # pragma implementation
00021 #endif
00022
00023 #include "analysis/smplstat.h"
00024 #include <iostream>
00025 #include <cmath>
00026
00027 #ifndef HUGE
00028 #define HUGE 1e30
00029 #endif
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 SampleStatistic:: SampleStatistic(){ reset();}
00053
00054
00055
00056
00057 double tval(double p, int df)
00058 {
00059 double t;
00060 int positive = p >= 0.5;
00061 p = (positive)? 1.0 - p : p;
00062 if (p <= 0.0 || df <= 0)
00063 t = HUGE;
00064 else if (p == 0.5)
00065 t = 0.0;
00066 else if (df == 1)
00067 t = 1.0 / tan((p + p) * 1.57079633);
00068 else if (df == 2)
00069 t = sqrt(1.0 / ((p + p) * (1.0 - p)) - 2.0);
00070 else
00071 {
00072 double ddf = df;
00073 double a = sqrt(log(1.0 / (p * p)));
00074 double aa = a * a;
00075 a = a - ((2.515517 + (0.802853 * a) + (0.010328 * aa)) /
00076 (1.0 + (1.432788 * a) + (0.189269 * aa) +
00077 (0.001308 * aa * a)));
00078 t = ddf - 0.666666667 + 1.0 / (10.0 * ddf);
00079 t = sqrt(ddf * (exp(a * a * (ddf - 0.833333333) / (t * t)) - 1.0));
00080 }
00081 return (positive)? t : -t;
00082 }
00083
00084 void
00085 SampleStatistic::reset()
00086 {
00087 n = 0; x = x2 = 0.0;
00088 maxValue = -HUGE;
00089 minValue = HUGE;
00090 }
00091
00092 void
00093 SampleStatistic::operator+=(double value)
00094 {
00095 n += 1;
00096 x += value;
00097 x2 += (value * value);
00098 if ( minValue > value) minValue = value;
00099 if ( maxValue < value) maxValue = value;
00100 }
00101
00102
00103 double
00104 SampleStatistic::mean()const
00105 {
00106 if ( n > 0) {
00107 return (x / n);
00108 }
00109 else {
00110 return ( 0.0 );
00111 }
00112 }
00113
00114 double
00115 SampleStatistic::var()const
00116 {
00117 if ( n > 1) {
00118 return(( x2 - ((x * x) / n)) / ( n - 1));
00119 }
00120 else {
00121 return ( 0.0 );
00122 }
00123 }
00124
00125 double
00126 SampleStatistic::stdDev()const
00127 {
00128 if ( n <= 0 || this -> var() <= 0) {
00129 return(0);
00130 } else {
00131 return( (double) sqrt( var() ) );
00132 }
00133 }
00134
00135 double
00136 SampleStatistic::confidence(int interval)const
00137 {
00138 long df = n - 1;
00139 if (df <= 0) return HUGE;
00140 double t = tval(double(100 + interval) * 0.005, df);
00141 if (t == HUGE)
00142 return t;
00143 else
00144 return (t * stdDev()) / sqrt(double(n));
00145 }
00146
00147 double
00148 SampleStatistic::confidence(double p_value)const
00149 {
00150 long df = n - 1;
00151 if (df <= 0) return HUGE;
00152 double t = tval((1.0 + p_value) * 0.5, df);
00153 if (t == HUGE)
00154 return t;
00155 else
00156 return (t * stdDev()) / sqrt(double(n));
00157 }
00158
00159