public void TestHMM2() { var comparer = new SequenceComparer <string>(); var states = new[] { "Healthy", "Fever" }; var emits = new[] { "normal", "cold", "dizzy" }; Uncertain <string>[] source = new[] { new Multinomial <string>(emits, new [] { 0.8, 0.1, 0.1 }), new Multinomial <string>(emits, new [] { 0.2, 0.7, 0.1 }), new Multinomial <string>(emits, new [] { 0.05, 0.05, 0.9 }), }; Uncertain <string[]> observations = source.USeq(3); var program = from obs in observations from result in HMM(obs) select result; var program1 = from obs in observations from result in HMM(obs).Inference(comparer) select result; var program2 = from obs in observations.Inference(comparer) from result in HMM(obs).Inference(comparer) select result; var output = program.Inference(comparer).Support().OrderByDescending(k => k.Probability).ToList(); var output1 = program1.Inference(comparer).Support().OrderByDescending(k => k.Probability).ToList(); var output2 = program2.Inference(comparer).Support().OrderByDescending(k => k.Probability).ToList(); var output3 = HMM2(observations).Inference(comparer).Support().OrderByDescending(k => k.Probability).ToList(); var timer = new System.Diagnostics.Stopwatch(); timer.Start(); var sampled = program.SampledInference(10000, comparer).Support().OrderByDescending(k => k.Probability).ToList(); timer.Stop(); System.Diagnostics.Debug.WriteLine(timer.Elapsed); timer.Reset(); timer.Start(); var sampled1 = program1.SampledInference(10000, comparer).Support().OrderByDescending(k => k.Probability).ToList(); timer.Stop(); System.Diagnostics.Debug.WriteLine(timer.Elapsed); timer.Reset(); timer.Start(); var sampled2 = program2.SampledInference(10000, comparer).Support().OrderByDescending(k => k.Probability).ToList(); timer.Stop(); System.Diagnostics.Debug.WriteLine(timer.Elapsed); Assert.IsTrue(comparer.Equals(output[0].Value, sampled[0].Value)); Assert.IsTrue(Math.Abs(output[0].Probability - sampled[0].Probability) < 0.1); Assert.IsTrue(comparer.Equals(output[0].Value, output3[0].Value)); Assert.IsTrue(Math.Abs(output[0].Probability - output3[0].Probability) < 0.1); int x = 10; }
public void Base_Binomial() { Uncertain <int> binomial = from a in new Flip(0.5) from b in new Flip(0.5) from c in new Flip(0.5) let path = Convert.ToInt32(a) + Convert.ToInt32(b) + Convert.ToInt32(c) select path; var dist = binomial.Inference().Support().OrderBy(k => k.Value).Select(k => k.Probability).ToList(); var items = new[] { 0.125, 0.375, 0.375, 0.125 }; for (int i = 0; i < items.Length; i++) { Assert.IsTrue(items[i] == dist[i]); } }