28 : mStatistics( stats )
35 mFirst = std::numeric_limits<double>::quiet_NaN();
36 mLast = std::numeric_limits<double>::quiet_NaN();
42 mMin = std::numeric_limits<double>::max();
43 mMax = -std::numeric_limits<double>::max();
70 for (
double value : values )
84 mMin = std::min( mMin, value );
85 mMax = std::max( mMax, value );
89 mValueCount.insert( value, mValueCount.value( value, 0 ) + 1 );
91 if ( mRequiresAllValueStorage )
97 bool convertOk =
false;
98 if ( !value.isValid() || value.isNull() )
102 double val = value.toDouble( &convertOk );
114 mFirst = std::numeric_limits<double>::quiet_NaN();
115 mLast = std::numeric_limits<double>::quiet_NaN();
116 mMin = std::numeric_limits<double>::quiet_NaN();
117 mMax = std::numeric_limits<double>::quiet_NaN();
118 mMean = std::numeric_limits<double>::quiet_NaN();
119 mMedian = std::numeric_limits<double>::quiet_NaN();
120 mStdev = std::numeric_limits<double>::quiet_NaN();
121 mSampleStdev = std::numeric_limits<double>::quiet_NaN();
122 mMinority = std::numeric_limits<double>::quiet_NaN();
123 mMajority = std::numeric_limits<double>::quiet_NaN();
124 mFirstQuartile = std::numeric_limits<double>::quiet_NaN();
125 mThirdQuartile = std::numeric_limits<double>::quiet_NaN();
129 mMean = mSum / mCount;
133 double sumSquared = 0;
134 Q_FOREACH (
double value, mValues )
136 double diff = value - mMean;
137 sumSquared += diff * diff;
139 mStdev = std::pow( sumSquared / mValues.count(), 0.5 );
140 mSampleStdev = std::pow( sumSquared / ( mValues.count() - 1 ), 0.5 );
148 std::sort( mValues.begin(), mValues.end() );
149 bool even = ( mCount % 2 ) < 1;
152 mMedian = ( mValues[mCount / 2 - 1] + mValues[mCount / 2] ) / 2.0;
156 mMedian = mValues[( mCount + 1 ) / 2 - 1];
161 || mStatistics & QgsStatisticalSummary::InterQuartileRange )
163 if ( ( mCount % 2 ) < 1 )
165 int halfCount = mCount / 2;
166 bool even = ( halfCount % 2 ) < 1;
169 mFirstQuartile = ( mValues[halfCount / 2 - 1] + mValues[halfCount / 2] ) / 2.0;
173 mFirstQuartile = mValues[( halfCount + 1 ) / 2 - 1];
178 int halfCount = mCount / 2 + 1;
179 bool even = ( halfCount % 2 ) < 1;
182 mFirstQuartile = ( mValues[halfCount / 2 - 1] + mValues[halfCount / 2] ) / 2.0;
186 mFirstQuartile = mValues[( halfCount + 1 ) / 2 - 1];
192 || mStatistics & QgsStatisticalSummary::InterQuartileRange )
194 if ( ( mCount % 2 ) < 1 )
196 int halfCount = mCount / 2;
197 bool even = ( halfCount % 2 ) < 1;
200 mThirdQuartile = ( mValues[ halfCount + halfCount / 2 - 1] + mValues[ halfCount + halfCount / 2] ) / 2.0;
204 mThirdQuartile = mValues[( halfCount + 1 ) / 2 - 1 + halfCount ];
209 int halfCount = mCount / 2 + 1;
210 bool even = ( halfCount % 2 ) < 1;
213 mThirdQuartile = ( mValues[ halfCount + halfCount / 2 - 2 ] + mValues[ halfCount + halfCount / 2 - 1 ] ) / 2.0;
217 mThirdQuartile = mValues[( halfCount + 1 ) / 2 - 2 + halfCount ];
224 QList<int> valueCounts = mValueCount.values();
225 std::sort( valueCounts.begin(), valueCounts.end() );
228 mMinority = mValueCount.key( valueCounts.first() );
230 if ( mStatistics & QgsStatisticalSummary::Majority )
232 mMajority = mValueCount.key( valueCounts.last() );
273 return mValueCount.count();
275 return mFirstQuartile;
277 return mThirdQuartile;
279 return mThirdQuartile - mFirstQuartile;
295 return QObject::tr(
"Count" );
297 return QObject::tr(
"Count (missing)" );
299 return QObject::tr(
"Sum" );
301 return QObject::tr(
"Mean" );
303 return QObject::tr(
"Median" );
305 return QObject::tr(
"St dev (pop)" );
307 return QObject::tr(
"St dev (sample)" );
309 return QObject::tr(
"Minimum" );
311 return QObject::tr(
"Maximum" );
313 return QObject::tr(
"Range" );
315 return QObject::tr(
"Minority" );
317 return QObject::tr(
"Majority" );
319 return QObject::tr(
"Variety" );
321 return QObject::tr(
"Q1" );
323 return QObject::tr(
"Q3" );
325 return QObject::tr(
"IQR" );
327 return QObject::tr(
"First" );
329 return QObject::tr(
"Last" );
341 return QStringLiteral(
"count" );
343 return QStringLiteral(
"countmissing" );
345 return QStringLiteral(
"sum" );
347 return QStringLiteral(
"mean" );
349 return QStringLiteral(
"median" );
351 return QStringLiteral(
"stdev" );
353 return QStringLiteral(
"stdevsample" );
355 return QStringLiteral(
"min" );
357 return QStringLiteral(
"max" );
359 return QStringLiteral(
"range" );
361 return QStringLiteral(
"minority" );
363 return QStringLiteral(
"majority" );
365 return QStringLiteral(
"variety" );
367 return QStringLiteral(
"q1" );
369 return QStringLiteral(
"q3" );
371 return QStringLiteral(
"iqr" );
373 return QStringLiteral(
"first" );
375 return QStringLiteral(
"last" );
void reset()
Resets the calculated values.
Statistic
Enumeration of flags that specify statistics to be calculated.
Variety (count of distinct) values.
void finalize()
Must be called after adding all values with addValues() and before retrieving any calculated statisti...
void addVariant(const QVariant &value)
Adds a single value to the statistics calculation.
Sample standard deviation of values.
Number of missing (null) values.
Inter quartile range (IQR)
QgsStatisticalSummary(QgsStatisticalSummary::Statistics stats=QgsStatisticalSummary::All)
Constructor for QgsStatisticalSummary.
First value (since QGIS 3.6)
Last value (since QGIS 3.6)
double statistic(QgsStatisticalSummary::Statistic stat) const
Returns the value of a specified statistic.
static QString shortName(QgsStatisticalSummary::Statistic statistic)
Returns a short, friendly display name for a statistic, suitable for use in a field name...
static QString displayName(QgsStatisticalSummary::Statistic statistic)
Returns the friendly display name for a statistic.
void calculate(const QList< double > &values)
Calculates summary statistics for a list of values.
Standard deviation of values.
Range of values (max - min)
void addValue(double value)
Adds a single value to the statistics calculation.