-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorrelation_analysis.cpp
41 lines (31 loc) · 1.36 KB
/
correlation_analysis.cpp
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
#include <cmath>
#include <numeric>
#include "./includes/correlation_analysis.hpp"
CorrelationAnalysis::CorrelationAnalysis(PriceHistory &historyA, PriceHistory &historyB)
: historyA(historyA), historyB(historyB) {}
double CorrelationAnalysis::calculateCorrelation() {
size_t minDataPoints = std::min(historyA.dataPointsCount(), historyB.dataPointsCount());
std::vector<double> pricesA;
std::vector<double> pricesB;
for (size_t i = 0; i < minDataPoints; ++i) {
pricesA.push_back(historyA.getDataPoint(i).getClosing());
pricesB.push_back(historyB.getDataPoint(i).getClosing());
}
double meanA = std::accumulate(pricesA.begin(), pricesA.end(), 0.0) / pricesA.size();
double meanB = std::accumulate(pricesB.begin(), pricesB.end(), 0.0) / pricesB.size();
double covariance = 0.0;
double varianceA = 0.0;
double varianceB = 0.0;
for (size_t i = 0; i < minDataPoints; ++i) {
double deviationA = pricesA[i] - meanA;
double deviationB = pricesB[i] - meanB;
covariance += deviationA * deviationB;
varianceA += deviationA * deviationA;
varianceB += deviationB * deviationB;
}
covariance /= minDataPoints;
varianceA /= minDataPoints;
varianceB /= minDataPoints;
double correlation = covariance / (std::sqrt(varianceA) * std::sqrt(varianceB));
return correlation;
}