Getting Started with Infer.Net
-
Friday, December 02, 2011 12:51 PM
Hi all,
Just download infer.net and tested it by using the coin example from the pdc2009 video. It tested for 2 heads and I got the right answer. I would like to learn the use of infer.net but it appears you need some good probability backing so as to do something useful with it. I will like to learn with 2 examples. If someone can help me with the code for those 2, it will serve as a good place for me to start learning.
1. Given a bag of 3 coins, 1 loaded (0.9H, 0.1T) and 2 normal(0.5H|T), if I pick a coin at random (1/3 chance of picking each coin), toss it and observe HHHTH, what is the probability that I picked the loaded coin.
2. Given a bag of 3 dice, 1 loaded (0.5(One), 0.1(Two-Six)) and 2 normal (1/6 of each value), if I pick a coin at random (1/3 chance of picking each coin), roll it and observer 1-1-1-5-1, what is the probability that I picked the loaded die?
PS: The 2nd question is the same as the 1st except with dice so I can see how the code changes if it is not just true/false.
Thanks in advance.
All Replies
-
Friday, December 02, 2011 8:22 PMOwner
Here is one way to implement this for 1.
2 is similar but uses Discrete distributions and integer random variables.
John
// The model
var numCoins = Variable.New<int>();
var numTosses = Variable.New<int>();
Range c = new Range(numCoins);
Range t = new Range(numTosses);
var coin = Variable.DiscreteUniform(c);
var toss = Variable.Array<bool>(t);
var coinPrior = Variable.Array<Bernoulli>(c);
using (Variable.Switch(coin))
{
toss[t] = Variable<bool>.Random(coinPrior[coin]).ForEach(t);
}
// Observations. true = heads, baised coin is first one
bool[] observedTosses = new bool[] { true, true, true, false, true };
Bernoulli[] observedCoinPriors = new Bernoulli[] {
new Bernoulli(0.9), new Bernoulli(0.5), new Bernoulli(0.5) };
// Hook up the observations
numCoins.ObservedValue = observedCoinPriors.Length;
coinPrior.ObservedValue = observedCoinPriors;
numTosses.ObservedValue = observedTosses.Length;
toss.ObservedValue = observedTosses;
// Run the inference
InferenceEngine engine = new InferenceEngine();
Discrete postCoin = engine.Infer<Discrete>(coin);
Console.WriteLine("Probability I picked biased coin = {0:0.000}", postCoin.GetProbs()[0]);
-
Saturday, December 03, 2011 1:01 PM
Hi John,
Thanks for the response. I'll try it and get back to you.
regards,