Infer.NET user guide : Tutorials and examples

## 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 src/Tutorials/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:

- The parameters are exactly known - in this case the priors on the parameters are irrelevant as the parameters are directly observed. We can then query the model in a variety of ways.
- The parameters are unknown, and we want to infer the parameters from observations on the primary variables
- The parameters are known with uncertainty (for example through Bayesian inference), and we want to query the model in the presence of that uncertainty

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: