eigen.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 "../wdm.hpp"
10 #include <Eigen/Dense>
11 
12 namespace wdm {
13 
14 namespace utils {
15 
16 inline std::vector<double>
17 convert_vec(const Eigen::VectorXd& x)
18 {
19  std::vector<double> xx(x.size());
20  if (x.size() > 0)
21  Eigen::VectorXd::Map(&xx[0], x.size()) = x;
22  return xx;
23 }
24 }
25 
35 inline double
36 wdm(const Eigen::VectorXd& x,
37  const Eigen::VectorXd& y,
38  std::string method,
39  Eigen::VectorXd weights = Eigen::VectorXd(),
40  bool remove_missing = true,
41  std::vector<int> seeds = std::vector<int>())
42 {
43  return wdm(utils::convert_vec(x),
44  utils::convert_vec(y),
45  method,
46  utils::convert_vec(weights),
47  remove_missing,
48  seeds);
49 }
50 
63 inline Eigen::MatrixXd
64 wdm(const Eigen::MatrixXd& x,
65  std::string method,
66  Eigen::VectorXd weights = Eigen::VectorXd(),
67  bool remove_missing = true,
68  std::vector<int> seeds = std::vector<int>())
69 {
70  size_t d = x.cols();
71  if (d == 1)
72  throw std::runtime_error("x must have at least 2 columns.");
73 
74  Eigen::MatrixXd ms = Eigen::MatrixXd::Identity(d, d);
75  for (size_t i = 0; i < d; i++) {
76  for (size_t j = i + 1; j < d; j++) {
77  ms(i, j) = wdm(utils::convert_vec(x.col(i)),
78  utils::convert_vec(x.col(j)),
79  method,
80  utils::convert_vec(weights),
81  remove_missing,
82  seeds);
83  if (methods::is_chatterjee(method)) {
84  ms(j, i) = wdm(utils::convert_vec(x.col(j)),
85  utils::convert_vec(x.col(i)),
86  method,
87  utils::convert_vec(weights),
88  remove_missing,
89  seeds);
90  } else {
91  ms(j, i) = ms(i, j);
92  }
93  }
94  }
95 
96  return ms;
97 }
98 
99 }
Weighted dependence measures.
Definition: wdm.hpp:19
double wdm(std::vector< double > x, std::vector< double > y, std::string method, std::vector< double > weights=std::vector< double >(), bool remove_missing=true, std::vector< int > seeds=std::vector< int >())
Definition: wdm.hpp:49