22 inline std::vector<double> rank(
23 std::vector<double> x,
24 std::vector<double> weights = std::vector<double>(),
25 std::string ties_method =
"min")
28 if (weights.size() == 0)
29 weights = std::vector<double>(n, 1.0);
31 std::vector<size_t> perm = utils::get_order(x);
33 double w_acc = 0.0, w_batch;
34 if ((ties_method !=
"min") && (ties_method !=
"average"))
35 throw std::runtime_error(
"ties_method must be either 'min' or 'average.");
36 for (
size_t i = 0, reps; i < n; i += reps) {
40 while ((i + reps < n) && (x[perm[i]] == x[perm[i + reps]])) {
41 w_batch += weights[perm[i]];
46 for (
size_t k = 0; k < reps; ++k) {
47 if (ties_method ==
"min")
48 x[perm[i + k]] = w_acc;
50 x[perm[i + k]] = w_acc + w_batch / 2.0;
54 for (
size_t k = 0; k < reps; ++k)
55 w_acc += weights[perm[i + k]];
65 inline std::vector<double> bivariate_rank(
66 std::vector<double> x,
67 std::vector<double> y,
68 std::vector<double> weights = std::vector<double>())
70 utils::check_sizes(x, y, weights);
73 std::vector<size_t> perm_x = utils::get_order(x);
74 perm_x = utils::invert_permutation(perm_x);
77 utils::sort_all(x, y, weights);
80 std::vector<size_t> perm_y = utils::get_order(y,
false);
81 perm_y = utils::invert_permutation(perm_y);
84 std::vector<double> counts(y.size(), 0.0);
85 utils::merge_sort_count_per_element(y, weights, counts);
88 std::vector<double> counts_tmp = counts;
89 for (
size_t i = 0; i < counts.size(); i++)
90 counts[i] = counts_tmp[perm_y[perm_x[i]]];
97 inline double median(
const std::vector<double>& x,
98 std::vector<double> weights = std::vector<double>())
100 utils::check_sizes(x, x, weights);
104 auto perm = utils::get_order(x);
107 for (
size_t i = 0; i < n; i++) {
110 w[i] = weights[perm[i]];
114 auto ranks = rank(xx, w,
"average");
115 if (weights.size() == 0)
116 weights = std::vector<double>(n, 1.0);
117 double rank_avrg = utils::perm_sum(weights, 2) / utils::sum(weights);
121 while (ranks[i] < rank_avrg) i++;
122 if (ranks[i] == rank_avrg)
125 return 0.5 * (xx[i - 1] + xx[i]);
Weighted dependence measures.
Definition: bbeta.hpp:11