21#ifndef JKQTPSTATHISTOGRAM_H_INCLUDED
22#define JKQTPSTATHISTOGRAM_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 "jkqtmath/jkqtpstatbasics.h"
72template <
class InputIt,
class OutputIt>
74 double minV=0, maxV=0;
76 jkqtpstatMinMax<InputIt>(first, last, minV, maxV,
nullptr,
nullptr, &N);
78 std::vector<double> histX;
79 std::vector<double> histY;
81 const double range=maxV-minV;
82 const double binw=range/
static_cast<double>(bins);
85 for (
int i=0; i<bins; i++) {
86 histX.push_back(minV+
static_cast<double>(i)*binw);
91 for (
auto it=first; it!=last; ++it) {
94 size_t b=jkqtp_bounded<size_t>(0,
static_cast<size_t>(floor((v-minV)/binw)), bins-1);
107 NNorm=
static_cast<double>(N);
110 for (
size_t i=0; i<histX.size(); i++) {
111 *histogramXOut=histX[i]+xoffset;
112 if (cummulative) h+=(histY[i]/NNorm);
113 else h=histY[i]/NNorm;
136template <
class InputIt,
class OutputIt>
138 double minV=0, maxV=0;
140 jkqtpstatMinMax<InputIt>(first, last, minV, maxV,
nullptr,
nullptr, &N);
142 std::vector<double> histX;
143 std::vector<double> histY;
145 const double range=maxV-minV;
146 const double binw=binWidth;
147 const int bins=
static_cast<int>(ceil(range/binWidth));
150 for (
int i=0; i<bins; i++) {
151 histX.push_back(minV+
static_cast<double>(i)*binw);
156 for (
auto it=first; it!=last; ++it) {
159 size_t b=jkqtp_bounded<size_t>(0,
static_cast<size_t>(floor((v-minV)/binw)), bins-1);
172 NNorm=
static_cast<double>(N);
175 for (
size_t i=0; i<histX.size(); i++) {
176 *histogramXOut=histX[i]+xoffset;
177 if (cummulative) h+=(histY[i]/NNorm);
178 else h=histY[i]/NNorm;
204template <
class InputIt,
class BinsInputIt,
class OutputIt>
206 double minV=0, maxV=0;
208 jkqtpstatMinMax<InputIt>(first, last, minV, maxV,
nullptr,
nullptr, &N);
210 std::vector<double> histX;
211 std::vector<double> histY;
215 for (
auto it=binsFirst; it!=binsLast; ++it) {
219 std::sort(histX.begin(), histX.end());
222 for (
auto it=first; it!=last; ++it) {
225 auto itb=std::lower_bound(histX.begin(), histX.end(), v);
226 size_t bin=jkqtp_bounded<size_t>(0,
static_cast<size_t>(std::abs(std::distance(histX.begin(), itb))), histY.size()-1);
235 NNorm=
static_cast<double>(N);
238 for (
size_t i=0; i<histX.size(); i++) {
242 if (i==0 && i+1<histX.size()) binw=histX[1]-histX[0];
243 else if (i==histX.size()-1 &&
static_cast<int>(i)-1>0) binw=histX[histX.size()-1]-histX[histX.size()-2];
244 else if (i<histX.size() && i+1<histX.size()) binw=histX[i+1]-histX[i];
249 *histogramXOut=histX[i]+xoffset;
250 if (cummulative) h+=(histY[i]/NNorm);
251 else h=histY[i]/NNorm;
286template <
class InputItX,
class InputItY,
class OutputIt>
287inline void jkqtpstatHistogram2D(InputItX firstX, InputItX lastX, InputItY firstY, InputItY lastY, OutputIt histogramImgOut,
double xmin,
double xmax,
double ymin,
double ymax,
size_t xbins=10,
size_t ybins=10,
bool normalized=
true) {
289 const double binwx=fabs(xmax-xmin)/
static_cast<double>(xbins);
290 const double binwy=fabs(ymax-ymin)/
static_cast<double>(ybins);
292 std::vector<double> hist;
293 std::fill_n(std::back_inserter(hist), xbins*ybins, 0.0);
299 for (; (itX!=lastX) && (itY!=lastY); ++itX, ++itY) {
303 const size_t bx=jkqtp_bounded<size_t>(0,
static_cast<size_t>(floor((vx-xmin)/binwx)), xbins-1);
304 const size_t by=jkqtp_bounded<size_t>(0,
static_cast<size_t>(floor((vy-ymin)/binwy)), ybins-1);
314 NNorm=
static_cast<double>(N);
316 std::transform(hist.begin(), hist.end(), histogramImgOut, [NNorm](
double v) { return v/NNorm; });