-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStatistics.cpp
More file actions
121 lines (105 loc) · 3.04 KB
/
Statistics.cpp
File metadata and controls
121 lines (105 loc) · 3.04 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
/*! \class Statistics
\brief A class of static methods for calculating statistical information.
*/
#include <cmath>
#include "Statistics.h"
#include "AdditionalMethods.h"
#include "DataObject.h"
#include <time.h>
#include <stdlib.h>
#include <iostream>
double Statistics::getAverage(ObjectMatrix om, int k)
{
double average;
int n = om.getObjectCount();
double s = 0.0;
for (int i = 0; i < n; i++)
s += om.getObjectAt(i).getFeatureAt(k);
average = s / n;
return average;
}
std::vector<double> Statistics::getAverageColumns(ObjectMatrix om)
{
std::vector<double> columnSum;
int n = om.getObjectCount(), i, j;
int m = om.getObjectAt(0).getFeatureCount();
std::vector<double> average;
average.reserve(m);
double s, oCaunt = float(n);
for (i = 0; i < m; i++) //hold features
{
s = 0.0;
for (j = 0; j < n; j++) //for each object
s += om.getObjectAt(j).getFeatureAt(i);
average.push_back(s / oCaunt);
}
return average;
}
double Statistics::getCorrCoef(ObjectMatrix om, int k, int l)
{
double to_return = 0.0;
double avgFeatureK = Statistics::getAverage(om, k);
double avgFeatureL = Statistics::getAverage(om, l);
double fractionTop = 0.0, fractionBottom = 0.1, tmp1 = 0.0, tmp2 = 0.0, diffK, diffL;
int n = om.getObjectCount();
for (int i = 0; i < n; i++)
{
diffK = om.getObjectAt(i).getFeatureAt(k) - avgFeatureK;
diffL = om.getObjectAt(i).getFeatureAt(l) - avgFeatureL;
fractionTop += diffK * diffL;
tmp1 += std::pow(diffK, 2);
tmp2 += std::pow(diffL, 2);
}
fractionBottom = std::sqrt(tmp1 * tmp2);
to_return = fractionTop / fractionBottom;
return to_return;
}
double Statistics::getCovCoef(ObjectMatrix om, int k, int l)
{
double to_return = 0.0;
double avgFeatureK = Statistics::getAverage(om, k);
double avgFeatureL = Statistics::getAverage(om, l);
double diffK, diffL;
int n = om.getObjectCount();
for (int i = 0; i < n; i++)
{
diffK = om.getObjectAt(i).getFeatureAt(k) - avgFeatureK;
diffL = om.getObjectAt(i).getFeatureAt(l) - avgFeatureL;
to_return += diffK * diffL;
}
to_return = to_return / (n - 1);
return to_return;
}
double Statistics::getRandom()
{
//srand(time(NULL) + AdditionalMethods::PID);
return (double)rand()/RAND_MAX;
}
double Statistics::getRandom(double min, double max)
{
return min + Statistics::getRandom() * (max - min);
}
ObjectMatrix Statistics::getCovMatrix(ObjectMatrix om)
{
double tmp = 0.0;
std::vector<double> dd;
int n = om.getObjectAt(0).getFeatureCount();
ObjectMatrix cov(n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
tmp = Statistics::getCovCoef(om, i, j);
dd.push_back(tmp);
}
DataObject dobj(dd);
cov.addObject(dobj);
dd.clear();
}
return cov;
}
void Statistics::initSeed()
{
srand(time(NULL) + AdditionalMethods::PID);
//std::cout << rand() << " ";
}