bbeta.hpp
1 // Copyright © 2020 Thomas Nagler
2 //
3 // This file is part of the wdm library and licensed under the terms of
4 // the MIT license. For a copy, see the LICENSE file in the root directory
5 // or https://github.com/tnagler/wdm/blob/master/LICENSE.
6 
7 #pragma once
8 
9 #include "ranks.hpp"
10 #include "utils.hpp"
11 
12 namespace wdm {
13 
14 namespace impl {
15 
19 inline double
20 bbeta(const std::vector<double>& x,
21  const std::vector<double>& y,
22  std::vector<double> weights = std::vector<double>())
23 {
24  utils::check_sizes(x, y, weights);
25  size_t n = x.size();
26 
27  // find the medians
28  double med_x = impl::median(x, weights);
29  double med_y = impl::median(y, weights);
30 
31  if (weights.size() == 0)
32  weights = std::vector<double>(n, 1.0);
33 
34  // count elements in lower left and upper right quadrants
35  double w_acc{ 0.0 };
36  for (size_t i = 0; i < n; i++) {
37  if ((x[i] <= med_x) && (y[i] <= med_y))
38  w_acc += weights[i];
39  else if ((x[i] > med_x) && (y[i] > med_y))
40  w_acc += weights[i];
41  }
42 
43  return 2 * w_acc / utils::sum(weights) - 1;
44 }
45 
46 }
47 
48 }
Weighted dependence measures.
Definition: wdm.hpp:19