|
Infer.NET user guide : The Infer.NET Modelling API
Applying functions and operators to variablesAs well as creating random variables directly from prior distributions, you can also derive them from other variables by operations such as addition, multiplication, and comparison. For example, if a and b are random variables, then (a+b) returns a new random variable. All of the operators in C# have been overloaded to provide this functionality. Additionally, the library provides functions on random variables that are useful for statistical modelling, such as taking the inner product between random vectors. These can all be found as static methods of the Variable class. A wide variety of models can be constructed from these primitives. If you need new primitive operations on random variables, you can use the extension mechanism described in How to add new factors and message operators. Deriving a variable using overloaded operatorsHere is an example of creating a random variable y which is the sum of the variables a and b:
This creates a boolean random variable c which indicates if a is greater than b:
Notice that the results of operators are strongly typed as appropriate (in this case the result is a boolean random variable). Operators and functions can also be chained together to produce complex expressions, such as
Under the hood, each overloaded operator is converted into an equivalent call to Variable<double>. For example, the definition of y above is equivalent to:
All overloaded operators have equivalent calls to static methods on Factor such as Sum, Difference, Product, Ratio, And, Or, and GreaterThan. This underlying mechanism for invoking functions is described below. Deriving a variable using a functionBesides overloaded operators, the Variable class also provides static methods to create derived variables. For example:
Like overloaded operators, these calls convert to an equivalent call to Variable<double>.Factor. For example, the definition of h and x above are equivalent to:
The most general way of creating a derived random variable is:
Variable<T> var = Variable<T>.Factor(MethodName, arg1, arg2, ...); which creates a new random variable of type T, as the result of calling MethodName with the specified arguments (which may be random variables, constants or parameters). The term factor comes from factor graphs. The Factor class contains most of the functions built into Infer.NET. Built-in factor methods include: And, Bernoulli, Difference, Gaussian, InnerProduct, IsBetween, IsPositive, MatrixMultiply, Not, Or, Product, Ratio, Sum and VectorGaussian (see the full list of factors for more). You can also define your own factor methods.
|


