21#ifndef JKQTPSTATBASICS_H_INCLUDED
22#define JKQTPSTATBASICS_H_INCLUDED
37#include "jkqtmath/jkqtmath_imexport.h"
38#include "jkqtmath/jkqtplinalgtools.h"
39#include "jkqtmath/jkqtparraytools.h"
40#include "jkqtcommon/jkqtpdebuggingtools.h"
41#include "jkqtcommon/jkqtpmathtools.h"
61template <
class InputIt>
65 for (
auto it=first; it!=last; ++it) {
72 if (Noutput) *Noutput=NN;
74 else return sum/
static_cast<double>(NN);
100template <
class InputIt,
class InputWeightIt>
105 auto itW=firstWeight;
106 for (
auto it=first; it!=last; ++it,++itW) {
115 if (Noutput) *Noutput=NN;
117 else return sum/sumW;
134template <
class InputIt>
138 for (
auto it=first; it!=last; ++it) {
167template <
class InputIt>
168inline void jkqtpstatMinMax(InputIt first, InputIt last,
double& min,
double& max, InputIt* minPos=
nullptr, InputIt* maxPos=
nullptr,
size_t* Noutput=
nullptr) {
173 for (
auto it=first; it!=last; ++it) {
199 if (Noutput) *Noutput=NN;
200 if (minPos) *minPos=minp;
201 if (maxPos) *maxPos=maxp;
222template <
class InputIt>
223inline double jkqtpstatMinimum(InputIt first, InputIt last, InputIt* minPos=
nullptr,
size_t* Noutput=
nullptr) {
228 for (
auto it=first; it!=last; ++it) {
244 if (Noutput) *Noutput=NN;
245 if (minPos) *minPos=minp;
264template <
class InputIt>
265inline double jkqtpstatMaximum(InputIt first, InputIt last, InputIt* maxPos=
nullptr,
size_t* Noutput=
nullptr) {
270 for (
auto it=first; it!=last; ++it) {
286 if (Noutput) *Noutput=NN;
287 if (maxPos) *maxPos=maxp;
319template <
class InputIt,
class FF>
323 for (
auto it=first; it!=last; ++it) {
326 sum=sum+modifierFunctor(v);
330 if (Noutput) *Noutput=NN;
354template <
class InputIt>
355inline double jkqtpstatSum(InputIt first, InputIt last,
size_t* Noutput=
nullptr) {
376template <
class InputIt>
402template <
class InputIt,
class OutputIt>
405 for (
auto it=first; it!=last; ++it) {
430template <
class InputIt,
class OutputIt>
433 for (
auto it=first; it!=last; ++it) {
472template <
class InputIt>
473inline double jkqtpstatVariance(InputIt first, InputIt last,
double* averageOut=
nullptr,
size_t* Noutput=
nullptr) {
477 for (
auto it=first; it!=last; ++it) {
487 else *averageOut=sum/
static_cast<double>(NN);
489 if (Noutput) *Noutput=NN;
491 else return ( sum2 - sum*sum/
static_cast<double>(NN) ) /
static_cast<double>(NN-1);
514template <
class InputIt>
515inline double jkqtpstatStdDev(InputIt first, InputIt last,
double* averageOut=
nullptr,
size_t* Noutput=
nullptr) {
542template <
class InputIt,
class InputWeightIt>
543inline double jkqtpstatWeightedVariance(InputIt first, InputIt last, InputWeightIt firstWeight,
double* averageOut=
nullptr,
size_t* Noutput=
nullptr) {
548 auto itW=firstWeight;
549 for (
auto it=first; it!=last; ++it,++itW) {
558 if (averageOut) *averageOut=avg;
559 if (Noutput) *Noutput=NN;
561 else return sum2/sumW;
586template <
class InputIt,
class InputWeightIt>
587inline double jkqtpstatWeightedStdDev(InputIt first, InputIt last, InputWeightIt firstWeight,
double* averageOut=
nullptr,
size_t* Noutput=
nullptr) {
614template <
class InputIt>
615inline double jkqtpstatSkewness(InputIt first, InputIt last,
double* averageOut=
nullptr,
size_t* Noutput=
nullptr) {
620 for (
auto it=first; it!=last; ++it) {
628 if (averageOut) *averageOut=avg;
629 if (Noutput) *Noutput=NN;
631 const double down=
jkqtp_cube(sum2/
double(NN));
632 return sum3/double(NN)/sqrt(down);
659template <
class InputIt>
660inline double jkqtpstatCentralMoment(InputIt first, InputIt last,
int order,
double* averageOut=
nullptr,
size_t* Noutput=
nullptr) {
664 for (
auto it=first; it!=last; ++it) {
667 sum=sum+pow(v, order);
671 if (averageOut) *averageOut=avg;
672 if (Noutput) *Noutput=NN;
674 return sum/double(NN);
700template <
class InputIt>
701inline double jkqtpstatMoment(InputIt first, InputIt last,
int order,
size_t* Noutput=
nullptr) {
704 for (
auto it=first; it!=last; ++it) {
707 sum=sum+pow(v, order);
711 if (Noutput) *Noutput=NN;
713 return sum/double(NN);
742template <
class InputIt1,
class InputIt2>
743inline double jkqtpstatCorrelationCoefficient(InputIt1 first1, InputIt1 last1, InputIt2 first2,
double* averageOut1=
nullptr,
double* averageOut2=
nullptr,
size_t* Noutput=
nullptr) {
748 for (
auto it=first1; it!=last1; ++it,++it2) {
757 if (Noutput) *Noutput=NN;
760 else *averageOut1=xbar/
static_cast<double>(NN);
764 else *averageOut2=ybar/
static_cast<double>(NN);
774 for (
auto it=first1; it!=last1; ++it,++it2) {
783 return sumxy/sqrt(sumx*sumy);
799template <
class TVector>
801 if (data.size()<=0) {
802 if (Noutput) *Noutput=0;
805 if (Noutput) *Noutput=data.size();
806 if (data.size()==1)
return data[0];
807 else if (data.size()%2==0)
return (data[(data.size()-1)/2]+data[(data.size()-1)/2+1])/2.0;
808 else return data[(data.size()-1)/2];
827template <
class InputIt>
830 const size_t n=std::distance(first,last);
838 const auto middleItr = first + n / 2;
839 std::nth_element(first, middleItr, last);
843 const auto leftMiddleItr = std::max_element(first, middleItr);
845 return (*leftMiddleItr + *middleItr) / 2.0;
867template <
class InputIt>
870 std::vector<double> dataFiltered;
872 const size_t n=dataFiltered.size();
876 if (Noutput) *Noutput=0;
910template <
class TVector>
911inline void jkqtpstat5NumberStatisticsOfSortedVector(
const TVector& data,
double* minimum=
nullptr,
double minimumQuantile=0,
double* median=
nullptr,
double* maximum=
nullptr,
double maximumQuantile=1,
double* quantile1=
nullptr,
double quantile1Spec=0.25,
double* quantile2=
nullptr,
double quantile2Spec=0.75,
double* IQR=
nullptr,
double* IQRSignificance=
nullptr,
size_t* Noutput=
nullptr) {
912 if (data.size()<=0) {
918 if (Noutput) *Noutput=0;
920 const double qmin=data[jkqtp_bounded<size_t>(0,
static_cast<size_t>(minimumQuantile*
static_cast<double>(data.size()-1)), data.size()-1)];
921 const double qmax=data[jkqtp_bounded<size_t>(0,
static_cast<size_t>(maximumQuantile*
static_cast<double>(data.size()-1)), data.size()-1)];
922 if (minimum) *minimum=qmin;
923 if (maximum) *maximum=qmax;
927 const double q1=data[jkqtp_bounded<size_t>(0,
static_cast<size_t>(quantile1Spec*
static_cast<double>(data.size()-1)), data.size()-1)];
928 const double q2=data[jkqtp_bounded<size_t>(0,
static_cast<size_t>(quantile2Spec*
static_cast<double>(data.size()-1)), data.size()-1)];
938 if (IQRSignificance) {
939 *IQRSignificance=2.0*(1.58*(q2-q1))/sqrt(
static_cast<double>(data.size()));
941 if (Noutput) *Noutput=data.size();
972template <
class TVector,
class OutputIt>
973inline void jkqtpstat5NumberStatisticsAndOutliersOfSortedVector(
const TVector& data, OutputIt outliersout,
double* minimum=
nullptr,
double minimumQuantile=0,
double* median=
nullptr,
double* maximum=
nullptr,
double maximumQuantile=1,
double* quantile1=
nullptr,
double quantile1Spec=0.25,
double* quantile2=
nullptr,
double quantile2Spec=0.75,
double* IQR=
nullptr,
double* IQRSignificance=
nullptr,
size_t* Noutput=
nullptr) {
974 if (data.size()<=0) {
980 if (Noutput) *Noutput=0;
982 const double qmin=data[jkqtp_bounded<size_t>(0,
static_cast<size_t>(minimumQuantile*
static_cast<double>(data.size()-1)), data.size()-1)];
983 const double qmax=data[jkqtp_bounded<size_t>(0,
static_cast<size_t>(maximumQuantile*
static_cast<double>(data.size()-1)), data.size()-1)];
984 if (minimum) *minimum=qmin;
985 if (maximum) *maximum=qmax;
986 for (
auto it=data.begin(); it!=data.end(); ++it) {
987 if (*it<qmin || *it>qmax) {
995 const double q1=data[jkqtp_bounded<size_t>(0,
static_cast<size_t>(quantile1Spec*
static_cast<double>(data.size()-1)), data.size()-1)];
996 const double q2=data[jkqtp_bounded<size_t>(0,
static_cast<size_t>(quantile2Spec*
static_cast<double>(data.size()-1)), data.size()-1)];
1006 if (IQRSignificance) {
1007 *IQRSignificance=2.0*(1.58*(q2-q1))/sqrt(
static_cast<double>(data.size()));
1009 if (Noutput) *Noutput=data.size();
1042template <
class InputIt>
1043inline void jkqtpstat5NumberStatistics(InputIt first, InputIt last,
double* minimum,
double minimumQuantile=0,
double* median=
nullptr,
double* maximum=
nullptr,
double maximumQuantile=1,
double quantile1Spec=0.25,
double* quantile1=
nullptr,
double quantile2Spec=0.75,
double* quantile2=
nullptr,
double* IQR=
nullptr,
double* IQRSignificance=
nullptr,
size_t* Noutput=
nullptr) {
1044 std::vector<double> dataFiltered;
1046 std::sort(dataFiltered.begin(), dataFiltered.end());
1047 jkqtpstat5NumberStatisticsOfSortedVector(dataFiltered, minimum, minimumQuantile, median, maximum, maximumQuantile, quantile1, quantile1Spec, quantile2, quantile2Spec, IQR, IQRSignificance, Noutput);
1079template <
class InputIt,
class OutputIt>
1080inline void jkqtpstat5NumberStatisticsAndOutliers(InputIt first, InputIt last, OutputIt outliersout,
double* minimum=
nullptr,
double minimumQuantile=0,
double* median=
nullptr,
double* maximum=
nullptr,
double maximumQuantile=1,
double* quantile1=
nullptr,
double quantile1Spec=0.25,
double* quantile2=
nullptr,
double quantile2Spec=0.75,
double* IQR=
nullptr,
double* IQRSignificance=
nullptr,
size_t* Noutput=
nullptr) {
1081 std::vector<double> dataFiltered;
1083 std::sort(dataFiltered.begin(), dataFiltered.end());
1084 jkqtpstat5NumberStatisticsAndOutliersOfSortedVector(dataFiltered, outliersout, minimum, minimumQuantile, median, maximum, maximumQuantile, quantile1, quantile1Spec, quantile2, quantile2Spec, IQR, IQRSignificance, Noutput);
1142template <
class InputIt>
1145 jkqtpstat5NumberStatisticsAndOutliers(first, last, std::back_inserter(res.
outliers), &(res.
minimum), minimumQuantile, &(res.
median), &(res.
maximum), maximumQuantile, &(res.
quantile1), quantile1Spec,&(res.
quantile2),quantile2Spec,
nullptr,
nullptr,&(res.
N));
1169template <
class InputIt>
1170inline double jkqtpstatQuantile(InputIt first, InputIt last,
double quantile,
size_t* Noutput=
nullptr) {
1171 std::vector<double> dataFiltered;
1173 if (dataFiltered.size()<=0) {
1174 if (Noutput) *Noutput=0;
1177 if (Noutput) *Noutput=dataFiltered.size();
1178 auto qelement=dataFiltered.begin()+jkqtp_bounded<size_t>(0,
static_cast<size_t>(quantile*
static_cast<double>(dataFiltered.size()-1)), dataFiltered.size()-1);
1179 std::nth_element(dataFiltered.begin(), qelement, dataFiltered.end());
1209template <
class InputIt>
1210inline double jkqtpstatMAD(InputIt first, InputIt last,
double* median=
nullptr,
size_t* Noutput=
nullptr) {
1211 std::vector<double> dataFiltered;
1213 if (dataFiltered.size()<=0) {
1214 if (Noutput) *Noutput=0;
1218 if (Noutput) *Noutput=dataFiltered.size();
1220 if (median) *median=med;
1221 for(
double& v: dataFiltered) {
1253template <
class InputIt>
1254inline double jkqtpstatNMAD(InputIt first, InputIt last,
double* median=
nullptr,
size_t* Noutput=
nullptr) {
1255 return jkqtpstatMAD(first, last, median, Noutput)/0.6745;
#define jkqtmath_LIB_EXPORT
Definition jkqtmath_imexport.h:87
represents the Five-Number Statistical Summary (minimum, median, maximum and two user-defined quantil...
Definition jkqtpstatbasics.h:1091
double minimumQuantile
specifies a quantile for the return value minimum (default is 0 for the real minimum,...
Definition jkqtpstatbasics.h:1097
double quantile1
first quantile value (specified by quantile1Spec)
Definition jkqtpstatbasics.h:1099
double quantile2
second quantile value (specified by quantile1Spec)
Definition jkqtpstatbasics.h:1105
std::vector< double > outliers
list with the outlier values < minimum and > maximum
Definition jkqtpstatbasics.h:1119
JKQTPStat5NumberStatistics()
double minimum
minimum value
Definition jkqtpstatbasics.h:1095
double IQRSignificanceEstimate() const
interquartile range, calculated as
double median
median value
Definition jkqtpstatbasics.h:1103
size_t N
number of values used to calculate the summary
Definition jkqtpstatbasics.h:1113
double maximumQuantile
specifies a quantile for the return value maximum (default is 1 for the real maximum,...
Definition jkqtpstatbasics.h:1111
double quantile2Spec
specifies the second quantile (range: 0..1)
Definition jkqtpstatbasics.h:1107
double quantile1Spec
specifies the first quantile (range: 0..1)
Definition jkqtpstatbasics.h:1101
double maximum
maximum value
Definition jkqtpstatbasics.h:1109
double IQR() const
the interquarzile range