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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
| #include "calc.hpp"
#include <algorithm> // for std::count
#include <cmath> // for std::sqrt
#include <iostream>
#include <unordered_map>
#include <vector>
Calc::Calc(std::vector<std::vector<double>>& data) {
try {
cnt = data.size();
for (auto d : data) {
data_x.push_back(d[0]);
data_y.push_back(d[1]);
}
} catch (...) {
throw;
}
}
/**
* @brief ケンドール順位相関係数の計算
*
* @return RCC(double)
*/
double Calc::kendall() {
std::vector<double> rank_x; // ランク(X)
std::vector<double> rank_y; // ランク(Y)
int p = 0; // P
int q = 0; // Q
std::unordered_map<double, unsigned int> tai_x; // 同順位の数(X)
std::unordered_map<double, unsigned int> tai_y; // 同順位の数(Y)
double t_x; // Tx の sum 部分
double t_y; // Ty の sum 部分
double nn; // (n * n - n) / 2.0
double rcc = 0.0; // 順位相関係数
try {
// ランク付け
if (!rank_dbl(data_x, rank_x)) {
std::cout << "[ERROR] Failed to rank!" << std::endl;
return rcc;
}
if (!rank_dbl(data_y, rank_y)) {
std::cout << "[ERROR] Failed to rank!" << std::endl;
return rcc;
}
// P, Q 計算
if (!calc_pq(rank_x, rank_y, p, q)) {
std::cout << "[ERROR] Failed to calculate P, Q!" << std::endl;
return rcc;
}
// 同順位の数(2個以上のみ)
if (!count_tai(rank_x, tai_x)) {
std::cout << "[ERROR] Failed to count tais!" << std::endl;
return rcc;
}
if (!count_tai(rank_y, tai_y)) {
std::cout << "[ERROR] Failed to count tais!" << std::endl;
return rcc;
}
// Tx, Ty の sum 部分
t_x = sum_t(tai_x);
t_y = sum_t(tai_y);
// 順位相関係数
nn = (cnt * cnt - cnt) / 2.0;
rcc = (p - q) / (std::sqrt(nn - t_x) * std::sqrt(nn - t_y));
} catch (...) {
throw;
}
return rcc; // 計算成功
}
/**
* @brief ランク付け
*
* @param[ref] 元配列 ns (dbl)
* @param[ref] ランク配列 rs (int)
* @return 真偽(bool)
* @retval true 成功
* @retval false 失敗
*/
bool Calc::rank_dbl(std::vector<double>& ns, std::vector<double>& rs) {
unsigned int i;
unsigned int j;
unsigned int c;
try {
for (i = 0; i < cnt; i++) {
c = 0;
for (j = 0; j < cnt; j++)
if (ns[i] < ns[j]) c++;
rs.push_back(c + 1);
}
} catch (...) {
return false;
}
return true;
}
/**
* @brief P(x_s と x_t, y_s と y_t の大小関係が一致する組の数)
* (x_s = x_t or y_s = y_t は除く)
*
* @param[ref] ランク配列 rank_x (dbl)
* @param[ref] ランク配列 rank_y (dbl)
* @param[ref] P (int)
* @param[ref] Q (int)
* @return 真偽(bool)
* @retval true 成功
* @retval false 失敗
*/
bool Calc::calc_pq(
std::vector<double>& rank_x, std::vector<double>& rank_y, int& p, int& q) {
unsigned int i;
unsigned int j;
double w;
try {
for (i = 0; i < cnt - 1; i++) {
for (j = i + 1; j < cnt; j++) {
w = (rank_x[i] - rank_x[j]) * (rank_y[i] - rank_y[j]);
if (w > 0) p++;
if (w < 0) q++;
}
}
} catch (...) {
return false;
}
return true;
}
/**
* @brief 同順位の数
* (数が 2 未満は除去)
*
* @param[ref] ランク配列 rank (double)
* @param[ref] 同順位の数の連想配列 tai (<double, unsigned int>)
* @return 真偽(bool)
* @retval true 成功
* @retval false 失敗
*/
bool Calc::count_tai(
std::vector<double>& rank, std::unordered_map<double, unsigned int>& tai) {
unsigned int i;
try {
for (i = 0; i < cnt; i++) {
if (tai.find(rank[i]) == tai.end()) {
tai[rank[i]] = 1;
continue;
}
tai[rank[i]]++;
}
// 個数が 2 未満のものは削除
for (auto it = tai.begin(); it != tai.end();) {
if (it->second < 2) {
it = tai.erase(it);
} else {
++it;
}
}
} catch (...) {
return false;
}
return true;
}
/**
* @brief Tx, Ty の sum 部分
*
* @param[in] 同順位の数の連想配列 tai (<double, unsigned int>)
* @return Tx, Ty の sum
*/
double Calc::sum_t(std::unordered_map<double, unsigned int> tai) {
double s = 0.0;
try {
for (const auto [k, v] : tai) {
s += (v * v * v - v) / 2.0;
}
} catch (...) {
return s;
}
return s;
}
|