dotnet / infer

Infer.NET is a framework for running Bayesian inference in graphical models
https://dotnet.github.io/infer/
MIT License
1.56k stars 230 forks source link

How to create a simple discrete example like Monty Hall #196

Closed jacowp357 closed 4 years ago

jacowp357 commented 4 years ago

I would like to get help with creating a very simple discrete model like the Monty Hall problem without parameter learning? Not sure if this is the appropriate channel to ask this.

tminka commented 4 years ago

There is an implementation of the Monty Hall problem in the Examples and the tests.

jacowp357 commented 4 years ago

I'm new to c# and infer.net, perhaps starting with a simpler example in my own code will help me figure it out.

I would like to know a simple way to create the factor p(y|x,z) in my code below:

code

Here is a drawing of the graph I want to experiment with:

RB 2019-11-18 12.45.04.pdf

tminka commented 4 years ago

The simplest way is to use boolean variables along with logical operators:

var y = ((x == z) == Variable.Bernoulli(0.1));
var r = (y == Variable.Bernoulli(0.8));
jacowp357 commented 4 years ago

It works thanks! However, for VMP it gives the following error:

"Unhandled exception. Microsoft.ML.Probabilistic.Compiler.CompilationFailedException: MessageTransform failed with 2 error(s) and 0 warning(s): Error 0: This model is not supported with VariationalMessagePassing due to Factor.AreEqual(bool areEqual, bool a, bool b). Try using a different algorithm or expressing the model differently in Factor.AreEqual(vbool2_use, vbool3_use) Details: Variational Message Passing does not support an AreEqual factor with fixed output. using parameter types: [areEqual] bool,[a] Bernoulli,[to_a] Bernoulli,[b] Bernoulli,[to_b] Bernoulli,[result] Bernoulli"

How can the model be expressed differently in this case?

tminka commented 4 years ago

Another way, that VMP will like, is to branch on y:

var r = Variable.New<bool>();
using(Variable.If(y)) {
  r.SetTo(Variable.Bernoulli(0.8));
}
using(Variable.IfNot(y)) {
  r.SetTo(Variable.Bernoulli(0.2));
}
jacowp357 commented 4 years ago

How can I interpret the branching on y? Did the structure of the graph change? How did this change make it possible for VMP?

tminka commented 4 years ago

Infer.NET can show you the factor graph. The theory behind branching, how it changes the factor graph, and how it interacts with message-passing inference algorithms is described in the paper Gates: A Graphical Notation for Mixture Models.

jacowp357 commented 4 years ago

Here is the factor graph generated with ShowFactorGraph.

infer net-factor-graph

I'm not sure how to translate this graph to the gate notation in the paper you shared (maybe Figure 1 (b)?). Please see my attempt attached. I'm not sure if random variable y should be included in the gate?

RB 2019-12-02 11.22.34.pdf

tminka commented 4 years ago

In the generated graph, y is the gate condition, and the other two subgraphs leading to "False" (which is r) are inside the gates. y is not inside the gates, nor connected to anything inside the gates.

tminka commented 4 years ago

I have edited the Clinical trial tutorial (which is similar to the model above) to discuss how the factor graph is displayed.

jacowp357 commented 4 years ago

I'm running Visual Studio for Mac (8.3.10) and battling with the windows forms etc. Is there a workaround to getting the DGML format? Or other requirements I need to install perhaps?

tminka commented 4 years ago

You can now get the graph in DGML format on any platform.