A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
common.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_MATH_COMMON_H_
2 #define GENESIS_UTILS_MATH_COMMON_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2017 Lucas Czech
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
34 #include <cmath>
35 #include <limits>
36 #include <stdexcept>
37 #include <vector>
38 
39 namespace genesis {
40 namespace utils {
41 
42 // =================================================================================================
43 // Constants
44 // =================================================================================================
45 
49 constexpr double PI = 3.141592653589793238463;
50 
51 // =================================================================================================
52 // Structures and Classes
53 // =================================================================================================
54 
60 template< typename T >
61 struct MinMaxPair
62 {
63  T min;
64  T max;
65 };
66 
74 {
75  double mean;
76  double stddev;
77 };
78 
83 struct Quartiles
84 {
85  double q0 = 0.0;
86  double q1 = 0.0;
87  double q2 = 0.0;
88  double q3 = 0.0;
89  double q4 = 0.0;
90 };
91 
92 // =================================================================================================
93 // Statistics
94 // =================================================================================================
95 
105 MeanStddevPair mean_stddev( std::vector<double> const& vec, double epsilon = -1.0 );
106 
112 double median( std::vector<double> const& vec );
113 
119 Quartiles quartiles( std::vector<double> const& vec );
120 
128  std::vector<double> const& vec_a,
129  std::vector<double> const& vec_b
130 );
131 
139  std::vector<double> const& vec_a,
140  std::vector<double> const& vec_b
141 );
142 
154 double fisher_transformation( double correlation_coefficient );
155 
161 std::vector<double> fisher_transformation( std::vector<double> const& correlation_coefficients );
162 
163 // =================================================================================================
164 // Ranking
165 // =================================================================================================
166 
176 std::vector<size_t> ranking_standard( std::vector<double> const& vec );
177 
187 std::vector<size_t> ranking_modified( std::vector<double> const& vec );
188 
197 std::vector<size_t> ranking_dense( std::vector<double> const& vec );
198 
207 std::vector<size_t> ranking_ordinal( std::vector<double> const& vec );
208 
218 std::vector<double> ranking_fractional( std::vector<double> const& vec );
219 
220 // =================================================================================================
221 // Number Handling
222 // =================================================================================================
223 
230 template< typename T > inline constexpr
231 T abs_diff( T const& lhs, T const& rhs )
232 {
233  return ((lhs > rhs) ? (lhs - rhs) : (rhs - lhs));
234 }
235 
239 template <typename T> inline constexpr
240 int signum(T x, std::false_type )
241 {
242  // The tag type `std::false_type` is unnamed in order to avoid compiler warnings about an
243  // unused parameter. As this function is `constexpr`, we cannot shut this warning down by
244  // using `(void) param`, so making it unnamed is a reasonable solution in this case.
245  return T(0) < x;
246 }
247 
251 template <typename T> inline constexpr
252 int signum(T x, std::true_type )
253 {
254  // The tag type `std::false_type` is unnamed in order to avoid compiler warnings about an
255  // unused parameter. As this function is `constexpr`, we cannot shut this warning down by
256  // using `(void) param`, so making it unnamed is a reasonable solution in this case.
257  return (T(0) < x) - (x < T(0));
258 }
259 
268 template <typename T> inline constexpr
269 int signum(T x)
270 {
271  return signum( x, std::is_signed<T>() );
272 }
273 
278  double lhs,
279  double rhs,
280  double max_rel_diff = std::numeric_limits<double>::epsilon()
281 ) {
282  // Calculate the difference.
283  auto diff = std::abs( lhs - rhs );
284 
285  // Find the larger number.
286  auto largest = std::max( std::abs( lhs ), std::abs( rhs ));
287 
288  // Do the comparison.
289  return ( diff <= largest * max_rel_diff );
290 }
291 
295 inline double round_to( double x, size_t accuracy_order )
296 {
297  double factor = std::pow( 10, accuracy_order );
298  return std::round( x * factor ) / factor;
299 }
300 
311 inline size_t int_pow( size_t base, size_t exp )
312 {
313  // Using Exponentiation by squaring, see
314  // http://stackoverflow.com/a/101613/4184258
315  size_t result = 1;
316  while( exp ) {
317  if( exp & 1 ) {
318  result *= base;
319  }
320  exp >>= 1;
321  base *= base;
322  }
323  return result;
324 }
325 
331 inline bool is_valid_int_pow( size_t base, size_t exp )
332 {
333  return std::pow( base, exp ) < std::numeric_limits<size_t>::max();
334 }
335 
336 } // namespace utils
337 } // namespace genesis
338 
339 #endif // include guard
std::vector< size_t > ranking_dense(std::vector< double > const &vec)
Return the ranking of the given values, using Dense ranking ("1223" ranking).
Definition: common.cpp:301
bool is_valid_int_pow(size_t base, size_t exp)
Return whether the given power can be stored within a size_t.
Definition: common.hpp:331
size_t int_pow(size_t base, size_t exp)
Calculate the power base^exp for positive integer values.
Definition: common.hpp:311
double fisher_transformation(double correlation_coefficient)
Apply Fisher z-transformation to a correlation coefficient.
Definition: common.cpp:210
double spearmans_rank_correlation_coefficient(std::vector< double > const &vec_a, std::vector< double > const &vec_b)
Calculate Spearman's Rank Correlation Coefficient between the entries of two vectors.
Definition: common.cpp:199
Store a pair of min and max values.
Definition: common.hpp:61
constexpr int signum(T x, std::false_type)
Implementation of signum(T x) for unsigned types. See there for details.
Definition: common.hpp:240
double round_to(double x, size_t accuracy_order)
Retun the value of x, rounded to the decimal digit given by accuracy_order.
Definition: common.hpp:295
double median(std::vector< double > const &vec, size_t l, size_t r)
Local helper function to get the median in between a range.
Definition: common.cpp:85
std::vector< size_t > ranking_ordinal(std::vector< double > const &vec)
Return the ranking of the given values, using Ordinal ranking ("1234" ranking).
Definition: common.cpp:329
constexpr double PI
Make the world go round.
Definition: common.hpp:49
double pearson_correlation_coefficient(std::vector< double > const &vec_a, std::vector< double > const &vec_b)
Calculate the Pearson Correlation Coefficient between the entries of two vectors. ...
Definition: common.cpp:162
MeanStddevPair mean_stddev(std::vector< double > const &vec, double epsilon)
Calcualte the mean and standard deviation of a vector of double.
Definition: common.cpp:47
Store a mean and a standard deviation value.
Definition: common.hpp:73
std::vector< double > ranking_fractional(std::vector< double > const &vec)
Return the ranking of the given values, using Fractional ranking ("1 2.5 2.5 4" ranking).
Definition: common.cpp:348
std::vector< size_t > ranking_standard(std::vector< double > const &vec)
Return the ranking of the given values, using Standard competition ranking ("1224" ranking)...
Definition: common.cpp:238
Quartiles quartiles(std::vector< double > const &vec)
Calculate the Quartiles of a vector of double.
Definition: common.cpp:126
std::vector< size_t > ranking_modified(std::vector< double > const &vec)
Return the ranking of the given values, using Modified competition ranking ("1334" ranking)...
Definition: common.cpp:266
Store the values of quartiles: q0 == min, q1 == 25%, q2 == 50%, q3 == 75%, q4 == max.
Definition: common.hpp:83
constexpr T abs_diff(T const &lhs, T const &rhs)
Calculate the absolute differenence between two values.
Definition: common.hpp:231
bool almost_equal_relative(double lhs, double rhs, double max_rel_diff=std::numeric_limits< double >::epsilon())
Check whether two doubles are almost equal, using a relative epsilon to compare them.
Definition: common.hpp:277