OpenMS
OpenMS::Math Namespace Reference

Math namespace. More...

Classes

class  RandomShuffler
 
struct  SummaryStatistics
 Helper class to gather (and dump) some statistics from a e.g. vector<double>. More...
 
class  BasicStatistics
 Calculates some basic statistical parameters of a distribution: sum, mean, variance, and provides the normal approximation. More...
 
class  GammaDistributionFitter
 Implements a fitter for the Gamma distribution. More...
 
class  GaussFitter
 Implements a fitter for Gaussian functions. More...
 
class  GumbelDistributionFitter
 Implements a fitter for the Gumbel distribution. More...
 
class  GumbelMaxLikelihoodFitter
 Implements a fitter for the Gumbel distribution. More...
 
class  Histogram
 Representation of a histogram. More...
 
class  PosteriorErrorProbabilityModel
 Implements a mixture model of the inverse gumbel and the gauss distribution or a gaussian mixture. More...
 
class  BilinearInterpolation
 Provides access to bilinearly interpolated values (and derivatives) from discrete data points. Values beyond the given range of data points are implicitly taken as zero. More...
 
class  LinearInterpolation
 Provides access to linearly interpolated values (and derivatives) from discrete data points. Values beyond the given range of data points are implicitly taken as zero. More...
 
struct  RANSACParam
 A simple struct to carry all the parameters required for a RANSAC run. More...
 
class  RANSAC
 This class provides a generic implementation of the RANSAC outlier detection algorithm. Is implemented and tested after the SciPy reference: http://wiki.scipy.org/Cookbook/RANSAC. More...
 
class  RansacModel
 Generic plug-in template base class using 'Curiously recurring template pattern' (CRTP) to allow for arbitrary RANSAC models (e.g. linear or quadratic fits). More...
 
class  RansacModelLinear
 Implementation of a linear RANSAC model fit. More...
 
class  RansacModelQuadratic
 Implementation of a quadratic RANSAC model fit. More...
 
class  LinearRegression
 This class offers functions to perform least-squares fits to a straight line model, \( Y(c,x) = c_0 + c_1 x \). More...
 
class  LinearRegressionWithoutIntercept
 This class offers functions to perform least-squares fits to a straight line model, \( Y(c,x) = c_0 + c_1 x \). More...
 
class  QuadraticRegression
 
class  ROCCurve
 ROCCurves show the trade-off in sensitivity and specificity for binary classifiers using different cutoff values. More...
 

Typedefs

using BinContainer = std::vector< RangeBase >
 

Functions

template<typename T >
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. More...
 
template<typename T >
bool contains (T value, T min, T max)
 Is a value contained in [min, max] ? More...
 
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,inf]). More...
 
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. More...
 
double ceilDecimal (double x, int decPow)
 rounds x up to the next decimal power 10 ^ decPow More...
 
double roundDecimal (double x, int decPow)
 rounds x to the next decimal power 10 ^ decPow More...
 
double intervalTransformation (double x, double left1, double right1, double left2, double right2)
 transforms point x of interval [left1,right1] into interval [left2,right2] More...
 
double linear2log (double x)
 Transforms a number from linear to log10 scale. Avoids negative logarithms by adding 1. More...
 
double log2linear (double x)
 Transforms a number from log10 to to linear scale. Subtracts the 1 added by linear2log(double) More...
 
bool isOdd (UInt x)
 Returns true if the given integer is odd. More...
 
template<typename T >
round (T x)
 Rounds the value. More...
 
bool approximatelyEqual (double a, double b, double tol)
 Returns if a is approximately equal b , allowing a tolerance of tol. More...
 
template<typename T >
gcd (T a, T b)
 Returns the greatest common divisor (gcd) of two numbers by applying the Euclidean algorithm. More...
 
template<typename T >
gcd (T a, T b, T &u1, T &u2)
 Returns the greatest common divisor by applying the extended Euclidean algorithm (Knuth TAoCP vol. 2, p342). Calculates u1, u2 and u3 (which is returned) so that a * u1 + b * u2 = u3 = gcd(a, b, u1, u2) More...
 
template<typename T >
getPPM (T mz_obs, T mz_ref)
 Compute parts-per-million of two m/z values. More...
 
template<typename T >
getPPMAbs (T mz_obs, T mz_ref)
 Compute absolute parts-per-million of two m/z values. More...
 
template<typename T >
ppmToMass (T ppm, T mz_ref)
 Compute the mass diff in [Th], given a ppm value and a reference point. More...
 
template<typename T >
ppmToMassAbs (T ppm, T mz_ref)
 
std::pair< double, double > getTolWindow (double val, double tol, bool ppm)
 Return tolerance window around val given tolerance tol. More...
 
template<typename T1 >
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. More...
 
template<class T >
void spline_bisection (const T &peak_spline, double const left_neighbor_mz, double const right_neighbor_mz, double &max_peak_mz, double &max_peak_int, double const threshold=1e-6)
 
template<typename IteratorType >
static void checkIteratorsNotNULL (IteratorType begin, IteratorType end)
 Helper function checking if two iterators are not equal. More...
 
template<typename IteratorType >
static void checkIteratorsEqual (IteratorType begin, IteratorType end)
 Helper function checking if two iterators are equal. More...
 
template<typename IteratorType1 , typename IteratorType2 >
static void checkIteratorsAreValid (IteratorType1 begin_b, IteratorType1 end_b, IteratorType2 begin_a, IteratorType2 end_a)
 Helper function checking if an iterator and a co-iterator both have a next element. More...
 
template<typename IteratorType >
static double sum (IteratorType begin, IteratorType end)
 Calculates the sum of a range of values. More...
 
template<typename IteratorType >
static double mean (IteratorType begin, IteratorType end)
 Calculates the mean of a range of values. More...
 
template<typename IteratorType >
static double median (IteratorType begin, IteratorType end, bool sorted=false)
 Calculates the median of a range of values. More...
 
template<typename IteratorType >
double MAD (IteratorType begin, IteratorType end, double median_of_numbers)
 median absolute deviation (MAD) More...
 
template<typename IteratorType >
double MeanAbsoluteDeviation (IteratorType begin, IteratorType end, double mean_of_numbers)
 mean absolute deviation (MeanAbsoluteDeviation) More...
 
template<typename IteratorType >
static double quantile1st (IteratorType begin, IteratorType end, bool sorted=false)
 Calculates the first quantile of a range of values. More...
 
template<typename IteratorType >
static double quantile3rd (IteratorType begin, IteratorType end, bool sorted=false)
 Calculates the third quantile of a range of values. More...
 
template<typename IteratorType >
static double variance (IteratorType begin, IteratorType end, double mean=std::numeric_limits< double >::max())
 
template<typename IteratorType >
static double sd (IteratorType begin, IteratorType end, double mean=std::numeric_limits< double >::max())
 Calculates the standard deviation of a range of values. More...
 
template<typename IteratorType >
static double absdev (IteratorType begin, IteratorType end, double mean=std::numeric_limits< double >::max())
 Calculates the absolute deviation of a range of values. More...
 
template<typename IteratorType1 , typename IteratorType2 >
static double covariance (IteratorType1 begin_a, IteratorType1 end_a, IteratorType2 begin_b, IteratorType2 end_b)
 Calculates the covariance of two ranges of values. More...
 
template<typename IteratorType1 , typename IteratorType2 >
static double meanSquareError (IteratorType1 begin_a, IteratorType1 end_a, IteratorType2 begin_b, IteratorType2 end_b)
 Calculates the mean square error for the values in [begin_a, end_a) and [begin_b, end_b) More...
 
template<typename IteratorType1 , typename IteratorType2 >
static double classificationRate (IteratorType1 begin_a, IteratorType1 end_a, IteratorType2 begin_b, IteratorType2 end_b)
 Calculates the classification rate for the values in [begin_a, end_a) and [begin_b, end_b) More...
 
template<typename IteratorType1 , typename IteratorType2 >
static double matthewsCorrelationCoefficient (IteratorType1 begin_a, IteratorType1 end_a, IteratorType2 begin_b, IteratorType2 end_b)
 Calculates the Matthews correlation coefficient for the values in [begin_a, end_a) and [begin_b, end_b) More...
 
template<typename IteratorType1 , typename IteratorType2 >
static double pearsonCorrelationCoefficient (IteratorType1 begin_a, IteratorType1 end_a, IteratorType2 begin_b, IteratorType2 end_b)
 Calculates the Pearson correlation coefficient for the values in [begin_a, end_a) and [begin_b, end_b) More...
 
template<typename Value >
static void computeRank (std::vector< Value > &w)
 Replaces the elements in vector w by their ranks. More...
 
template<typename IteratorType1 , typename IteratorType2 >
static double rankCorrelationCoefficient (IteratorType1 begin_a, IteratorType1 end_a, IteratorType2 begin_b, IteratorType2 end_b)
 calculates the rank correlation coefficient for the values in [begin_a, end_a) and [begin_b, end_b) More...
 
template<typename ValueType , typename BinSizeType >
std::ostream & operator<< (std::ostream &os, const Histogram< ValueType, BinSizeType > &hist)
 Print the contents to a stream. More...
 

Detailed Description

Math namespace.

Uses bisection to find the maximum point of a spline.

Contains mathematical auxiliary functions.

Should work with BSpline2d and CubicSpline2d

Typedef Documentation

◆ BinContainer

using BinContainer = std::vector<RangeBase>

Function Documentation

◆ computeRank()

static void OpenMS::Math::computeRank ( std::vector< Value > &  w)
static

Replaces the elements in vector w by their ranks.

Referenced by rankCorrelationCoefficient().

◆ contains()

bool OpenMS::Math::contains ( value,
min,
max 
)

Is a value contained in [min, max] ?

Template Parameters
TType, e.g. double
Returns
True if contained, false otherwise

Referenced by RangeBase::contains(), ListUtils::contains(), and QApplicationTOPP::QApplicationTOPP().

◆ createBins()

BinContainer OpenMS::Math::createBins ( double  min,
double  max,
uint32_t  number_of_bins,
double  extend_margin = 0 
)
inline

Split a range [min,max] into number_of_bins (with optional overlap) and return the ranges of each bin.

Optionally, bins can be made overlapping, by extending each bins' left and right margin by extend_margin. The overlap between neighboring bins will thus be 2 x extend_margin. The borders of the original interval will not be extended.

Parameters
minThe minimum of the range; must be smaller than max
maxThe maximum of the range
number_of_binsHow many bins should the range be divided into? Must be 1 or larger
extend_marginOverlap of neighboring bins (=0 for no overlap). Negative values will shrink the range (feature).
Returns
Vector with number_of_bins elements, each representing the margins of one bin
Exceptions
OpenMS::Preconditionif min >= max or number_of_bins == 0

References OPENMS_PRECONDITION.

◆ extendRange()

bool OpenMS::Math::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.

Parameters
minThe current minimum of the range
maxThe current maximum of the range
valueThe new value which may extend the range
Returns
true if the range was modified

◆ getPPM()

T OpenMS::Math::getPPM ( mz_obs,
mz_ref 
)

Compute parts-per-million of two m/z values.

The returned ppm value can be either positive (mz_obs > mz_ref) or negative (mz_obs < mz_ref)!

Parameters
mz_obsObserved (experimental) m/z
mz_refReference (theoretical) m/z
Returns
The ppm value

Referenced by getPPMAbs().

◆ getPPMAbs()

T OpenMS::Math::getPPMAbs ( mz_obs,
mz_ref 
)

Compute absolute parts-per-million of two m/z values.

The returned ppm value is always >= 0.

Parameters
mz_obsObserved (experimental) m/z
mz_refReference (theoretical) m/z
Returns
The absolute ppm value

References getPPM().

◆ getTolWindow()

std::pair<double, double> OpenMS::Math::getTolWindow ( double  val,
double  tol,
bool  ppm 
)
inline

Return tolerance window around val given tolerance tol.

Note that when ppm is used, the window is not symmetric. In this case, (right - val) > (val - left), i.e., the tolerance window also includes the largest value x which still has val in its tolerance window for the given ppms, so the compatibility relation is symmetric.

Parameters
valValue
tolTolerance
ppmWhether tol is in ppm or absolute
Returns
Tolerance window boundaries

◆ operator<<()

std::ostream& OpenMS::Math::operator<< ( std::ostream &  os,
const Histogram< ValueType, BinSizeType > &  hist 
)

◆ ppmToMass()

T OpenMS::Math::ppmToMass ( ppm,
mz_ref 
)

Compute the mass diff in [Th], given a ppm value and a reference point.

The returned mass diff can be either positive (ppm > 0) or negative (ppm < 0)!

Parameters
ppmParts-per-million error
mz_refReference m/z
Returns
The mass diff in [Th]

Referenced by PpmTrait::allowedTol(), GaussFilterAlgorithm::filter(), and ppmToMassAbs().

◆ ppmToMassAbs()

T OpenMS::Math::ppmToMassAbs ( ppm,
mz_ref 
)

References ppmToMass().

◆ quantile()

T1::value_type OpenMS::Math::quantile ( const T1 &  x,
double  q 
)

Returns the value of the q th quantile (0-1) in a sorted non-empty vector x.

References OpenMS::Constants::h.

◆ spline_bisection()

void OpenMS::Math::spline_bisection ( const T &  peak_spline,
double const  left_neighbor_mz,
double const  right_neighbor_mz,
double &  max_peak_mz,
double &  max_peak_int,
double const  threshold = 1e-6 
)

◆ variance()

static double OpenMS::Math::variance ( IteratorType  begin,
IteratorType  end,
double  mean = std::numeric_limits<double>::max() 
)
static
 @brief Calculates the variance of a range of values

The mean can be provided explicitly to save computation time. If left at default, it will be computed internally.

 @exception Exception::InvalidRange is thrown if the range is empty

 @ingroup MathFunctionsStatistics

References checkIteratorsNotNULL(), and mean().

Referenced by BasicStatistics< RealT >::normalDensity_sqrt2pi(), sd(), BasicStatistics< RealT >::setVariance(), and SummaryStatistics< T >::SummaryStatistics().

◆ zoomIn()

std::pair<double, double> OpenMS::Math::zoomIn ( const double  left,
const double  right,
const float  factor,
const float  align 
)
inline

Zoom into an interval [left, right], decreasing its width by factor (which must be in [0,inf]).

To actually zoom in, the factor needs to be within [0,1]. Chosing a factor > 1 actually zooms out. align (between [0,1]) determines where the zoom happens: i.e. align = 0 leaves left the same and reduces right (or extends if factor>1) whereas align = 0.5 zooms into the center of the range etc

You can do round trips, i.e. undo a zoom in, by inverting the factor:

[a2, b2] = zoomIn(a1, b1, 0.5, al); // zoom in
[a1, b1] === zoomIn(a2, b2, 2, al); // zoom out again (inverting)
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:88
Parameters
leftStart of interval
rightEnd of interval
factorNumber between [0,1] to shrink, or >1 to extend the span (=right-left)
alignWhere to position the smaller/shrunk interval (0 = left, 1 = right, 0.5=center etc)
Returns
[new_left, new_right] as pair

References OPENMS_PRECONDITION.