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 "utils.hpp"
10 
11 namespace wdm {
12 
13 namespace impl {
14 
18 inline double bbeta(const std::vector<double>& x,
19  const std::vector<double>& y,
20  std::vector<double> weights = std::vector<double>())
21 {
22  utils::check_sizes(x, y, weights);
23  size_t n = x.size();
24 
25  // find the medians
26  double med_x = impl::median(x, weights);
27  double med_y = impl::median(y, weights);
28 
29  if (weights.size() == 0)
30  weights = std::vector<double>(n, 1.0);
31 
32  // count elements in lower left and upper right quadrants
33  double w_acc{0.0};
34  for (size_t i = 0; i < n; i++) {
35  if ((x[i] <= med_x) && (y[i] <= med_y))
36  w_acc += weights[i];
37  else if ((x[i] > med_x) && (y[i] > med_y))
38  w_acc += weights[i];
39  }
40 
41  return 2 * w_acc / utils::sum(weights) - 1;
42 }
43 
44 }
45 
46 }
Weighted dependence measures.
Definition: bbeta.hpp:11