versatile-mcmc  0.1.7
math.hpp
Go to the documentation of this file.
1 
13 #ifndef VMCMC_MATH_H_
14 #define VMCMC_MATH_H_
15 
16 #include <cmath>
17 #include <type_traits>
18 
19 #include <boost/math/special_functions/pow.hpp>
20 #include <boost/math/constants/constants.hpp>
21 #include <boost/math/distributions/normal.hpp>
22 
23 namespace vmcmc
24 {
25 
26 namespace constants = boost::math::double_constants;
27 
28 namespace math
29 {
30 
31 using boost::math::pow;
32 
33 // DECLARATIONS
34 
35 template <typename T,
36 class = typename std::enable_if<std::is_integral<T>::value>::type>
37 bool isOdd(const T& v);
38 
39 template <typename T,
40 class = typename std::enable_if<std::is_integral<T>::value>::type>
41 bool isEven(const T& v);
42 
43 template <typename T>
44 inline T& constrain(T& input, const T& min, const T& max);
45 
46 template <typename T>
47 inline T constrain(T&& input, const T& min, const T& max);
48 
56 double normalPDF(double x, double mean = 0.0, double sigma = 1.0);
57 
69 double biVariateNormalPDF(double x1, double x2, double mean1 = 0.0, double mean2 = 0.0, double sigma1 = 1.0, double sigma2 = 1.0, double corr = 0.0);
70 
71 double normal1SidedCDF(double nSigmas = 1.0);
72 double normal1SidedQuantile(double prob = 0.682689);
73 
74 double chiSquareQuantile(double prob, size_t nParams);
75 double chiSquareQuantileFromSigmas(double nSigmas, size_t nParams);
76 
77 double chiSquareCDF(double value, size_t nParams);
78 double chiSquareToSigmas(double value, size_t nParams);
79 
80 
81 // DEFINITIONS
82 
83 inline double normalPDF(double x, double mean, double sigma)
84 {
85  return boost::math::pdf( boost::math::normal(mean, sigma), x );
86 }
87 
88 template <typename T, class>
89 inline bool isOdd(const T& v)
90 {
91  return (v % 2 == 1);
92 }
93 
94 template <typename T, class>
95 inline bool isEven(const T& v)
96 {
97  return (v % 2 == 0);
98 }
99 
100 template <typename T>
101 inline T& constrain(T& input, const T& min, const T& max)
102 {
103  if (min > max)
104  return input;
105 
106  if (input < min)
107  input = min;
108  else if (input > max)
109  input = max;
110 
111  return input;
112 }
113 
114 template <typename T>
115 inline T constrain(T&& input, const T& min, const T& max)
116 {
117  T result = input;
118  constrain(result, min, max);
119  return result;
120 }
121 
122 
123 } /* namespace math */
124 
125 } /* namespace vmcmc */
126 
127 #endif /* VMCMC_MATH_H_ */
Definition: algorithm.cpp:28
double normalPDF(double x, double mean=0.0, double sigma=1.0)
A standard normal (Gaussian) distribution.
Definition: math.hpp:83
double biVariateNormalPDF(double x1, double x2, double mean1, double mean2, double sigma1, double sigma2, double corr)
A 2-dimensional normal distribution.
Definition: math.cpp:27