Пример #1
0
        public void TestMethod1()
        {
            // Person says 'there are _some_ nice people in the world'
            // Given the world has 4 people (very simple world)
            // _some_ implies not all -- but a literal listener
            // does not infer this implication.
            Uncertain <int> literaloutput = Implicature.LiteralListener(Implicature.Utterance.Some);
            var             literalresult = new[] { 0.33, 0.33, 0.33 };

            foreach (var item in literaloutput.Support().Zip(literalresult, Tuple.Create))
            {
                Assert.IsTrue(ApproxEqual(item.Item1.Probability, item.Item2));
            }

            // Here we frame the listener as someone that reasons
            // about a speaker's implication. In other words,
            // we infer that the person is implying that not all
            // people are nice
            Uncertain <int> inferredoutput = Implicature.Listener(Implicature.Utterance.Some);
            var             inferredresult = new[] { 42, 0.42, 0.15 };

            foreach (var item in literaloutput.Support().OrderBy(k => k.Value).Zip(literalresult, Tuple.Create))
            {
                Assert.IsTrue(ApproxEqual(item.Item1.Probability, item.Item2));
            }
        }
Пример #2
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;
        }
Пример #3
0
        public static Uncertain <T> SampledInference <T>(this Uncertain <T> source, int samplesize, IEqualityComparer <T> comparer = null)
        {
            var sampler = Sampler.Create(source);
            // cache data
            var data = sampler.Take(samplesize).ToList();

            return(RunInference(data, comparer));
        }
Пример #4
0
        public void Constant_Sample()
        {
            // arrange
            Uncertain <double> X = 5.0;
            var sampler          = Sampler.Create(X);
            // act
            var S = sampler.First();

            // assert
            Assert.AreEqual(S.Value, 5.0);
        }
Пример #5
0
        private static IEnumerable <Uncertain <int> > LiftedFunc(IEnumerable <Uncertain <int> > unput)
        {
            Uncertain <int> ustate = 0;

            foreach (var u in unput)
            {
                ustate = from s in ustate
                         from i in u
                         let f = Func(s, i)
                                 select f;
                yield return(ustate);
            }
        }
Пример #6
0
        public void Base_Equality()
        {
            Uncertain <double> X = 5.0;

            if ((X == null).Pr())
            {
                Assert.Fail();
            }

            if ((null == X).Pr())
            {
                Assert.Fail();
            }
        }
Пример #7
0
        public void TestDan()
        {
            var r = new RandomMath();
            int N = 50;

            // Generate an array of independent estimates of whether a signal
            // is high or low
            Uncertain <bool>[] data = (from i in Enumerable.Range(0, N)
                                       let noise = r.NextGaussian(0, 0.01)
                                                   let vad = i > 15 && i < 30 ? 0.9 : 0.1
                                                             let param = Math.Abs(vad + noise)
                                                                         let f = new Bernoulli(param > 1 ? 1 : param)
                                                                                 select f).ToArray();
            // history operator we chatted about
            Uncertain <bool[]> history = data.USeq(N);

            // Inference computes a weighted bool[] object: effectively a histogram
            // The call to SampledInference needs to know (i) how many samples to take and how to compare bool[]
            Uncertain <bool[]> posterior = history.SampledInference(10000, new SequenceComparer <bool>());

            // now inspect by materializing a list
            List <Weighted <bool[]> > top5 = posterior
                                             .Support()                             // enumerate the histogram
                                             .OrderByDescending(k => k.Probability) // sorted by probability
                                             .Take(5)                               // just top 5
                                             .ToList();                             // produce list

            //var program = from bools in history
            //              let sum = bools.Select(Convert.ToInt32).Sum()
            //              from prior in new Gaussian(20, 0.01)
            //              where sum == (int) prior
            //              select bools;
            //Uncertain<bool[]> posterior1 = program.SampledInference(10000, new BoolArrayEqualityComparer());

            Func <bool[], bool[]> Intervalize = _ => _;

            var program = from bools in data.USeq(N)
                          select Intervalize(bools);

            // now inspect by materializing a list
            List <Weighted <bool[]> > top51 = posterior
                                              .Support()                             // enumerate the histogram
                                              .OrderByDescending(k => k.Probability) // sorted by probability
                                              .Take(5)                               // just top 5
                                              .ToList();                             // produce list


            // set breakpoint
            int x = 10;
        }
Пример #8
0
        public void Base_Implicit()
        {
            // arrange
            Uncertain <double> X = 5.0;
            Uncertain <double> Y = 6.0;
            // act
            Uncertain <double> Z = from x in X
                                   from y in Y
                                   select x + y; // Y is implicitly cast

            // assert
            MeanAndConfidenceInterval m2 = Z.ExpectedValueWithConfidence();

            Assert.IsTrue(m2.Mean > 11.0 - eps && m2.Mean < 11.0 + eps);
        }
Пример #9
0
        public void Constant_BNN_Sample()
        {
            // arrange
            Uncertain <double> X = new Constant <double>(5.0);
            Uncertain <double> Y = new Constant <double>(6.0);
            Uncertain <double> Z = from x in X
                                   from y in Y
                                   select x + y;
            var sampler = Sampler.Create(Z);
            // act
            var S = sampler.First();

            // assert
            Assert.AreEqual(S.Value, 11.0);
        }
Пример #10
0
        public void Base_Implicit()
        {
            // arrange
            Uncertain <double> X = 5.0;
            Uncertain <double> Y = 6.0;
            // act
            Uncertain <double> Z = from x in X
                                   from y in Y
                                   select x + y;
            var sampler = Sampler.Create(Z);
            var s       = sampler.First();

            // assert
            Assert.AreEqual(s.Value, 11.0);
        }
Пример #11
0
        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]);
            }
        }
Пример #12
0
        public void Gaussian_BNN_Sample()
        {
            // arrange
            Uncertain <double> X = new Gaussian(1.0, 1.0);
            Uncertain <double> Y = new Gaussian(4.0, 1.0);
            Uncertain <double> Z = from x in X
                                   from y in Y
                                   select x + y;
            var sampler = Sampler.Create(Z);

            foreach (var s in sampler.Take(100))
            {
                // assert
                Assert.IsTrue(s.Value >= -3 && s.Value <= 13.0);
            }
        }
Пример #13
0
        public void Uniform_BNN_Sample()
        {
            // arrange
            Uncertain <double> X = new Uniform <double>(1.0, 3.0);
            Uncertain <double> Y = new Uniform <double>(4.0, 5.0);
            Uncertain <double> Z = from x in X from y in Y select x + y;
            var sampler          = Sampler.Create(Z);

            // act
            foreach (var p in sampler.Take(100))
            {
                // act
                double s = p.Value;
                // assert
                Assert.IsTrue(s >= 1.0 && s <= 8.0);
            }
        }
Пример #14
0
        public static Uncertain <T[]> MarkovModel <T>(this Uncertain <T[]> observations, Uncertain <T> init, Func <T, Uncertain <T> > transition, Func <T, Uncertain <T> > emission) where T : IEquatable <T>
        {
            Func <IEnumerable <T>, Uncertain <T[]> > RunOne = obs =>
            {
                var initlst = from prior in init
                              select FunctionalList.Cons(prior, FunctionalList.Empty <T>());

                return(obs.Aggregate <T, Uncertain <FunctionalList <T> >, Uncertain <T[]> >(
                           initlst,
                           (list, obs_i) =>
                {
                    var program = from head in list
                                  let state = head.Head
                                              from next in transition(state)
                                              from emit in emission(next)
                                              where obs_i.Equals(emit)
                                              select FunctionalList.Cons(next, head);
                    return program;
                },
                           uncertainlst =>
                {
                    return from sample in uncertainlst
                    select sample.Reverse().Skip(1) /* skip prior */.ToArray();
                }));
            };

            return(from obs in observations
                   from result in RunOne(obs)
                   select result);

            //Uncertain < T[] > output = observations.Aggregate<Uncertain<T>, Uncertain<FunctionalList<T>>, Uncertain<T[]>>(
            //    FunctionalList.Empty<T>(),
            //    (i, j) =>
            //    {
            //        return from lst in i
            //               from sample in j
            //               select FunctionalList.Cons(sample, lst);
            //    },
            //    uncertainlst =>
            //    {
            //        return from sample in uncertainlst
            //               select sample.Reverse().ToArray();
            //    });
            //return output;
        }
Пример #15
0
        public static Uncertain <T[]> USeq <T>(this IEnumerable <Uncertain <T> > source, int num)
        {
            Uncertain <T[]> output = source.Take(num).Aggregate <Uncertain <T>, Uncertain <FunctionalList <T> >, Uncertain <T[]> >(
                FunctionalList.Empty <T>(),
                (i, j) =>
            {
                return(from lst in i
                       from sample in j
                       select FunctionalList.Cons(sample, lst));
            },
                uncertainlst =>
            {
                return(from sample in uncertainlst
                       select sample.Reverse().ToArray());
            });

            return(output);
        }
Пример #16
0
        public static Uncertain <R[]> USeq2 <T, R>(this IEnumerable <Uncertain <T> > source, Func <T[], R[]> selector)
        {
            Uncertain <R[]> output = source.Aggregate <Uncertain <T>, Uncertain <FunctionalList <T> >, Uncertain <R[]> >(
                FunctionalList.Empty <T>(),
                (i, j) =>
            {
                return(from lst in i
                       from sample in j
                       select FunctionalList.Cons(sample, lst));
            },
                uncertainlst =>
            {
                return(from sample in uncertainlst
                       let vec = sample.Reverse().ToArray()
                                 select selector(vec));
            });

            return(output);
        }
Пример #17
0
        public void Base_Sample()
        {
            Uncertain <GeoLocation> roads = null;
            Uncertain <GeoLocation> gps   = null;
            Func <GeoLocation, GeoLocation, double> Likelihood = (a, b) => 1.0;
            var p =
                from pos in gps
                from road in roads
                let prob = Likelihood(pos, road)
                           select new Weighted <GeoLocation>(pos, prob);

            // arrange
            Uncertain <double> X = 5.0;
            // act
            var sampler = Sampler.Create(X);
            var S       = sampler.First();

            // assert
            Assert.AreEqual(S.Value, 5.0);
        }
Пример #18
0
        static Uncertain <string[]> HMM2(Uncertain <string[]> observations)
        {
            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");
            };

            return(observations.MarkovModel <string>(start_probability, transition_probability, emission_probability));
        }
Пример #19
0
        public void Gaussian_BNN_Mean()
        {
            // arrange
            Uncertain <double> X = new Gaussian(1.0, 1.0);
            Uncertain <double> Y = new Gaussian(4.0, 2.0);
            Uncertain <double> Z = from x in X
                                   from y in Y
                                   select x + y;
            double Sum = 0.0;
            // act
            var sampler = Sampler.Create(Z);

            foreach (var s in sampler.Take(100))
            {
                Sum += s.Value;
            }
            Sum /= 100.0;
            // assert
            // Z is known to be Gaussian(5.0, sqrt(5))
            // If everything is working, this has about a 0.003% chance of a false positive
            // (99.9997% confidence interval with n=100, sigma=sqrt(5) is +/- 0.89)
            Assert.IsTrue(Sum >= 4.11 && Sum <= 5.89);
        }
Пример #20
0
 public static Uncertain <T> Inference <T>(this Uncertain <T> source, IEqualityComparer <T> comparer = null)
 {
     return(RunInference(source.Support().ToList(), comparer));
 }
Пример #21
0
 public static Uncertain <T> Where <T>(this Uncertain <T> source, Predicate <T> predicate)
 {
     return(new Where <T>(source, predicate));
 }