Exemple #1
0
        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;
        }
Exemple #2
0
        public void TestHMM3()
        {
            var comparer = new SequenceComparer <string>();
            var states   = new[] { "Healthy", "Fever" };
            var emits    = new[] { "normal", "cold", "dizzy" };

            var start_probability = new Multinomial <string>(states, new[] { 0.6, 0.4 });

            Func <string, Multinomial <string> > transition_probability = state =>
            {
                if (state == "Healthy")
                {
                    return(new Multinomial <string>(states, new[] { 0.7, 0.3 }));
                }
                if (state == "Fever")
                {
                    return(new Multinomial <string>(states, new[] { 0.4, 0.6 }));
                }

                throw new Exception("Unknown state");
            };

            Func <string, Multinomial <string> > emission_probability = state =>
            {
                if (state == "Healthy")
                {
                    return(new Multinomial <string>(emits, new[] { 0.5, 0.4, 0.1 }));
                }
                if (state == "Fever")
                {
                    return(new Multinomial <string>(emits, new[] { 0.1, 0.3, 0.6 }));
                }

                throw new Exception("Unknown state");
            };

            var r = new Random(0);

            var observations = Enumerable.Range(0, 20).Select(_ =>
            {
                var probs           = new[] { r.NextDouble(), r.NextDouble(), r.NextDouble() };
                double total        = probs.Sum();
                var probsNormalized = from p in probs select p / total;
                return(new Multinomial <string>(emits, probsNormalized));
            }).USeq <string>(20);

            //var program = observations.MarkovModel<string>(start_probability, transition_probability, emission_probability);
            //var output = program.SampledInference(1, comparer).Support().OrderByDescending(k => k.Probability).ToList();

            int x = 10;
        }