Exemplo n.º 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));
            }
        }
Exemplo n.º 2
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;
        }
Exemplo n.º 3
0
 public static Uncertain <T> Inference <T>(this Uncertain <T> source, IEqualityComparer <T> comparer = null)
 {
     return(RunInference(source.Support().ToList(), comparer));
 }