Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

SmplStat.cxx

Go to the documentation of this file.
00001 // This may look like C code, but it is really -*- C++ -*-
00002 /*
00003 Copyright (C) 1988 Free Software Foundation
00004     written by Dirk Grunwald (grunwald@cs.uiuc.edu)
00005 
00006 This file is part of the GNU C++ Library.  This library is free
00007 software; you can redistribute it and/or modify it under the terms of
00008 the GNU Library General Public License as published by the Free
00009 Software Foundation; either version 2 of the License, or (at your
00010 option) any later version.  This library is distributed in the hope
00011 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00012 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00013 PURPOSE.  See the GNU Library General Public License for more details.
00014 You should have received a copy of the GNU Library General Public
00015 License along with this library; if not, write to the Free Software
00016 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
00017 */
00018 #ifdef __GNUG__
00019 #pragma implementation
00020 #endif
00021 #include <iostream.h>
00022 #include "SmplStat.h"
00023 #include <math.h>
00024 
00025 // error handling
00026 /*
00027 void default_SampleStatistic_error_handler(const char* msg)
00028 {
00029   cerr << "Fatal SampleStatistic error. " << msg << "\n";
00030   exit(1);
00031 }
00032 
00033 one_arg_error_handler_t SampleStatistic_error_handler = default_SampleStatistic_error_handler;
00034 
00035 one_arg_error_handler_t set_SampleStatistic_error_handler(one_arg_error_handler_t f)
00036 {
00037   one_arg_error_handler_t old = SampleStatistic_error_handler;
00038   SampleStatistic_error_handler = f;
00039   return old;
00040 }
00041 
00042 void SampleStatistic::error(const char* msg)
00043 {
00044   (*SampleStatistic_error_handler)(msg);
00045 }
00046 */
00047 // t-distribution: given p-value and degrees of freedom, return t-value
00048 // adapted from Peizer & Pratt JASA, vol63, p1416
00049 
00050 //#include <builtin.h>
00051 // following put in by THB: couldn't find it
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 

Generated at Wed Nov 21 12:20:26 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000