29 : mStatistics( stats )
42 mFirst = std::numeric_limits<double>::quiet_NaN();
43 mLast = std::numeric_limits<double>::quiet_NaN();
49 mMin = std::numeric_limits<double>::max();
50 mMax = -std::numeric_limits<double>::max();
77 for (
const double value : values )
91 mMin = std::min( mMin, value );
92 mMax = std::max( mMax, value );
96 mValueCount.insert( value, mValueCount.value( value, 0 ) + 1 );
98 if ( mRequiresAllValueStorage )
104 bool convertOk =
false;
109 const double val = value.toDouble( &convertOk );
121 mFirst = std::numeric_limits<double>::quiet_NaN();
122 mLast = std::numeric_limits<double>::quiet_NaN();
123 mMin = std::numeric_limits<double>::quiet_NaN();
124 mMax = std::numeric_limits<double>::quiet_NaN();
125 mMean = std::numeric_limits<double>::quiet_NaN();
126 mMedian = std::numeric_limits<double>::quiet_NaN();
127 mStdev = std::numeric_limits<double>::quiet_NaN();
128 mSampleStdev = std::numeric_limits<double>::quiet_NaN();
129 mMinority = std::numeric_limits<double>::quiet_NaN();
130 mMajority = std::numeric_limits<double>::quiet_NaN();
131 mFirstQuartile = std::numeric_limits<double>::quiet_NaN();
132 mThirdQuartile = std::numeric_limits<double>::quiet_NaN();
136 mMean = mSum / mCount;
140 double sumSquared = 0;
141 const auto constMValues = mValues;
142 for (
const double value : constMValues )
144 const double diff = value - mMean;
145 sumSquared += diff * diff;
147 mStdev = std::pow( sumSquared / mValues.count(), 0.5 );
148 mSampleStdev = std::pow( sumSquared / ( mValues.count() - 1 ), 0.5 );
156 std::sort( mValues.begin(), mValues.end() );
157 const bool even = ( mCount % 2 ) < 1;
160 mMedian = ( mValues[mCount / 2 - 1] + mValues[mCount / 2] ) / 2.0;
164 mMedian = mValues[( mCount + 1 ) / 2 - 1];
171 if ( ( mCount % 2 ) < 1 )
173 const int halfCount = mCount / 2;
174 const bool even = ( halfCount % 2 ) < 1;
177 mFirstQuartile = ( mValues[halfCount / 2 - 1] + mValues[halfCount / 2] ) / 2.0;
181 mFirstQuartile = mValues[( halfCount + 1 ) / 2 - 1];
186 const int halfCount = mCount / 2 + 1;
187 const bool even = ( halfCount % 2 ) < 1;
190 mFirstQuartile = ( mValues[halfCount / 2 - 1] + mValues[halfCount / 2] ) / 2.0;
194 mFirstQuartile = mValues[( halfCount + 1 ) / 2 - 1];
202 if ( ( mCount % 2 ) < 1 )
204 const int halfCount = mCount / 2;
205 const bool even = ( halfCount % 2 ) < 1;
208 mThirdQuartile = ( mValues[ halfCount + halfCount / 2 - 1] + mValues[ halfCount + halfCount / 2] ) / 2.0;
212 mThirdQuartile = mValues[( halfCount + 1 ) / 2 - 1 + halfCount ];
217 const int halfCount = mCount / 2 + 1;
218 const bool even = ( halfCount % 2 ) < 1;
221 mThirdQuartile = ( mValues[ halfCount + halfCount / 2 - 2 ] + mValues[ halfCount + halfCount / 2 - 1 ] ) / 2.0;
225 mThirdQuartile = mValues[( halfCount + 1 ) / 2 - 2 + halfCount ];
232 QList<int> valueCounts = mValueCount.values();
236 mMinority = mValueCount.key( *std::min_element( valueCounts.begin(), valueCounts.end() ) );
240 mMajority = mValueCount.key( *std::max_element( valueCounts.begin(), valueCounts.end() ) );
281 return mValueCount.count();
283 return mFirstQuartile;
285 return mThirdQuartile;
287 return mThirdQuartile - mFirstQuartile;
303 return QObject::tr(
"Count" );
305 return QObject::tr(
"Count (missing)" );
307 return QObject::tr(
"Sum" );
309 return QObject::tr(
"Mean" );
311 return QObject::tr(
"Median" );
313 return QObject::tr(
"St dev (pop)" );
315 return QObject::tr(
"St dev (sample)" );
317 return QObject::tr(
"Minimum" );
319 return QObject::tr(
"Maximum" );
321 return QObject::tr(
"Range" );
323 return QObject::tr(
"Minority" );
325 return QObject::tr(
"Majority" );
327 return QObject::tr(
"Variety" );
329 return QObject::tr(
"Q1" );
331 return QObject::tr(
"Q3" );
333 return QObject::tr(
"IQR" );
335 return QObject::tr(
"First" );
337 return QObject::tr(
"Last" );
349 return QStringLiteral(
"count" );
351 return QStringLiteral(
"countmissing" );
353 return QStringLiteral(
"sum" );
355 return QStringLiteral(
"mean" );
357 return QStringLiteral(
"median" );
359 return QStringLiteral(
"stdev" );
361 return QStringLiteral(
"stdevsample" );
363 return QStringLiteral(
"min" );
365 return QStringLiteral(
"max" );
367 return QStringLiteral(
"range" );
369 return QStringLiteral(
"minority" );
371 return QStringLiteral(
"majority" );
373 return QStringLiteral(
"variety" );
375 return QStringLiteral(
"q1" );
377 return QStringLiteral(
"q3" );
379 return QStringLiteral(
"iqr" );
381 return QStringLiteral(
"first" );
383 return QStringLiteral(
"last" );
Statistic
Available generic statistics.
@ StDev
Standard deviation of values.
@ FirstQuartile
First quartile.
@ Median
Median of values.
@ First
First value (since QGIS 3.6)
@ Range
Range of values (max - min)
@ Minority
Minority of values.
@ CountMissing
Number of missing (null) values.
@ Majority
Majority of values.
@ Variety
Variety (count of distinct) values.
@ StDevSample
Sample standard deviation of values.
@ Last
Last value (since QGIS 3.6)
@ ThirdQuartile
Third quartile.
@ InterQuartileRange
Inter quartile range (IQR)
QFlags< Statistic > Statistics
Statistics to be calculated for generic values.
void addVariant(const QVariant &value)
Adds a single value to the statistics calculation.
static QString displayName(Qgis::Statistic statistic)
Returns the friendly display name for a statistic.
static QString shortName(Qgis::Statistic statistic)
Returns a short, friendly display name for a statistic, suitable for use in a field name.
void calculate(const QList< double > &values)
Calculates summary statistics for a list of values.
void addValue(double value)
Adds a single value to the statistics calculation.
QgsStatisticalSummary(Qgis::Statistics stats=Qgis::Statistic::All)
Constructor for QgsStatisticalSummary.
void reset()
Resets the calculated values.
double statistic(Qgis::Statistic stat) const
Returns the value of a specified statistic.
void finalize()
Must be called after adding all values with addValues() and before retrieving any calculated statisti...
void setStatistics(Qgis::Statistics stats)
Sets flags which specify which statistics will be calculated.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.