Isis 3 Programmer Reference
MultivariateStatistics.cpp
Go to the documentation of this file.
1 
24 #include <float.h>
25 #include <string>
26 #include <iostream>
27 
28 #include "IException.h"
29 #include "MultivariateStatistics.h"
30 #include "PvlGroup.h"
31 #include "PvlKeyword.h"
32 #include "PvlObject.h"
33 
34 
35 namespace Isis {
36 
42  Reset();
43  }
44 
45 
52  Reset();
53  fromPvl(inStats);
54  }
55 
56 
59  p_x.Reset();
60  p_y.Reset();
61  p_sumxy = 0.0;
62 
63  p_validPixels = 0;
64  p_invalidPixels = 0;
65  p_totalPixels = 0;
66  }
67 
68 
71 
72 
82  void MultivariateStatistics::AddData(const double *x, const double *y,
83  const unsigned int count) {
84  for(unsigned int i = 0; i < count; i++) {
85  double yVal = y[i];
86  double xVal = x[i];
87  p_totalPixels++;
88 
89  if(Isis::IsValidPixel(xVal) && Isis::IsValidPixel(yVal)) {
90  p_x.AddData(xVal);
91  p_y.AddData(yVal);
92  p_sumxy += xVal * yVal;
93  p_validPixels++;
94  }
95  else {
97  }
98  }
99  }
100 
101 
110  void MultivariateStatistics::AddData(double x, double y, unsigned int count) {
111  p_totalPixels += count;
112 
113  if(IsValidPixel(x) && IsValidPixel(y)) {
114  p_sumxy += x * y * count;
115  p_validPixels += count;
116 
117  for (unsigned int i = 0; i < count; i++) {
118  p_x.AddData(x);
119  p_y.AddData(y);
120  }
121  }
122  else {
123  p_invalidPixels += count;
124  }
125  }
126 
127 
140  void MultivariateStatistics::RemoveData(const double *x, const double *y,
141  const unsigned int count) {
142  for(unsigned int i = 0; i < count; i++) {
143  p_totalPixels--;
144 
145  if(Isis::IsValidPixel(x[i]) && Isis::IsValidPixel(y[i])) {
146  p_x.RemoveData(&x[i], 1);
147  p_y.RemoveData(&y[i], 1);
148  p_sumxy -= x[i] * y[i];
149  p_validPixels--;
150  }
151  else {
152  p_invalidPixels--;
153  }
154  }
155 
156  if(p_totalPixels < 0) {
157  std::string m = "You are removing non-existant data in [MultivariateStatistics::RemoveData]";
159  }
160  }
161 
162 
170  if(p_validPixels <= 1) return Isis::NULL8;
171  double covar = p_sumxy - p_y.Average() * p_x.Sum() - p_x.Average() * p_y.Sum() +
172  p_x.Average() * p_y.Average() * (double)p_validPixels;
173  return covar / (double)(p_validPixels - 1);
174  }
175 
176 
188 
189  if(p_validPixels <= 1) return Isis::NULL8;
190  double covar = Covariance();
191  double stdX = p_x.StandardDeviation();
192  double stdY = p_y.StandardDeviation();
193  if(stdX == 0.0 || stdX == Isis::NULL8) return Isis::NULL8;
194  if(stdY == 0.0 || stdY == Isis::NULL8) return Isis::NULL8;
195  if(covar == Isis::NULL8) return Isis::NULL8;
196  return covar / (stdX * stdY);
197  }
198 
199 
206  return p_totalPixels;
207  }
208 
209 
218  return p_validPixels;
219  }
220 
221 
228  return p_invalidPixels;
229  }
230 
231 
238  void MultivariateStatistics::LinearRegression(double &a, double &b) const {
239  // From Modern Elementary Statistics - 5th edition, Freund, pp 367
240  double denom = (double)p_validPixels * p_x.SumSquare() - p_x.Sum() * p_x.Sum();
241  if(denom == 0.0) {
242  std::string msg = "Unable to compute linear regression in Multivariate Statistics";
244  }
245  a = p_y.Sum() * p_x.SumSquare() - p_x.Sum() * p_sumxy;
246  a = a / denom;
247 
248  b = (double)p_validPixels * p_sumxy - p_x.Sum() * p_y.Sum();
249  b = b / denom;
250  }
251 
252 
259  return p_sumxy;
260  }
261 
262 
269  return p_x;
270  };
271 
272 
279  return p_y;
280  };
281 
282 
289  p_sumxy = inStats["SumXY"];
290  p_validPixels = inStats["ValidPixels"];
291  p_invalidPixels = inStats["InvalidPixels"];
292  p_totalPixels = inStats["TotalPixels"];
293 
294  // unserialize the X and Y Statistics as well
295  PvlGroup xStats = inStats.findGroup("XStatistics"); //
296  p_x = Statistics(xStats);
297  PvlGroup yStats = inStats.findGroup("YStatistics");
298  p_y = Statistics(yStats);
299  }
300 
301 
310  if (name.isEmpty()) {
311  name = "MultivariateStatistics";
312  }
313  PvlObject mStats(name);
314  mStats += PvlKeyword("Covariance" , toString(Covariance()));
315  mStats += PvlKeyword("Correlation", toString(Correlation()));
316  mStats += PvlKeyword("SumXY", toString(SumXY()));
317  mStats += PvlKeyword("ValidPixels", toString(ValidPixels()));
318  mStats += PvlKeyword("InvalidPixels", toString(InvalidPixels()));
319  mStats += PvlKeyword("TotalPixels", toString(TotalPixels()));
320 
321  PvlKeyword linReg("LinearRegression");
322  double a, b;
323  try {
324  LinearRegression(a, b);
325  linReg += toString(a);
326  linReg += toString(b);
327  } catch (IException &e) {
328  // It is possible one of the overlaps was constant and therefore
329  // the regression would be a vertical line (x=c instead of y=ax+b)
330  }
331  mStats += linReg;
332 
333  PvlGroup xStats = X().toPvl("XStatistics");
334  PvlGroup yStats = Y().toPvl("YStatistics");
335  mStats.addGroup(xStats);
336  mStats.addGroup(yStats);
337 
338  return mStats;
339  }
340 
341 }
342 
343 
344 
long long int BigInt
Big int.
Definition: Constants.h:65
Isis::Statistics Y() const
Returns a Stats object for all of the Y data fed through the AddData method.
void RemoveData(const double *data, const unsigned int count)
Remove an array of doubles from the accumulators and counters.
Definition: Statistics.cpp:219
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition: PvlObject.h:141
double Sum() const
Returns the sum of all the data.
Definition: Statistics.cpp:354
BigInt p_invalidPixels
The number of invalid (ignored) pixels.
double StandardDeviation() const
Computes and returns the standard deviation.
Definition: Statistics.cpp:325
void LinearRegression(double &a, double &b) const
Fits a line through the data.
double SumSquare() const
Returns the sum of all the squared data.
Definition: Statistics.cpp:364
double Covariance() const
Computes and returns the covariance between the two data sets If there are no valid data (pixels) the...
BigInt TotalPixels() const
Returns the total number of pixels processed.
double p_sumxy
The sum of x and y.
BigInt ValidPixels() const
Returns the number of valid pixels processed.
void addGroup(const Isis::PvlGroup &group)
Add a group to the object.
Definition: PvlObject.h:198
~MultivariateStatistics()
Destructs a MultivariateStatistics object.
MultivariateStatistics()
Constructs a Multivariate Statistics object with accumulators and counters set to zero...
PvlObject toPvl(QString name="MultivariateStatistics") const
Serializes a multivariate statistics object as a PvlObject.
Isis::Statistics X() const
Returns a Stats object for all of the X data fed through the AddData method.
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:226
bool IsValidPixel(const double d)
Returns if the input pixel is valid.
Definition: SpecialPixel.h:238
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:162
Isis::Statistics p_x
A Statistics object holding x data.
double Correlation() const
Computes and returns the coefficient of correlation (between -1.0 and 1.0) of the two data sets...
This class is used to accumulate statistics on double arrays.
Definition: Statistics.h:107
PvlGroup toPvl(QString name="Statistics") const
Serialize statistics as a pvl group.
Definition: Statistics.cpp:700
BigInt p_totalPixels
The total number of pixels (invalid and valid).
Contains multiple PvlContainers.
Definition: PvlGroup.h:57
Isis::Statistics p_y
A Statistics object holding y data.
void Reset()
Reset all accumulators and counters to zero.
Definition: Statistics.cpp:126
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
A single keyword-value pair.
Definition: PvlKeyword.h:98
BigInt p_validPixels
The number of valid (computed) pixels.
BigInt InvalidPixels() const
Returns the number of invalid pixels encountered.
void RemoveData(const double *x, const double *y, const unsigned int count)
Remove an array of doubles from the accumulators and counters.
void Reset()
Resets all accumulators to zero.
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
void fromPvl(const PvlObject &inStats)
Unserializes a multivariate statistics object from a PvlObject.
void AddData(const double *x, const double *y, const unsigned int count)
Add two arrays of doubles to the accumulators and counters.
void AddData(const double *data, const unsigned int count)
Add an array of doubles to the accumulators and counters.
Definition: Statistics.cpp:154
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:74
double Average() const
Computes and returns the average.
Definition: Statistics.cpp:313
double SumXY() const
Returns the sum of x*y for all data given through the AddData method.