1 // This may look like C code, but it is really -*- C++ -*-
3 Copyright (C) 1988 Free Software Foundation
4 written by Dirk Grunwald (grunwald@cs.uiuc.edu)
6 This file is part of the GNU C++ Library. This library is free
7 software; you can redistribute it and/or modify it under the terms of
8 the GNU Library General Public License as published by the Free
9 Software Foundation; either version 2 of the License, or (at your
10 option) any later version. This library is distributed in the hope
11 that it will be useful, but WITHOUT ANY WARRANTY; without even the
12 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE. See the GNU Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
26 #define HUGE_VAL DBL_MAX
33 #include <simgear/debug/logstream.hxx>
34 #include "SGSmplstat.hxx"
37 void SampleStatistic::error (const char *msg)
39 SG_LOG(SG_GENERAL, SG_ALERT, msg);
42 // t-distribution: given p-value and degrees of freedom, return t-value
43 // adapted from Peizer & Pratt JASA, vol63, p1416
45 double tval (double p, int df)
48 int positive = p >= 0.5;
49 p = (positive) ? 1.0 - p : p;
50 if (p <= 0.0 || df <= 0)
55 t = 1.0 / tan ((p + p) * 1.57079633);
57 t = sqrt (1.0 / ((p + p) * (1.0 - p)) - 2.0);
61 double a = sqrt (log (1.0 / (p * p)));
63 a = a - ((2.515517 + (0.802853 * a) + (0.010328 * aa)) /
64 (1.0 + (1.432788 * a) + (0.189269 * aa) +
65 (0.001308 * aa * a)));
66 t = ddf - 0.666666667 + 1.0 / (10.0 * ddf);
67 t = sqrt (ddf * (exp (a * a * (ddf - 0.833333333) / (t * t)) - 1.0));
69 return (positive) ? t : -t;
72 void SampleStatistic::reset ()
81 void SampleStatistic::operator += (double value)
86 cumulativeTime += value;
87 x2 += (value * value);
96 double SampleStatistic::mean () const
108 double SampleStatistic::var () const
112 return ((x2 - ((x * x) / n)) / (n - 1));
120 double SampleStatistic::stdDev () const
122 if (n <= 0 || this->var () <= 0)
128 return ((double) sqrt (var ()));
132 double SampleStatistic::confidence (int interval) const
137 double t = tval (double (100 + interval) * 0.005, df);
141 return (t * stdDev ()) / sqrt (double (n));
144 double SampleStatistic::confidence (double p_value) const
149 double t = tval ((1.0 + p_value) * 0.5, df);
153 return (t * stdDev ()) / sqrt (double (n));