nxtruong_ac posted on 12-16-2010 7:48 PM
Hi all,
I'm learning infer.NET. I wrote an IronPython program for the sprinkler/rain example by Kevin Murphy (webpage, following this
C# code). The Python code is as given below. The problem is that the result is very wrong: S|W=T is Bernoulli(0.8333) and R|W=T is Bernoulli(0.3333), while the correct result
should be 0.430 and 0.708 respectively. I know it is approximated, but it's too far from the exact result. What's wrong in my code?
BTW, I used Infer.NET 2.4 beta with .NET 4, VS 2010, on Windows 7 running inside a VMWare virtual machine.
Thanks,
Truong
######################
import System
from System import *
import clr
import sys
sys.path.append(r'C:\Program Files\Microsoft Research\Infer.NET 2.4 Beta\bin\Release')
clr.AddReferenceToFile("Infer.Compiler.dll")
clr.AddReferenceToFile("Infer.Runtime.dll")
# import all namespaces
import MicrosoftResearch.Infer
import MicrosoftResearch.Infer.Models
import MicrosoftResearch.Infer.Distributions
from MicrosoftResearch.Infer import *
from MicrosoftResearch.Infer.Models import *
from MicrosoftResearch.Infer.Distributions import *
##### The model
C = Variable.Bernoulli(0.5).Named("Cloudy") # cloudy variable
# sprinkler & rain conditioned on C
with (Variable.If(C)):
S = Variable.Bernoulli(0.1)
R = Variable.Bernoulli(0.8)
with (Variable.IfNot(C)):
S = Variable.Bernoulli(0.5)
R = Variable.Bernoulli(0.2)
# wet grass
with (Variable.If(S)):
with (Variable.If(R)):
W = Variable.Bernoulli(0.99)
with (Variable.IfNot(R)):
W = Variable.Bernoulli(0.9)
with (Variable.IfNot(S)):
with (Variable.If(R)):
W = Variable.Bernoulli(0.9)
with (Variable.IfNot(R)):
W = Variable.Bernoulli(0.0)
## The inference
ie = InferenceEngine()
# ie.NumberOfIterations = 1000
W.ObservedValue = True
print "Probability of Sprinkler given Wet = True:", ie.Infer(S)
print "Probability of Rain given Wet = True:", ie.Infer(R)