| 1 |
// (C) Copyright Matt Borland 2022. |
| 2 |
// Use, modification and distribution are subject to the |
| 3 |
// Boost Software License, Version 1.0. (See accompanying file |
| 4 |
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 |
|
| 6 |
#ifndef BOOST_MATH_STATISTICS_CHATTERJEE_CORRELATION_HPP |
| 7 |
#define BOOST_MATH_STATISTICS_CHATTERJEE_CORRELATION_HPP |
| 8 |
|
| 9 |
#include <cstdint> |
| 10 |
#include <cmath> |
| 11 |
#include <algorithm> |
| 12 |
#include <iterator> |
| 13 |
#include <vector> |
| 14 |
#include <limits> |
| 15 |
#include <utility> |
| 16 |
#include <type_traits> |
| 17 |
#include <boost/math/tools/assert.hpp> |
| 18 |
#include <boost/math/tools/config.hpp> |
| 19 |
#include <boost/math/statistics/detail/rank.hpp> |
| 20 |
|
| 21 |
#ifdef BOOST_MATH_EXEC_COMPATIBLE |
| 22 |
#include <execution> |
| 23 |
#include <future> |
| 24 |
#include <thread> |
| 25 |
#endif |
| 26 |
|
| 27 |
namespace boost { namespace math { namespace statistics { |
| 28 |
|
| 29 |
namespace detail { |
| 30 |
|
| 31 |
template <typename BDIter> |
| 32 |
std::size_t chatterjee_transform(BDIter begin, BDIter end) |
| 33 |
{ |
| 34 |
std::size_t sum = 0; |
| 35 |
|
| 36 |
while(++begin != end) |
| 37 |
{ |
| 38 |
if(*begin > *std::prev(begin)) |
| 39 |
{ |
| 40 |
sum += *begin - *std::prev(begin); |
| 41 |
} |
| 42 |
else |
| 43 |
{ |
| 44 |
sum += *std::prev(begin) - *begin; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
return sum; |
| 49 |
} |
| 50 |
|
| 51 |
template <typename ReturnType, typename ForwardIterator> |
| 52 |
ReturnType chatterjee_correlation_seq_impl(ForwardIterator u_begin, ForwardIterator u_end, ForwardIterator v_begin, ForwardIterator v_end) |
| 53 |
{ |
| 54 |
using std::abs; |
| 55 |
|
| 56 |
BOOST_MATH_ASSERT_MSG(std::is_sorted(u_begin, u_end), "The x values must be sorted in order to use this functionality"); |
| 57 |
|
| 58 |
const std::vector<std::size_t> rank_vector = rank(v_begin, v_end); |
| 59 |
|
| 60 |
std::size_t sum = chatterjee_transform(rank_vector.begin(), rank_vector.end()); |
| 61 |
|
| 62 |
ReturnType result = static_cast<ReturnType>(1) - (static_cast<ReturnType>(3 * sum) / static_cast<ReturnType>(rank_vector.size() * rank_vector.size() - 1)); |
| 63 |
|
| 64 |
// If the result is 1 then Y is constant and all the elements must be ties |
| 65 |
if (abs(result - static_cast<ReturnType>(1)) < std::numeric_limits<ReturnType>::epsilon()) |
| 66 |
{ |
| 67 |
return std::numeric_limits<ReturnType>::quiet_NaN(); |
| 68 |
} |
| 69 |
|
| 70 |
return result; |
| 71 |
} |
| 72 |
|
| 73 |
} // Namespace detail |
| 74 |
|
| 75 |
template <typename Container, typename Real = typename Container::value_type, |
| 76 |
typename ReturnType = typename std::conditional<std::is_integral<Real>::value, double, Real>::type> |
| 77 |
inline ReturnType chatterjee_correlation(const Container& u, const Container& v) |
| 78 |
{ |
| 79 |
return detail::chatterjee_correlation_seq_impl<ReturnType>(std::begin(u), std::end(u), std::begin(v), std::end(v)); |
| 80 |
} |
| 81 |
|
| 82 |
}}} // Namespace boost::math::statistics |
| 83 |
|
| 84 |
#ifdef BOOST_MATH_EXEC_COMPATIBLE |
| 85 |
|
| 86 |
namespace boost::math::statistics { |
| 87 |
|
| 88 |
namespace detail { |
| 89 |
|
| 90 |
template <typename ReturnType, typename ExecutionPolicy, typename ForwardIterator> |
| 91 |
ReturnType chatterjee_correlation_par_impl(ExecutionPolicy&& exec, ForwardIterator u_begin, ForwardIterator u_end, |
| 92 |
ForwardIterator v_begin, ForwardIterator v_end) |
| 93 |
{ |
| 94 |
using std::abs; |
| 95 |
BOOST_MATH_ASSERT_MSG(std::is_sorted(std::forward<ExecutionPolicy>(exec), u_begin, u_end), "The x values must be sorted in order to use this functionality"); |
| 96 |
|
| 97 |
auto rank_vector = rank(std::forward<ExecutionPolicy>(exec), v_begin, v_end); |
| 98 |
|
| 99 |
const auto num_threads = std::thread::hardware_concurrency() == 0 ? 2u : std::thread::hardware_concurrency(); |
| 100 |
std::vector<std::future<std::size_t>> future_manager {}; |
| 101 |
const auto elements_per_thread = std::ceil(static_cast<double>(rank_vector.size()) / num_threads); |
| 102 |
|
| 103 |
auto it = rank_vector.begin(); |
| 104 |
auto end = rank_vector.end(); |
| 105 |
for(std::size_t i {}; i < num_threads - 1; ++i) |
| 106 |
{ |
| 107 |
future_manager.emplace_back(std::async(std::launch::async | std::launch::deferred, [it, elements_per_thread]() -> std::size_t |
| 108 |
{ |
| 109 |
return chatterjee_transform(it, std::next(it, elements_per_thread)); |
| 110 |
})); |
| 111 |
it = std::next(it, elements_per_thread - 1); |
| 112 |
} |
| 113 |
|
| 114 |
future_manager.emplace_back(std::async(std::launch::async | std::launch::deferred, [it, end]() -> std::size_t |
| 115 |
{ |
| 116 |
return chatterjee_transform(it, end); |
| 117 |
})); |
| 118 |
|
| 119 |
std::size_t sum {}; |
| 120 |
for(std::size_t i {}; i < future_manager.size(); ++i) |
| 121 |
{ |
| 122 |
sum += future_manager[i].get(); |
| 123 |
} |
| 124 |
|
| 125 |
ReturnType result = static_cast<ReturnType>(1) - (static_cast<ReturnType>(3 * sum) / static_cast<ReturnType>(rank_vector.size() * rank_vector.size() - 1)); |
| 126 |
|
| 127 |
// If the result is 1 then Y is constant and all the elements must be ties |
| 128 |
if (abs(result - static_cast<ReturnType>(1)) < std::numeric_limits<ReturnType>::epsilon()) |
| 129 |
{ |
| 130 |
return std::numeric_limits<ReturnType>::quiet_NaN(); |
| 131 |
} |
| 132 |
|
| 133 |
return result; |
| 134 |
} |
| 135 |
|
| 136 |
} // Namespace detail |
| 137 |
|
| 138 |
template <typename ExecutionPolicy, typename Container, typename Real = typename Container::value_type, |
| 139 |
typename ReturnType = std::conditional_t<std::is_integral_v<Real>, double, Real>> |
| 140 |
inline ReturnType chatterjee_correlation(ExecutionPolicy&& exec, const Container& u, const Container& v) |
| 141 |
{ |
| 142 |
if constexpr (std::is_same_v<std::remove_reference_t<decltype(exec)>, decltype(std::execution::seq)>) |
| 143 |
{ |
| 144 |
return detail::chatterjee_correlation_seq_impl<ReturnType>(std::cbegin(u), std::cend(u), |
| 145 |
std::cbegin(v), std::cend(v)); |
| 146 |
} |
| 147 |
else |
| 148 |
{ |
| 149 |
return detail::chatterjee_correlation_par_impl<ReturnType>(std::forward<ExecutionPolicy>(exec), |
| 150 |
std::cbegin(u), std::cend(u), |
| 151 |
std::cbegin(v), std::cend(v)); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
} // Namespace boost::math::statistics |
| 156 |
|
| 157 |
#endif |
| 158 |
|
| 159 |
#endif // BOOST_MATH_STATISTICS_CHATTERJEE_CORRELATION_HPP |