Stats_example_1.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 // An example of robust statistics found in lib/geomatics
40 // compute Robust statistics. Also demonstrate the use
41 // of random number generators.
42 
43 #include <iostream>
44 #include <cstdlib>
45 #include <ctime>
46 
47 #include "RobustStats.hpp"
48 #include "random.hpp"
49 #include "Stats.hpp"
50 
51 using namespace std;
52 using namespace gnsstk;
53 using namespace Robust;
54 
55 
56 int main(int argc, char* argv[])
57 {
58  double *s = nullptr;
59  double mean = 10.0;
60  double stdDev = 2.0;
61  double badMeasurement = 10000;
62 
63  try
64  {
65 
66  // Generate a set of random numbers that are normally distributed
67  size_t N = 1000;
68  s= new double[N];
69 
70  Stats<double> simpleStats;
71 
72  for (size_t i = 0; i<N; i++)
73  {
74  s[i] = RandNorm(stdDev)+mean;
75  // Note the 1.0 keeps the math floating point,
76  // otherwise the division is integer division.
77 
78  simpleStats.Add(s[i]);
79  }
80 
81  // Show the sample mean and std deviation before adding
82  // perturbed samples.
83  cout << endl << "Before perturbation: sample mean is "
84  << simpleStats.Average() << ", " << endl;
85  cout << " sample standard deviation is "
86  << simpleStats.StdDev() << endl << endl;
87 
88  // Now perturb the sample data set with a "bad" measurement.
89  srand(time(0));
90  size_t i = (rand()%N);
91  s[i] = badMeasurement;
92  cout << "Altering measurement " << i << " to take the value of "
93  << badMeasurement << endl;
94 
95  // Show how the sample mean and std deviation are altered.
96  Stats<double> secondStats;
97  for (size_t i = 0; i<N; i++)
98  {
99  secondStats.Add(s[i]);
100  }
101 
102  // Show the sample mean and std deviation before adding
103  // perturbed samples.
104  cout << endl << "After perturbation: sample mean is "
105  << secondStats.Average() << ", " << endl;
106  cout << " sample standard deviation is "
107  << secondStats.StdDev() << endl << endl;
108 
109  double median,mad,Q1,Q3;
110 
111  QSort(s,N);
112  Robust::Quartiles(s,N,Q1,Q3);
114 
115  cout << "Robust statistics:\n";
116  cout << " number = " << N << endl;
117  cout << " quartiles = " << setw(11)
118  << setprecision(8) << Q1
119  << " " << setw(11) << setprecision(8) << Q3 << endl;
120  cout << " median = " << setw(11)
121  << setprecision(8) << median << endl;
122  cout << " MAD = " << setw(11)
123  << setprecision(8) << mad << endl;
124 
125 
126  // Show how the sample mean and std deviation are altered.
127  Stats<double> thirdStats;
128  for (size_t i = 0; i<N; i++)
129  {
130  if ((fabs(s[i] - median)/mad)<8)
131  thirdStats.Add(s[i]);
132  }
133 
134 
135  // Compute the mean and std deviation now given robust statistics.
136  cout << endl << "Using robust stats: sample mean is "
137  << thirdStats.Average() << ", " << endl;
138  cout << " sample standard deviation is "
139  << thirdStats.StdDev() << endl << endl;
140 
141  }
142  catch (bad_alloc& b)
143  {
144  cerr << "Allocation error. Out of memory?" << endl;
145  exit(1);
146  }
147 
148  // Delete the dynamic array allocation.
149  // (don't just rely on the operating system to do this).
150  delete[] s;
151  s = 0;
152 
153  return 0;
154 }
gnsstk::Stats::StdDev
T StdDev(void) const
return computed standard deviation
Definition: Stats.hpp:347
random.hpp
gnsstk::Robust::Quartiles
void Quartiles(const T *xd, const int nd, T &Q1, T &Q3)
Definition: RobustStats.hpp:425
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::Stats< double >
Stats.hpp
main
int main(int argc, char *argv[])
Definition: Stats_example_1.cpp:56
example4.time
time
Definition: example4.py:103
RobustStats.hpp
std
Definition: Angle.hpp:142
gnsstk::QSort
void QSort(T *sa, int na, int(*comp)(const T &, const T &)=gnsstk::Qsort_compare)
Definition: RobustStats.hpp:139
gnsstk::RandNorm
double RandNorm(double sigma)
Definition: random.cpp:126
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::median
T median(const Vector< T > &v)
Compute the median of a gnsstk::Vector.
Definition: Stats.hpp:59
gnsstk::mad
T mad(const gnsstk::Vector< T > &v)
median absolute deviation of a gnsstk::Vector
Definition: Stats.hpp:85
gnsstk::Robust::MedianAbsoluteDeviation
T MedianAbsoluteDeviation(T *xd, int nd, T &M, bool save_flag=true)
Definition: RobustStats.hpp:467


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