Loading [MathJax]/jax/output/NativeMML/config.js
Isis 3 Programmer Reference
MultivariateStatistics.cpp
1
5
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
19namespace Isis {
20
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;
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];
72
73 if(Isis::IsValidPixel(xVal) && Isis::IsValidPixel(yVal)) {
74 p_x.AddData(xVal);
75 p_y.AddData(yVal);
76 p_sumxy += xVal * yVal;
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++) {
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];
134 }
135 else {
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
192
193
204
205
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 exception class.
Definition IException.h:91
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition IException.h:146
void LinearRegression(double &a, double &b) const
Fits a line.
void RemoveData(const double *x, const double *y, const unsigned int count)
Remove an array of doubles from the accumulators and counters.
Isis::Statistics p_y
A Statistics object holding y data.
double Correlation() const
Computes and returns the coefficient of correlation (between -1.0 and 1.0) of the two data sets.
Isis::Statistics Y() const
Returns a Stats object for all of the Y data fed through the AddData method.
Isis::Statistics p_x
A Statistics object holding x data.
BigInt p_validPixels
The number of valid (computed) pixels.
double Covariance() const
Computes and returns the covariance between the two data sets If there are no valid data (pixels) the...
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.
BigInt TotalPixels() const
Returns the total number of pixels processed.
BigInt InvalidPixels() const
Returns the number of invalid pixels encountered.
double SumXY() const
Returns the sum of x*y for all data given through the AddData method.
~MultivariateStatistics()
Destructs a MultivariateStatistics object.
double p_sumxy
The sum of x and y.
BigInt p_invalidPixels
The number of invalid (ignored) pixels.
BigInt ValidPixels() const
Returns the number of valid pixels processed.
Isis::Statistics X() const
Returns a Stats object for all of the X data fed through the AddData method.
void fromPvl(const PvlObject &inStats)
Unserializes a multivariate statistics object from a PvlObject.
void Reset()
Resets all accumulators to zero.
void AddData(const double *x, const double *y, const unsigned int count)
Add two arrays of doubles to the accumulators and counters.
BigInt p_totalPixels
The total number of pixels (invalid and valid).
Contains multiple PvlContainers.
Definition PvlGroup.h:41
A single keyword-value pair.
Definition PvlKeyword.h:87
Contains Pvl Groups and Pvl Objects.
Definition PvlObject.h:61
void addGroup(const Isis::PvlGroup &group)
Add a group to the object.
Definition PvlObject.h:186
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition PvlObject.h:129
This class is used to accumulate statistics on double arrays.
Definition Statistics.h:93
PvlGroup toPvl(QString name="Statistics") const
Serialize statistics as a pvl group.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition IString.cpp:211
bool IsValidPixel(const double d)
Returns if the input pixel is valid.
long long int BigInt
Big int.
Definition Constants.h:49