28 : mStatistics( stats )
41 mFirst = std::numeric_limits<double>::quiet_NaN();
42 mLast = std::numeric_limits<double>::quiet_NaN();
48 mMin = std::numeric_limits<double>::max();
49 mMax = -std::numeric_limits<double>::max();
76 for (
double value : values )
90 mMin = std::min( mMin, value );
91 mMax = std::max( mMax, value );
95 mValueCount.insert( value, mValueCount.value( value, 0 ) + 1 );
97 if ( mRequiresAllValueStorage )
103 bool convertOk =
false;
104 if ( !value.isValid() || value.isNull() )
108 double val = value.toDouble( &convertOk );
120 mFirst = std::numeric_limits<double>::quiet_NaN();
121 mLast = std::numeric_limits<double>::quiet_NaN();
122 mMin = std::numeric_limits<double>::quiet_NaN();
123 mMax = std::numeric_limits<double>::quiet_NaN();
124 mMean = std::numeric_limits<double>::quiet_NaN();
125 mMedian = std::numeric_limits<double>::quiet_NaN();
126 mStdev = std::numeric_limits<double>::quiet_NaN();
127 mSampleStdev = std::numeric_limits<double>::quiet_NaN();
128 mMinority = std::numeric_limits<double>::quiet_NaN();
129 mMajority = std::numeric_limits<double>::quiet_NaN();
130 mFirstQuartile = std::numeric_limits<double>::quiet_NaN();
131 mThirdQuartile = std::numeric_limits<double>::quiet_NaN();
135 mMean = mSum / mCount;
139 double sumSquared = 0;
140 const auto constMValues = mValues;
141 for (
double value : constMValues )
143 double diff = value - mMean;
144 sumSquared += diff * diff;
146 mStdev = std::pow( sumSquared / mValues.count(), 0.5 );
147 mSampleStdev = std::pow( sumSquared / ( mValues.count() - 1 ), 0.5 );
155 std::sort( mValues.begin(), mValues.end() );
156 bool even = ( mCount % 2 ) < 1;
159 mMedian = ( mValues[mCount / 2 - 1] + mValues[mCount / 2] ) / 2.0;
163 mMedian = mValues[( mCount + 1 ) / 2 - 1];
168 || mStatistics & QgsStatisticalSummary::InterQuartileRange )
170 if ( ( mCount % 2 ) < 1 )
172 int halfCount = mCount / 2;
173 bool even = ( halfCount % 2 ) < 1;
176 mFirstQuartile = ( mValues[halfCount / 2 - 1] + mValues[halfCount / 2] ) / 2.0;
180 mFirstQuartile = mValues[( halfCount + 1 ) / 2 - 1];
185 int halfCount = mCount / 2 + 1;
186 bool even = ( halfCount % 2 ) < 1;
189 mFirstQuartile = ( mValues[halfCount / 2 - 1] + mValues[halfCount / 2] ) / 2.0;
193 mFirstQuartile = mValues[( halfCount + 1 ) / 2 - 1];
199 || mStatistics & QgsStatisticalSummary::InterQuartileRange )
201 if ( ( mCount % 2 ) < 1 )
203 int halfCount = mCount / 2;
204 bool even = ( halfCount % 2 ) < 1;
207 mThirdQuartile = ( mValues[ halfCount + halfCount / 2 - 1] + mValues[ halfCount + halfCount / 2] ) / 2.0;
211 mThirdQuartile = mValues[( halfCount + 1 ) / 2 - 1 + halfCount ];
216 int halfCount = mCount / 2 + 1;
217 bool even = ( halfCount % 2 ) < 1;
220 mThirdQuartile = ( mValues[ halfCount + halfCount / 2 - 2 ] + mValues[ halfCount + halfCount / 2 - 1 ] ) / 2.0;
224 mThirdQuartile = mValues[( halfCount + 1 ) / 2 - 2 + halfCount ];
231 QList<int> valueCounts = mValueCount.values();
232 std::sort( valueCounts.begin(), valueCounts.end() );
235 mMinority = mValueCount.key( valueCounts.first() );
237 if ( mStatistics & QgsStatisticalSummary::Majority )
239 mMajority = mValueCount.key( valueCounts.last() );
280 return mValueCount.count();
282 return mFirstQuartile;
284 return mThirdQuartile;
286 return mThirdQuartile - mFirstQuartile;
302 return QObject::tr(
"Count" );
304 return QObject::tr(
"Count (missing)" );
306 return QObject::tr(
"Sum" );
308 return QObject::tr(
"Mean" );
310 return QObject::tr(
"Median" );
312 return QObject::tr(
"St dev (pop)" );
314 return QObject::tr(
"St dev (sample)" );
316 return QObject::tr(
"Minimum" );
318 return QObject::tr(
"Maximum" );
320 return QObject::tr(
"Range" );
322 return QObject::tr(
"Minority" );
324 return QObject::tr(
"Majority" );
326 return QObject::tr(
"Variety" );
328 return QObject::tr(
"Q1" );
330 return QObject::tr(
"Q3" );
332 return QObject::tr(
"IQR" );
334 return QObject::tr(
"First" );
336 return QObject::tr(
"Last" );
348 return QStringLiteral(
"count" );
350 return QStringLiteral(
"countmissing" );
352 return QStringLiteral(
"sum" );
354 return QStringLiteral(
"mean" );
356 return QStringLiteral(
"median" );
358 return QStringLiteral(
"stdev" );
360 return QStringLiteral(
"stdevsample" );
362 return QStringLiteral(
"min" );
364 return QStringLiteral(
"max" );
366 return QStringLiteral(
"range" );
368 return QStringLiteral(
"minority" );
370 return QStringLiteral(
"majority" );
372 return QStringLiteral(
"variety" );
374 return QStringLiteral(
"q1" );
376 return QStringLiteral(
"q3" );
378 return QStringLiteral(
"iqr" );
380 return QStringLiteral(
"first" );
382 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 setStatistics(QgsStatisticalSummary::Statistics stats)
Sets flags which specify which statistics will be calculated.
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.