SGSmplstat.cxx

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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 #ifdef __GNUG__
00019 #pragma implementation
00020 #endif
00021 
00022 #include <math.h>
00023 
00024 #ifndef HUGE_VAL
00025 #ifdef HUGE
00026 #define HUGE_VAL HUGE
00027 #else
00028 #include <float.h>
00029 #define HUGE_VAL DBL_MAX
00030 #endif
00031 #endif
00032 
00033 
00034 #include <iostream>
00035 #include <fstream>
00036 #include <simgear/debug/logstream.hxx>
00037 #include "SGSmplstat.hxx"
00038 
00039 
00040 void SampleStatistic::error (const char *msg)
00041 {
00042   SG_LOG(SG_GENERAL, SG_ALERT,  msg);
00043 }
00044 
00045 // t-distribution: given p-value and degrees of freedom, return t-value
00046 // adapted from Peizer & Pratt JASA, vol63, p1416
00047 
00048 double tval (double p, int df)
00049 {
00050   double t;
00051   int positive = p >= 0.5;
00052   p = (positive) ? 1.0 - p : p;
00053   if (p <= 0.0 || df <= 0)
00054     t = HUGE_VAL;
00055   else if (p == 0.5)
00056     t = 0.0;
00057   else if (df == 1)
00058     t = 1.0 / tan ((p + p) * 1.57079633);
00059   else if (df == 2)
00060     t = sqrt (1.0 / ((p + p) * (1.0 - p)) - 2.0);
00061   else
00062     {
00063       double ddf = df;
00064       double a = sqrt (log (1.0 / (p * p)));
00065       double aa = a * a;
00066       a = a - ((2.515517 + (0.802853 * a) + (0.010328 * aa)) /
00067                (1.0 + (1.432788 * a) + (0.189269 * aa) +
00068                 (0.001308 * aa * a)));
00069       t = ddf - 0.666666667 + 1.0 / (10.0 * ddf);
00070       t = sqrt (ddf * (exp (a * a * (ddf - 0.833333333) / (t * t)) - 1.0));
00071     }
00072   return (positive) ? t : -t;
00073 }
00074 
00075 void SampleStatistic::reset ()
00076 {
00077   n = 0;
00078   x = x2 = 0.0;
00079   maxValue = -HUGE_VAL;
00080   minValue = HUGE_VAL;
00081 }
00082 
00083 void SampleStatistic::operator += (double value)
00084 {
00085   n += 1;
00086   x += value;
00087   x2 += (value * value);
00088   if (minValue > value)
00089     minValue = value;
00090   if (maxValue < value)
00091     maxValue = value;
00092 }
00093 
00094 double SampleStatistic::mean () const
00095 {
00096   if (n > 0)
00097     {
00098       return (x / n);
00099     }
00100   else
00101     {
00102       return (0.0);
00103     }
00104 }
00105 
00106 double SampleStatistic::var () const
00107 {
00108   if (n > 1)
00109     {
00110       return ((x2 - ((x * x) / n)) / (n - 1));
00111     }
00112   else
00113     {
00114       return (0.0);
00115     }
00116 }
00117 
00118 double SampleStatistic::stdDev () const
00119 {
00120   if (n <= 0 || this->var () <= 0)
00121     {
00122       return (0);
00123     }
00124   else
00125     {
00126       return ((double) sqrt (var ()));
00127     }
00128 }
00129 
00130 double SampleStatistic::confidence (int interval) const
00131 {
00132   int df = n - 1;
00133   if (df <= 0)
00134     return HUGE_VAL;
00135   double t = tval (double (100 + interval) * 0.005, df);
00136   if (t == HUGE_VAL)
00137     return t;
00138   else
00139     return (t * stdDev ()) / sqrt (double (n));
00140 }
00141 
00142 double SampleStatistic::confidence (double p_value) const
00143 {
00144   int df = n - 1;
00145   if (df <= 0)
00146     return HUGE_VAL;
00147   double t = tval ((1.0 + p_value) * 0.5, df);
00148   if (t == HUGE_VAL)
00149     return t;
00150   else
00151     return (t * stdDev ()) / sqrt (double (n));
00152 }

Generated on Mon Dec 17 09:30:55 2007 for SimGear by  doxygen 1.5.1