public void TestEvalBatching()
        {
            var m = new Multinomial(2018);

            var src = new TensorProxy
            {
                data = new Tensor(2, 3, new[]
                {
                    Mathf.Log(0.1f) - 50, Mathf.Log(0.2f) - 50, Mathf.Log(0.7f) - 50,
                    Mathf.Log(0.3f) - 25, Mathf.Log(0.4f) - 25, Mathf.Log(0.3f) - 25
                }),
                valueType = TensorProxy.TensorType.FloatingPoint
            };

            var dst = new TensorProxy
            {
                data      = new Tensor(2, 3),
                valueType = TensorProxy.TensorType.FloatingPoint
            };

            DiscreteActionOutputApplier.Eval(src, dst, m);

            float[] reference = { 2, 2, 2, 0, 1, 0 };
            for (var i = 0; i < dst.data.length; i++)
            {
                Assert.AreEqual(reference[i], dst.data[i]);
                ++i;
            }
        }
Exemple #2
0
        public void MontyHall()
        {
            var doors   = Enumerable.Range(0, 3);
            var cardoor = new Multinomial <int>(doors);

            var program = from carDoorNum in new Multinomial <int>(doors)
                          from chosenDoor in new Multinomial <int>(doors)
                          let match = carDoorNum == chosenDoor
                                      let possibleOpenDoors2 = doors.Where(i => i != carDoorNum).ToList()
                                                               let possibleOpenDoors1 = doors.Where(i => i != carDoorNum && i != chosenDoor).ToList()
                                                                                        let nextdoor = match ? new Multinomial <int>(possibleOpenDoors1) : new Multinomial <int>(possibleOpenDoors2)
                                                                                                       from otherDoorContainsCar in nextdoor
                                                                                                       let chosenDoorContainsCar = chosenDoor == carDoorNum
                                                                                                                                   select chosenDoorContainsCar;
            // note chosenDoorContainsCar is never used but implicitly impacts the result
            // because we need to compute over the nextdoor probabilistic choice.

            var ans    = program.Inference();
            var result = new[] { 0.66, 0.33 };

            foreach (var item in ans.Support().OrderBy(k => k.Value).Zip(result, Tuple.Create))
            {
                Assert.IsTrue(ApproxEqual(item.Item1.Probability, item.Item2));
            }

            var sampled = program.SampledInference(10000);

            foreach (var item in sampled.Support().OrderBy(k => k.Value).Zip(result, Tuple.Create))
            {
                Assert.IsTrue(ApproxEqual(item.Item1.Probability, item.Item2));
            }
        }
Exemple #3
0
        public void TestEvalBatching()
        {
            Multinomial m = new Multinomial(2018);

            TensorProxy src = new TensorProxy
            {
                Data = new Tensor(2, 3, new []
                {
                    Mathf.Log(0.1f) - 50, Mathf.Log(0.2f) - 50, Mathf.Log(0.7f) - 50,
                    Mathf.Log(0.3f) - 25, Mathf.Log(0.4f) - 25, Mathf.Log(0.3f) - 25
                }),
                ValueType = TensorProxy.TensorType.FloatingPoint
            };

            TensorProxy dst = new TensorProxy
            {
                Data      = new Tensor(2, 3),
                ValueType = TensorProxy.TensorType.FloatingPoint
            };

            m.Eval(src, dst);

            float[] reference = { 2, 2, 2, 0, 1, 0 };
            for (var i = 0; i < dst.Data.length; i++)
            {
                Assert.AreEqual(reference[i], dst.Data[i]);
                ++i;
            }
        }
        public void TestEvalP()
        {
            Multinomial m = new Multinomial(2018);

            Tensor src = new Tensor
            {
                Data = new float[1, 3] {
                    { 0.1f, 0.2f, 0.7f }
                },
                ValueType = Tensor.TensorType.FloatingPoint
            };

            Tensor dst = new Tensor
            {
                Data      = new float[1, 3],
                ValueType = Tensor.TensorType.FloatingPoint
            };

            m.Eval(src, dst);

            float[] reference = { 2, 2, 1 };
            int     i         = 0;

            foreach (var f in dst.Data)
            {
                Assert.AreEqual(reference[i], f);
                ++i;
            }
        }
        public void TestEvalBatching()
        {
            Multinomial m = new Multinomial(2018);

            Tensor src = new Tensor
            {
                Data = new float[2, 3]
                {
                    { Mathf.Log(0.1f) - 50, Mathf.Log(0.2f) - 50, Mathf.Log(0.7f) - 50 },
                    { Mathf.Log(0.3f) - 25, Mathf.Log(0.4f) - 25, Mathf.Log(0.3f) - 25 },
                },
                ValueType = Tensor.TensorType.FloatingPoint
            };

            Tensor dst = new Tensor
            {
                Data      = new float[2, 3],
                ValueType = Tensor.TensorType.FloatingPoint
            };

            m.Eval(src, dst);

            float[] reference = { 2, 2, 2, 0, 1, 0 };
            int     i         = 0;

            foreach (var f in dst.Data)
            {
                Assert.AreEqual(reference[i], f);
                ++i;
            }
        }
        public void TestEvalP()
        {
            var m = new Multinomial(2018);

            var src = new TensorProxy
            {
                data      = new Tensor(1, 3, new[] { 0.1f, 0.2f, 0.7f }),
                valueType = TensorProxy.TensorType.FloatingPoint
            };

            var dst = new TensorProxy
            {
                data      = new Tensor(1, 3),
                valueType = TensorProxy.TensorType.FloatingPoint
            };

            DiscreteActionOutputApplier.Eval(src, dst, m);

            float[] reference = { 2, 2, 1 };
            for (var i = 0; i < dst.data.length; i++)
            {
                Assert.AreEqual(reference[i], dst.data[i]);
                ++i;
            }
        }
Exemple #7
0
 public DiscreteActionOutputApplier(ActionSpec actionSpec, int seed, ITensorAllocator allocator)
 {
     m_ActionSize  = actionSpec.BranchSizes;
     m_Multinomial = new Multinomial(seed);
     m_Allocator   = allocator;
     m_ActionSpec  = actionSpec;
 }
Exemple #8
0
        public void ValidateToString()
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
            var b = new Multinomial(_smallP, 4);

            Assert.AreEqual("Multinomial(Dimension = 3, Number of Trails = 4)", b.ToString());
        }
Exemple #9
0
        public void TestEvalP()
        {
            Multinomial m = new Multinomial(2018);

            TensorProxy src = new TensorProxy
            {
                Data      = new Tensor(1, 3, new[] { 0.1f, 0.2f, 0.7f }),
                ValueType = TensorProxy.TensorType.FloatingPoint
            };

            TensorProxy dst = new TensorProxy
            {
                Data      = new Tensor(1, 3),
                ValueType = TensorProxy.TensorType.FloatingPoint
            };

            m.Eval(src, dst);

            float[] reference = { 2, 2, 1 };
            for (var i = 0; i < dst.Data.length; i++)
            {
                Assert.AreEqual(reference[i], dst.Data[i]);
                ++i;
            }
        }
        public void ValidateVariance(double[] p, int n, double[] res)
        {
            var b = new Multinomial(p, n);

            for (var i = 0; i < b.P.Length; i++)
            {
                AssertHelpers.AlmostEqual(res[i], b.Variance[i], 12);
            }
        }
Exemple #11
0
        public void ValidateProbability(
            [Values(new[] { 0.3, 0.7 }, new[] { 0.1, 0.3, 0.6 }, new[] { 0.15, 0.35, 0.3, 0.2 })] double[] p,
            [Values(new[] { 1, 9 }, new[] { 1, 3, 6 }, new[] { 1, 1, 1, 7 })] int[] x,
            [Values(0.121060821, 0.105815808, 0.000145152)] double res)
        {
            var b = new Multinomial(p, x.Sum());

            AssertHelpers.AlmostEqual(b.Probability(x), res, 12);
        }
Exemple #12
0
        public void TestDim1()
        {
            var m   = new Multinomial(2018);
            var cdf = new[] { 1f };

            Assert.AreEqual(0, m.Sample(cdf));
            Assert.AreEqual(0, m.Sample(cdf));
            Assert.AreEqual(0, m.Sample(cdf));
        }
        public void ValidateMean(double[] p, int n, double[] res)
        {
            var b = new Multinomial(p, n);

            for (var i = 0; i < b.P.Length; i++)
            {
                AssertHelpers.AlmostEqualRelative(res[i], b.Mean[i], 12);
            }
        }
Exemple #14
0
        public override TVal this[TRowKey rowKey, TColKey colKey]
        {
            get
            {
                TVal result;
                if (TryGetValue(rowKey, colKey, out result))
                {
                    return(result);
                }
                else
                {
                    throw new Exception(string.Format("[{0},{1}] is missing.", rowKey, colKey));
                }
            }
            set
            {
                if (MissingValue.Equals(value))
                {
                    throw new Exception("Can't assign missing value using this[row,col]. Use SetValueOrMissing(row, col, value) or Remove(row,vol)");
                }

                var binaryKeys = _multKeyToBinaryKeys[rowKey];
                Helper.CheckCondition <InvalidOperationException>(binaryKeys.Count > 0, "Every multistate key must have at least one binary key. {0} does not.", rowKey);
                if (binaryKeys.Count == 1 && binaryKeys[0].Equals(rowKey))
                {
                    _binaryMatrix[binaryKeys[0], colKey] = value;
                }

                else
                {
                    //int state = _cnvrtTValToState.ConvertForward(value);
                    Multinomial m = _cnvrtTValToState.ConvertForward(value);
                    Helper.CheckCondition(m.NumOutcomes == binaryKeys.Count || m.IsDefinite && m.NumOutcomes <= binaryKeys.Count,
                                          "{0}, which maps to state {1} is outside the value range of values for this state ([{2},{3}])",
                                          value, m, 0, binaryKeys.Count - 1);

                    int state = 0;
                    foreach (var key in binaryKeys)
                    {
                        double      prob     = m[state];
                        Multinomial binomial = new Multinomial(1 - prob, prob);
                        _binaryMatrix[key, colKey] = _cnvrtTValToState.ConvertBackward(binomial);
                        state++;
                    }

                    //int hotState = 0;
                    //TVal trueState = _cnvrtTValToState.ConvertBackward(1);
                    //TVal falseState = _cnvrtTValToState.ConvertBackward(0);
                    //foreach (var key in binaryKeys)
                    //{
                    //    _binaryMatrix[key, colKey] = hotState == state ? trueState : falseState;
                    //    hotState++;
                    //}
                }
            }
        }
Exemple #15
0
        public void Base_Sample()
        {
            // arrange
            Uncertain <double> x = new Multinomial <double>(new[] { 5.0 });
            // act
            MeanAndConfidenceInterval m2 = x.ExpectedValueWithConfidence();

            // assert
            Assert.IsTrue(m2.Mean > 5.0 - eps && m2.Mean < 5.0 + eps);
        }
Exemple #16
0
        public void TestDim3()
        {
            var m   = new Multinomial(2018);
            var cdf = new[] { 0.1f, 0.3f, 1.0f };

            Assert.AreEqual(2, m.Sample(cdf));
            Assert.AreEqual(2, m.Sample(cdf));
            Assert.AreEqual(2, m.Sample(cdf));
            Assert.AreEqual(1, m.Sample(cdf));
        }
Exemple #17
0
        public void TestDim3Unscaled()
        {
            var m   = new Multinomial(2018);
            var cdf = new[] { 0.05f, 0.15f, 0.5f };

            Assert.AreEqual(2, m.Sample(cdf));
            Assert.AreEqual(2, m.Sample(cdf));
            Assert.AreEqual(2, m.Sample(cdf));
            Assert.AreEqual(1, m.Sample(cdf));
        }
Exemple #18
0
        public void TestSrcInt()
        {
            Multinomial m = new Multinomial(2018);

            TensorProxy src = new TensorProxy
            {
                ValueType = TensorProxy.TensorType.Integer
            };

            Assert.Throws <NotImplementedException>(() => m.Eval(src, null));
        }
        public void CanCreateMultinomialFromHistogram()
        {
            double[] smallDataset = { 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5 };
            var      hist         = new Histogram(smallDataset, 10, 0.0, 10.0);
            var      m            = new Multinomial(hist, 7);

            foreach (var t in m.P)
            {
                Assert.AreEqual(1.0, t);
            }
        }
        public void TestSrcInt()
        {
            var m = new Multinomial(2018);

            var src = new TensorProxy
            {
                valueType = TensorProxy.TensorType.Integer
            };

            Assert.Throws <NotImplementedException>(
                () => DiscreteActionOutputApplier.Eval(src, null, m));
        }
Exemple #21
0
        public void ValidateMean(
            [Values(new[] { 0.3, 0.7 }, new[] { 0.1, 0.3, 0.6 }, new[] { 0.15, 0.35, 0.3, 0.2 })] double[] p,
            [Values(5, 10, 20)] int n,
            [Values(new[] { 1.5, 3.5 }, new[] { 1.0, 3.0, 6.0 }, new[] { 3.0, 7.0, 6.0, 4.0 })] double[] res)
        {
            var b = new Multinomial(p, n);

            for (var i = 0; i < b.P.Length; i++)
            {
                AssertHelpers.AlmostEqual(res[i], b.Mean[i], 12);
            }
        }
Exemple #22
0
        public void ValidateVariance(
            [Values(new[] { 0.3, 0.7 }, new[] { 0.1, 0.3, 0.6 }, new[] { 0.15, 0.35, 0.3, 0.2 })] double[] p,
            [Values(5, 10, 20)] int n,
            [Values(new[] { 1.05, 1.05 }, new[] { 0.9, 2.1, 2.4 }, new[] { 2.55, 4.55, 4.2, 3.2 })] double[] res)
        {
            var b = new Multinomial(p, n);

            for (var i = 0; i < b.P.Length; i++)
            {
                AssertHelpers.AlmostEqual(res[i], b.Variance[i], 12);
            }
        }
Exemple #23
0
        public void ValidateSkewness(
            [Values(new[] { 0.3, 0.7 }, new[] { 0.1, 0.3, 0.6 }, new[] { 0.15, 0.35, 0.3, 0.2 })] double[] p,
            [Values(5, 10, 20)] int n,
            [Values(new[] { 0.390360029179413, -0.390360029179413 }, new[] { 0.843274042711568, 0.276026223736942, -0.129099444873581 }, new[] { 0.438357003759605, 0.140642169281549, 0.195180014589707, 0.335410196624968 })] double[] res)
        {
            var b = new Multinomial(p, n);

            for (var i = 0; i < b.P.Length; i++)
            {
                AssertHelpers.AlmostEqual(res[i], b.Skewness[i], 12);
            }
        }
Exemple #24
0
        static Uncertain <string[]> HMM(string[] obs)
        {
            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
                (from prior in start_probability

                 from state0 in transition_probability(prior)
                 from emit0 in emission_probability(state0)
                 where obs[0] == emit0

                 from state1 in transition_probability(state0)
                 from emit1 in emission_probability(state1)
                 where obs[1] == emit1

                 from state2 in transition_probability(state1)
                 from emit2 in emission_probability(state2)
                 where obs[2] == emit2

                 select new[] { state0, state1, state2 });
        }
Exemple #25
0
        public void Base_TestMarginal()
        {
            var X = new Multinomial <int>(Enumerable.Range(0, 4), new[] { 0.1, 0.6, 0.1, 0.2 });
            var Y = new Multinomial <int>(Enumerable.Range(0, 4), new[] { 0.6, 0.1, 0.2, 0.1 });
            //P(X,Y)
            var joint = from x in X
                        from y in Y
                        select x == 0 ? Tuple.Create(x, 1) : Tuple.Create(x, y);
            // p(Y)
            Func <int, Uncertain <bool> > marginal = y =>
                                                     from pair in joint
                                                     select pair.Item2 == y;

            // \sum_{y} p(y) == 1
            var tmp = from a in marginal(0)
                      from b in marginal(1)
                      from c in marginal(2)
                      from d in marginal(3)
                      select a | b | c | d;
            var sum = tmp.Inference().Support().Select(k => k.Probability).Sum();

            //(marginal(0) | marginal(1) | marginal(2) | marginal(3)).Prob();
            Assert.IsTrue(sum > 1.0 - eps && sum < 1.0 + eps);

            // Here is the entire joint distribution p(x,y):
            //                     X
            //         -------------------------
            //         |  0  |  1  |  2  |  3  |
            //   ------|------------------------
            //   |  0  |  0  | 0.36| 0.06| 0.12|  0.54
            // Y |  1  | 0.1 | 0.06| 0.01| 0.02|  0.19
            //   |  2  |  0  | 0.12| 0.02| 0.04|  0.18
            //   |  3  |  0  | 0.06| 0.01| 0.02|  0.09
            //   -------------------------------
            //           0.1   0.6   0.1   0.2    1.0

            var m0 = marginal(0).ExpectedValueWithConfidence().Mean;  // p(y=0) = 0.54

            Assert.IsTrue(m0 > 0.54 - eps && m0 < 0.54 + eps);

            var m1 = marginal(1).ExpectedValueWithConfidence().Mean;  // p(y=1) = 0.19

            Assert.IsTrue(m1 > 0.19 - eps && m1 < 0.19 + eps);

            var m2 = marginal(2).ExpectedValueWithConfidence().Mean;  // p(y=2) = 0.18

            Assert.IsTrue(m2 > 0.18 - eps && m2 < 0.18 + eps);

            var m3 = marginal(3).ExpectedValueWithConfidence().Mean;  // p(y=3) = 0.09

            Assert.IsTrue(m3 > 0.09 - eps && m3 < 0.09 + eps);
        }
Exemple #26
0
        public LegacyDiscreteActionOutputApplier(ActionSpec actionSpec, int seed, ITensorAllocator allocator)
        {
            m_ActionSize         = actionSpec.BranchSizes;
            m_Multinomial        = new Multinomial(seed);
            m_ActionSpec         = actionSpec;
            m_StartActionIndices = Utilities.CumSum(m_ActionSize);

            // Scratch space for computing the cumulative distribution function.
            // In order to reuse it, make it the size of the largest branch.
            var largestBranch = Mathf.Max(m_ActionSize);

            m_CdfBuffer = new float[largestBranch];
        }
Exemple #27
0
        static Uncertain <string> CompilerListenerWithExample(string utterance, IList <Tuple <string, IEnumerable <Tuple <int, int, int> > > > examples)//string input, IEnumerable<Tuple<int,int,int>> correct)
        {
            var tmp     = new Multinomial <Tuple <string, IEnumerable <Tuple <int, int, int> > > >(examples);
            var program = from stmt in PossibleInterpretations2(utterance) //PossibleInterpretations(utterance, 2)
                          let re                         = new Parser(stmt).Parse()
                                               let codes = new Compiler().Compile(re)
                                                           from example in tmp
                                                           let matches = new Interpreter().Run(codes.ToList(), example.Item1)
                                                                         where Cmp(matches, example.Item2)
                                                                         select stmt;

            return(program.SampledInference(10000));
        }
Exemple #28
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;
        }
Exemple #29
0
        public void Base_TestMultinomial()
        {
            var age = new Multinomial <int>(new[] { 0, 1, 2, 3 }, new[] { 0.1, 0.6, 0.1, 0.2 });

            // sum of binomial's should = 1.0 because of the law of total probabilty.
            var tmp = from a in age
                      from b in age
                      from c in age
                      from d in age
                      select a == 0 | b == 1 | c == 2 | d == 3;
            var sum = tmp.Inference().Support().Select(k => k.Probability).Sum();

            Assert.IsTrue(sum > 1.0 - eps && sum < 1.0 + eps);
        }
Exemple #30
0
        /// <summary>
        /// Draw samples from a multinomial distribution based on log-probabilities specified
        /// in tensor src. The samples will be saved in the dst tensor.
        /// </summary>
        /// <param name="src">2-D tensor with shape batch_size x num_classes</param>
        /// <param name="dst">Allocated tensor with size batch_size x num_samples</param>
        /// <param name="multinomial">Multinomial object used to sample values</param>
        /// <exception cref="NotImplementedException">
        /// Multinomial doesn't support integer tensors
        /// </exception>
        /// <exception cref="ArgumentException">Issue with tensor shape or type</exception>
        /// <exception cref="ArgumentNullException">
        /// At least one of the tensors is not allocated
        /// </exception>
        public static void Eval(TensorProxy src, TensorProxy dst, Multinomial multinomial)
        {
            if (src.DataType != typeof(float))
            {
                throw new NotImplementedException("Only float tensors are currently supported");
            }

            if (src.valueType != dst.valueType)
            {
                throw new ArgumentException(
                          "Source and destination tensors have different types!");
            }

            if (src.data == null || dst.data == null)
            {
                throw new ArgumentNullException();
            }

            if (src.data.batch != dst.data.batch)
            {
                throw new ArgumentException("Batch size for input and output data is different!");
            }

            var cdf = new float[src.data.channels];

            for (var batch = 0; batch < src.data.batch; ++batch)
            {
                // Find the class maximum
                var maxProb = float.NegativeInfinity;
                for (var cls = 0; cls < src.data.channels; ++cls)
                {
                    maxProb = Mathf.Max(src.data[batch, cls], maxProb);
                }

                // Sum the log probabilities and compute CDF
                var sumProb = 0.0f;
                for (var cls = 0; cls < src.data.channels; ++cls)
                {
                    sumProb += Mathf.Exp(src.data[batch, cls] - maxProb);
                    cdf[cls] = sumProb;
                }

                // Generate the samples
                for (var sample = 0; sample < dst.data.channels; ++sample)
                {
                    dst.data[batch, sample] = multinomial.Sample(cdf);
                }
            }
        }
 public void MultinomialCreateFailsWithNegativeRatios()
 {
     var m = new Multinomial(badP, 4);
 }
 public void ValidateProbability(
     [Values(new[] { 0.3, 0.7 }, new[] { 0.1, 0.3, 0.6 }, new[] { 0.15, 0.35, 0.3, 0.2 })] double[] p, 
     [Values(new[] { 1, 9 }, new[] { 1, 3, 6 }, new[] { 1, 1, 1, 7 })] int[] x, 
     [Values(0.121060821, 0.105815808, 0.000145152)] double res)
 {
     var b = new Multinomial(p, x.Sum());
     AssertHelpers.AlmostEqual(b.Probability(x), res, 12);
 }
 public void ValidateVariance(
     [Values(new[] { 0.3, 0.7 }, new[] { 0.1, 0.3, 0.6 }, new[] { 0.15, 0.35, 0.3, 0.2 })] double[] p, 
     [Values(5, 10, 20)] int n, 
     [Values(new[] { 1.05, 1.05 }, new[] { 0.9, 2.1, 2.4 }, new[] { 2.55, 4.55, 4.2, 3.2 })] double[] res)
 {
     var b = new Multinomial(p, n);
     for (var i = 0; i < b.P.Length; i++)
     {
         AssertHelpers.AlmostEqual(res[i], b.Variance[i], 12);
     }
 }
 public void ValidateProbabilityLn([Values(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, new[] { 1, 1, 1, 2, 2, 2, 3, 3, 3 }, new[] { 5, 6, 7, 8, 7, 6, 5, 4, 3 })] int[] x)
 {
     var b = new Multinomial(_largeP, x.Sum());
     AssertHelpers.AlmostEqual(b.ProbabilityLn(x), Math.Log(b.Probability(x)), 12);
 }
Exemple #35
0
 public void Base_Sample()
 {
     // arrange
     Uncertain<double> x = new Multinomial<double>(new[] { 5.0 });
     // act
     MeanAndConfidenceInterval m2 = x.ExpectedValueWithConfidence();
     // assert
     Assert.IsTrue(m2.Mean > 5.0 - eps && m2.Mean < 5.0 + eps);
 }
 public void CanCreateMultinomial()
 {
     var m = new Multinomial(_largeP, 4);
     CollectionAssert.AreEqual(_largeP, m.P);
 }
        public void CanCreateMultinomialFromHistogram()
        {
            double[] smallDataset = { 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5 };
            var hist = new Histogram(smallDataset, 10, 0.0, 10.0);
            var m = new Multinomial(hist, 7);

            foreach (var t in m.P)
            {
                Assert.AreEqual(1.0, t);
            }
        }
 public void ValidateProbabilityLn(int[] x)
 {
     var b = new Multinomial(_largeP, x.Sum());
     AssertHelpers.AlmostEqualRelative(b.ProbabilityLn(x), Math.Log(b.Probability(x)), 12);
 }
 public void SetProbabilityFails()
 {
     var b = new Multinomial(_largeP, 4);
     Assert.Throws<ArgumentOutOfRangeException>(() => b.P = _badP);
 }
Exemple #40
0
        public void Base_TestMultinomial()
        {
            var age = new Multinomial<int>(new[] { 0, 1, 2, 3 }, new[] { 0.1, 0.6, 0.1, 0.2 });

            // sum of binomial's should = 1.0 because of the law of total probabilty.
            var tmp = from a in age
                      from b in age
                      from c in age
                      from d in age
                      select a == 0 | b == 1 | c == 2 | d == 3;
            var sum = tmp.Inference().Support().Select(k => k.Probability).Sum();
            Assert.IsTrue(sum > 1.0 - eps && sum < 1.0 + eps);
        }
 public void ValidateMean(
     [Values(new[] { 0.3, 0.7 }, new[] { 0.1, 0.3, 0.6 }, new[] { 0.15, 0.35, 0.3, 0.2 })] double[] p, 
     [Values(5, 10, 20)] int n, 
     [Values(new[] { 1.5, 3.5 }, new[] { 1.0, 3.0, 6.0 }, new[] { 3.0, 7.0, 6.0, 4.0 })] double[] res)
 {
     var b = new Multinomial(p, n);
     for (var i = 0; i < b.P.Length; i++)
     {
         AssertHelpers.AlmostEqual(res[i], b.Mean[i], 12);
     }
 }
 public void CanSetProbability()
 {
     var b = new Multinomial(largeP, 4);
     b.P = smallP;
 }
 public void CanCreateMultinomial()
 {
     var m = new Multinomial(largeP, 4);
     Assert.AreEqual<double[]>(largeP, m.P);
 }
 public void SetProbabilityFails()
 {
     var b = new Multinomial(largeP, 4);
     b.P = badP;
 }
 public void ValidateToString()
 {
     var b = new Multinomial(smallP, 4);
     Assert.AreEqual<string>("Multinomial(Dimension = 3, Number of Trails = 4)", b.ToString());
 }
 public void ValidateVariance(double[] p, int n, double[] res)
 {
     var b = new Multinomial(p, n);
     for (var i = 0; i < b.P.Length; i++)
     {
         AssertHelpers.AlmostEqual(res[i], b.Variance[i], 12);
     }
 }
 public void CanSample()
 {
     var n = new Multinomial(largeP, 4);
     var d = n.Sample();
 }
 public void ValidateSkewness(
     [Values(new[] { 0.3, 0.7 }, new[] { 0.1, 0.3, 0.6 }, new[] { 0.15, 0.35, 0.3, 0.2 })] double[] p, 
     [Values(5, 10, 20)] int n, 
     [Values(new[] { 0.390360029179413, -0.390360029179413 }, new[] { 0.843274042711568, 0.276026223736942, -0.129099444873581 }, new[] { 0.438357003759605, 0.140642169281549, 0.195180014589707, 0.335410196624968 })] double[] res)
 {
     var b = new Multinomial(p, n);
     for (var i = 0; i < b.P.Length; i++)
     {
         AssertHelpers.AlmostEqual(res[i], b.Skewness[i], 12);
     }
 }
 public void MultinomialCreateFailsWithAllZeroRatios()
 {
     var m = new Multinomial(badP2, 4);
 }
 public void ValidateMean(double[] p, int n, double[] res)
 {
     var b = new Multinomial(p, n);
     for (var i = 0; i < b.P.Length; i++)
     {
         AssertHelpers.AlmostEqualRelative(res[i], b.Mean[i], 12);
     }
 }
 public void ValidateProbability(double[] p, int[] x, double res)
 {
     var b = new Multinomial(p, x.Sum());
     AssertHelpers.AlmostEqualRelative(b.Probability(x), res, 12);
 }
Exemple #52
0
        public void MontyHall()
        {
            var doors = Enumerable.Range(0, 3);
            var cardoor = new Multinomial<int>(doors);

            var program = from carDoorNum in new Multinomial<int>(doors)
                          from chosenDoor in new Multinomial<int>(doors)
                          let match = carDoorNum == chosenDoor
                          let possibleOpenDoors2 = doors.Where(i => i != carDoorNum).ToList()
                          let possibleOpenDoors1 = doors.Where(i => i != carDoorNum && i != chosenDoor).ToList()
                          let nextdoor = match ? new Multinomial<int>(possibleOpenDoors1) : new Multinomial<int>(possibleOpenDoors2)
                          from otherDoorContainsCar in nextdoor
                          let chosenDoorContainsCar = chosenDoor == carDoorNum
                          select chosenDoorContainsCar;
            // note chosenDoorContainsCar is never used but implicitly impacts the result
            // because we need to compute over the nextdoor probabilistic choice.

            var ans = program.Inference();
            var result = new[] { 0.66, 0.33 };

            foreach (var item in ans.Support().OrderBy(k => k.Value).Zip(result, Tuple.Create))
                Assert.IsTrue(ApproxEqual(item.Item1.Probability, item.Item2));

            var sampled = program.SampledInference(10000);

            foreach (var item in sampled.Support().OrderBy(k => k.Value).Zip(result, Tuple.Create))
                Assert.IsTrue(ApproxEqual(item.Item1.Probability, item.Item2));
        }
 public void SetProbabilityFails()
 {
     var b = new Multinomial(_largeP, 4);
     Assert.That(() => b.P = _badP, Throws.ArgumentException);
 }
Exemple #54
0
        public void Base_TestMarginal()
        {
            var X = new Multinomial<int>(Enumerable.Range(0, 4), new[] { 0.1, 0.6, 0.1, 0.2 });
            var Y = new Multinomial<int>(Enumerable.Range(0, 4), new[] { 0.6, 0.1, 0.2, 0.1 });
            //P(X,Y)
            var joint = from x in X
                        from y in Y
                        select x == 0 ? Tuple.Create(x, 1) : Tuple.Create(x, y);
            // p(Y)
            Func<int, Uncertain<bool>> marginal = y =>
                from pair in joint
                select pair.Item2 == y;

            // \sum_{y} p(y) == 1
            var tmp = from a in marginal(0)
                      from b in marginal(1)
                      from c in marginal(2)
                      from d in marginal(3)
                      select a | b | c | d;
            var sum = tmp.Inference().Support().Select(k => k.Probability).Sum();
            //(marginal(0) | marginal(1) | marginal(2) | marginal(3)).Prob();
            Assert.IsTrue(sum > 1.0 - eps && sum < 1.0 + eps);

            // Here is the entire joint distribution p(x,y):
            //                     X
            //         -------------------------
            //         |  0  |  1  |  2  |  3  |
            //   ------|------------------------
            //   |  0  |  0  | 0.36| 0.06| 0.12|  0.54
            // Y |  1  | 0.1 | 0.06| 0.01| 0.02|  0.19
            //   |  2  |  0  | 0.12| 0.02| 0.04|  0.18
            //   |  3  |  0  | 0.06| 0.01| 0.02|  0.09
            //   -------------------------------
            //           0.1   0.6   0.1   0.2    1.0

            var m0 = marginal(0).ExpectedValueWithConfidence().Mean;  // p(y=0) = 0.54
            Assert.IsTrue(m0 > 0.54 - eps && m0 < 0.54 + eps);

            var m1 = marginal(1).ExpectedValueWithConfidence().Mean;  // p(y=1) = 0.19
            Assert.IsTrue(m1 > 0.19 - eps && m1 < 0.19 + eps);

            var m2 = marginal(2).ExpectedValueWithConfidence().Mean;  // p(y=2) = 0.18
            Assert.IsTrue(m2 > 0.18 - eps && m2 < 0.18 + eps);

            var m3 = marginal(3).ExpectedValueWithConfidence().Mean;  // p(y=3) = 0.09
            Assert.IsTrue(m3 > 0.09 - eps && m3 < 0.09 + eps);
        }