BivarStats.hpp
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 
44 #ifndef INCLUDE_GNSSTK_BIVARSTATS_HPP
45 #define INCLUDE_GNSSTK_BIVARSTATS_HPP
46 
47 #include "MiscMath.hpp"
48 #include "Vector.hpp"
49 #include "Exception.hpp"
50 #include "Stats.hpp"
51 
52 namespace gnsstk
53 {
54 
56 
57 
62  template <class T>
63  class BivarStats
64  {
65  public:
72  BivarStats(bool scale=false);
73  BivarStats(const T& x, const T&, bool scale=false);
74  BivarStats(const std::vector<T>& x, const std::vector<T>& y,
75  bool scale=false);
76  BivarStats(const std::vector< std::pair<T, T> >& d, bool scale=false);
77  BivarStats(const Vector<T>& x, const Vector<T>& y, bool scale=false);
83  void add(const T& x, const T& y);
84  void add(const std::vector<T>& x, const std::vector<T>& y);
85  void add(const std::vector< std::pair<T, T> >& d);
86  void add(const Vector<T>& x, const Vector<T>& y);
92  void subtract(const T& x, const T& y);
93  void subtract(const std::vector<T>& x, const std::vector<T>& y);
94  void subtract(const std::vector< std::pair<T, T> >& d);
95  void subtract(const Vector<T>& x, const Vector<T>& y);
98  void clear(void);
99  size_t n(void) const;
100 
101  T minimumX(void) const;
102  T maximumX(void) const;
103  T minimumY(void) const;
104  T maximumY(void) const;
105 
106  T averageX(void) const;
107  T averageY(void) const;
108 
109  T varianceX(void) const;
110  T varianceY(void) const;
111  T stdDevX(void) const;
112  T stdDevY(void) const;
113 
115  T slope(void) const;
117  T intercept(void) const;
119  T sigmaSlope(void) const;
120 
121  T correlation(void) const;
122 
124  T sigmaYX(void) const;
125 
127  T eval(const T& x) const {return intercept() + x * slope();};
128 
132 
133  Stats<T> estimateDeviation(const std::vector< std::pair<T, T> >& d) const;
134 
135  private:
136 
138  size_t ns;
139 
142  bool scaled;
144  }; // end class BivarStats
145 
147 
149  template <class T>
150  std::ostream& operator<<(std::ostream& s, const BivarStats<T>& BVS)
151  {
152  s << " N = " << BVS.n() << std::endl
153  << " Minimum: X = " << BVS.minimumX()
154  << " Y = " << BVS.minimumY()
155  << " Maximum: X = " << BVS.maximumX()
156  << " Y = " << BVS.maximumY() << std::endl
157  << " Average: X = " << BVS.averageX()
158  << " Y = " << BVS.averageY()
159  << " Std Dev: X = " << BVS.stdDevX()
160  << " Y = " << BVS.stdDevY() << std::endl
161  << " Intercept = " << BVS.intercept()
162  << " Slope = " << BVS.slope()
163  << " with uncertainty = " << BVS.sigmaSlope() << std::endl
164  << " Conditional uncertainty (sigma y given x) = " << BVS.sigmaYX()
165  << " Correlation = " << BVS.correlation() << std::endl;
166  return s;
167  }
168 
169  template<class T>
171  :ns(0), scaled(s)
172  {}
173 
174  template<class T>
175  BivarStats<T>::BivarStats(const T& x, const T& y, bool s)
176  :ns(0), scaled(s)
177  {
178  add(x,y);
179  }
180 
181  template<class T>
182  BivarStats<T>::BivarStats(const std::vector<T>& x, const std::vector<T>& y,
183  bool s )
184  :ns(0), scaled(s)
185  {
186  add(x,y);
187  }
188 
189  template<class T>
190  BivarStats<T>::BivarStats(const std::vector< std::pair<T, T> >& d, bool s)
191  :ns(0), scaled(s)
192  {
193  add(d);
194  }
195 
196  template<class T>
197  BivarStats<T>::BivarStats(const Vector<T>& x, const Vector<T>& y, bool s)
198  :ns(0), scaled(s)
199  {
200  add(x,y);
201  }
202 
203  template<class T>
204  void BivarStats<T>::add(const T& x, const T& y)
205  {
206  if (ns == 0)
207  {
208  sumX = sumY = sumX2 = sumY2 = sumXY = T(0);
209  xMin = xMax = x;
210  yMin = yMax = y;
211  scaleX = scaleY = T(1);
212  }
213 
214  if (scaled)
215  {
216  if (scaleX==T(1) && x!=T()) scaleX=ABS(x);
217  if (scaleY==T(1) && y!=T()) scaleY=ABS(y);
218  T tx(x/scaleX);
219  T ty(y/scaleY);
220  sumX += tx;
221  sumY += ty;
222  sumX2 += tx*tx;
223  sumY2 += ty*ty;
224  sumXY += tx*ty;
225  }
226  else
227  {
228  sumX += x;
229  sumY += y;
230  sumX2 += x*x;
231  sumY2 += y*y;
232  sumXY += x*y;
233  }
234 
235  if(x < xMin) xMin=x;
236  if(x > xMax) xMax=x;
237  if(y < yMin) yMin=y;
238  if(y > yMax) yMax=y;
239  ns++;
240  }
241 
242  template<class T>
243  void BivarStats<T>::add(const std::vector<T>& x, const std::vector<T>& y)
244  {
245  size_t m = x.size() < y.size() ? x.size() : y.size();
246  if(m==0)
247  return;
248  for (size_t i=0; i<m; i++)
249  add(x[i], y[i]);
250  }
251 
252  template<class T>
253  void BivarStats<T>::add(const std::vector< std::pair<T, T> >& d)
254  {
255  size_t max( d.size() );
256  for (size_t i=0; i<max; i++)
257  add(d[i].first, d[i].second);
258  }
259 
260  template<class T>
261  void BivarStats<T>::add(const Vector<T>& x, const Vector<T>& y)
262  {
263  size_t m = x.size() < y.size() ? x.size() : y.size();
264  if (m==0)
265  return;
266  for (size_t i=0; i<m; i++)
267  add(x(i), y(i));
268  }
269 
270  template<class T>
271  void BivarStats<T>::subtract(const T& x, const T& y)
272  {
273  if (ns < 2)
274  {
275  ns = 0;
276  return;
277  }
278 
279  if (scaled)
280  {
281  T tx(x/scaleX);
282  T ty(y/scaleY);
283  sumX -= tx;
284  sumY -= ty;
285  sumX2 -= tx*tx;
286  sumY2 -= ty*ty;
287  sumXY -= tx*ty;
288  }
289  else
290  {
291  sumX -= x;
292  sumY -= y;
293  sumX2 -= x*x;
294  sumY2 -= y*y;
295  sumXY -= x*y;
296  }
297 
298  ns--;
299  }
300 
301  template<class T>
302  void BivarStats<T>::subtract(const std::vector<T>& x, const std::vector<T>& y)
303  {
304  size_t m = x.size()<y.size() ? x.size() : y.size();
305  if(m==0)
306  return;
307  for (size_t i=0; i<m; i++)
308  subtract(x[i], y[i]);
309  }
310 
311  template<class T>
312  void BivarStats<T>::subtract(const std::vector< std::pair<T, T> >& d)
313  {
314  size_t max( d.size() );
315  for (size_t i=0; i<max; i++)
316  subtract(d[i].first, d[i].second);
317  }
318 
319  template<class T>
321  {
322  size_t m = x.size()<y.size() ? x.size() : y.size();
323  if (m==0)
324  return;
325  for (size_t i=0; i<m; i++)
326  subtract(x(i), y(i));
327  }
328 
330  template<class T>
331  void BivarStats<T>::clear(void) { ns=0; }
332 
333  template<class T>
334  inline size_t BivarStats<T>::n(void) const { return ns; }
335 
336  template<class T>
337  T BivarStats<T>::minimumX(void) const { return ns>0 ? xMin : T(0); }
338  template<class T>
339  T BivarStats<T>::maximumX(void) const { return ns>0 ? xMax : T(0); }
340  template<class T>
341  T BivarStats<T>::minimumY(void) const { return ns>0 ? yMin : T(0); }
342  template<class T>
343  T BivarStats<T>::maximumY(void) const { return ns>0 ? yMax : T(0); }
344 
345  template<class T>
347  { return ns>0 ? scaleX*sumX/T(ns) : T(0); }
348  template<class T>
350  { return ns>0 ? scaleY*sumY/T(ns) : T(0); }
351 
352 
353  template<class T>
355  {
356  return (ns>1) ? scaleX*scaleX * (sumX2 - sumX*sumX/T(ns)) / T(ns-1) : T(0);
357  }
358 
359  template<class T>
361  {
362  return (ns>1) ? scaleY*scaleY * (sumY2 - sumY*sumY/T(ns)) / T(ns-1) : T(0);
363  }
364 
365  template<class T>
366  T BivarStats<T>::stdDevX(void) const { return SQRT(varianceX()); }
367  template<class T>
368  T BivarStats<T>::stdDevY(void) const { return SQRT(varianceY()); }
369 
370  template<class T>
371  T BivarStats<T>::slope(void) const
372  {
373  if (ns>0)
374  return (scaleY/scaleX) * (sumXY - sumX*sumY/T(ns)) /
375  (sumX2 - sumX*sumX/T(ns));
376  else
377  return T();
378  }
379 
380  template<class T>
382  {
383  if (ns>0)
384  return averageY() - slope() * averageX();
385  else
386  return T();
387  }
388 
389  template<class T>
391  {
392  if (ns>2)
393  return sigmaYX() / (stdDevX() * SQRT(T(ns-1)));
394  else
395  return T();
396  }
397 
398  template<class T>
400  {
401  if (ns>1)
402  return scaleX*scaleY * (sumXY - sumX*sumY/T(ns)) /
403  (stdDevX() * stdDevY() * T(ns-1));
404  else
405  return T();
406  }
407 
408  template<class T>
410  {
411  if (ns>2)
412  return (stdDevY() * SQRT(T(ns-1) / T(ns-2))
413  * SQRT(T(1) - correlation() * correlation()) );
414  else return T();
415  }
416 
419  template<class T>
421  {
422  if(ns + S.ns == 0) return *this;
423  xMin = std::min(xMin, S.xMin);
424  xMax = std::max(xMax, S.xMax);
425  yMin = std::min(yMin, S.yMin);
426  yMax = std::max(yMax, S.yMax);
427  T xscaler( S.scaleX/scaleX ), yscaler( S.scaleY/scaleY );
428  sumX += xscaler * S.sumX;
429  sumY += yscaler * S.sumY;
430  sumX2 += xscaler * xscaler * S.sumX2;
431  sumY2 += yscaler * yscaler * S.sumY2;
432  sumXY += xscaler * yscaler * S.sumXY;
433  ns += S.ns;
434  return *this;
435  }
437 
438  template<class T>
439  Stats<T> BivarStats<T>::estimateDeviation(const std::vector< std::pair<T, T> >& d) const
440  {
441  Stats<T> estats;
442  size_t max( d.size() );
443  for (size_t i=0; i<max; i++)
444  estats.Add(std::abs(d[i].second - eval(d[i].first)));
445  return estats;
446  }
447 
448 } // namespace
449 
450 #endif
gnsstk::BivarStats::averageX
T averageX(void) const
Definition: BivarStats.hpp:346
gnsstk::BivarStats::BivarStats
BivarStats(bool scale=false)
Definition: BivarStats.hpp:170
gnsstk::BivarStats::sigmaYX
T sigmaYX(void) const
return conditional uncertainty = uncertainty y given x
Definition: BivarStats.hpp:409
gnsstk::BivarStats::scaled
bool scaled
Definition: BivarStats.hpp:142
SQRT
#define SQRT(x)
Definition: MathBase.hpp:74
gnsstk::BivarStats::stdDevX
T stdDevX(void) const
Definition: BivarStats.hpp:366
gnsstk::BivarStats::averageY
T averageY(void) const
Definition: BivarStats.hpp:349
gnsstk::BivarStats::stdDevY
T stdDevY(void) const
Definition: BivarStats.hpp:368
gnsstk::max
T max(const SparseMatrix< T > &SM)
Maximum element - return 0 if empty.
Definition: SparseMatrix.hpp:881
gnsstk::BivarStats::varianceX
T varianceX(void) const
Definition: BivarStats.hpp:354
gnsstk::BivarStats::eval
T eval(const T &x) const
compute intercept + x * slope
Definition: BivarStats.hpp:127
gnsstk::BivarStats::clear
void clear(void)
Remove all data and start over.
Definition: BivarStats.hpp:331
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::BivarStats::sumY2
T sumY2
Definition: BivarStats.hpp:143
gnsstk::Stats
Definition: Stats.hpp:137
MiscMath.hpp
Stats.hpp
gnsstk::BivarStats::subtract
void subtract(const T &x, const T &y)
Definition: BivarStats.hpp:271
gnsstk::BivarStats::sumXY
T sumXY
Definition: BivarStats.hpp:143
example6.second
second
Definition: example6.py:69
y
page HOWTO subpage DoxygenGuide Documenting Your Code page DoxygenGuide Documenting Your Code todo Flesh out this document section doctips Tips for Documenting When defining make sure that the prototype is identical between the cpp and hpp including both the namespaces and the parameter names for you have std::string as the return type in the hpp file and string as the return type in the cpp Doxygen may get confused and autolink to the cpp version with no documentation If you don t use the same parameter names between the cpp and hpp that will also confuse Doxygen Don t put type information in return or param documentation It doesn t really add anything and will often cause Doxygen to complain and not produce the documentation< br > use note Do not put a comma after a param name unless you mean to document multiple parameters< br/> the output stream</code >< br/> y
Definition: DOCUMENTING.dox:15
gnsstk::BivarStats::maximumY
T maximumY(void) const
Definition: BivarStats.hpp:343
gnsstk::BivarStats::yMax
T yMax
Definition: BivarStats.hpp:140
gnsstk::BivarStats::correlation
T correlation(void) const
Definition: BivarStats.hpp:399
gnsstk::BivarStats::ns
size_t ns
Number of samples added to the statistics so far.
Definition: BivarStats.hpp:138
gnsstk::BivarStats::intercept
T intercept(void) const
Return intercept of best-fit line Y=slope*X + intercept.
Definition: BivarStats.hpp:381
gnsstk::operator<<
std::ostream & operator<<(std::ostream &s, const ObsEpoch &oe) noexcept
Definition: ObsEpochMap.cpp:54
gnsstk::BivarStats::xMax
T xMax
Definition: BivarStats.hpp:140
gnsstk::BivarStats::minimumY
T minimumY(void) const
Definition: BivarStats.hpp:341
gnsstk::min
T min(const SparseMatrix< T > &SM)
Maximum element - return 0 if empty.
Definition: SparseMatrix.hpp:858
gnsstk::BivarStats::sumY
T sumY
Definition: BivarStats.hpp:143
gnsstk::Vector
Definition: Vector.hpp:67
gnsstk::BivarStats::varianceY
T varianceY(void) const
Definition: BivarStats.hpp:360
ABS
#define ABS(x)
Definition: MathBase.hpp:73
gnsstk::BivarStats::operator+=
BivarStats< T > & operator+=(BivarStats< T > &S)
Definition: BivarStats.hpp:420
gnsstk::BivarStats::xMin
T xMin
Definition: BivarStats.hpp:140
gnsstk::BivarStats::scaleY
T scaleY
Definition: BivarStats.hpp:141
gnsstk::Vector::size
size_t size() const
STL size.
Definition: Vector.hpp:207
Exception.hpp
gnsstk::BivarStats::yMin
T yMin
Definition: BivarStats.hpp:140
gnsstk::Stats::Add
void Add(const T &x)
Definition: Stats.hpp:158
gnsstk::BivarStats::scaleX
T scaleX
Definition: BivarStats.hpp:141
gnsstk::BivarStats::maximumX
T maximumX(void) const
Definition: BivarStats.hpp:339
gnsstk::BivarStats
Definition: BivarStats.hpp:63
gnsstk::BivarStats::sumX2
T sumX2
Definition: BivarStats.hpp:143
gnsstk::BivarStats::n
size_t n(void) const
Return the sample size.
Definition: BivarStats.hpp:334
gnsstk::BivarStats::sumX
T sumX
Definition: BivarStats.hpp:143
Vector.hpp
gnsstk::BivarStats::add
void add(const T &x, const T &y)
Definition: BivarStats.hpp:204
gnsstk::BivarStats::sigmaSlope
T sigmaSlope(void) const
Return uncertainty in slope.
Definition: BivarStats.hpp:390
gnsstk::BivarStats::estimateDeviation
Stats< T > estimateDeviation(const std::vector< std::pair< T, T > > &d) const
Definition: BivarStats.hpp:439
gnsstk::BivarStats::minimumX
T minimumX(void) const
Definition: BivarStats.hpp:337
gnsstk::BivarStats::slope
T slope(void) const
Return slope of best-fit line Y=slope*X + intercept.
Definition: BivarStats.hpp:371


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