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 
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 // error handling
00031 /*
00032 void default_SampleStatistic_error_handler(const char* msg)
00033 {
00034   cerr << "Fatal SampleStatistic error. " << msg << "\n";
00035   exit(1);
00036 }
00037 
00038 one_arg_error_handler_t SampleStatistic_error_handler = default_SampleStatistic_error_handler;
00039 
00040 one_arg_error_handler_t set_SampleStatistic_error_handler(one_arg_error_handler_t f)
00041 {
00042   one_arg_error_handler_t old = SampleStatistic_error_handler;
00043   SampleStatistic_error_handler = f;
00044   return old;
00045 }
00046 
00047 void SampleStatistic::error(const char* msg)
00048 {
00049   (*SampleStatistic_error_handler)(msg);
00050 }
00051 */
00052 SampleStatistic:: SampleStatistic(){ reset();}
00053 
00054 // t-distribution: given p-value and degrees of freedom, return t-value
00055 // adapted from Peizer & Pratt JASA, vol63, p1416
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 

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