Infer.NET user guide : The Infer.NET Modelling API
Markov chains and grids
A wide variety of Markov chain and grid structured models can be created using VariableArrays. The basic idea is that when looping over a range with a ForEach block, you can access the loop counter i and use expressions of the form (i-k) or (i+k) where k is a constant integer. This is called "offset indexing" and allows you to link the array element at index i with index i-k of the same array or another array.
For example, suppose we want to simulate a Gaussian random walk where x[t] holds the position at time t. This can be written using a VariableArray as follows:
The key expression here is x[t-1] where t is the loop counter. This creates the link between consecutive elements of the array. Note that in order to use this expression, we had to test for t>0, because a ForEach block always loops from 0 to the end of the range. In general, if you use (i-k) in a statement, you must surround the statement with If(i > k-1), and if you use (i+k) in a statement, you must surround the statement with If(i < length-k). Similarly, the base case must be defined using If(t == 0) or Case(t,0), and this must precede the definition of the increment case.
Because the model is defined using a VariableArray and not a C# array of Variable objects, the Infer.NET compiler can generate compact code that iterates over the array during inference. Also, the array length can be changed at runtime (by setting numTimes.ObservedValue).
If we wanted the position at time t to depend on the two previous times, we could write:
Array elements can also be linked via constraints. For example, the definition of array x could be changed to:
Note this defines a somewhat different model to the above.
A grid model is constructed by offset indexing on a two dimensional array (or jagged array).
Here we have linked rows by parent factors and columns via constraints, to show both approaches. Since jagged arrays can have any depth, you can use this pattern to define higher-dimensional structures like cubes and hypercubes. There is no limitation on which elements can be connected---a node can be connected to 8 neighbors, for example.
Maximum of an array
Infer.NET provides a Max operation between two double variables. Using offset indexing, we can extend this to compute the Max of a VariableArray. The idea is to incrementally build up the partial maximums in an array, and then return the last element:
The same idea can be used for counting. Infer.NET provides a CountTrue factor, but if it didn't, you could simulate one using offset indexing. The idea is to incrementally build up the partial sums in an array, and then return the last element:
The subtlety here is setting up the ValueRange for the sumUpTo array. Each element of sumUpTo ranges over a different number of values, so the size of its ValueRange must depend on n. A similar approach can be used to solve other counting problems, such as computing the probability of observing a run of H heads in a sequence of N coin flips.
See Chess Analysis for another example of offset indexing.