Class MMath
This class provides mathematical constants and special functions, analogous to System.Math. It cannot be instantiated and consists of static members only.
Inherited Members
Namespace: Microsoft.ML.Probabilistic.Math
Assembly: Microsoft.ML.Probabilistic.dll
Syntax
public static class MMath
Remarks
In order to provide the highest accuracy, some routines return their results in log form or logit form. These transformations expand the domain to cover the full range of double-precision values, ensuring all bits of the representation are utilized. A good example of this is the NormalCdf function, whose value lies between 0 and 1. Numbers between 0 and 1 use only a small fraction of the capacity of a double-precision number. The function NormalCdfLogit transforms the result p according to log(p/(1-p)), providing full use of the range from -Infinity to Infinity and (potentially) much higher precision.
To get maximal use out of these transformations, you want to stay in the expanded form as long as possible. Every time you transform into a smaller domain, you lose precision. Thus helper functions are provided which allow you to perform common tasks directly in the log form and logit form. For logs, you have addition. For logit, you have averaging.
Fields
CbrtUlp1
Math.Pow(Ulp(1.0), 1.0 / 3)
Declaration
public static readonly double CbrtUlp1
Field Value
| Type | Description |
|---|---|
| Double |
Digamma1
Digamma(1)
Declaration
public const double Digamma1 = -0.57721566490153287
Field Value
| Type | Description |
|---|---|
| Double |
EulerGamma
The Euler-Mascheroni Constant.
Declaration
public const double EulerGamma = 0.57721566490153287
Field Value
| Type | Description |
|---|---|
| Double |
InvSqrt2PI
1.0/Math.Sqrt(2*Math.PI)
Declaration
public const double InvSqrt2PI = 0.3989422804014327
Field Value
| Type | Description |
|---|---|
| Double |
Ln2
Math.Log(2)
Declaration
public const double Ln2 = 0.69314718055994529
Field Value
| Type | Description |
|---|---|
| Double |
LnPI
Math.Log(Math.PI)
Declaration
public const double LnPI = 1.1447298858494
Field Value
| Type | Description |
|---|---|
| Double |
LnSqrt2PI
Math.Log(Math.Sqrt(2*Math.PI)).
Declaration
public const double LnSqrt2PI = 0.91893853320467278
Field Value
| Type | Description |
|---|---|
| Double |
Sqrt2
Math.Sqrt(2)
Declaration
public const double Sqrt2 = 1.4142135623730951
Field Value
| Type | Description |
|---|---|
| Double |
Sqrt2PI
Math.Sqrt(2*Math.PI)
Declaration
public const double Sqrt2PI = 2.5066282746310007
Field Value
| Type | Description |
|---|---|
| Double |
Sqrt3
Math.Sqrt(3)
Declaration
public const double Sqrt3 = 1.7320508075688772
Field Value
| Type | Description |
|---|---|
| Double |
SqrtHalf
Math.Sqrt(0.5)
Declaration
public const double SqrtHalf = 0.70710678118654757
Field Value
| Type | Description |
|---|---|
| Double |
SqrtUlp1
Math.Sqrt(Ulp(1.0))
Declaration
public static readonly double SqrtUlp1
Field Value
| Type | Description |
|---|---|
| Double |
Ulp1
Ulp(1.0) == NextDouble(1.0) - 1.0
Declaration
public static readonly double Ulp1
Field Value
| Type | Description |
|---|---|
| Double |
Zeta2
Zeta(2) = Trigamma(1) = pi^2/6.
Declaration
public const double Zeta2 = 1.6449340668482264
Field Value
| Type | Description |
|---|---|
| Double |
Methods
AbsDiff(Double, Double)
Returns the distance between two numbers.
Declaration
public static double AbsDiff(double x, double y)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | |
| Double | y |
Returns
| Type | Description |
|---|---|
| Double |
|
AbsDiff(Double, Double, Double)
Returns the relative distance between two numbers.
Declaration
public static double AbsDiff(double x, double y, double rel)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | |
| Double | y | |
| Double | rel | An offset to avoid division by zero. |
Returns
| Type | Description |
|---|---|
| Double |
|
Remarks
This routine is often used to measure the error of y in estimating x.
AreEqual(Double, Double)
Returns true if two numbers are equal when represented in double precision.
Declaration
public static bool AreEqual(double x, double y)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | |
| Double | y |
Returns
| Type | Description |
|---|---|
| Boolean |
Average(Double, Double)
Returns (a+b)/2, avoiding overflow. The result is guaranteed to be between a and b.
Declaration
public static double Average(double a, double b)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | a | |
| Double | b |
Returns
| Type | Description |
|---|---|
| Double |
Average(Int64, Int64)
Returns (a+b)/2, avoiding overflow. The result is guaranteed to be between a and b.
Declaration
public static long Average(long a, long b)
Parameters
| Type | Name | Description |
|---|---|---|
| Int64 | a | |
| Int64 | b |
Returns
| Type | Description |
|---|---|
| Int64 |
BesselI(Double, Double)
Modified Bessel function of the first kind
Declaration
public static double BesselI(double a, double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | a | Order parameter. Any real number except a negative integer. |
| Double | x | Argument of the Bessel function. Non-negative real number. |
Returns
| Type | Description |
|---|---|
| Double | BesselI(a,x) |
Remarks
Reference: "A short note on parameter approximation for von Mises-Fisher distributions, And a fast implementation of Is(x)" Suvrit Sra Computational Statistics, 2011 http://people.kyb.tuebingen.mpg.de/suvrit/papers/vmfFinal.pdf
Beta(Double, Double, Double, Double)
Computes the regularized incomplete beta function: int_0^x t^(a-1) (1-t)^(b-1) dt / Beta(a,b)
Declaration
public static double Beta(double x, double a, double b, double epsilon = 1E-15)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | The first argument - any real number between 0 and 1. |
| Double | a | The second argument - any real number greater than 0. |
| Double | b | The third argument - any real number greater than 0. |
| Double | epsilon | A tolerance for terminating the series calculation. |
Returns
| Type | Description |
|---|---|
| Double | The incomplete beta function at ( |
Remarks
The beta function is obtained by setting x to 1.
ChooseLn(Double, Double)
Evaluates the natural logarithm of Gamma(n+1)/(Gamma(k+1)*Gamma(n-k+1))
Declaration
public static double ChooseLn(double n, double k)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | n | |
| Double | k |
Returns
| Type | Description |
|---|---|
| Double |
ChooseLn(Double, Double[])
Evaluates the natural logarithm of Gamma(n+1)/(prod_i Gamma(k[i]+1))
Declaration
public static double ChooseLn(double n, double[] k)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | n | |
| Double[] | k |
Returns
| Type | Description |
|---|---|
| Double |
DifferenceOfExp(Double, Double)
Computes exp(x)-exp(y) to high accuracy.
Declaration
public static double DifferenceOfExp(double x, double y)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | Any real number |
| Double | y | Any real number |
Returns
| Type | Description |
|---|---|
| Double | exp(x)-exp(y) |
DiffLogSumExp(Double, Double, Double)
Computes log(exp(x)+exp(a))-log(exp(x)+exp(b)) to high accuracy.
Declaration
public static double DiffLogSumExp(double x, double a, double b)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | Any real number from -Inf to Inf, or NaN. |
| Double | a | A finite real number. |
| Double | b | A finite real number. |
Returns
| Type | Description |
|---|---|
| Double | log(exp(x)+exp(a))-log(exp(x)+exp(b)) |
Remarks
This function provides higher accuracy than a direct evaluation of
LogSumExp(x,a)-LogSumExp(x,b), particularly when x is large.
Digamma(Double)
Evaluates Digamma(x), the derivative of ln(Gamma(x)).
Declaration
public static double Digamma(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | Any real value. |
Returns
| Type | Description |
|---|---|
| Double | Digamma(x). |
Digamma(Double, Double)
Derivative of the natural logarithm of the multivariate Gamma function.
Declaration
public static double Digamma(double x, double d)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | A real value >= 0 |
| Double | d | The dimension, an integer > 0 |
Returns
| Type | Description |
|---|---|
| Double | digamma_d(x) |
Erfc(Double)
Computes the complementary error function. This function is defined by 2/sqrt(pi) * integral from x to infinity of exp (-t^2) dt.
Declaration
public static double Erfc(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | Any real value. |
Returns
| Type | Description |
|---|---|
| Double | The complementary error function at x. |
ErfcInv(Double)
Computes the inverse of the complementary error function, i.e.
erfcinv(erfc(x)) == x.
Declaration
public static double ErfcInv(double y)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | y | A real number between 0 and 2. |
Returns
| Type | Description |
|---|---|
| Double | A number x such that |
ExpMinus1(Double)
Computes the exponential of x and subtracts 1.
Declaration
public static double ExpMinus1(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | Any real number from -Inf to Inf, or NaN. |
Returns
| Type | Description |
|---|---|
| Double | exp(x)-1 |
Remarks
This function is more accurate than a direct evaluation of exp(x)-1 when x is small.
It is the inverse function to Log1Plus: ExpMinus1(Log1Plus(x)) == x.
ExpMinus1RatioMinus1RatioMinusHalf(Double)
Computes ((exp(x)-1)/x - 1)/x - 0.5
Declaration
public static double ExpMinus1RatioMinus1RatioMinusHalf(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | Any real number from -Inf to Inf, or NaN. |
Returns
| Type | Description |
|---|---|
| Double | ((exp(x)-1)/x - 1)/x - 0.5 |
Gamma(Double)
Evaluates Gamma(x), defined as the integral from 0 to x of t^(x-1)*exp(-t) dt.
Declaration
public static double Gamma(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | Any real value. |
Returns
| Type | Description |
|---|---|
| Double | Gamma(x). |
GammaLn(Double)
Computes the natural logarithm of the Gamma function.
Declaration
public static double GammaLn(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | A real value >= 0. |
Returns
| Type | Description |
|---|---|
| Double | ln(Gamma(x)). |
Remarks
This function provides higher accuracy than Math.Log(Gamma(x)), which may fail for large x.
GammaLn(Double, Double)
Computes the natural logarithm of the multivariate Gamma function.
Declaration
public static double GammaLn(double x, double d)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | A real value >= 0. |
| Double | d | The dimension, an integer > 0. |
Returns
| Type | Description |
|---|---|
| Double | ln(Gamma_d(x)) |
Remarks
The multivariate Gamma function is defined as Gamma_d(x) = pi^(d*(d-1)/4)*prod_(i=1..d) Gamma(x + (1-i)/2)
GammaLower(Double, Double)
Compute the regularized lower incomplete Gamma function: int_0^x t^(a-1) exp(-t) dt / Gamma(a)
Declaration
public static double GammaLower(double a, double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | a | The shape parameter, > 0 |
| Double | x | The upper bound of the integral, >= 0 |
Returns
| Type | Description |
|---|---|
| Double |
GammaUpper(Double, Double, Boolean)
Compute the regularized upper incomplete Gamma function: int_x^inf t^(a-1) exp(-t) dt / Gamma(a)
Declaration
public static double GammaUpper(double a, double x, bool regularized = true)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | a | The shape parameter. Must be > 0 if regularized is true or x is 0. |
| Double | x | The lower bound of the integral, >= 0 |
| Boolean | regularized | If true, result is divided by Gamma(a) |
Returns
| Type | Description |
|---|---|
| Double |
GammaUpperLogScale(Double, Double)
Computes log(x^a e^(-x)/Gamma(a)) to high accuracy.
Declaration
public static double GammaUpperLogScale(double a, double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | a | A positive real number |
| Double | x |
Returns
| Type | Description |
|---|---|
| Double |
GammaUpperScale(Double, Double)
Computes x^a e^(-x)/Gamma(a) to high accuracy.
Declaration
public static double GammaUpperScale(double a, double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | a | A positive real number |
| Double | x |
Returns
| Type | Description |
|---|---|
| Double |
IndexOfMaximum<T>(IEnumerable<T>)
Returns the index of the maximum element, or -1 if empty.
Declaration
public static int IndexOfMaximum<T>(IEnumerable<T> list)
where T : IComparable<T>
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | list |
Returns
| Type | Description |
|---|---|
| Int32 |
Type Parameters
| Name | Description |
|---|---|
| T |
IndexOfMaximumDouble(IList<Double>)
Returns the index of the maximum element, or -1 if empty.
Declaration
public static int IndexOfMaximumDouble(IList<double> list)
Parameters
| Type | Name | Description |
|---|---|---|
| IList<Double> | list |
Returns
| Type | Description |
|---|---|
| Int32 |
IndexOfMinimum<T>(IEnumerable<T>)
Returns the index of the minimum element, or -1 if empty.
Declaration
public static int IndexOfMinimum<T>(IEnumerable<T> list)
where T : IComparable<T>
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | list |
Returns
| Type | Description |
|---|---|
| Int32 |
Type Parameters
| Name | Description |
|---|---|
| T |
Log1MinusExp(Double)
Computes log(1 - exp(x)) to high accuracy.
Declaration
public static double Log1MinusExp(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | A non-positive real number: -Inf <= x <= 0, or NaN. |
Returns
| Type | Description |
|---|---|
| Double | log(1-exp(x)), which is always <= 0. |
Remarks
This function provides higher accuracy than a direct evaluation of log(1-exp(x)),
particularly when x < -5 or x > -1e-5.
Log1Plus(Double)
Computes the natural logarithm of 1+x.
Declaration
public static double Log1Plus(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | A real number in the range -1 <= x <= Inf, or NaN. |
Returns
| Type | Description |
|---|---|
| Double | log(1+x), which is always >= 0. |
Remarks
This function provides higher accuracy than a direct evaluation of log(1+x),
particularly when x is small.
Log1PlusExp(Double)
Computes log(1 + exp(x)) to high accuracy.
Declaration
public static double Log1PlusExp(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | Any real number from -Inf to Inf, or NaN. |
Returns
| Type | Description |
|---|---|
| Double | log(1+exp(x)), which is always >= 0. |
Remarks
This function provides higher accuracy than a direct evaluation of log(1+exp(x)),
particularly when x < -36 or x > 50.
Log1PlusExpGaussian(Double, Double)
Evaluates E[log(1+exp(x))] under a Gaussian distribution with specified mean and variance.
Declaration
public static double Log1PlusExpGaussian(double mean, double variance)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | mean | |
| Double | variance |
Returns
| Type | Description |
|---|---|
| Double |
LogDifferenceOfExp(Double, Double)
Computes log(exp(x) - exp(y)) to high accuracy.
Declaration
public static double LogDifferenceOfExp(double x, double y)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | Any real number from -Inf to Inf, or NaN. Must be greater or equal to y. |
| Double | y | Any real number from -Inf to Inf, or NaN. Must be less or equal to x. |
Returns
| Type | Description |
|---|---|
| Double |
Remarks
This function provides higher accuracy than a direct evaluation of log(exp(x)-exp(y)).
LogExpMinus1(Double)
Computes log(exp(x)-1) for non-negative x.
Declaration
public static double LogExpMinus1(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | A non-negative real number: 0 <= x <= Inf, or NaN. |
Returns
| Type | Description |
|---|---|
| Double |
|
Remarks
This function is more accurate than a direct evaluation of log(exp(x)-1) when x < 1e-3
or x > 50.
It is the inverse function to Log1PlusExp: LogExpMinus1(Log1PlusExp(x)) == x.
Logistic(Double)
Computes the logistic function 1/(1+exp(-x)).
Declaration
public static double Logistic(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | Any real number from -Inf to Inf, or NaN. |
Returns
| Type | Description |
|---|---|
| Double | 1/(1+exp(-x)). |
LogisticGaussian(Double, Double)
Calculate sigma(m,v) = \int N(x;m,v) logistic(x) dx
Declaration
public static double LogisticGaussian(double mean, double variance)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | mean | Mean |
| Double | variance | Variance |
Returns
| Type | Description |
|---|---|
| Double | The value of this special function. |
Remarks
Note 1-LogisticGaussian(m,v) = LogisticGaussian(-m,v) which is more accurate.
For large v we can use the big v approximation \sigma(m,v)=normcdf(m/sqrt(v+pi^2/3)).
For small and moderate v we use Gauss-Hermite quadrature.
For moderate v we first find the mode of the (log concave) function since this may be quite far from m.
LogisticGaussianDerivative(Double, Double)
Calculate \sigma'(m,v)=\int N(x;m,v)logistic'(x) dx
Declaration
public static double LogisticGaussianDerivative(double mean, double variance)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | mean | Mean. |
| Double | variance | Variance. |
Returns
| Type | Description |
|---|---|
| Double | The value of this special function. |
Remarks
For large v we can use the big v approximation \sigma'(m,v)=N(m,0,v+pi^2/3).
For small and moderate v we use Gauss-Hermite quadrature.
For moderate v we first find the mode of the (log concave) function since this may be quite far from m.
LogisticGaussianDerivative2(Double, Double)
Calculate \sigma''(m,v)=\int N(x;m,v)logistic''(x) dx
Declaration
public static double LogisticGaussianDerivative2(double mean, double variance)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | mean | Mean. |
| Double | variance | Variance. |
Returns
| Type | Description |
|---|---|
| Double | The value of this special function. |
Remarks
For large v we can use the big v approximation \sigma'(m,v)=-m/(v+pi^2/3)*N(m,0,v+pi^2/3).
For small and moderate v we use Gauss-Hermite quadrature.
The function is multimodal so mode finding is difficult and probably won't help.
LogisticGaussianRatio(Double, Double, Int32)
Calculate (kth derivative of LogisticGaussian)exp(0.5mean^2/variance)
Declaration
public static double LogisticGaussianRatio(double mean, double variance, int k)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | mean | |
| Double | variance | |
| Int32 | k |
Returns
| Type | Description |
|---|---|
| Double |
LogisticLn(Double)
Compute the natural logarithm of the logistic function, i.e. -log(1+exp(-x)).
Declaration
public static double LogisticLn(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | Any real number from -Inf to Inf, or NaN. |
Returns
| Type | Description |
|---|---|
| Double | -log(1+exp(-x)). |
Remarks
This function provides higher accuracy than a direct evaluation of -log(1+exp(-x)),
which can fail for x < -50 and x > 36.
Logit(Double)
Computes the log-odds function log(p/(1-p)).
Declaration
public static double Logit(double p)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | p | Any number between 0 and 1, inclusive. |
Returns
| Type | Description |
|---|---|
| Double | log(p/(1-p)) |
Remarks
This function is the inverse of the logistic function,
i.e. Logistic(Logit(p)) == p.
LogitFromLog(Double)
Compute log(p/(1-p)) from log(p).
Declaration
public static double LogitFromLog(double logp)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | logp | Any number between -infinity and 0, inclusive. |
Returns
| Type | Description |
|---|---|
| Double | log(exp(logp)/(1-exp(logp))) = -log(exp(-logp)-1). |
LogRatio(Double, Double)
Computes log(numerator/denominator) to high accuracy.
Declaration
public static double LogRatio(double numerator, double denominator)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | numerator | Any positive real number. |
| Double | denominator | Any positive real number. |
Returns
| Type | Description |
|---|---|
| Double | log(numerator/denominator) |
LogSumExp(IEnumerable<Double>)
Returns the log of the sum of exponentials of a list of doubles
Declaration
public static double LogSumExp(IEnumerable<double> list)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<Double> | list |
Returns
| Type | Description |
|---|---|
| Double |
LogSumExp(Double, Double)
Computes log(exp(x) + exp(y)) to high accuracy.
Declaration
public static double LogSumExp(double x, double y)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | Any real number from -Inf to Inf, or NaN. |
| Double | y | Any real number from -Inf to Inf, or NaN. |
Returns
| Type | Description |
|---|---|
| Double | log(exp(x)+exp(y)), which is always >= max(x,y). |
Remarks
This function provides higher accuracy than a direct evaluation of log(exp(x)+exp(y)).
LogSumExpSparse(IEnumerable<Double>)
Returns the log of the sum of exponentials of a list of doubles
Declaration
public static double LogSumExpSparse(IEnumerable<double> list)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<Double> | list |
Returns
| Type | Description |
|---|---|
| Double |
Max(IEnumerable<Double>)
Returns the maximum of a list of doubles
Declaration
public static double Max(IEnumerable<double> list)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<Double> | list |
Returns
| Type | Description |
|---|---|
| Double |
Median(Double[])
Returns the median of the array elements.
Declaration
public static double Median(double[] array)
Parameters
| Type | Name | Description |
|---|---|---|
| Double[] | array |
Returns
| Type | Description |
|---|---|
| Double | The median ignoring NaNs. |
Median(Double[], Int32, Int32)
Returns the median of elements in a subrange of an array.
Declaration
public static double Median(double[] array, int start, int length)
Parameters
| Type | Name | Description |
|---|---|---|
| Double[] | array | |
| Int32 | start | Starting index of the range. |
| Int32 | length | The number of elements in the range. |
Returns
| Type | Description |
|---|---|
| Double | The median of array[start:(start+length-1)], ignoring NaNs. |
Min(IEnumerable<Double>)
Returns the minimum of a list of doubles
Declaration
public static double Min(IEnumerable<double> list)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<Double> | list |
Returns
| Type | Description |
|---|---|
| Double |
NextDouble(Double)
Returns the smallest double precision number greater than value, if one exists. Otherwise returns value.
Declaration
public static double NextDouble(double value)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | value |
Returns
| Type | Description |
|---|---|
| Double |
NextDoubleWithPositiveDifference(Double)
Returns the smallest double precision number such that when value is subtracted from it, the result is strictly greater than zero, if one exists. Otherwise returns value.
Declaration
public static double NextDoubleWithPositiveDifference(double value)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | value |
Returns
| Type | Description |
|---|---|
| Double |
NormalCdf(Double)
Computes the cumulative Gaussian distribution, defined as the
integral from -infinity to x of N(t;0,1) dt.
For example, NormalCdf(0) == 0.5.
Declaration
public static double NormalCdf(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | Any real number. |
Returns
| Type | Description |
|---|---|
| Double | The cumulative Gaussian distribution at |
NormalCdf(Double, Double, Double)
Computes the cumulative bivariate normal distribution.
Declaration
public static double NormalCdf(double x, double y, double r)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | First upper limit. |
| Double | y | Second upper limit. |
| Double | r | Correlation coefficient. |
Returns
| Type | Description |
|---|---|
| Double |
|
Remarks
The cumulative bivariate normal distribution is defined as
int_(-inf)^x int_(-inf)^y N([x;y],[0;0],[1 r; r 1]) dx dy
where N([x;y],[0;0],[1 r; r 1]) = exp(-0.5*(x^2+y^2-2xyr)/(1-r^2))/(2pi*sqrt(1-r^2)).
NormalCdf(Double, Double, Double, Double)
Declaration
public static ExtendedDouble NormalCdf(double x, double y, double r, double sqrtomr2)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | |
| Double | y | |
| Double | r | |
| Double | sqrtomr2 |
Returns
| Type | Description |
|---|---|
| ExtendedDouble |
NormalCdfDiff(Double, Double)
Computes NormalCdf(x) - NormalCdf(y) to high accuracy.
Declaration
public static ExtendedDouble NormalCdfDiff(double x, double y)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | |
| Double | y |
Returns
| Type | Description |
|---|---|
| ExtendedDouble | The difference. |
NormalCdfExtended(Double)
Computes the cumulative Gaussian distribution, defined as the
integral from -infinity to x of N(t;0,1) dt.
For example, NormalCdf(0) == 0.5.
Declaration
public static ExtendedDouble NormalCdfExtended(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | Any real number. |
Returns
| Type | Description |
|---|---|
| ExtendedDouble | The cumulative Gaussian distribution at |
NormalCdfExtended(Double, Double, Double)
Computes the cumulative bivariate normal distribution.
Declaration
public static ExtendedDouble NormalCdfExtended(double x, double y, double r)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | First upper limit. |
| Double | y | Second upper limit. |
| Double | r | Correlation coefficient. |
Returns
| Type | Description |
|---|---|
| ExtendedDouble |
|
Remarks
The cumulative bivariate normal distribution is defined as
int_(-inf)^x int_(-inf)^y N([x;y],[0;0],[1 r; r 1]) dx dy
where N([x;y],[0;0],[1 r; r 1]) = exp(-0.5*(x^2+y^2-2xyr)/(1-r^2))/(2pi*sqrt(1-r^2)).
NormalCdfIntegral(Double, Double, Double)
Declaration
public static double NormalCdfIntegral(double x, double y, double r)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | |
| Double | y | |
| Double | r |
Returns
| Type | Description |
|---|---|
| Double |
NormalCdfIntegral(Double, Double, Double, Double)
Computes the integral of the cumulative bivariate normal distribution wrt x,
from -infinity to x.
Declaration
public static ExtendedDouble NormalCdfIntegral(double x, double y, double r, double sqrtomr2)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | First upper limit. |
| Double | y | Second upper limit. |
| Double | r | Correlation coefficient. |
| Double | sqrtomr2 | sqrt(1-r*r) |
Returns
| Type | Description |
|---|---|
| ExtendedDouble | r such that r*exp(exponent) is the integral |
NormalCdfIntegralRatio(Double, Double, Double)
Computes the integral of the cumulative bivariate normal distribution wrt x, divided by the cumulative bivariate normal distribution.
Declaration
public static double NormalCdfIntegralRatio(double x, double y, double r)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | First upper limit. |
| Double | y | Second upper limit. |
| Double | r | Correlation coefficient. |
Returns
| Type | Description |
|---|---|
| Double |
NormalCdfIntegralRatio(Double, Double, Double, Double)
Computes the integral of the cumulative bivariate normal distribution wrt x, divided by the cumulative bivariate normal distribution.
Declaration
public static double NormalCdfIntegralRatio(double x, double y, double r, double sqrtomr2)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | First upper limit. |
| Double | y | Second upper limit. |
| Double | r | Correlation coefficient. |
| Double | sqrtomr2 | sqrt(1-r*r) |
Returns
| Type | Description |
|---|---|
| Double |
NormalCdfInv(Double)
Computes the inverse of the cumulative Gaussian distribution,
i.e. NormalCdf(NormalCdfInv(p)) == p.
For example, NormalCdfInv(0.5) == 0.
This is also known as the Gaussian quantile function.
Declaration
public static double NormalCdfInv(double p)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | p | A real number in [0,1]. |
Returns
| Type | Description |
|---|---|
| Double | A number x such that |
NormalCdfLn(Double)
The natural logarithm of the cumulative Gaussian distribution.
Declaration
public static double NormalCdfLn(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | Any real number. |
Returns
| Type | Description |
|---|---|
| Double | ln(NormalCdf(x)). |
Remarks
This function provides higher accuracy than Math.Log(NormalCdf(x)), which can fail for x < -7.
NormalCdfLn(Double, Double, Double)
Computes the natural logarithm of the cumulative bivariate normal distribution.
Declaration
public static double NormalCdfLn(double x, double y, double r)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | First upper limit. |
| Double | y | Second upper limit. |
| Double | r | Correlation coefficient. |
Returns
| Type | Description |
|---|---|
| Double |
|
NormalCdfLogit(Double)
The log-odds of the cumulative Gaussian distribution.
Declaration
public static double NormalCdfLogit(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | Any real number. |
Returns
| Type | Description |
|---|---|
| Double | ln(NormalCdf(x)/(1-NormalCdf(x))). |
NormalCdfMomentRatio(Int32, Double)
Computes int_0^infinity t^n N(t;x,1) dt / (n! N(x;0,1))
Declaration
public static double NormalCdfMomentRatio(int n, double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | n | The exponent |
| Double | x | Any real number |
Returns
| Type | Description |
|---|---|
| Double |
NormalCdfMomentRatioDiff(Int32, Double, Double, Int32)
Computes NormalCdfMomentRatio(n, x+delta)-NormalCdfMomentRatio(n, x)
Declaration
public static double NormalCdfMomentRatioDiff(int n, double x, double delta, int startingIndex = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | n | A non-negative integer |
| Double | x | Any real number |
| Double | delta | A finite real number with absolute value less than 9.9, or less than 70% of the absolute value of x. |
| Int32 | startingIndex | The first moment to use in the power series. Used to skip leading terms. For example, 2 will skip NormalCdfMomentRatio(n+1, x). |
Returns
| Type | Description |
|---|---|
| Double |
NormalCdfRatio(Double)
Computes NormalCdf(x)/N(x;0,1) to high accuracy.
Declaration
public static double NormalCdfRatio(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | Any real number. |
Returns
| Type | Description |
|---|---|
| Double |
NormalCdfRatioDiff(Double, Double, Int32)
Computes NormalCdfRatio(x+delta)-NormalCdfRatio(x)
Declaration
public static double NormalCdfRatioDiff(double x, double delta, int startingIndex = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | A real number less than 37 |
| Double | delta | A finite real number with absolute value less than 9.9, or less than 70% of the absolute value of x. |
| Int32 | startingIndex | The first moment to use in the power series. Used to skip leading terms. For example, 2 will skip NormalCdfMomentRatio(1, x). |
Returns
| Type | Description |
|---|---|
| Double |
NormalCdfRatioLn(Double)
Computes ln(NormalCdf(x)/N(x;0,1)) to high accuracy.
Declaration
public static double NormalCdfRatioLn(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | Any real number. |
Returns
| Type | Description |
|---|---|
| Double |
NormalCdfRatioLn(Double, Double, Double, Double)
Computes the natural logarithm of the cumulative bivariate normal distribution, minus the log-density of the bivariate normal distribution at x and y, plus 0.5log(1-rr).
Declaration
public static double NormalCdfRatioLn(double x, double y, double r, double sqrtomr2)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | First upper limit. Must be finite. |
| Double | y | Second upper limit. Must be finite. |
| Double | r | Correlation coefficient. |
| Double | sqrtomr2 | sqrt(1-r*r) |
Returns
| Type | Description |
|---|---|
| Double |
|
OneMinusSqrtOneMinus(Double)
Computes 1-sqrt(1-x) to high accuracy.
Declaration
public static double OneMinusSqrtOneMinus(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | A real number between 0 and 1 |
Returns
| Type | Description |
|---|---|
| Double |
PreviousDouble(Double)
Returns the largest double precision number less than value, if one exists. Otherwise returns value.
Declaration
public static double PreviousDouble(double value)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | value |
Returns
| Type | Description |
|---|---|
| Double |
PreviousDoubleWithPositiveDifference(Double)
Returns the biggest double precision number such that when it is subtracted from value, the result is strictly greater than zero, if one exists. Otherwise returns value.
Declaration
public static double PreviousDoubleWithPositiveDifference(double value)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | value |
Returns
| Type | Description |
|---|---|
| Double |
ReciprocalFactorialMinus1(Double)
Computes 1/Gamma(x+1) - 1 to high accuracy
Declaration
public static double ReciprocalFactorialMinus1(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | A real number >= 0 |
Returns
| Type | Description |
|---|---|
| Double |
RemoveNaNs(Double[], Int32, Int32)
Given an array, returns a new array with all NANs removed.
Declaration
public static double[] RemoveNaNs(double[] array, int start, int length)
Parameters
| Type | Name | Description |
|---|---|---|
| Double[] | array | The source array |
| Int32 | start | The start index in the source array |
| Int32 | length | How many items to look at in the source array |
Returns
| Type | Description |
|---|---|
| Double[] |
RisingFactorialLnOverN(Double, Double)
Computes the logarithm of the Pochhammer function, divided by n: (GammaLn(x + n) - GammaLn(x))/n
Declaration
public static double RisingFactorialLnOverN(double x, double n)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | A real number > 0 |
| Double | n | If zero, result is 0. |
Returns
| Type | Description |
|---|---|
| Double |
Softmax(IList<Double>)
Exponentiate array elements and normalize to sum to 1.
Declaration
public static Vector Softmax(IList<double> x)
Parameters
| Type | Name | Description |
|---|---|---|
| IList<Double> | x | May be +/-infinity |
Returns
| Type | Description |
|---|---|
| Vector | A Vector p where |
Remarks
Sparse lists and vectors are handled efficiently
Tetragamma(Double)
Evaluates Tetragamma, the forth derivative of logGamma(x)
Declaration
public static double Tetragamma(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x |
Returns
| Type | Description |
|---|---|
| Double |
ToStringExact(Double)
Returns a decimal string that exactly equals a double-precision number, unlike double.ToString which always returns a rounded result.
Declaration
public static string ToStringExact(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x |
Returns
| Type | Description |
|---|---|
| String |
Trigamma(Double)
Evaluates Trigamma(x), the derivative of Digamma(x).
Declaration
public static double Trigamma(double x)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | Any real value. |
Returns
| Type | Description |
|---|---|
| Double | Trigamma(x). |
Trigamma(Double, Double)
Second derivative of the natural logarithm of the multivariate Gamma function.
Declaration
public static double Trigamma(double x, double d)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | x | A real value >= 0 |
| Double | d | The dimension, an integer > 0 |
Returns
| Type | Description |
|---|---|
| Double | trigamma_d(x) |
Ulp(Double)
Returns the positive distance between a value and the next representable value that is larger in magnitude.
Declaration
public static double Ulp(double value)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | value | Any double-precision value. |
Returns
| Type | Description |
|---|---|
| Double |
WeightedAverage(Double, Double, Double, Double)
Returns (weight1value1 + weight2value2)/(weight1 + weight2), avoiding underflow and overflow.
The result is guaranteed to be between value1 and value2, monotonic in value1 and value2,
and equal to Average(value1, value2) when weight1 == weight2.
Declaration
public static double WeightedAverage(double weight1, double value1, double weight2, double value2)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | weight1 | Any number >=0 |
| Double | value1 | Any number |
| Double | weight2 | Any number >=0 |
| Double | value2 | Any number |
Returns
| Type | Description |
|---|---|
| Double |