Skip to main content

Factors and Constraints

## Double factors

This page lists the built-in methods and operators for creating random variables of type double. For both static methods and operators, you can often pass in random variables as arguments e.g. `Variable<double>` instead of double. For compactness, this is not shown in the syntax below.

These methods provide a convenient short alternative to using `Variable<T>.Factor` and passing in the factor method, as described on this page.

#### Distribution Factors

A distribution factor creates a random variable from a parameterised distribution.

Distribution Syntax Description
Gaussian `Variable.GaussianFromMeanAndPrecision(double mean, double precision)` Creates a double random variable with a Gaussian distribution of the specified mean and precision (inverse variance).
Gaussian `Variable.GaussianFromMeanAndVariance(double mean, double variance)` Creates a double random variable with a Gaussian distribution of the specified mean and variance.
Gamma `Variable.GammaFromShapeAndScale(double shape, double scale)` Creates a positive double random variable with a Gamma distribution of the specified shape and scale.
Gamma `Variable.GammaFromShapeAndRate(double shape, double rate)` Creates a positive double random variable with a Gamma distribution of the specified shape and rate.
Gamma `Variable.GammaFromMeanAndVariance(double mean, double variance)` Creates a positive double random variable with a Gamma distribution of the specified mean and variance.
Beta `Variable.Beta(double trueCount, double falseCount)` Creates a double random variable with a Beta distribution with the specified counts.
Beta `Variable.BetaFromMeanAndVariance(double mean, double variance)` Creates a double random variable with a Beta distribution of the specified mean and variance.
Truncated Gaussian `Variable.TruncatedGaussian(double mean, double variance, double lowerBound, double upperBound)` Creates a double random variable with a TruncatedGaussian distribution of the specified mean, variance, lower bound, and upper bound.
Truncated Gamma `Variable.TruncatedGamma(double shape, double rate, double lowerBound, double upperBound)` Creates a double random variable with a TruncatedGamma distribution of the specified shape, rate, lower bound, and upper bound.

#### Arithmetic and Mathematical Operations

Arithmetic operations are supported via operator overloads or static methods.

Operation Syntax Description
Plus a + b Creates a double random variable equal to the sum of `a` and `b`.
Minus a - b Creates a double random variable equal to the difference of `a` and `b`.
Negative -a Creates a double random variable equal to the negative of `a`.
Times a * b Creates a double random variable equal to the product of `a` and `b`.
Divide a / b Creates a double random variable equal to the ratio of `a` to `b`.
Power a ^ b Creates a doublerandom variable equal to `a` raised to the power `b`.
Sum of elements `Variable.Sum(double[] array)` `Variable.Sum(IList<double> array)` Creates a double random variable equal to the sum of the elements of the array.
Exp `Variable.Exp(double exponent)` Creates a double random variable which takes e to the power of exponent.
Log `Variable.Exp(double x)` Creates a double random variable equal to the natural log of `x`.
Logistic `Variable.Logistic(double x)` Creates a double random variable equal to `1/(1+exp(-x))`.
Max `Variable.Max(double a, double b)` Creates a double random variable equal to the maximum of `a` and `b`.
Min `Variable.Min(double a, double b)` Creates a double random variable equal to the minimum of `a` and `b`.
Conditional sum of elements `Variable.SumWhere(bool[] a, Vector b)` Creates a double random variable equal to the sum of the elements of `b` where `a` is true.
Inner product `Variable.InnerProduct(Vector a, Vector b)` `Variable.InnerProduct(double[] a, Vector b)` `Variable.InnerProduct(double[] a, double[] b)` Creates a double random variable equal to the inner product of vectors `a` and `b`.

#### Miscellaneous Operations

Operation Syntax Description
FunctionEvaluate `Variable.FunctionEvaluate(IFunction func, Vector x)` Evaluate a random function at a point. Used to construct Gaussian Process models like this one.
GetItem `Variable.GetItem(Vector source, int index)` Extract an element of a random vector.
Double `Variable.Double(int integer)` Convert an integer to double.