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