QGIS API Documentation  2.14.0-Essen
qgsstatisticalsummary.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsstatisticalsummary.h
3  --------------------------------------
4  Date : May 2015
5  Copyright : (C) 2015 by Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSSTATISTICALSUMMARY_H
17 #define QGSSTATISTICALSUMMARY_H
18 
19 #include <QMap>
20 
21 /***************************************************************************
22  * This class is considered CRITICAL and any change MUST be accompanied with
23  * full unit tests in testqgsstatisticalsummary.cpp.
24  * See details in QEP #17
25  ****************************************************************************/
26 
39 class CORE_EXPORT QgsStatisticalSummary
40 {
41  public:
42 
44  enum Statistic
45  {
46  Count = 1,
47  Sum = 2,
48  Mean = 4,
49  Median = 8,
50  StDev = 16,
51  StDevSample = 32,
52  Min = 64,
53  Max = 128,
54  Range = 256,
55  Minority = 512,
56  Majority = 1024,
57  Variety = 2048,
58  FirstQuartile = 4096,
59  ThirdQuartile = 8192,
60  InterQuartileRange = 16384,
61  All = Count | Sum | Mean | Median | StDev | Max | Min | Range | Minority | Majority | Variety | FirstQuartile | ThirdQuartile | InterQuartileRange
62  };
63  Q_DECLARE_FLAGS( Statistics, Statistic )
64 
65 
68  QgsStatisticalSummary( const QgsStatisticalSummary::Statistics& stats = All );
69 
70  virtual ~QgsStatisticalSummary();
71 
76  Statistics statistics() const { return mStatistics; }
77 
83  void setStatistics( const Statistics& stats ) { mStatistics = stats; }
84 
87  void reset();
88 
92  void calculate( const QList<double>& values );
93 
98  double statistic( Statistic stat ) const;
99 
102  int count() const { return mCount; }
103 
106  double sum() const { return mSum; }
107 
110  double mean() const { return mMean; }
111 
115  double median() const { return mMedian; }
116 
119  double min() const { return mMin; }
120 
123  double max() const { return mMax; }
124 
127  double range() const { return mMax - mMin; }
128 
133  double stDev() const { return mStdev; }
134 
139  double sampleStDev() const { return mSampleStdev; }
140 
145  int variety() const { return mValueCount.count(); }
146 
152  double minority() const { return mMinority; }
153 
159  double majority() const { return mMajority; }
160 
166  double firstQuartile() const { return mFirstQuartile; }
167 
173  double thirdQuartile() const { return mThirdQuartile; }
174 
180  double interQuartileRange() const { return mThirdQuartile - mFirstQuartile; }
181 
185  static QString displayName( Statistic statistic );
186 
187  private:
188 
189  Statistics mStatistics;
190 
191  int mCount;
192  double mSum;
193  double mMean;
194  double mMedian;
195  double mMin;
196  double mMax;
197  double mStdev;
198  double mSampleStdev;
199  double mMinority;
200  double mMajority;
201  double mFirstQuartile;
202  double mThirdQuartile;
203  QMap< double, int > mValueCount;
204 };
205 
206 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsStatisticalSummary::Statistics )
207 
208 #endif // QGSSTATISTICALSUMMARY_H
Statistic
Enumeration of flags that specify statistics to be calculated.
double min() const
Returns calculated minimum from values.
int variety() const
Returns variety of values.
double mean() const
Returns calculated mean of values.
void setStatistics(const Statistics &stats)
Sets flags which specify which statistics will be calculated.
double firstQuartile() const
Returns the first quartile of the values.
double range() const
Returns calculated range (difference between maximum and minimum values).
int count() const
Returns calculated count of values.
double minority() const
Returns minority of values.
double stDev() const
Returns population standard deviation.
double sampleStDev() const
Returns sample standard deviation.
double median() const
Returns calculated median of values.
double majority() const
Returns majority of values.
double sum() const
Returns calculated sum of values.
double thirdQuartile() const
Returns the third quartile of the values.
double max() const
Returns calculated maximum from values.
Calculator for summary statistics for a list of doubles.
double interQuartileRange() const
Returns the inter quartile range of the values.