nan_handling.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 <limits>
10 #include <sstream>
11 
12 namespace wdm {
13 
14 namespace utils {
15 
16 inline void remove_incomplete(std::vector<double>& x,
17  std::vector<double>& y,
18  std::vector<double>& w)
19 {
20  // if observation conatins nan, move it to the end
21  size_t last = x.size() - 1;
22  for (size_t i = 0; i < last + 1; i++) {
23  bool row_has_nan = (std::isnan(x[i]) | std::isnan(y[i]));
24  if (w.size() > 0)
25  row_has_nan = (row_has_nan | std::isnan(w[i]));
26  if (row_has_nan) {
27  if (w.size() > 0)
28  std::swap(w[i], w[last]);
29  std::swap(x[i], x[last]);
30  std::swap(y[i--], y[last--]);
31  }
32  }
33 
34  x.resize(last + 1);
35  y.resize(last + 1);
36  if (w.size() > 0)
37  w.resize(last + 1);
38 }
39 
40 inline bool any_nan(const std::vector<double>& x) {
41  for (size_t i = 0; (i < x.size()); i++) {
42  if (std::isnan(x[i]))
43  return true;
44  }
45 
46  return false;
47 }
48 
49 inline std::string preproc(std::vector<double>& x,
50  std::vector<double>& y,
51  std::vector<double>& weights,
52  std::string method,
53  bool remove_missing)
54 {
55  size_t min_nobs = (method == "hoeffding") ? 5 : 2;
56  if (remove_missing) {
57  utils::remove_incomplete(x, y, weights);
58  if (x.size() < min_nobs)
59  return "return_nan";
60  } else {
61  std::stringstream msg;
62  if (utils::any_nan(x) | utils::any_nan(y) | utils::any_nan(weights)) {
63  msg << "there are missing values in the data; " <<
64  "try remove_missing = TRUE";
65  } else if (x.size() < min_nobs) {
66  msg << "need at least " << min_nobs << "observations.";
67  }
68  if (!msg.str().empty())
69  throw std::runtime_error(msg.str());
70  }
71 
72  return "continue";
73 }
74 
75 } // end utils
76 
77 } // end wdm
Weighted dependence measures.
Definition: bbeta.hpp:11