-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_correlation.cpp
More file actions
134 lines (113 loc) · 4.58 KB
/
data_correlation.cpp
File metadata and controls
134 lines (113 loc) · 4.58 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
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <limits>
#include "data.h"
// Calculate Pearson correlation coefficient
double SharedData::pearsonCorrelation(const std::vector<int>& series1, const std::vector<int>& series2) {
// if (series1.size() != series2.size()) {
// std::cerr << "Error: Time series must have the same length." << std::endl;
// return 0;
// }
int n = series1.size();
double mean1 = 0, mean2 = 0;
double numerator = 0, denominator1 = 0, denominator2 = 0;
// Calculate mean
for (int i = 0; i < n; ++i) {
mean1 += series1[i];
mean2 += series2[i];
}
mean1 /= n;
mean2 /= n;
// Calculate numerator and denominator of correlation coefficient
for (int i = 0; i < n; ++i) {
double diff1 = series1[i] - mean1;
double diff2 = series2[i] - mean2;
numerator += diff1 * diff2;
denominator1 += diff1 * diff1;
denominator2 += diff2 * diff2;
}
return numerator / std::sqrt(denominator1 * denominator2);
}
std::vector<std::vector<double>> SharedData::Count_Correlation(const std::vector<std::vector<int>>& timeSeries) {
std::vector<std::vector<double>> correlationMatrix (M + 1 , std::vector<double>(M + 1, 0));
//int numSeries = timeSeries.size();
// Calculate correlation matrix
std::vector<std::vector<int>> correlatd_pairs;
for (int i = 1; i <= M; ++i) {
for (int j = i + 1; j <= M; ++j) {
correlationMatrix[i][j] = correlationMatrix[j][i] = pearsonCorrelation(timeSeries[i-1], timeSeries[j-1]);
}
}
return correlationMatrix;
}
std::pair<int, int> SharedData::findMaxCorrelationPair(const std::vector<std::vector<double>>& correlationMatrix, std::vector<bool>& paired) {
int maxM = -1, maxN = -1;
double maxCorrelation = std::numeric_limits<double>::lowest();
for (int i = 1; i <= M; ++i) {
for (int j = 1; j <= M; ++j) {
if (i != j && !paired[i] && !paired[j] && correlationMatrix[i][j] > maxCorrelation) {
maxCorrelation = correlationMatrix[i][j];
maxM = i;
maxN = j;
}
}
}
return {maxM, maxN};
}
// void SharedData::Allocated() {
// std::vector<std::vector<double>> correlationMatrix = Count_Correlation(fre_read);// Calculate correlation matrix
// std::vector<int> max_correlation_pairs(M + 1, 0);
// std::vector<int> tag_paired(M + 1, 0);// Record paired tags
// for (int i = 1; i <= M; i++) {
// double tag_max_correlation = 0;
// for (int j = 1; j <= M && !tag_paired[j] && j!=i; j++) {
// double current_correlation = correlationMatrix[i][j];
// if(current_correlation > tag_max_correlation) {
// tag_max_correlation = current_correlation;
// max_correlation_pairs[i] = j;
// tag_paired[i] = 1;
// }
// }
// }
// // Assign values to disk partition array based on pre-processed data
// int total_capacity = 0;
// for (int m = 1; m <= M ; m++) {
// //int tag_total_write=0;
// int max_diff = 0;
// for (int j = 0; j < (T - 1) / FrePerSlicing + 1; j++) {
// int diff = fre_write[m-1][j] - fre_del[m-1][j];
// if (diff > max_diff) {
// max_diff = diff;
// }
// }
// tag_max_capacity[m] = max_diff;//in one slice
// total_capacity += tag_max_capacity[m];
// }
// std::vector<int> tag_capacity(M + 1, 1 ); //
// int available_space = static_cast<int>(V * (1 - buffer)); // Available disk space 1 - buffer
// int allocated_space = 0;
// for (int m = 1; m <= M; m++) {
// double ratio = static_cast<double>(tag_max_capacity[m]) / total_capacity;
// // Disk space occupied by each tag
// int space_for_tag = static_cast<int>(available_space * ratio);
// tag_capacity[m] = space_for_tag;
// }
// std::vector<int> allocated(M + 1,0);
// int count=0;
// int m = 1;
// while(count < M - 1) {
// int n = max_correlation_pairs[m];
// int total_capacity = tag_capacity[m] + tag_capacity[n];
// disk_partition[m][0] = allocated_space;
// disk_partition[m][1] = allocated_space + total_capacity;
// disk_partition[n][0] = allocated_space;
// disk_partition[n][0] = allocated_space + total_capacity;
// allocated[m] = 1;
// allocated[n] = 1;
// // Accumulate allocated space
// allocated_space += total_capacity;
// m = n;
// }
// }