16 #include <boost/random/mersenne_twister.hpp>
17 #include <boost/random/uniform_int.hpp>
19 #include <boost/math/special_functions/binomial.hpp>
20 #include <boost/math/special_functions/gamma.hpp>
21 #include <boost/math/special_functions/log1p.hpp>
22 #include <boost/math/distributions/binomial.hpp>
23 #include <boost/math/distributions/complement.hpp>
72 return min <= value && value <= max;
95 inline std::pair<double, double>
zoomIn(
const double left,
const double right,
const float factor,
const float align)
100 std::pair<double, double> res;
101 auto old_width = right - left;
102 auto offset_left = (1.0f - factor) * old_width * align;
103 res.first = left + offset_left;
104 res.second = res.first + old_width * factor;
128 std::vector<RangeBase> res(number_of_bins);
129 const double bin_width = (max - min) / number_of_bins;
130 for (uint32_t i = 0; i < number_of_bins; ++i)
132 res[i] =
RangeBase(min + i * bin_width, min + (i + 1) * bin_width);
133 res[i].extendLeftRight(extend_margin);
135 res.front().setMin(min);
136 res.back().setMax(max);
155 return (ceil(x / pow(10.0, decPow))) * pow(10.0, decPow);
170 if (x > 0)
return (floor(0.5 + x / pow(10.0, decPow))) * pow(10.0, decPow);
172 return -((floor(0.5 + fabs(x) / pow(10.0, decPow))) * pow(10.0, decPow));
182 return left2 + (x - left1) * (right2 - left2) / (right1 - left1);
206 return pow(10, x) - 1;
250 for (
int i = 0; i < digits; ++i)
255 for (
int i = 0; i < -digits; ++i)
288 return roundTo(value * 100.0 / total, digits);
298 return std::fabs(a - b) <= tol;
335 T
gcd(T a, T b, T& u1, T& u2)
376 return (mz_obs - mz_ref) / mz_ref * 1e6;
391 return std::fabs(
getPPM(mz_obs, mz_ref));
406 return (ppm / T(1e6)) * mz_ref;
421 return std::fabs(
ppmToMass(ppm, mz_ref));
437 inline std::pair<double, double>
getTolWindow(
double val,
double tol,
bool ppm)
443 left = val - val * tol * 1e-6;
444 right = val / (1.0 - tol * 1e-6);
452 return std::make_pair(left, right);
458 template<
typename T1>
459 typename T1::value_type
quantile(
const T1& x,
double q)
461 if (x.empty())
throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
"Quantile requested from empty container.");
465 const auto n = x.size();
466 const auto id = std::max(0., n * q - 1);
467 const auto lo = floor(
id);
468 const auto hi = ceil(
id);
469 const auto qs = x[lo];
470 const auto h = (
id - lo);
472 return (1.0 -
h) * qs +
h * x[hi];
491 template<
class RandomAccessIterator>
494 for (
auto i = (last - first) - 1; i > 0; --i)
496 boost::uniform_int<decltype(i)> d(0, i);
497 std::swap(first[i], first[d(rng_)]);
520 throw std::invalid_argument(
"k cannot be greater than n in binomial coefficient");
523 if (
k == 0 ||
k == n)
534 return boost::math::lgamma(n + 1.0) - boost::math::lgamma(
k + 1.0) - boost::math::lgamma(n -
k + 1.0);
547 if (std::isinf(x) && x < 0)
return y;
548 if (std::isinf(y) && y < 0)
return x;
551 double max_val = std::max(x, y);
552 return max_val + std::log(std::exp(x - max_val) + std::exp(y - max_val));
569 if (p < 0.0 || p > 1.0)
571 throw std::invalid_argument(
"Probability p must be between 0 and 1");
575 throw std::invalid_argument(
"n cannot be greater than N");
578 if (n == 0)
return 1.0;
579 if (p == 0.0)
return (n == 0) ? 1.0 : 0.0;
580 if (p == 1.0)
return 1.0;
582 const boost::math::binomial_distribution<double> dist(N, p);
583 return boost::math::cdf(boost::math::complement(dist, n - 1));
Exception indicating that an invalid parameter was handed over to an algorithm.
Definition: Exception.h:316
Invalid value exception.
Definition: Exception.h:305
Definition: MathFunctions.h:477
RandomShuffler(int seed)
Definition: MathFunctions.h:479
boost::mt19937_64 rng_
Definition: MathFunctions.h:490
void seed(uint64_t val)
Definition: MathFunctions.h:501
~RandomShuffler()=default
RandomShuffler(const boost::mt19937_64 &mt_rng)
Definition: MathFunctions.h:483
void portable_random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
Definition: MathFunctions.h:492
A more convenient string class.
Definition: String.h:34
unsigned int UInt
Unsigned integer type.
Definition: Types.h:64
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:94
T gcd(T a, T b)
Returns the greatest common divisor (gcd) of two numbers by applying the Euclidean algorithm.
Definition: MathFunctions.h:310
double intervalTransformation(double x, double left1, double right1, double left2, double right2)
transforms point x of interval [left1,right1] into interval [left2,right2]
Definition: MathFunctions.h:180
double linear2log(double x)
Transforms a number from linear to log10 scale. Avoids negative logarithms by adding 1.
Definition: MathFunctions.h:192
T round(T x)
Rounds the value.
Definition: MathFunctions.h:225
double log2linear(double x)
Transforms a number from log10 to to linear scale. Subtracts the 1 added by linear2log(double)
Definition: MathFunctions.h:204
double ceilDecimal(double x, int decPow)
rounds x up to the next decimal power 10 ^ decPow
Definition: MathFunctions.h:153
bool approximatelyEqual(double a, double b, double tol)
Returns if a is approximately equal b , allowing a tolerance of tol.
Definition: MathFunctions.h:296
double roundDecimal(double x, int decPow)
rounds x to the next decimal power 10 ^ decPow
Definition: MathFunctions.h:168
bool isOdd(UInt x)
Returns true if the given integer is odd.
Definition: MathFunctions.h:214
const double c
Definition: Constants.h:188
const double k
Definition: Constants.h:132
const double h
Definition: Constants.h:141
BinContainer createBins(double min, double max, uint32_t number_of_bins, double extend_margin=0)
Split a range [min,max] into number_of_bins (with optional overlap) and return the ranges of each bin...
Definition: MathFunctions.h:124
bool extendRange(T &min, T &max, const T &value)
Given an interval/range and a new value, extend the range to include the new value if needed.
Definition: MathFunctions.h:49
T getPPM(T mz_obs, T mz_ref)
Compute parts-per-million of two m/z values.
Definition: MathFunctions.h:374
T1::value_type quantile(const T1 &x, double q)
Returns the value of the q th quantile (0-1) in a sorted non-empty vector x.
Definition: MathFunctions.h:459
T roundTo(const T value, int digits)
Definition: MathFunctions.h:245
T ppmToMass(T ppm, T mz_ref)
Compute the mass diff in [Th], given a ppm value and a reference point.
Definition: MathFunctions.h:404
double binomial_cdf_complement(unsigned N, unsigned n, double p)
Calculate binomial cumulative distribution function P(X ≥ n)
Definition: MathFunctions.h:567
double log_binomial_coef(unsigned n, unsigned k)
Calculate logarithm of binomial coefficient C(n,k) using log-gamma function.
Definition: MathFunctions.h:515
std::pair< double, double > zoomIn(const double left, const double right, const float factor, const float align)
Zoom into an interval [left, right], decreasing its width by factor (which must be in [0,...
Definition: MathFunctions.h:95
double log_sum_exp(double x, double y)
Log-sum-exp operation for numerical stability.
Definition: MathFunctions.h:544
bool contains(T value, T min, T max)
Is a value contained in [min, max] ?
Definition: MathFunctions.h:70
T getPPMAbs(T mz_obs, T mz_ref)
Compute absolute parts-per-million of two m/z values.
Definition: MathFunctions.h:389
T ppmToMassAbs(T ppm, T mz_ref)
Definition: MathFunctions.h:419
double percentOf(T value, T total, int digits)
Definition: MathFunctions.h:280
std::pair< double, double > getTolWindow(double val, double tol, bool ppm)
Return tolerance window around val given tolerance tol.
Definition: MathFunctions.h:437
std::vector< RangeBase > BinContainer
Definition: MathFunctions.h:108
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Base class for a simple range with minimum and maximum.
Definition: RangeManager.h:37