Exemplo n.º 1
0
        public void SemanticWebTest3()
        {
            // Simplified name model
            var prop0 = Variable.Random(NamePrior()).Named("prop0");
            // Simplified date model any string ending in a digit
            var prop1 = Variable.Random(
                StringDistribution.Char(DiscreteChar.Digit()) + StringDistribution.Any() + StringDistribution.Char(DiscreteChar.Digit()))
                        .Named("prop1");
            var template = Variable.Random(StringDistribution.String("{0}") + WordStrings.WordMiddle() + StringDistribution.String("{1}"))
                           .Named("template");
            var text = Variable.StringFormat(template, prop0, prop1).Named("text");

            text.ObservedValue = "Tony Blair was born on 6 May 1953";

            var engine = new InferenceEngine();

            engine.Compiler.RecommendedQuality = QualityBand.Experimental;
            engine.NumberOfIterations          = 1;

            var prop0Dist    = engine.Infer <StringDistribution>(prop0);
            var prop1Dist    = engine.Infer <StringDistribution>(prop1);
            var templateDist = engine.Infer <StringDistribution>(template);

            StringInferenceTestUtilities.TestProbability(prop0Dist, 1.0, "Tony Blair");
            StringInferenceTestUtilities.TestProbability(prop1Dist, 0.5, "1953", "6 May 1953");
            StringInferenceTestUtilities.TestProbability(templateDist, 0.5, "{0} was born on {1}", "{0} was born on 6 May {1}");
        }
Exemplo n.º 2
0
        public void PointMassDetection()
        {
            StringDistribution s1     = StringDistribution.OneOf("hello", "world", "people");
            StringDistribution s2     = StringDistribution.OneOf("greetings", "people", "animals");
            StringDistribution point1 = s1.Product(s2);

            Assert.True(point1.IsPointMass);
            Assert.Equal("people", point1.Point);

            StringDistribution point2 = StringDistribution.OneOf(new Dictionary <string, double> {
                { "a", 3.0 }, { "b", 0.0 }
            });

            Assert.True(point2.IsPointMass);
            Assert.Equal("a", point2.Point);

            StringDistribution point3 = StringDistribution.CaseInvariant("123");

            Assert.True(point3.IsPointMass);
            Assert.Equal("123", point3.Point);

            StringDistribution point4 = StringDistribution.Char('Z');

            Assert.True(point4.IsPointMass);
            Assert.Equal("Z", point4.Point);

            StringDistribution point5 = StringDistribution.OneOf(1.0, StringDistribution.String("!"), 0.0, StringDistribution.Any());

            Assert.True(point5.IsPointMass);
            Assert.Equal("!", point5.Point);

            StringDistribution point6 = StringDistribution.Repeat('@', minTimes: 3, maxTimes: 3);

            Assert.True(point6.IsPointMass);
            Assert.Equal("@@@", point6.Point);

            StringDistribution point7 = StringDistribution.String("hello").Append(StringDistribution.String(" world"));

            Assert.True(point7.IsPointMass);
            Assert.Equal("hello world", point7.Point);

            string          point           = string.Empty;
            StringAutomaton point8Automaton = StringAutomaton.Empty();

            for (int i = 0; i < 22; ++i)
            {
                const string PointElement = "a";
                point8Automaton.AppendInPlace(StringAutomaton.ConstantOn(1.0, PointElement, PointElement));
                point += PointElement;
            }

            StringDistribution point8 = StringDistribution.FromWeightFunction(point8Automaton);

            Assert.True(point8.IsPointMass);
            Assert.Equal(point, point8.Point);
        }
Exemplo n.º 3
0
        public void Product5()
        {
            var uniformThenChar = StringDistribution.Any() + StringDistribution.Char('a');
            var charThenUniform = StringDistribution.Char('a') + StringDistribution.Any();
            var expectedProduct = StringDistribution.OneOf(
                StringDistribution.Char('a') + StringDistribution.Any() + StringDistribution.Char('a'),
                StringDistribution.Char('a'));

            StringInferenceTestUtilities.TestProduct(uniformThenChar, charThenUniform, expectedProduct, string.Empty, "a", "aba", "abaa", "abd");
        }
Exemplo n.º 4
0
        public void HelloWorld()
        {
            var a        = Variable.Random(WordString()).Named("a");
            var b        = Variable.Random(WordString()).Named("b");
            var template = Variable.Random(TemplateArgString() + StringDistribution.Char(' ') + TemplateArgString()).Named("template");
            var c        = Variable.StringFormat(template, a, b).Named("c");
            var c2       = Variable.StringFormat(template, a, b).Named("c2");

            var engine = new InferenceEngine();

            engine.Compiler.RecommendedQuality = QualityBand.Experimental;
            engine.Compiler.GivePriorityTo(typeof(ReplicateOp_NoDivide));
            //engine.Compiler.BrowserMode = BrowserMode.Always;
            engine.NumberOfIterations = 5;
            //Console.WriteLine("c="+engine.Infer(c)); // expect Uniform(*)

            c.ObservedValue = "Hello World";
            Test("a", engine.Infer <IDistribution <string> >(a), "Hello", "World");
            Test("b", engine.Infer <IDistribution <string> >(b), "Hello", "World");
            Test("t", engine.Infer <IDistribution <string> >(template), "{0} {1}", "{1} {0}");
            Test("c2", engine.Infer <IDistribution <string> >(c2), "Hello World", "Hello Hello", "World Hello", "World World");

            b.ObservedValue = "World";
            TestUnif("a", engine.Infer <IDistribution <string> >(a), "Hello");
            TestUnif("t", engine.Infer <IDistribution <string> >(template), "{0} {1}");
            TestUnif("c2", engine.Infer <IDistribution <string> >(c2), "Hello World");
            b.ClearObservedValue();
            a.ObservedValue = "Hello";
            TestUnif("b", engine.Infer <IDistribution <string> >(b), "World");
            TestUnif("t", engine.Infer <IDistribution <string> >(template), "{0} {1}");
            TestUnif("c2", engine.Infer <IDistribution <string> >(c2), "Hello World");
            b.ObservedValue = "World";
            TestUnif("t", engine.Infer <IDistribution <string> >(template), "{0} {1}");
            TestUnif("c2", engine.Infer <IDistribution <string> >(c2), "Hello World");

            template.ObservedValue = "{0} {1}";
            a.ClearObservedValue();
            b.ClearObservedValue();
            Test("a", engine.Infer <IDistribution <string> >(a), "Hello");
            Test("b", engine.Infer <IDistribution <string> >(b), "World");
            Test("c2", engine.Infer <IDistribution <string> >(c2), "Hello World");

            b.ObservedValue = "World";
            Test("a", engine.Infer <IDistribution <string> >(a), "Hello");
            Test("c2", engine.Infer <IDistribution <string> >(c2), "Hello World");

            b.ClearObservedValue();
            a.ObservedValue = "Hello";
            Test("b", engine.Infer <IDistribution <string> >(b), "World");
            Test("c2", engine.Infer <IDistribution <string> >(c2), "Hello World");
        }
Exemplo n.º 5
0
        public void InefficientPermutationRepresentation()
        {
            StringDistribution placeholder = StringDistribution.Char('{') + StringDistribution.Char(DiscreteChar.Digit()) + StringDistribution.Char('}');
            StringDistribution formatPrior = placeholder + StringDistribution.String(" ");

            formatPrior = StringDistribution.ZeroOrMore(formatPrior) + placeholder;

            const int ArgCount  = 6;
            const int ArgLength = 3;

            StringDistribution[] args = Util.ArrayInit(ArgCount, i => StringDistribution.String(new string((char)('0' + i), ArgLength)));

            StringDistribution str = StringFormatOp_AllowMissingPlaceholders_NoArgumentNames.StrAverageConditional(formatPrior, args);

            Console.WriteLine(str.GetWorkspaceOrPoint().States.Count);

            StringInferenceTestUtilities.TestIfIncludes(str, "111 333 222", "111 222", "222 111", "111", "222", "333");
            StringInferenceTestUtilities.TestIfExcludes(str, string.Empty, "111 111", "222 222 111", "333 111 222 333", "112", "1");
        }
Exemplo n.º 6
0
        public void StringFromItems()
        {
            Variable <string> str   = Variable.StringOfLength(2);
            Variable <char>   char1 = Variable.GetItem(str, 0);
            Variable <char>   char2 = Variable.GetItem(str, 1);

            var engine = new InferenceEngine();

            char1.ObservedValue = 'a';
            char2.ObservedValue = 'b';

            Assert.Equal(StringDistribution.PointMass("ab"), engine.Infer <StringDistribution>(str), ProbEps);

            char1.ClearObservedValue();

            Assert.Equal(
                StringDistribution.Char(DiscreteChar.Any()) + StringDistribution.Char('b'),
                engine.Infer <StringDistribution>(str),
                ProbEps);
        }
Exemplo n.º 7
0
        public void Char()
        {
            var charDist1 = StringDistribution.Char('a');

            StringInferenceTestUtilities.TestProbability(charDist1, 1.0, "a");
            StringInferenceTestUtilities.TestProbability(charDist1, 0.0, "aa", string.Empty);

            var charDist2 = StringDistribution.Char(DiscreteChar.InRange('a', 'c'));

            StringInferenceTestUtilities.TestProbability(charDist2, 1.0 / 3.0, "a", "b", "c");
            StringInferenceTestUtilities.TestProbability(charDist2, 0.0, "ab", string.Empty);

            Vector charProbs3 = PiecewiseVector.Zero(char.MaxValue + 1);

            charProbs3['a'] = 0.1;
            charProbs3['b'] = 0.9;
            var charDist3 = StringDistribution.SingleElement(DiscreteChar.FromVector(charProbs3));

            StringInferenceTestUtilities.TestProbability(charDist3, 0.1, "a");
            StringInferenceTestUtilities.TestProbability(charDist3, 0.9, "b");
            StringInferenceTestUtilities.TestProbability(charDist3, 0.0, "c", "ab", string.Empty);
        }
Exemplo n.º 8
0
 /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="SingleOp"]/message_doc[@name="StrAverageConditional(DiscreteChar)"]/*'/>
 public static StringDistribution StrAverageConditional(DiscreteChar character)
 {
     return(StringDistribution.Char(character));
 }
Exemplo n.º 9
0
 private static StringDistribution TemplateArgString() =>
 StringDistribution.Char('{') + StringDistribution.Char(DiscreteChar.Digit()) + StringDistribution.Char('}');
Exemplo n.º 10
0
 private static StringDistribution NamePart() => StringDistribution.Char(DiscreteChar.Upper()) + StringDistribution.Lower(minLength: 0);
Exemplo n.º 11
0
 /// <summary>Evidence message for EP.</summary>
 /// <param name="concat">Constant value for <c>concat</c>.</param>
 /// <param name="ch">Incoming message from <c>ch</c>.</param>
 /// <param name="str">Incoming message from <c>str</c>.</param>
 /// <returns>Logarithm of the factor's contribution the EP model evidence.</returns>
 /// <remarks>
 ///   <para>The formula for the result is <c>log(sum_(ch,str) p(ch,str) factor(concat,ch,str))</c>. Adding up these values across all factors and variables gives the log-evidence estimate for EP.</para>
 /// </remarks>
 public static double LogEvidenceRatio(string concat, DiscreteChar ch, StringDistribution str)
 {
     return(StringConcatOp.LogEvidenceRatio(concat, StringDistribution.Char(ch), str));
 }
Exemplo n.º 12
0
 /// <summary>Evidence message for EP.</summary>
 /// <param name="concat">Incoming message from <c>concat</c>.</param>
 /// <param name="ch">Incoming message from <c>ch</c>.</param>
 /// <param name="str">Incoming message from <c>str</c>.</param>
 /// <returns>Logarithm of the factor's average value across the given argument distributions.</returns>
 /// <remarks>
 ///   <para>The formula for the result is <c>log(sum_(concat,ch,str) p(concat,ch,str) factor(concat,ch,str))</c>.</para>
 /// </remarks>
 public static double LogAverageFactor(StringDistribution concat, DiscreteChar ch, StringDistribution str)
 {
     return(StringConcatOp.LogAverageFactor(concat, StringDistribution.Char(ch), str));
 }
Exemplo n.º 13
0
 /// <summary>EP message to <c>str</c>.</summary>
 /// <param name="concat">Incoming message from <c>concat</c>.</param>
 /// <param name="ch">Incoming message from <c>ch</c>.</param>
 /// <returns>The outgoing EP message to the <c>str</c> argument.</returns>
 /// <remarks>
 ///   <para>The outgoing message is a distribution matching the moments of <c>str</c> as the random arguments are varied. The formula is <c>proj[p(str) sum_(concat,ch) p(concat,ch) factor(concat,ch,str)]/p(str)</c>.</para>
 /// </remarks>
 public static StringDistribution StrAverageConditional(StringDistribution concat, DiscreteChar ch)
 {
     return(StringConcatOp.Str2AverageConditional(concat, StringDistribution.Char(ch)));
 }
Exemplo n.º 14
0
 /// <summary>EP message to <c>concat</c>.</summary>
 /// <param name="ch">Incoming message from <c>ch</c>.</param>
 /// <param name="str">Incoming message from <c>str</c>.</param>
 /// <returns>The outgoing EP message to the <c>concat</c> argument.</returns>
 /// <remarks>
 ///   <para>The outgoing message is a distribution matching the moments of <c>concat</c> as the random arguments are varied. The formula is <c>proj[p(concat) sum_(ch,str) p(ch,str) factor(concat,ch,str)]/p(concat)</c>.</para>
 /// </remarks>
 public static StringDistribution ConcatAverageConditional(DiscreteChar ch, StringDistribution str)
 {
     return(StringConcatOp.ConcatAverageConditional(StringDistribution.Char(ch), str));
 }
Exemplo n.º 15
0
        public void WordModel()
        {
            // We want to build a word model as a reasonably simple StringDistribution. It
            // should satisfy the following:
            // (1) The probability of a word of moderate length should not be
            //     significantly less than the probability of a shorter word.
            // (2) The probability of a specific word conditioned on its length matches that of
            //     words in the target language.
            // We achieve this by putting non-normalized character distributions on the edges. The
            // StringDistribution is unaware that these are non-normalized.
            // The StringDistribution itself is non-normalizable.
            const double TargetProb1  = 0.05;
            const double Ratio1       = 0.4;
            const double TargetProb2  = TargetProb1 * Ratio1;
            const double Ratio2       = 0.2;
            const double TargetProb3  = TargetProb2 * Ratio2;
            const double TargetProb4  = TargetProb3 * Ratio2;
            const double TargetProb5  = TargetProb4 * Ratio2;
            const double Ratio3       = 0.999;
            const double TargetProb6  = TargetProb5 * Ratio3;
            const double TargetProb7  = TargetProb6 * Ratio3;
            const double TargetProb8  = TargetProb7 * Ratio3;
            const double Ratio4       = 0.9;
            const double TargetProb9  = TargetProb8 * Ratio4;
            const double TargetProb10 = TargetProb9 * Ratio4;

            var targetProbabilitiesPerLength = new double[]
            {
                TargetProb1, TargetProb2, TargetProb3, TargetProb4, TargetProb5, TargetProb6, TargetProb7, TargetProb8, TargetProb9, TargetProb10
            };

            var charDistUpper          = DiscreteChar.Upper();
            var charDistLower          = DiscreteChar.Lower();
            var charDistUpperNarrow    = DiscreteChar.OneOf('A', 'B');
            var charDistLowerNarrow    = DiscreteChar.OneOf('a', 'b');
            var charDistUpperScaled    = DiscreteChar.Uniform();
            var charDistLowerScaled1   = DiscreteChar.Uniform();
            var charDistLowerScaled2   = DiscreteChar.Uniform();
            var charDistLowerScaled3   = DiscreteChar.Uniform();
            var charDistLowerScaledEnd = DiscreteChar.Uniform();

            charDistUpperScaled.SetToPartialUniformOf(charDistUpper, Math.Log(TargetProb1));
            charDistLowerScaled1.SetToPartialUniformOf(charDistLower, Math.Log(Ratio1));
            charDistLowerScaled2.SetToPartialUniformOf(charDistLower, Math.Log(Ratio2));
            charDistLowerScaled3.SetToPartialUniformOf(charDistLower, Math.Log(Ratio3));
            charDistLowerScaledEnd.SetToPartialUniformOf(charDistLower, Math.Log(Ratio4));

            var wordModel = StringDistribution.Concatenate(
                new List <DiscreteChar>
            {
                charDistUpperScaled,
                charDistLowerScaled1,
                charDistLowerScaled2,
                charDistLowerScaled2,
                charDistLowerScaled2,
                charDistLowerScaled3,
                charDistLowerScaled3,
                charDistLowerScaled3,
                charDistLowerScaledEnd
            },
                true,
                true);

            const string Word = "Abcdefghij";

            const double Eps                   = 1e-5;
            var          broadDist             = StringDistribution.Char(charDistUpper);
            var          narrowDist            = StringDistribution.Char(charDistUpperNarrow);
            var          narrowWord            = "A";
            var          expectedProbForNarrow = 0.5;

            for (var i = 0; i < targetProbabilitiesPerLength.Length; i++)
            {
                var currentWord     = Word.Substring(0, i + 1);
                var probCurrentWord = Math.Exp(wordModel.GetLogProb(currentWord));
                Assert.Equal(targetProbabilitiesPerLength[i], probCurrentWord, Eps);

                var logAvg = Math.Exp(wordModel.GetLogAverageOf(broadDist));
                Assert.Equal(targetProbabilitiesPerLength[i], logAvg, Eps);

                var prod = StringDistribution.Zero();
                prod.SetToProduct(broadDist, wordModel);
                Xunit.Assert.True(prod.GetWorkspaceOrPoint().HasElementLogValueOverrides);
                probCurrentWord = Math.Exp(prod.GetLogProb(currentWord));
                Assert.Equal(targetProbabilitiesPerLength[i], probCurrentWord, Eps);

                prod.SetToProduct(narrowDist, wordModel);
                Xunit.Assert.False(prod.GetWorkspaceOrPoint().HasElementLogValueOverrides);
                var probNarrowWord = Math.Exp(prod.GetLogProb(narrowWord));
                Assert.Equal(expectedProbForNarrow, probNarrowWord, Eps);

                broadDist              = broadDist.Append(charDistLower);
                narrowDist             = narrowDist.Append(charDistLowerNarrow);
                narrowWord            += "a";
                expectedProbForNarrow *= 0.5;
            }

            // Copied model
            var copiedModel = StringDistribution.FromWorkspace(StringTransducer.Copy().ProjectSource(wordModel.GetWorkspaceOrPoint()));

            // Under transducer.
            for (var i = 0; i < targetProbabilitiesPerLength.Length; i++)
            {
                var currentWord     = Word.Substring(0, i + 1);
                var probCurrentWord = Math.Exp(copiedModel.GetLogProb(currentWord));
                Assert.Equal(targetProbabilitiesPerLength[i], probCurrentWord, Eps);
            }

            // Rescaled model
            var scale                = 0.5;
            var newTargetProb1       = TargetProb1 * scale;
            var charDistUpperScaled1 = DiscreteChar.Uniform();

            charDistUpperScaled1.SetToPartialUniformOf(charDistUpper, Math.Log(newTargetProb1));
            var reWeightingTransducer =
                StringTransducer.Replace(StringDistribution.Char(charDistUpper).GetWorkspaceOrPoint(), StringDistribution.Char(charDistUpperScaled1).GetWorkspaceOrPoint())
                .Append(StringTransducer.Copy());
            var reWeightedWordModel = StringDistribution.FromWorkspace(reWeightingTransducer.ProjectSource(wordModel.GetWorkspaceOrPoint()));

            for (var i = 0; i < targetProbabilitiesPerLength.Length; i++)
            {
                var currentWord     = Word.Substring(0, i + 1);
                var probCurrentWord = Math.Exp(reWeightedWordModel.GetLogProb(currentWord));
                Assert.Equal(scale * targetProbabilitiesPerLength[i], probCurrentWord, Eps);
            }
        }