JKQTPlotter trunk/v5.0.0
an extensive Qt5+Qt6 Plotter framework (including a feature-richt plotter widget, a speed-optimized, but limited variant and a LaTeX equation renderer!), written fully in C/C++ and without external dependencies
Loading...
Searching...
No Matches
jkqtpstatgrouped.h
1/*
2 Copyright (c) 2008-2024 Jan W. Krieger (<jan@jkrieger.de>)
3
4 last modification: $LastChangedDate$ (revision $Rev$)
5
6 This software is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License (LGPL) as published by
8 the Free Software Foundation, either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License (LGPL) for more details.
15
16 You should have received a copy of the GNU Lesser General Public License (LGPL)
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20
21#ifndef JKQTPSTATGROUPED_H_INCLUDED
22#define JKQTPSTATGROUPED_H_INCLUDED
23
24#include <stdint.h>
25#include <cmath>
26#include <stdlib.h>
27#include <string.h>
28#include <iostream>
29#include <stdio.h>
30#include <limits>
31#include <vector>
32#include <utility>
33#include <cfloat>
34#include <ostream>
35#include <iomanip>
36#include <sstream>
37#include "jkqtmath/jkqtmath_imexport.h"
38#include "jkqtmath/jkqtplinalgtools.h"
39#include "jkqtmath/jkqtparraytools.h"
40#include "jkqtcommon/jkqtpdebuggingtools.h"
41#include "jkqtmath/jkqtpstatbasics.h"
42
43/*! \brief a functor \f$ f(x): \mathbb{R}\rightarrow\mathbb{R} \f$ which assignes a value \f$ x \f$ to a group center \f$ f(x) \f$
44 \ingroup jkqtptools_math_statistics_grouped
45
46 The simplest version are e.g.
47 - jkqtpstatGroupingIdentity1D()
48 - jkqtpstatGroupingRound1D()
49 - jkqtpstatGroupingCustomRound1D()
50 .
51
52 \see jkqtpstatGroupData
53*/
54typedef std::function<double(double)> JKQTPStatGroupDefinitionFunctor1D;
55
56
57
58/*! \brief use a column value as group ID directly
59 \ingroup jkqtptools_math_statistics_grouped
60
61 \see JKQTPStatGroupDefinitionFunctor1D
62*/
64/*! \brief use a rounded column value as group ID directly \f$ f(x)=\mbox{round}(x) \f$
65 \ingroup jkqtptools_math_statistics_grouped
66
67 \see JKQTPStatGroupDefinitionFunctor1D
68*/
70/*! \brief assign each value to groups \f$ \mbox{firstGroupCenter} \f$ , \f$ \mbox{firstGroupCenter}\pm\mbox{groupWidth}/2\f$ , \f$ \mbox{firstGroupCenter}\pm2\cdot\mbox{groupWidth}/2 \f$ , \f$ \mbox{firstGroupCenter}\pm3\cdot\mbox{groupWidth}/2 \f$ , ...
71 \ingroup jkqtptools_math_statistics_grouped
72
73 This is equivalent to \f$ \mbox{round}\left(\frac{x-\mbox{firstGroupCenter}}{\mbox{groupWidth}/2}\right) \f$
74
75 \see JKQTPStatGroupDefinitionFunctor1D, jkqtpstatMakeGroupingCustomRound1D() for a factory-function that returns a functor of this function bound to specific arguments.
76*/
77jkqtmath_LIB_EXPORT double jkqtpstatGroupingCustomRound1D(double v, double firstGroupCenter, double groupWidth);
78/*! \brief generates a functor of jkqtpstatGroupingCustomRound1D() with the two paramaters \a firstGroupCenter and \a groupWidth fixed to the given values
79 \ingroup jkqtptools_math_statistics_grouped
80
81 This is equivalent to \c std::bind(&jkqtpstatGroupingCustomRound1D,std::placeholders::_1,firstGroupCenter,groupWidth);
82
83 \see JKQTPStatGroupDefinitionFunctor1D, jkqtpstatGroupingCustomRound1D()
84*/
86
87
88
89/*! \brief groups data from an input range \a inFirstCat / \a inFirstValue ... \a inLastCat / \a outFirstCat representing pairs \f$ (c_i,v_i) \f$ of a
90 category value \f$ c_i \f$ and a group value \f$ v_i \f$ into groups \f$ V_j=\{v_{i}|c_i\equiv c_{\text{out},j}\} \f$ of data that were assigned
91 to the same group, i.e. \f$ c_i\equiv c_{\text{out},j} \f$ . A functor \a groupDefFunc assigns a group \f$ c_{\text{out},j} \f$ to each category value \f$ c_i \f$ .
92 \ingroup jkqtptools_math_statistics_grouped
93
94 \tparam InputCatIt standard iterator type of \a inFirstCat and \a inLastCat
95 \tparam InputValueIt standard iterator type of \a inFirstValue and \a inLastValue
96 \param inFirstCat iterator pointing to the first item in the category dataset to use \f$ c_1 \f$
97 \param inLastCat iterator pointing behind the last item in the category dataset to use \f$ c_N \f$
98 \param inFirstValue iterator pointing to the first item in the category dataset to use \f$ v_1 \f$
99 \param inLastValue iterator pointing behind the last item in the category dataset to use \f$ v_N \f$
100 \param[out] groupeddata receives the grouped data, each key in the map represents one group, each map-value contains a vector with the value data
101 \param groupDefFunc assigns a group \f$ c_{\text{out},j} \f$ to each category value \f$ c_i \f$ .
102
103 \note the contents of \a groupeddata is not cleared before usage, so you can also use this fucntion to append to a group!
104
105 \see JKQTPStatGroupDefinitionFunctor1D, \ref JKQTPlotterBasicJKQTPDatastoreStatisticsGroupedStat
106*/
107template <class InputCatIt, class InputValueIt>
108inline void jkqtpstatGroupData(InputCatIt inFirstCat, InputCatIt inLastCat, InputValueIt inFirstValue, InputValueIt inLastValue, std::map<double, std::vector<double> >& groupeddata, JKQTPStatGroupDefinitionFunctor1D groupDefFunc=&jkqtpstatGroupingIdentity1D) {
109 auto inCat=inFirstCat;
110 auto inVal=inFirstValue;
111 for (; inCat!=inLastCat && inVal!=inLastValue; ++inCat, ++inVal) {
112 const double c=jkqtp_todouble(*inCat);
113 const double v=jkqtp_todouble(*inVal);
114 if (JKQTPIsOKFloat(c) && JKQTPIsOKFloat(v)) {
115 const double g=groupDefFunc(c);
116 groupeddata[g].push_back(v);
117 }
118 }
119}
120
121/*! \brief groups data from an input range \a inFirstCat / \a inFirstValue ... \a inLastCat / \a outFirstCat representing pairs \f$ (c_i,v_i) \f$ of a
122 category value \f$ c_i \f$ and a group value \f$ v_i \f$ into groups \f$ V_j=\{v_{i}|c_i\equiv c_{\text{out},j}\} \f$ of data that were assigned
123 to the same group, i.e. \f$ c_i\equiv c_{\text{out},j} \f$ . A functor \a groupDefFunc assigns a group \f$ c_{\text{out},j} \f$ to each category value \f$ c_i \f$ .
124 \ingroup jkqtptools_math_statistics_grouped
125
126 \tparam InputCatIt standard iterator type of \a inFirstCat and \a inLastCat
127 \tparam InputValueIt standard iterator type of \a inFirstValue and \a inLastValue
128 \tparam OutputGroupIt standard output iterator type of \a outFirstCategory
129 \param inFirstCat iterator pointing to the first item in the category dataset to use \f$ c_1 \f$
130 \param inLastCat iterator pointing behind the last item in the category dataset to use \f$ c_N \f$
131 \param inFirstValue iterator pointing to the first item in the category dataset to use \f$ v_1 \f$
132 \param inLastValue iterator pointing behind the last item in the category dataset to use \f$ v_N \f$
133 \param[out] outFirstCategory for each element in the range \a inFirstCat / \a inFirstValue ... \a inLastCat / \a outFirstCat this receives the calculated category
134 \param[out] groupeddata receives the grouped data, each key in the map represents one group, each map-value contains a vector with the value data
135 \param groupDefFunc assigns a group \f$ c_{\text{out},j} \f$ to each category value \f$ c_i \f$ .
136
137 \note the contents of \a groupeddata is not cleared before usage, so you can also use this fucntion to append to a group!
138
139 \see JKQTPStatGroupDefinitionFunctor1D, \ref JKQTPlotterBasicJKQTPDatastoreStatisticsGroupedStat
140*/
141template <class InputCatIt, class InputValueIt, class OutputGroupIt>
142inline void jkqtpstatGroupData(InputCatIt inFirstCat, InputCatIt inLastCat, InputValueIt inFirstValue, InputValueIt inLastValue, OutputGroupIt outFirstCategory, std::map<double, std::vector<double> >& groupeddata, JKQTPStatGroupDefinitionFunctor1D groupDefFunc=&jkqtpstatGroupingIdentity1D) {
143 auto inCat=inFirstCat;
144 auto inVal=inFirstValue;
145 auto outCat=outFirstCategory;
146 for (; inCat!=inLastCat && inVal!=inLastValue; ++inCat, ++inVal) {
147 const double c=jkqtp_todouble(*inCat);
148 const double v=jkqtp_todouble(*inVal);
149 if (JKQTPIsOKFloat(c) && JKQTPIsOKFloat(v)) {
150 const double g=groupDefFunc(c);
151 groupeddata[g].push_back(v);
152 *outCat=g;
153 } else {
154 *outCat=JKQTP_DOUBLE_NAN;
155 }
156 ++outCat;
157 }
158}
159
160
161/*! \brief groups data from an input range \a inFirstCat / \a inFirstValue ... \a inLastCat / \a outFirstCat representing pairs \f$ (c_i,v_i) \f$ of a
162 category value \f$ c_i \f$ and a group value \f$ v_i \f$ into groups \f$ V_j=\{v_{i}|c_i\equiv c_{\text{out},j}\} \f$ of data that were assigned
163 to the same group, i.e. \f$ c_i\equiv c_{\text{out},j} \f$ . A functor \a groupDefFunc assigns a group \f$ c_{\text{out},j} \f$ to each category value \f$ c_i \f$ .
164 \ingroup jkqtptools_math_statistics_grouped
165
166 \tparam InputCatIt standard iterator type of \a inFirstCat and \a inLastCat
167 \tparam InputValueIt standard iterator type of \a inFirstValue and \a inLastValue
168 \param inFirstCat iterator pointing to the first item in the category dataset to use \f$ c_1 \f$
169 \param inLastCat iterator pointing behind the last item in the category dataset to use \f$ c_N \f$
170 \param inFirstValue iterator pointing to the first item in the category dataset to use \f$ v_1 \f$
171 \param inLastValue iterator pointing behind the last item in the category dataset to use \f$ v_N \f$
172 \param[out] groupeddata receives the grouped data, each key in the map represents one group, each map-value contains two vecors with the category and value data respectively
173 \param groupDefFunc assigns a group \f$ c_{\text{out},j} \f$ to each category value \f$ c_i \f$ .
174
175 \note the contents of \a groupeddata is not cleared before usage, so you can also use this fucntion to append to a group!
176
177 \see JKQTPStatGroupDefinitionFunctor1D, \ref JKQTPlotterBasicJKQTPDatastoreStatisticsGroupedStat
178*/
179template <class InputCatIt, class InputValueIt>
180inline void jkqtpstatGroupData(InputCatIt inFirstCat, InputCatIt inLastCat, InputValueIt inFirstValue, InputValueIt inLastValue, std::map<double, std::pair<std::vector<double>,std::vector<double> > >& groupeddata, JKQTPStatGroupDefinitionFunctor1D groupDefFunc=&jkqtpstatGroupingIdentity1D) {
181 auto inCat=inFirstCat;
182 auto inVal=inFirstValue;
183 for (; inCat!=inLastCat && inVal!=inLastValue; ++inCat, ++inVal) {
184 const double c=jkqtp_todouble(*inCat);
185 const double v=jkqtp_todouble(*inVal);
186 if (JKQTPIsOKFloat(c) && JKQTPIsOKFloat(v)) {
187 const double g=groupDefFunc(c);
188 groupeddata[g].first.push_back(c);
189 groupeddata[g].second.push_back(v);
190 }
191 }
192}
193
194
195/*! \brief groups data from an input range \a inFirstCat / \a inFirstValue ... \a inLastCat / \a outFirstCat representing pairs \f$ (c_i,v_i) \f$ of a
196 category value \f$ c_i \f$ and a group value \f$ v_i \f$ into groups \f$ V_j=\{v_{i}|c_i\equiv c_{\text{out},j}\} \f$ of data that were assigned
197 to the same group, i.e. \f$ c_i\equiv c_{\text{out},j} \f$ . A functor \a groupDefFunc assigns a group \f$ c_{\text{out},j} \f$ to each category value \f$ c_i \f$ .
198 \ingroup jkqtptools_math_statistics_grouped
199
200 \tparam InputCatIt standard iterator type of \a inFirstCat and \a inLastCat
201 \tparam InputValueIt standard iterator type of \a inFirstValue and \a inLastValue
202 \tparam OutputGroupIt standard output iterator type of \a outFirstCategory
203 \param inFirstCat iterator pointing to the first item in the category dataset to use \f$ c_1 \f$
204 \param inLastCat iterator pointing behind the last item in the category dataset to use \f$ c_N \f$
205 \param inFirstValue iterator pointing to the first item in the category dataset to use \f$ v_1 \f$
206 \param inLastValue iterator pointing behind the last item in the category dataset to use \f$ v_N \f$
207 \param[out] outFirstCategory for each element in the range \a inFirstCat / \a inFirstValue ... \a inLastCat / \a outFirstCat this receives the calculated category
208 \param[out] groupeddata receives the grouped data, each key in the map represents one group, each map-value contains two vecors with the category and value data respectively
209 \param groupDefFunc assigns a group \f$ c_{\text{out},j} \f$ to each category value \f$ c_i \f$ .
210
211 \note the contents of \a groupeddata is not cleared before usage, so you can also use this fucntion to append to a group!
212
213 \see JKQTPStatGroupDefinitionFunctor1D, \ref JKQTPlotterBasicJKQTPDatastoreStatisticsGroupedStat
214*/
215template <class InputCatIt, class InputValueIt, class OutputGroupIt>
216inline void jkqtpstatGroupData(InputCatIt inFirstCat, InputCatIt inLastCat, InputValueIt inFirstValue, InputValueIt inLastValue, OutputGroupIt outFirstCategory, std::map<double, std::pair<std::vector<double>,std::vector<double> > >& groupeddata, JKQTPStatGroupDefinitionFunctor1D groupDefFunc=&jkqtpstatGroupingIdentity1D) {
217 auto inCat=inFirstCat;
218 auto inVal=inFirstValue;
219 auto outCat=outFirstCategory;
220 for (; inCat!=inLastCat && inVal!=inLastValue; ++inCat, ++inVal) {
221 const double c=jkqtp_todouble(*inCat);
222 const double v=jkqtp_todouble(*inVal);
223 if (JKQTPIsOKFloat(c) && JKQTPIsOKFloat(v)) {
224 const double g=groupDefFunc(c);
225 groupeddata[g].first.push_back(c);
226 groupeddata[g].second.push_back(v);
227 *outCat=g;
228 } else {
229 *outCat=JKQTP_DOUBLE_NAN;
230 }
231 ++outCat;
232 }
233}
234
235#endif // JKQTPSTATGROUPED_H_INCLUDED
236
237
#define jkqtmath_LIB_EXPORT
Definition jkqtmath_imexport.h:87
#define JKQTP_DOUBLE_NAN
double-value NotANumber
Definition jkqtpmathtools.h:77
constexpr double jkqtp_todouble(const T &d)
converts a boolean to a double, is used to convert boolean to double by JKQTPDatastore
Definition jkqtpmathtools.h:113
bool JKQTPIsOKFloat(T v)
check whether the dlotaing point number is OK (i.e. non-inf, non-NAN)
Definition jkqtpmathtools.h:496
jkqtmath_LIB_EXPORT double jkqtpstatGroupingRound1D(double v)
use a rounded column value as group ID directly
jkqtmath_LIB_EXPORT double jkqtpstatGroupingIdentity1D(double v)
use a column value as group ID directly
std::function< double(double)> JKQTPStatGroupDefinitionFunctor1D
a functor which assignes a value to a group center
Definition jkqtpstatgrouped.h:54
jkqtmath_LIB_EXPORT JKQTPStatGroupDefinitionFunctor1D jkqtpstatMakeGroupingCustomRound1D(double firstGroupCenter, double groupWidth)
generates a functor of jkqtpstatGroupingCustomRound1D() with the two paramaters firstGroupCenter and ...
void jkqtpstatGroupData(InputCatIt inFirstCat, InputCatIt inLastCat, InputValueIt inFirstValue, InputValueIt inLastValue, std::map< double, std::vector< double > > &groupeddata, JKQTPStatGroupDefinitionFunctor1D groupDefFunc=&jkqtpstatGroupingIdentity1D)
groups data from an input range inFirstCat / inFirstValue ... inLastCat / outFirstCat representing pa...
Definition jkqtpstatgrouped.h:108
jkqtmath_LIB_EXPORT double jkqtpstatGroupingCustomRound1D(double v, double firstGroupCenter, double groupWidth)
assign each value to groups , , , , ...