Ejemplo n.º 1
0
        public void Empty()
        {
            var empty = StringDistribution.Empty();

            StringInferenceTestUtilities.TestProbability(empty, 1.0, string.Empty);
            StringInferenceTestUtilities.TestProbability(empty, 0.0, "something");
            Assert.True(empty.IsPointMass);
            Assert.Equal(string.Empty, empty.Point);
        }
Ejemplo n.º 2
0
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="StringFromArrayOp"]/message_doc[@name="StrAverageConditional(IList{DiscreteChar})"]/*'/>
        public static StringDistribution StrAverageConditional(IList <DiscreteChar> characters)
        {
            StringDistribution result = StringDistribution.Empty();

            for (int i = 0; i < characters.Count; ++i)
            {
                result.AppendInPlace(characters[i]);
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a string distribution <c>P(s) = \prod_i P_i(s_i)^I[i != j]</c>,
        /// where <c>P_i(c)</c> is a given array of character distributions and <c>j</c> is a given position in the array.
        /// </summary>
        /// <param name="characters">The distributions over individual characters.</param>
        /// <param name="excludedPos">The character to skip.</param>
        /// <returns>The created distribution.</returns>
        private static StringDistribution GetCharWeighter(IList <DiscreteChar> characters, int excludedPos)
        {
            StringDistribution result = StringDistribution.Empty();

            for (int i = 0; i < characters.Count; ++i)
            {
                result.AppendInPlace(i == excludedPos ? DiscreteChar.Uniform() : characters[i]);
            }

            return(result);
        }
Ejemplo n.º 4
0
        public void EnumerateSupport()
        {
            Assert.Equal(string.Empty, StringDistribution.Empty().EnumerateSupport().Single());
            Assert.Equal("12345", StringDistribution.String("12345").EnumerateSupport().Single());

            var array = new[] { "aa", "aabb", "aac" };

            Assert.Equal(array, StringDistribution.OneOf(array).EnumerateSupport().ToArray());

            Assert.Equal(10, StringDistribution.Repeat('z', 1, 10).EnumerateSupport().Count());

            var diamondResults = new[] { "abd", "acd" };
            var diamond        = StringDistribution.String("a") + StringDistribution.OneOf(StringDistribution.String("b"), StringDistribution.String("c")) + StringDistribution.String("d");

            Assert.Equal(diamondResults, diamond.EnumerateSupport().ToArray());
        }
Ejemplo n.º 5
0
        public void MessageOperatorsTest()
        {
            var str1 = StringFromArrayOp.StrAverageConditional(new[] { DiscreteChar.PointMass('a'), DiscreteChar.OneOf('b', 'c'), DiscreteChar.OneOf('d', 'e') });

            Assert.Equal(StringDistribution.OneOf("abd", "abe", "acd", "ace"), str1, Eps);

            var str2 = StringFromArrayOp.StrAverageConditional(new DiscreteChar[0]);

            Assert.Equal(StringDistribution.Empty(), str2, Eps);

            var chars1 = StringFromArrayOp.CharactersAverageConditional(
                StringDistribution.OneOf("ab", "cd"),
                new[] { DiscreteChar.PointMass('a'), DiscreteChar.Uniform() },
                new DiscreteChar[2]);

            Assert.Equal(DiscreteChar.OneOf('a', 'c'), chars1[0], Eps);
            Assert.Equal(DiscreteChar.PointMass('b'), chars1[1], Eps);

            var chars2 = StringFromArrayOp.CharactersAverageConditional(
                StringDistribution.OneOf("ab", "ac"),
                new[] { DiscreteChar.Uniform(), DiscreteChar.Uniform() },
                new DiscreteChar[2]);

            Assert.Equal(DiscreteChar.PointMass('a'), chars2[0], Eps);
            Assert.Equal(DiscreteChar.OneOf('b', 'c'), chars2[1], Eps);

            var chars3 = StringFromArrayOp.CharactersAverageConditional(
                StringDistribution.OneOf("ab", "ac", "bc"),
                new[] { DiscreteChar.Uniform(), DiscreteChar.Uniform() },
                new DiscreteChar[2]);

            Assert.Equal(2.0 / 3.0, chars3[0]['a'], Eps);
            Assert.Equal(1.0 / 3.0, chars3[0]['b'], Eps);
            Assert.Equal(1.0 / 3.0, chars3[1]['b'], Eps);
            Assert.Equal(2.0 / 3.0, chars3[1]['c'], Eps);

            var chars4 = StringFromArrayOp.CharactersAverageConditional(
                StringDistribution.OneOf("ab", "cde"),
                new[] { DiscreteChar.Uniform(), DiscreteChar.Uniform() },
                new DiscreteChar[2]);

            Assert.Equal(DiscreteChar.PointMass('a'), chars4[0], Eps);
            Assert.Equal(DiscreteChar.PointMass('b'), chars4[1], Eps);

            var chars5 = StringFromArrayOp.CharactersAverageConditional(
                StringDistribution.OneOf("ab", "cb", "ae", "ax"),
                new[] { DiscreteChar.PointMass('a'), DiscreteChar.PointMass('b') },
                new DiscreteChar[2]);

            Assert.Equal(DiscreteChar.OneOf('a', 'c'), chars5[0], Eps);
            Assert.Equal(DiscreteChar.OneOf('b', 'e', 'x'), chars5[1], Eps);

            var chars6 = StringFromArrayOp.CharactersAverageConditional(
                StringDistribution.OneOf("abcd", "accd", "acce"),
                new[] { DiscreteChar.Uniform(), DiscreteChar.Uniform(), DiscreteChar.Uniform(), DiscreteChar.PointMass('d') },
                new DiscreteChar[4]);

            Assert.Equal(DiscreteChar.PointMass('a'), chars6[0], Eps);
            Assert.Equal(DiscreteChar.OneOf('b', 'c'), chars6[1], Eps);
            Assert.Equal(DiscreteChar.PointMass('c'), chars6[2], Eps);
            Assert.Equal(2.0 / 3.0, chars6[3]['d'], Eps);
            Assert.Equal(1.0 / 3.0, chars6[3]['e'], Eps);
        }