00001 
00002 
00003 
00004 
00005 
00006 #ifndef __UIUC_OSL_STATISTICS_H
00007 #define __UIUC_OSL_STATISTICS_H
00008 
00009 #include "math.h" 
00010 #include "stdio.h" 
00011 #include "pup.h" 
00012 
00027 template<class real,class ret>
00028 class CkSampleT {
00029     real lo,hi; 
00030     ret sum; 
00031     ret sq; 
00032     int n; 
00033 public:
00034     CkSampleT(void) {
00035         lo=(real)1.0e20; hi=(real)-1.0e20;
00036         sum=sq=(ret)0;
00037         n=0;
00038     }
00044     inline void add(real r) {
00045         if (r<lo) lo=r;
00046         if (r>hi) hi=r;
00047         sum+=(ret)r;
00048         sq+=(ret)(r*r);
00049         n++;
00050     }
00052     inline void operator+=(real r) { add(r); }
00053 
00055     inline void add(const CkSampleT<real,ret> &errs) {
00056         if (errs.lo<lo) lo=errs.lo;
00057         if (errs.hi>hi) hi=errs.hi;
00058         sum+=errs.sum;
00059         sq+=errs.sq;
00060         n+=errs.n;
00061     }
00062     
00067     ret getMean(void) const {
00068         return sum/n;
00069     }
00075     ret getVariance(void) const {
00076         return (sq-sum*sum/n)/(n-1);
00077     }
00082     ret getStddev(void) const {
00083         return (ret)sqrt(getVariance());
00084     }
00088     real getMin(void) const {return lo;}
00092     real getMax(void) const {return hi;}
00096     int getCount(void) const {return n;}
00097 
00099     ret getRMS(void) const {return sqrt(sq/n);}
00100     
00106     void print(FILE *dest) {
00107         fprintf(dest,"ave= %g  stddev= %g  min= %g  max= %g  n= %d\n",
00108             (double)getMean(), (double)getStddev(), (double)getMin(), (double)getMax(), (int)getCount());
00109     }
00110 
00114         void printMinAveMax(FILE *dest) {
00115         fprintf(dest,"min= %g  ave= %g  max= %g \n",
00116             (double)getMin(), (double)getMean(), (double)getMax());
00117     }
00118 
00122     void print(void) {print(stdout);}
00123     
00125     void pup(PUP::er &p) {
00126         p|lo; p|hi;
00127         p|sum; p|sq;
00128         p|n;
00129     }
00130     friend inline void operator|(PUP::er &p,CkSampleT<real,ret> &s) {s.pup(p);}
00131 };
00132 typedef CkSampleT<double,double> CkSample;
00133 
00134 #endif