-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistrib.cpp
More file actions
84 lines (60 loc) · 1.71 KB
/
Copy pathdistrib.cpp
File metadata and controls
84 lines (60 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "distrib.h"
Distrib::Distrib(int cFr)
: cFractions(cFr)
, arSuPoints(new double[cFr]) {
for (int ii = 0; ii < cFr; ++ii)
arSuPoints[ii] = 0.;
}
double Distrib::getDensityByProbabilityX(double pX) const {
int iPos = indexFraction(pX);
double ptProbX = getProbabilityX(iPos);
auto fn = [&](int i1, int i2) -> double {
double x1 = getProbabilityX(i1);
double x2 = getProbabilityX(i2);
double d1 = getDensity(i1);
double d2 = getDensity(i2);
return (pX - x1) / (x2 - x1) * (d2 - d1) + d1;
};
if (pX < ptProbX) {
if (!iPos)
return getDensity(iPos);
return fn(iPos-1, iPos);
}
if (pX > ptProbX) {
if (iPos == cFractions-1)
return getDensity(iPos);
return fn(iPos, iPos+1);
}
return getDensity(iPos);
}
int Distrib::indexFraction(double pX) const {
// pX: 0..1
int iPos = lround(floor(pX * cFractions));
if (iPos < 0) iPos = 0;
if (iPos >= cFractions) iPos = cFractions-1;
return iPos;
}
void Distrib::addValue(double pX, double v) {
arSuPoints[indexFraction(pX)] += v;
suAll += v;
}
double Distrib::probabilityX_byArea(double area) const {
// area: 0..1
if (area <= 0)
return 0.;
double suArea = 0;
for (int ii = 0; ii < cFractions; ++ii) {
double p = getAreaOfPoint(ii);
if (suArea + p > area)
return
FDIV(ii, cFractions) + 1. / cFractions * (area - suArea) / p;
suArea += p;
}
return 1.;
}
double Distrib::maxDensity() const {
double maProb = 0;
for (int ii = 0; ii < cFractions; ++ii)
maProb = std::max(maProb, getDensity(ii));
return maProb;
}