|
A library for working with phylogenetic and population genetic data.
v0.32.0
|
|
Go to the documentation of this file. 1 #ifndef GENESIS_UTILS_MATH_CORRELATION_H_
2 #define GENESIS_UTILS_MATH_CORRELATION_H_
70 template <
class ForwardIteratorA,
class ForwardIteratorB>
72 ForwardIteratorA first_a, ForwardIteratorA last_a,
73 ForwardIteratorB first_b, ForwardIteratorB last_b
82 [&](
double val_a,
double val_b ){
89 return std::numeric_limits<double>::quiet_NaN();
92 mean_a /=
static_cast<double>( count );
93 mean_b /=
static_cast<double>( count );
96 double numerator = 0.0;
97 double std_dev_a = 0.0;
98 double std_dev_b = 0.0;
102 [&](
double val_a,
double val_b ){
103 double const d1 = val_a - mean_a;
104 double const d2 = val_b - mean_b;
105 numerator += d1 * d2;
106 std_dev_a += d1 * d1;
107 std_dev_b += d2 * d2;
113 auto const pcc = numerator / ( std::sqrt( std_dev_a ) * std::sqrt( std_dev_b ) );
114 assert(( -1.0 <= pcc && pcc <= 1.0 ) || ( ! std::isfinite( pcc ) ));
124 std::vector<double>
const& vec_a,
125 std::vector<double>
const& vec_b
128 vec_a.begin(), vec_a.end(), vec_b.begin(), vec_b.end()
144 template <
class RandomAccessIteratorA,
class RandomAccessIteratorB>
146 RandomAccessIteratorA first_a, RandomAccessIteratorA last_a,
147 RandomAccessIteratorB first_b, RandomAccessIteratorB last_b
151 auto const cleaned =
finite_pairs( first_a, last_a, first_b, last_b );
156 assert( ranks_a.size() == ranks_b.size() );
167 std::vector<double>
const& vec_a,
168 std::vector<double>
const& vec_b
171 vec_a.begin(), vec_a.end(), vec_b.begin(), vec_b.end()
218 std::vector<double>
const& x,
219 std::vector<double>
const& y,
228 template <
class InputIteratorA,
class InputIteratorB>
230 InputIteratorA first_a, InputIteratorA last_a,
231 InputIteratorB first_b, InputIteratorB last_b,
236 auto const cleaned =
finite_pairs( first_a, last_a, first_b, last_b );
248 std::vector<double>
const& x,
249 std::vector<double>
const& y,
267 auto const r = correlation_coefficient;
268 if( r < -1.0 || r > 1.0 ) {
269 throw std::invalid_argument(
270 "Cannot apply fisher transformation to value " +
std::to_string( r ) +
271 " outside of [ -1.0, 1.0 ]."
277 return std::atanh( r );
287 auto res = correlation_coefficients;
288 for(
auto& elem : res ) {
297 #endif // include guard
Provides some valuable algorithms that are not part of the C++ 11 STL.
double pearson_correlation_coefficient(ForwardIteratorA first_a, ForwardIteratorA last_a, ForwardIteratorB first_b, ForwardIteratorB last_b)
Calculate the Pearson Correlation Coefficient between two ranges of double.
std::vector< double > ranking_fractional(RandomAccessIterator first, RandomAccessIterator last)
Return the ranking of the values in the given range, using Fractional ranking ("1 2....
std::string to_string(GenomeLocus const &locus)
@ kTauC
Compute Tau-c, (also called Stuart-Kendall Tau-c).
std::pair< std::vector< double >, std::vector< double > > finite_pairs(ForwardIteratorA first_a, ForwardIteratorA last_a, ForwardIteratorB first_b, ForwardIteratorB last_b)
Helper function that cleans two ranges of double of the same length from non-finite values.
double spearmans_rank_correlation_coefficient(RandomAccessIteratorA first_a, RandomAccessIteratorA last_a, RandomAccessIteratorB first_b, RandomAccessIteratorB last_b)
Calculate Spearman's Rank Correlation Coefficient between two ranges of double.
double kendalls_tau_correlation_coefficient_naive(std::vector< double > const &x, std::vector< double > const &y, KendallsTauMethod method)
Compute a simple version of Kendall's Tau Correlation Coefficient.
@ kTauA
Compute Tau-a, which does not make any adjustment for ties.
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
KendallsTauMethod
Method for computing Kendall's Tau.
double fisher_transformation(double correlation_coefficient)
Apply Fisher z-transformation to a correlation coefficient.
double kendalls_tau_correlation_coefficient(std::vector< double > const &x, std::vector< double > const &y, KendallsTauMethod method)
Compute Kendall's Tau Correlation Coefficient.
@ kTauB
Compute Tau-b, which does adjustments for ties.
void for_each_finite_pair(ForwardIteratorA first_a, ForwardIteratorA last_a, ForwardIteratorB first_b, ForwardIteratorB last_b, std::function< void(double value_a, double value_b)> execute)
Iterate two ranges of double values in parallel, and execute a function for each pair of values from ...