9 #include "wdm/ktau.hpp" 10 #include "wdm/hoeffd.hpp" 11 #include "wdm/prho.hpp" 12 #include "wdm/srho.hpp" 13 #include "wdm/bbeta.hpp" 14 #include "wdm/methods.hpp" 15 #include "wdm/nan_handling.hpp" 36 inline double wdm(std::vector<double> x,
37 std::vector<double> y,
39 std::vector<double> weights = std::vector<double>(),
40 bool remove_missing =
true)
42 utils::check_sizes(x, y, weights);
44 if (utils::preproc(x, y, weights, method, remove_missing) ==
"return_nan")
45 return std::numeric_limits<double>::quiet_NaN();
47 if (methods::is_hoeffding(method))
48 return impl::hoeffd(x, y, weights);
49 if (methods::is_kendall(method))
50 return impl::ktau(x, y, weights);
51 if (methods::is_pearson(method))
52 return impl::prho(x, y, weights);
53 if (methods::is_spearman(method))
54 return impl::srho(x, y, weights);
55 if (methods::is_blomqvist(method))
56 return impl::bbeta(x, y, weights);
57 throw std::runtime_error(
"method not implemented.");
88 std::vector<double> y,
90 std::vector<double> weights = std::vector<double>(),
91 bool remove_missing =
true,
96 utils::check_sizes(x, y, weights);
97 if (utils::preproc(x, y, weights, method, remove_missing) ==
"return_nan") {
98 n_eff_ = utils::effective_sample_size(x.size(), weights);
99 estimate_ = std::numeric_limits<double>::quiet_NaN();
100 statistic_ = std::numeric_limits<double>::quiet_NaN();
101 p_value_ = std::numeric_limits<double>::quiet_NaN();
103 n_eff_ = utils::effective_sample_size(x.size(), weights);
104 estimate_ =
wdm(x, y, method, weights,
false);
105 statistic_ = compute_test_stat(estimate_, method, n_eff_, x, y, weights);
106 p_value_ = compute_p_value(statistic_, method,
alternative, n_eff_);
111 std::string
method()
const {
return method_;}
117 double n_eff()
const {
return n_eff_;}
130 inline double compute_test_stat(
double estimate,
133 const std::vector<double>& x,
134 const std::vector<double>& y,
135 const std::vector<double>& weights)
139 estimate = 1 - 1e-12;
140 if (estimate == -1.0)
144 if (methods::is_hoeffding(method)) {
145 stat = estimate / 30.0 + 1.0 / (36.0 *
n_eff);
146 }
else if (methods::is_kendall(method)) {
147 stat = estimate * impl::ktau_stat_adjust(x, y, weights);
148 }
else if (methods::is_pearson(method)) {
149 stat = std::atanh(estimate) * std::sqrt(n_eff - 3);
150 }
else if (methods::is_spearman(method)) {
151 stat = std::atanh(estimate) * std::sqrt((n_eff - 3) / 1.06);
152 }
else if (methods::is_blomqvist(method)) {
153 stat = std::atanh(estimate) * std::sqrt(n_eff);
155 throw std::runtime_error(
"method not implemented.");
161 inline double compute_p_value(
double statistic,
167 if (methods::is_hoeffding(method)) {
169 throw std::runtime_error(
"must provide n_eff for method 'hoeffd'.");
170 if (alternative !=
"two-sided")
171 throw std::runtime_error(
"only two-sided test available for Hoeffding's D.");
172 p_value = impl::phoeffb(statistic, n_eff);
174 if (alternative ==
"two-sided") {
175 p_value = 2 * utils::normalCDF(-std::abs(statistic));
176 }
else if (alternative ==
"less") {
177 p_value = utils::normalCDF(statistic);
178 }
else if (alternative ==
"greater") {
179 p_value = 1 - utils::normalCDF(statistic);
181 throw std::runtime_error(
"alternative not implemented.");
189 std::string alternative_;
double wdm(const Eigen::VectorXd &x, const Eigen::VectorXd &y, std::string method, Eigen::VectorXd weights=Eigen::VectorXd(), bool remove_missing=true)
Definition: eigen.hpp:42
double n_eff() const
the effective sample size in the test
Definition: wdm.hpp:117
std::string alternative() const
the alternative hypothesis used for the test
Definition: wdm.hpp:114
double statistic() const
the test statistic
Definition: wdm.hpp:123
double estimate() const
the estimated dependence measure
Definition: wdm.hpp:120
std::string method() const
the method used for the test
Definition: wdm.hpp:111
Weighted dependence measures.
Definition: bbeta.hpp:11
double p_value() const
the p-value
Definition: wdm.hpp:126
Indep_test(std::vector< double > x, std::vector< double > y, std::string method, std::vector< double > weights=std::vector< double >(), bool remove_missing=true, std::string alternative="two-sided")
Definition: wdm.hpp:87