## Running inferenceInference is concerned with the calculation of posterior probabilities based on one or more data observations. The word 'posterior' is used to indicate that the calculation occurs
If you run this, you will get output as follows:
The Gaussian distribution shows the mean and variance of the mean marginal, and the Gamma distribution shows the shape and scale of the precision marginal. The pattern inherent in the this simple program is common to many inference problems. We start with a model which has some structure defined by some unknown parameters (considered as variables), their prior uncertainties, and their relationships. We then provide some observed data. Finally we perform inference and retrieve the posterior marginal distributions of the variables we are interested in. The term 'marginal' is a common term in statistics which refers to the operation of 'summing out' (in the case of discrete variables) or 'integrating out' (in the case of continuous variables) the uncertainty associated with all other variables in the problem. The inference methods in Infer.NET make heavy use of fully factorised approximations; i.e. approximations which are a product of functions of individual variables. Although the true posteriors may be joint distributions over the participating variables, the approximate posteriors in such models are naturally in the form of single variable marginal distributions. In the example the marginal posteriors, ## Creating an inference engineAll inferences in Infer.NET are achieved through the use of an
In this example above, we explicitly set the algorithm to Variational Message Passing (VMP). Other available algorithms are Expectation Propagation (EP) and Gibbs sampling. Between them, these three algorithms can solve a wide range of inference problems; they are discussed further in working with different inference algorithms. As well the inference algorithm, the engine has a number of other settings which you can modify to affect how inference is performed and what is displayed during the inference process. ## Performing inferenceHaving created an inference engine, you can use it to infer marginal distributions for variables you are interested in, using the
For array variables, you can specify an array type over distributions. Here are some examples, the first one showing inference on a 1-D random variable array where each element of the array has a Gaussian distribution, the second showing inference on a 1-D array of 2-D arrays of random variables where each element has a Gamma distribution.
Note that this syntax is for the usual situation where the random variables have been defined with Variable.Array as described in the sections on variable arrays and jagged variable arrays. If you use .NET arrays for your variables, then you must call infer for each element of the array. ## Inferring constant and observed variablesâ€‹Besides random variables, you can call
The result here will be an array of Gaussians, each a point mass on the observed value. This is useful when you want to experiment with making some variables observed vs. random. For example, if you change the definition of ## When does inference happen for a particular variable?When you call Infer() the first time, the inference engine will collect all factors and variables related to the variable that you are inferring (i.e. the model), compile an inference algorithm, run the algorithm, and return the result. This is an expensive process, so the results are cached at multiple levels. If you call Infer() on the same variable again without changing anything, then it will immediately return the cached result from the last call. If you call Infer() on another variable in the same model, then it will return the cached result for that variable, if one is available. For instance, in the example above, when In some cases, you want more control over what variables are inferred when you call Infer(). To do this, set the
After setting this property, the next call to Reasons for using - You want to remove the (normally small) overhead of calculating additional marginals that will not be used.
- You want to control when inference happens e.g. to ensure a user interface remains responsive
- You want to ensure that inference happens in one go, and not in a number of separate runs of the inference algorithm.
## Alternative inference queriesSo far, we have only considered how to use an inference engine to retrieve marginal distributions. However, some inference algorithms can perform other types of inference query to return other quantities (such as lists of samples from the marginal). To perform alternative inference queries, you pass a second argument to
When using QueryTypes, it is a good idea to tell the compiler in advance what QueryTypes you want to infer. Otherwise, inference may re-run on each query. To specify the QueryTypes in advance, attach them as attributes to the variable, like so:
Notice that in this case, you need to explicitly specify whether you want to infer Marginals. If you attach this attribute to each variable you want to sample, then when you request samples for multiple variables, the sampler is only run once. The samples at the same position in the returned lists correspond to a consistent vector sample. |