Wet grass/sprinkler/rain example
This example shows how to construct a discrete Bayesian network in Infer.NET and how to learn the parameters of the model (the conditional probability tables) from data. You can find the code for this example in the Examples Browser and (your install folder)/Samples/C#/ExamplesBrowser/WetGrassSprinklerRain.cs. The model is Kevin Murphy's wet grass/sprinkler/rain example.
Although all the variables in this example can only take on two values, the code is written in a way that is easily extendable to multiple states. The model is encapsulated in a class WetGrassSprinklerRainModel whose constructor defines the Infer.NET model. The model is defined in a way that all parameters are treated as random variables with priors settable at run time. The model class also has two static methods AddChildFromOneParent, and AddChildFromTwoParents which are general building blocks for discrete Bayesian networks and which you can incorporate into your model classes. These methods can be extended to more than two parents---see the forum thread How define a model with a variable dependent more than two.
There are three modes of operation:
These 3 cases are called in succession in the main program for this example. (1) and (3) are just illustrated for querying about Prob(Rain) under various assumptions; similar queries can be built up straightforwardly.
A simpler less general implementation of this model, which does not address parameter learning, can be found on the community forum.
Here is a factor graph of this model: