Isis 3 Programmer Reference
MultivariateStatistics.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 
8 #include <float.h>
9 #include <string>
10 #include <iostream>
11 
12 #include "IException.h"
13 #include "MultivariateStatistics.h"
14 #include "PvlGroup.h"
15 #include "PvlKeyword.h"
16 #include "PvlObject.h"
17 
18 
19 namespace Isis {
20 
26  Reset();
27  }
28 
29 
36  Reset();
37  fromPvl(inStats);
38  }
39 
40 
43  p_x.Reset();
44  p_y.Reset();
45  p_sumxy = 0.0;
46 
47  p_validPixels = 0;
48  p_invalidPixels = 0;
49  p_totalPixels = 0;
50  }
51 
52 
55 
56 
66  void MultivariateStatistics::AddData(const double *x, const double *y,
67  const unsigned int count) {
68  for(unsigned int i = 0; i < count; i++) {
69  double yVal = y[i];
70  double xVal = x[i];
71  p_totalPixels++;
72 
73  if(Isis::IsValidPixel(xVal) && Isis::IsValidPixel(yVal)) {
74  p_x.AddData(xVal);
75  p_y.AddData(yVal);
76  p_sumxy += xVal * yVal;
77  p_validPixels++;
78  }
79  else {
81  }
82  }
83  }
84 
85 
94  void MultivariateStatistics::AddData(double x, double y, unsigned int count) {
95  p_totalPixels += count;
96 
97  if(IsValidPixel(x) && IsValidPixel(y)) {
98  p_sumxy += x * y * count;
99  p_validPixels += count;
100 
101  for (unsigned int i = 0; i < count; i++) {
102  p_x.AddData(x);
103  p_y.AddData(y);
104  }
105  }
106  else {
107  p_invalidPixels += count;
108  }
109  }
110 
111 
124  void MultivariateStatistics::RemoveData(const double *x, const double *y,
125  const unsigned int count) {
126  for(unsigned int i = 0; i < count; i++) {
127  p_totalPixels--;
128 
129  if(Isis::IsValidPixel(x[i]) && Isis::IsValidPixel(y[i])) {
130  p_x.RemoveData(&x[i], 1);
131  p_y.RemoveData(&y[i], 1);
132  p_sumxy -= x[i] * y[i];
133  p_validPixels--;
134  }
135  else {
136  p_invalidPixels--;
137  }
138  }
139 
140  if(p_totalPixels < 0) {
141  std::string m = "You are removing non-existant data in [MultivariateStatistics::RemoveData]";
142  throw IException(IException::Programmer, m, _FILEINFO_);
143  }
144  }
145 
146 
154  if(p_validPixels <= 1) return Isis::NULL8;
155  double covar = p_sumxy - p_y.Average() * p_x.Sum() - p_x.Average() * p_y.Sum() +
156  p_x.Average() * p_y.Average() * (double)p_validPixels;
157  return covar / (double)(p_validPixels - 1);
158  }
159 
160 
172 
173  if(p_validPixels <= 1) return Isis::NULL8;
174  double covar = Covariance();
175  double stdX = p_x.StandardDeviation();
176  double stdY = p_y.StandardDeviation();
177  if(stdX == 0.0 || stdX == Isis::NULL8) return Isis::NULL8;
178  if(stdY == 0.0 || stdY == Isis::NULL8) return Isis::NULL8;
179  if(covar == Isis::NULL8) return Isis::NULL8;
180  return covar / (stdX * stdY);
181  }
182 
183 
190  return p_totalPixels;
191  }
192 
193 
202  return p_validPixels;
203  }
204 
205 
212  return p_invalidPixels;
213  }
214 
215 
222  void MultivariateStatistics::LinearRegression(double &a, double &b) const {
223  // From Modern Elementary Statistics - 5th edition, Freund, pp 367
224  double denom = (double)p_validPixels * p_x.SumSquare() - p_x.Sum() * p_x.Sum();
225  if(denom == 0.0) {
226  std::string msg = "Unable to compute linear regression in Multivariate Statistics";
227  throw IException(IException::Programmer, msg, _FILEINFO_);
228  }
229  a = p_y.Sum() * p_x.SumSquare() - p_x.Sum() * p_sumxy;
230  a = a / denom;
231 
232  b = (double)p_validPixels * p_sumxy - p_x.Sum() * p_y.Sum();
233  b = b / denom;
234  }
235 
236 
243  return p_sumxy;
244  }
245 
246 
253  return p_x;
254  };
255 
256 
263  return p_y;
264  };
265 
266 
273  p_sumxy = inStats["SumXY"];
274  p_validPixels = inStats["ValidPixels"];
275  p_invalidPixels = inStats["InvalidPixels"];
276  p_totalPixels = inStats["TotalPixels"];
277 
278  // unserialize the X and Y Statistics as well
279  PvlGroup xStats = inStats.findGroup("XStatistics"); //
280  p_x = Statistics(xStats);
281  PvlGroup yStats = inStats.findGroup("YStatistics");
282  p_y = Statistics(yStats);
283  }
284 
285 
294  if (name.isEmpty()) {
295  name = "MultivariateStatistics";
296  }
297  PvlObject mStats(name);
298  mStats += PvlKeyword("Covariance" , toString(Covariance()));
299  mStats += PvlKeyword("Correlation", toString(Correlation()));
300  mStats += PvlKeyword("SumXY", toString(SumXY()));
301  mStats += PvlKeyword("ValidPixels", toString(ValidPixels()));
302  mStats += PvlKeyword("InvalidPixels", toString(InvalidPixels()));
303  mStats += PvlKeyword("TotalPixels", toString(TotalPixels()));
304 
305  PvlKeyword linReg("LinearRegression");
306  double a, b;
307  try {
308  LinearRegression(a, b);
309  linReg += toString(a);
310  linReg += toString(b);
311  } catch (IException &e) {
312  // It is possible one of the overlaps was constant and therefore
313  // the regression would be a vertical line (x=c instead of y=ax+b)
314  }
315  mStats += linReg;
316 
317  PvlGroup xStats = X().toPvl("XStatistics");
318  PvlGroup yStats = Y().toPvl("YStatistics");
319  mStats.addGroup(xStats);
320  mStats.addGroup(yStats);
321 
322  return mStats;
323  }
324 
325 }
326 
327 
328 
Isis::MultivariateStatistics::p_invalidPixels
BigInt p_invalidPixels
The number of invalid (ignored) pixels.
Definition: MultivariateStatistics.h:101
Isis::Statistics::SumSquare
double SumSquare() const
Returns the sum of all the squared data.
Definition: Statistics.cpp:351
Isis::MultivariateStatistics::p_totalPixels
BigInt p_totalPixels
The total number of pixels (invalid and valid).
Definition: MultivariateStatistics.h:106
Isis::Statistics
This class is used to accumulate statistics on double arrays.
Definition: Statistics.h:94
Isis::PvlObject::findGroup
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition: PvlObject.h:129
Isis::Statistics::AddData
void AddData(const double *data, const unsigned int count)
Add an array of doubles to the accumulators and counters.
Definition: Statistics.cpp:141
Isis::Statistics::Sum
double Sum() const
Returns the sum of all the data.
Definition: Statistics.cpp:341
Isis::PvlObject
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:61
Isis::PvlKeyword
A single keyword-value pair.
Definition: PvlKeyword.h:82
Isis::MultivariateStatistics::Y
Isis::Statistics Y() const
Returns a Stats object for all of the Y data fed through the AddData method.
Definition: MultivariateStatistics.cpp:262
Isis::MultivariateStatistics::Covariance
double Covariance() const
Computes and returns the covariance between the two data sets If there are no valid data (pixels) the...
Definition: MultivariateStatistics.cpp:153
Isis::MultivariateStatistics::ValidPixels
BigInt ValidPixels() const
Returns the number of valid pixels processed.
Definition: MultivariateStatistics.cpp:201
Isis::MultivariateStatistics::p_x
Isis::Statistics p_x
A Statistics object holding x data.
Definition: MultivariateStatistics.h:86
Isis::MultivariateStatistics::~MultivariateStatistics
~MultivariateStatistics()
Destructs a MultivariateStatistics object.
Definition: MultivariateStatistics.cpp:54
Isis::Statistics::Reset
void Reset()
Reset all accumulators and counters to zero.
Definition: Statistics.cpp:113
Isis::MultivariateStatistics::InvalidPixels
BigInt InvalidPixels() const
Returns the number of invalid pixels encountered.
Definition: MultivariateStatistics.cpp:211
Isis::MultivariateStatistics::p_sumxy
double p_sumxy
The sum of x and y.
Definition: MultivariateStatistics.h:91
Isis::MultivariateStatistics::toPvl
PvlObject toPvl(QString name="MultivariateStatistics") const
Serializes a multivariate statistics object as a PvlObject.
Definition: MultivariateStatistics.cpp:293
Isis::toString
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:211
Isis::MultivariateStatistics::fromPvl
void fromPvl(const PvlObject &inStats)
Unserializes a multivariate statistics object from a PvlObject.
Definition: MultivariateStatistics.cpp:272
Isis::MultivariateStatistics::AddData
void AddData(const double *x, const double *y, const unsigned int count)
Add two arrays of doubles to the accumulators and counters.
Definition: MultivariateStatistics.cpp:66
Isis::MultivariateStatistics::X
Isis::Statistics X() const
Returns a Stats object for all of the X data fed through the AddData method.
Definition: MultivariateStatistics.cpp:252
Isis::Statistics::RemoveData
void RemoveData(const double *data, const unsigned int count)
Remove an array of doubles from the accumulators and counters.
Definition: Statistics.cpp:206
Isis::PvlGroup
Contains multiple PvlContainers.
Definition: PvlGroup.h:41
Isis::Statistics::StandardDeviation
double StandardDeviation() const
Computes and returns the standard deviation.
Definition: Statistics.cpp:312
Isis::BigInt
long long int BigInt
Big int.
Definition: Constants.h:49
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::Statistics::toPvl
PvlGroup toPvl(QString name="Statistics") const
Serialize statistics as a pvl group.
Definition: Statistics.cpp:695
Isis::MultivariateStatistics::MultivariateStatistics
MultivariateStatistics()
Constructs a Multivariate Statistics object with accumulators and counters set to zero.
Definition: MultivariateStatistics.cpp:25
Isis::PvlObject::addGroup
void addGroup(const Isis::PvlGroup &group)
Add a group to the object.
Definition: PvlObject.h:186
Isis::Statistics::Average
double Average() const
Computes and returns the average.
Definition: Statistics.cpp:300
Isis::MultivariateStatistics::LinearRegression
void LinearRegression(double &a, double &b) const
Fits a line.
Definition: MultivariateStatistics.cpp:222
Isis::IException::Programmer
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:146
Isis::IsValidPixel
bool IsValidPixel(const double d)
Returns if the input pixel is valid.
Definition: SpecialPixel.h:223
Isis::MultivariateStatistics::RemoveData
void RemoveData(const double *x, const double *y, const unsigned int count)
Remove an array of doubles from the accumulators and counters.
Definition: MultivariateStatistics.cpp:124
Isis::MultivariateStatistics::TotalPixels
BigInt TotalPixels() const
Returns the total number of pixels processed.
Definition: MultivariateStatistics.cpp:189
Isis::MultivariateStatistics::Correlation
double Correlation() const
Computes and returns the coefficient of correlation (between -1.0 and 1.0) of the two data sets.
Definition: MultivariateStatistics.cpp:171
Isis::MultivariateStatistics::p_y
Isis::Statistics p_y
A Statistics object holding y data.
Definition: MultivariateStatistics.h:88
Isis::MultivariateStatistics::Reset
void Reset()
Resets all accumulators to zero.
Definition: MultivariateStatistics.cpp:42
Isis::MultivariateStatistics::p_validPixels
BigInt p_validPixels
The number of valid (computed) pixels.
Definition: MultivariateStatistics.h:96
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::MultivariateStatistics::SumXY
double SumXY() const
Returns the sum of x*y for all data given through the AddData method.
Definition: MultivariateStatistics.cpp:242