-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompoundStats.cpp
More file actions
122 lines (98 loc) · 2.36 KB
/
CompoundStats.cpp
File metadata and controls
122 lines (98 loc) · 2.36 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
StatClass::StatClass(float lb, float ub){
lowerBound = lb;
upperBound = ub;
midPoint = lb+ub/2;
frequency=0;
}
long StatClass::getLowerBound(){
return lowerBound;
}
long StatClass::getUpperBound(){
return upperBound;
}
long StatClass::getFrequency(){
return frequency;
}
float StatClass::getMidPoint(){
return midPoint;
}
float StatClass::getSum(){
return frequency *midPoint;
}
bool StatClass::ValueBelongs(float val){
//Checks if val belongs to [lb,ub)
return val<upperBound && val>=lowerBound;
}
bool StatClass::ValueBelongsAdd(float val){
if (ValueBelongs(val)){
Add(val);
return 1;
}
return 0;
}
void StatClass::Add(int value){
frequency++;
}
CompoundStats::CompoundStats(float MinVal, float MaxVal, int ClassCount){
minVal=MinVal;
maxVal=MaxVal;
classCount=ClassCount;
classWidth = (maxVal-minVal)/classCount;
valueCount = 0;
//init classes
classes = (StatClass**) malloc(sizeof(StatClass*) * classCount);
float lb=minVal,ub;
for(int i=0; i<classCount; i++){
ub = lb+classWidth;
classes[i] = (new StatClass(lb,ub));
lb=ub;
}
}
CompoundStats::~CompoundStats(){
for(int i=0; i<classCount; i++)
delete classes[i];
free(classes);
}
void CompoundStats::AddStat(float value){
for(int i =0; i< classCount; i++)
if (classes[i]->ValueBelongsAdd(value))
break;
valueCount++;
}
float CompoundStats::Mean(){
float sum=0;
for(int i =0; i< classCount; i++){
sum += classes[i]-> getSum();
}
return sum/valueCount;
}
float CompoundStats::Median(){
// the median group is the one whose accumulated freq (sum of freqs of groups before)
// would be the first one thats bigger than n/2 (half the entries)
float n2 = valueCount/2;
float accFreq=0, prevAccFreq=0, freq=0;
for(int i = 0; i < classCount; i++){
prevAccFreq = accFreq;
freq = classes[i]->getFrequency();
accFreq += freq;
if(accFreq>n2){
float lowerBound = classes[i]->getLowerBound();
return lowerBound + ( ( (n2 - prevAccFreq) /freq) * classWidth);
}
}
Serial.print("WTF!!!");
return 0.0f;
}
float CompoundStats::Mode(){
//the modal group is the one whose freq is higher
//TODO: implement
}
float CompoundStats::MeanDeviation(){
//TODO: implement
}
float CompoundStats::Variance(){
//TODO: implement
}
float CompoundStats::StandarDeviation(){
//TODO: implement
}