PowerSum_T.cpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
39 #include <list>
40 #include <iostream>
41 
42 #include "Stats.hpp"
43 #include "PowerSum.hpp"
44 #include "Exception.hpp"
45 #include "TestUtil.hpp"
46 #include <stdlib.h>
47 #include <math.h>
48 
49 using namespace std;
50 
51 // Generate a norimally distributed deviate with zero mean and unit variance.
52 // Kudos to Press, Flannery, Teukolsky, and Veterling. And the man, Knuth.
53 // If you don't care about being thread safe, you can make v1 a static and use
54 // for every other call. Also this uses the libc standard rand() function
55 // which really blows by most accounts. If you want this to be a 'good'
56 // normal distribution, use a different rand()
57 
58 double gasdev()
59 {
60  const double mr2 = 2.0 / RAND_MAX;
61  double v1, v2, fac;
62 
63  double r;
64  do
65  {
66  v1 = mr2*rand() - 1;
67  v2 = mr2*rand() - 1;
68  r = v1*v1 + v2*v2;
69  } while (r >= 1);
70  fac = sqrt(-2*log(r)/r);
71  return v2*fac;
72 }
73 
74 
75 int main(int argc, char *argv[])
76 {
77  TUDEF("PowerSum", "fail");
80 
81  for (int i=0; i<100000; i++)
82  {
83  double rv = gasdev();
84  ps.add(rv);
85  s.Add(rv);
86  }
87 
88  //ps.dump(cout);
89 
90  //cout << "Stats class average:" << s.Average()
91  // << " stddev:" << s.StdDev() << endl;
92 
93  double e1 = std::abs(s.Average() - ps.average());
94  double e2 = std::abs(s.StdDev() - sqrt(ps.variance()));
95  //cout << "Disagreement in average: " << e1 << endl
96  // << "Disagreement in standard deviation: " << e2 << endl;
97 
98  try
99  {
100  TUCSM("average");
101  TUASSERT(e1 < 1e-3);
102  TUCSM("variance");
103  TUASSERT(e2 < 1e-3);
104  TUCSM("average");
105  TUASSERT(std::abs(ps.average()) < 1e-3);
106  // 2e-3 : tolerance is dependent on platform and can be improved with
107  // better random number generators
108  TUCSM("variance");
109  TUASSERT(std::abs(sqrt(ps.variance())-1) < 2e-3);
110  TUCSM("skew");
111  TUASSERT(std::abs(ps.skew()) < 0.01);
112  TUCSM("kurtosis");
113  TUASSERT(std::abs(ps.kurtosis()-3) < 0.05);
114  }
115  catch (gnsstk::Exception& e)
116  {
117  cout << e;
118  TUFAIL("Exception");
119  }
120 
121 
122  //if (e1 > 1e-3 || e2 > 1e-3 ||
123  // std::abs(ps.average()) > 1e-3 ||
124  // std::abs(sqrt(ps.variance())-1) > 2e-3 ||
125  // std::abs(ps.skew()) > 0.01 ||
126  // std::abs(ps.kurtosis()-3) > 0.05)
127  //{
128  // cout << "Error in computed values" << endl;
129  // return -1;
130  //}
131  //cout << "Looks good to me..." << endl;
132  //return 0;
133  TURETURN();
134 }
TUCSM
#define TUCSM(METHOD)
Definition: TestUtil.hpp:59
TUFAIL
#define TUFAIL(MSG)
Definition: TestUtil.hpp:228
gnsstk::Stats::StdDev
T StdDev(void) const
return computed standard deviation
Definition: Stats.hpp:347
gnsstk::PowerSum::variance
double variance() const noexcept
Definition: PowerSum.cpp:115
gnsstk::PowerSum::add
void add(double x) noexcept
Add a single value to the sums.
Definition: PowerSum.cpp:51
PowerSum.hpp
gnsstk::Stats< double >
Stats.hpp
gnsstk::Exception
Definition: Exception.hpp:151
TUASSERT
#define TUASSERT(EXPR)
Definition: TestUtil.hpp:63
TestUtil.hpp
TURETURN
#define TURETURN()
Definition: TestUtil.hpp:232
log
#define log
Definition: DiscCorr.cpp:625
gnsstk::PowerSum
Definition: PowerSum.hpp:57
TUDEF
#define TUDEF(CLASS, METHOD)
Definition: TestUtil.hpp:56
gasdev
double gasdev()
Definition: PowerSum_T.cpp:58
gnsstk::PowerSum::kurtosis
double kurtosis() const noexcept
Definition: PowerSum.cpp:129
Exception.hpp
std
Definition: Angle.hpp:142
gnsstk::Stats::Average
T Average(void) const
return the average
Definition: Stats.hpp:329
gnsstk::Stats::Add
void Add(const T &x)
Definition: Stats.hpp:158
gnsstk::PowerSum::skew
double skew() const noexcept
Definition: PowerSum.cpp:122
gnsstk::PowerSum::average
double average() const noexcept
Computes the indicated value.
Definition: PowerSum.cpp:107
main
int main(int argc, char *argv[])
Definition: PowerSum_T.cpp:75


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:40