public void Concat() { var hello = StringDistribution.String("hello"); var world = StringDistribution.String("world"); var helloworld = hello + world; StringInferenceTestUtilities.TestProbability(helloworld, 1.0, "helloworld"); StringInferenceTestUtilities.TestProbability(helloworld, 0.0, string.Empty, "hello", "world"); var any = StringDistribution.Any(); var hellostar = hello + any; StringInferenceTestUtilities.TestProbability(hellostar, 1.0, "helloworld", "hello", "hello world"); StringInferenceTestUtilities.TestProbability(hellostar, 0.0, "hhelloworld", string.Empty, "hell", "hhello"); var starhello = any + hello; StringInferenceTestUtilities.TestProbability(starhello, 1.0, "hhello", "hello", "well hello"); StringInferenceTestUtilities.TestProbability(starhello, 0.0, "hhelloworld", string.Empty, "hell", "hello!!"); var starhellostar = any + hello + any; StringInferenceTestUtilities.TestProbability(starhellostar, 1.0, "hello!!", "hhelloworld", "hhello", "hello", "well hello"); StringInferenceTestUtilities.TestProbability(starhellostar, 0.0, string.Empty, "hell"); var hellostarworld = hello + any + world; StringInferenceTestUtilities.TestProbability(hellostarworld, 1.0, "hello world", "helloworld", "hello uncertain world"); StringInferenceTestUtilities.TestProbability(hellostarworld, 0.0, "hello", "world", "hello world!!"); }
public void LengthBounds() { var lengthDist1 = StringDistribution.Any(minLength: 1, maxLength: 3); Assert.True(lengthDist1.IsProper()); StringInferenceTestUtilities.TestProbability(lengthDist1, StringInferenceTestUtilities.StringUniformProbability(1, 3, 65536), "a", "aa", "aaa"); StringInferenceTestUtilities.TestProbability(lengthDist1, 0.0, string.Empty, "aaaa"); var lengthDist2 = StringDistribution.Repeat(DiscreteChar.OneOf('a', 'b'), minTimes: 1, maxTimes: 3); Assert.True(lengthDist2.IsProper()); StringInferenceTestUtilities.TestProbability(lengthDist2, StringInferenceTestUtilities.StringUniformProbability(1, 3, 2), "a", "ab", "aba"); StringInferenceTestUtilities.TestProbability(lengthDist2, 0.0, string.Empty, "aaaa", "abab", "cc"); var lengthDist3 = StringDistribution.Repeat(DiscreteChar.OneOf('a', 'b'), minTimes: 2, maxTimes: 2); Assert.True(lengthDist3.IsProper()); StringInferenceTestUtilities.TestProbability(lengthDist3, StringInferenceTestUtilities.StringUniformProbability(2, 2, 2), "aa", "ab", "ba", "bb"); StringInferenceTestUtilities.TestProbability(lengthDist3, 0.0, string.Empty, "a", "abab", "cc"); var minLengthDist = StringDistribution.Any(minLength: 2); Assert.False(minLengthDist.IsProper()); StringInferenceTestUtilities.TestProbability(minLengthDist, 1.0, "aa", "123", "@*(@*&(@)"); StringInferenceTestUtilities.TestProbability(minLengthDist, 0.0, string.Empty, "a", "!"); var maxLengthDist = StringDistribution.ZeroOrMore(DiscreteChar.Digit(), maxTimes: 3); Assert.True(maxLengthDist.IsProper()); StringInferenceTestUtilities.TestProbability(maxLengthDist, StringInferenceTestUtilities.StringUniformProbability(0, 3, 10), string.Empty, "1", "32", "432"); StringInferenceTestUtilities.TestProbability(maxLengthDist, 0.0, "abc", "1234"); }
public void ZeroDetection() { StringDistribution dist1 = StringDistribution.OneOf(1.0, StringDistribution.Zero(), 0.0, StringDistribution.Any()); Assert.True(dist1.IsZero()); StringInferenceTestUtilities.TestProbability(dist1, 0.0, string.Empty, "a", "bc"); StringDistribution dist2 = StringDistribution.Capitalized(2, 4).Product(StringDistribution.Any(minLength: 5, maxLength: 7)); Assert.True(dist2.IsZero()); StringInferenceTestUtilities.TestProbability(dist2, 0.0, string.Empty, "Abc", "Abcdef"); StringDistribution dist3 = StringDistribution.Digits(minLength: 3, maxLength: 3).Product(StringDistribution.String("12a")); Assert.True(dist3.IsZero()); StringInferenceTestUtilities.TestProbability(dist3, 0.0, string.Empty, "12a", "1", "2", "666"); StringDistribution dist4 = StringDistribution.Any(minLength: 1, maxLength: 2).Product(StringDistribution.Any(minLength: 2, maxLength: 3).Product(StringDistribution.Any(minLength: 3, maxLength: 4))); Assert.True(dist4.IsZero()); StringInferenceTestUtilities.TestProbability(dist4, 0.0, string.Empty, "a", "ab", "abc", "abcd"); StringDistribution dist5 = StringDistribution.Any().Append(StringDistribution.Zero()); Assert.True(dist5.IsZero()); StringInferenceTestUtilities.TestProbability(dist5, 0.0, string.Empty, "a", "bc"); StringDistribution dist6 = StringDistribution.Zero().Append(StringDistribution.OneOf("abc", "def")); Assert.True(dist6.IsZero()); StringInferenceTestUtilities.TestProbability(dist6, 0.0, string.Empty, "a", "bc"); }
public void SemanticWebTest2b() { var prop0Dist = StringDistribution.OneOf("Anthony Blair", "Tony Blair"); var prop0 = Variable.Random(prop0Dist); var dateStrings = Variable.Observed(new[] { "6 May 1953", "May 6, 1953" }); var dateFormat = Variable.DiscreteUniform(dateStrings.Range); var prop1 = ArrayIndex(dateStrings, dateFormat); var template = Variable.Random(StringDistribution.Any()); var text = Variable.StringFormat(template, prop0, prop1); var engine = new InferenceEngine(); engine.Compiler.RecommendedQuality = QualityBand.Experimental; engine.NumberOfIterations = 1; var textDist = engine.Infer <StringDistribution>(text); Console.WriteLine("textDist={0}", textDist); Assert.False(double.IsNegativeInfinity(textDist.GetLogProb("6 May 1953 is the date of birth of Tony Blair."))); Assert.False(double.IsNegativeInfinity(textDist.GetLogProb("6 May 1953 is the date of birth of Anthony Blair."))); Assert.False(double.IsNegativeInfinity(textDist.GetLogProb("Mr. Tony Blair was born on May 6, 1953."))); }
public void StringFormatSimpleTest() { // number of objects var J = new Range(2).Named("J"); var names = Variable.Array <string>(J).Named("names"); names[J] = Variable.Random(WordString()).ForEach(J); Variable.ConstrainEqual(names[0], "John"); var i = Variable.DiscreteUniform(J); var template = Variable.Random(StringDistribution.Any()); var s = Variable.New <string>(); using (Variable.Switch(i)) { s.SetTo(Variable.StringFormat(template, names[i])); } s.ObservedValue = "John was here"; var engine = new InferenceEngine { NumberOfIterations = 10 }; engine.Compiler.RecommendedQuality = QualityBand.Experimental; engine.Compiler.GivePriorityTo(typeof(ReplicateOp_NoDivide)); Console.WriteLine("posterior=" + engine.Infer(i)); }
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}"); }
/// <summary>EP message to <c>args</c>.</summary> /// <param name="str">Incoming message from <c>str</c>.</param> /// <param name="format">Incoming message from <c>format</c>.</param> /// <param name="args">Incoming message from <c>args</c>.</param> /// <param name="result">Modified to contain the outgoing message.</param> /// <returns> /// <paramref name="result" /> /// </returns> /// <remarks> /// <para>The outgoing message is a distribution matching the moments of <c>args</c> as the random arguments are varied. The formula is <c>proj[p(args) sum_(str,format) p(str,format) factor(str,format,args)]/p(args)</c>.</para> /// </remarks> /// <typeparam name="TStringDistributionList">The type of an outgoing message to <c>args</c>.</typeparam> public static TStringDistributionList ArgsAverageConditional <TStringDistributionList>( StringDistribution str, StringDistribution format, IList <StringDistribution> args, TStringDistributionList result) where TStringDistributionList : class, IList <StringDistribution> { Argument.CheckIfNotNull(str, "str"); Argument.CheckIfNotNull(format, "format"); Argument.CheckIfNotNull(args, "args"); Argument.CheckIfValid(args.Count > 0, "args", "There must be at least one argument provided."); // TODO: relax? if (args.Count >= 10) { throw new NotImplementedException("Up to 10 arguments currently supported."); } var argsCopy = new List <StringDistribution>(args); for (int i = 0; i < args.Count; ++i) { argsCopy[i] = StringDistribution.Any(); StringDistribution toStr = StrAverageConditional(format, argsCopy); StringDistribution toStrTimesStr = toStr.Product(str); toStrTimesStr.PruneToGroup((byte)(i + 1)); result[i] = toStrTimesStr; argsCopy[i] = args[i]; } return(result); }
public void SimpleGatedModelTest4() { const double SelectorProbabilityTrue = 0.3; const string PreferredOption = "a"; Variable <string> str = Variable.StringUniform().Named("str"); Variable <bool> selector = Variable.Bernoulli(SelectorProbabilityTrue).Named("selector"); using (Variable.If(selector)) { Variable.ConstrainEqual(str, PreferredOption); } using (Variable.IfNot(selector)) { Variable.ConstrainEqualRandom(str, StringDistribution.Any()); } str.ObservedValue = PreferredOption; var engine = new InferenceEngine(); var selectorPosterior = engine.Infer <Bernoulli>(selector); Assert.Equal(1.0, selectorPosterior.GetProbTrue(), 1e-6); }
public void MessageToArgs7() { TestMessageToArgs( StringDistribution.String("XXX"), StringDistribution.String("{0}{1}"), Util.ArrayInit(2, i => StringDistribution.Any()), Util.ArrayInit(2, i => StringDistribution.OneOf("", "X", "XX", "XXX"))); }
public void MessageToArgs5() { TestMessageToArgs( StringDistribution.String("A and ."), StringDistribution.String("{0} and {1}."), Util.ArrayInit(2, i => StringDistribution.Any()), new[] { StringDistribution.String("A"), StringDistribution.String("") }); }
public void MessageToArgs3() { TestMessageToArgs( StringDistribution.String("A template with ab."), StringDistribution.String("A template with {0}{1}."), Util.ArrayInit(2, i => StringDistribution.Any()), new[] { StringDistribution.OneOf("", "a", "ab"), StringDistribution.OneOf("", "ab", "b") }); }
public void MessageToArgs2() { TestMessageToArgs( StringDistribution.String("A template with arg0 and arg1."), StringDistribution.String("A template with {0} and {1}."), Util.ArrayInit(2, i => StringDistribution.Any()), new[] { StringDistribution.String("arg0"), StringDistribution.String("arg1") }); }
public void AppendPointMassUniform() { var unifPlusH = StringDistribution.Any() + StringDistribution.String("h"); Assert.False(unifPlusH.IsProper()); StringInferenceTestUtilities.TestProbability(unifPlusH, 1.0, "h", "hh", "advahbdkjshbfjlhh"); StringInferenceTestUtilities.TestProbability(unifPlusH, 0.0, string.Empty, "jam"); }
public void MessageToArgs4() { TestMessageToArgs( StringDistribution.String("A template with ab."), StringDistribution.String("A template with {1}."), Util.ArrayInit(2, i => StringDistribution.Any()), new[] { StringDistribution.Zero(), StringDistribution.Zero() }, new[] { StringDistribution.Any(), StringDistribution.String("ab") }); }
public void Mixture2() { var dist1 = StringDistribution.Any(); var dist2 = StringDistribution.OneOf("c", "d", "e"); var mixture = StringDistribution.OneOf(dist1, dist2); Assert.False(mixture.IsProper()); StringInferenceTestUtilities.TestIfIncludes(mixture, "a", "b", "c", "d", "e"); }
public void Mixture3() { var unifMix = StringDistribution.Zero(); Assert.False(unifMix.IsProper()); unifMix.SetToSum(0.5, StringDistribution.Any(), 0.5, StringDistribution.String("hello")); StringInferenceTestUtilities.TestProbability(unifMix, 1.0, "hello"); StringInferenceTestUtilities.TestProbability(unifMix, 0.5, string.Empty, "something else"); }
public void Product4() { StringInferenceTestUtilities.TestProduct(StringDistribution.Zero(), StringDistribution.OneOf("a", "c"), StringDistribution.Zero(), "a", "c", string.Empty); StringInferenceTestUtilities.TestProduct(StringDistribution.Zero(), StringDistribution.String("a"), StringDistribution.Zero(), "a", "c", string.Empty); StringInferenceTestUtilities.TestProduct(StringDistribution.OneOf("a", "c"), StringDistribution.Zero(), StringDistribution.Zero(), "a", "c", string.Empty); StringInferenceTestUtilities.TestProduct(StringDistribution.String("a"), StringDistribution.Zero(), StringDistribution.Zero(), "a", "c", string.Empty); StringInferenceTestUtilities.TestProduct(StringDistribution.Zero(), StringDistribution.Zero(), StringDistribution.Zero(), "a", "c", string.Empty); StringInferenceTestUtilities.TestProduct(StringDistribution.Zero(), StringDistribution.Any(), StringDistribution.Zero(), "a", "c", string.Empty); StringInferenceTestUtilities.TestProduct(StringDistribution.Any(), StringDistribution.Zero(), StringDistribution.Zero(), "a", "c", string.Empty); }
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"); }
private static Variable <string> ArrayIndex(VariableArray <string> array, Variable <int> index) { var res = Variable.Random(StringDistribution.Any()); using (Variable.Switch(index)) { Variable.ConstrainEqual(res, array[index]); } return(res); }
public void Uniform() { var unif1 = StringDistribution.Any(); var unif2 = StringDistribution.Uniform(); Assert.True(unif1.IsUniform()); Assert.True(unif2.IsUniform()); Assert.False(unif1.IsProper()); Assert.False(unif2.IsProper()); StringInferenceTestUtilities.TestProbability(unif1, 1.0, "hello", string.Empty); StringInferenceTestUtilities.TestProbability(unif2, 1.0, "hello", string.Empty); }
/// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="StringsAreEqualOp"]/message_doc[@name="Str1AverageConditional(StringDistribution, Bernoulli, StringDistribution)"]/*'/> public static StringDistribution Str1AverageConditional( StringDistribution str2, [SkipIfUniform] Bernoulli areEqual, StringDistribution result) { StringDistribution uniform = StringDistribution.Any(); double probNotEqual = areEqual.GetProbFalse(); if (probNotEqual > 0.5) { throw new NotImplementedException("Non-equality case is not yet supported."); } double logWeight1 = Math.Log(1 - (2 * probNotEqual)); double logWeight2 = Math.Log(probNotEqual) + uniform.GetLogNormalizer(); result.SetToSumLog(logWeight1, str2, logWeight2, uniform); return(result); }
/// <summary>EP message to <c>str</c>.</summary> /// <param name="sub">Constant value for <c>sub</c>.</param> /// <param name="start">Constant value for <c>start</c>.</param> /// <param name="length">Constant value for <c>length</c>.</param> /// <returns>The outgoing EP message to the <c>str</c> argument.</returns> /// <remarks> /// <para>The outgoing message is the factor viewed as a function of <c>str</c> conditioned on the given values.</para> /// </remarks> public static StringDistribution StrAverageConditional(string sub, int start, int length) { Argument.CheckIfNotNull(sub, "sub"); Argument.CheckIfInRange(start >= 0, "start", "Start index must be non-negative."); Argument.CheckIfInRange(length >= 0, "length", "Length must be non-negative."); if (sub.Length != length) { return(StringDistribution.Zero()); } StringDistribution result = StringDistribution.Any(minLength: start, maxLength: start); result.AppendInPlace(sub); result.AppendInPlace(StringDistribution.Any()); return(result); }
public void InferSubstringFromStringTest() { Variable <string> str = Variable.Observed("Hello"); Variable <string> substr = Variable.Substring(str, 0, 2); var engine = new InferenceEngine(); var substrPosterior = engine.Infer <StringDistribution>(substr); StringInferenceTestUtilities.TestLogProbability(substrPosterior, 0, "He"); Variable <string> str2 = Variable.Random(StringDistribution.Any()); Variable <string> substr2 = Variable.Substring(str2, 0, 2); var substr2Posterior = engine.Infer <StringDistribution>(substr2); StringInferenceTestUtilities.TestIfIncludes(substr2Posterior, "ab", " ", "dd"); StringInferenceTestUtilities.TestIfExcludes(substr2Posterior, "abc", " ", string.Empty); }
public void SemanticWebTest1() { var prop0 = "Tony Blair"; var prop1 = "6 May 1953"; var template = Variable.Random(StringDistribution.Any()); var text = Variable.StringFormat(template, prop0, prop1); var engine = new InferenceEngine(); engine.Compiler.RecommendedQuality = QualityBand.Experimental; engine.NumberOfIterations = 1; var textDist = engine.Infer <StringDistribution>(text); Console.WriteLine("textDist={0}", textDist); Assert.False(double.IsNegativeInfinity(textDist.GetLogProb("6 May 1953 is the date of birth of Tony Blair."))); }
public void UniformDetection() { StringDistribution dist1 = StringDistribution.OneOf(0.0, StringDistribution.Zero(), 1.0, StringDistribution.Any()); Assert.True(dist1.IsUniform()); StringInferenceTestUtilities.TestProbability(dist1, 1.0, string.Empty, "a", "bc"); StringDistribution dist2 = StringDistribution.OneOf(0.3, StringDistribution.Any(), 0.7, StringDistribution.Any()); Assert.True(dist2.IsUniform()); StringInferenceTestUtilities.TestProbability(dist1, 1.0, string.Empty, "a", "bc"); StringDistribution dist3 = StringDistribution.OneOf(1.0, StringDistribution.Any(), 2.0, StringDistribution.OneOf(0.1, StringDistribution.Any(), 0.2, StringDistribution.Any())); Assert.True(dist3.IsUniform()); StringInferenceTestUtilities.TestProbability(dist3, 1.0, string.Empty, "a", "bc"); }
public void StringFormatPerformanceTest2() { Assert.Timeout(() => { Rand.Restart(777); Variable <string> template = Variable.Constant("{0} {1}").Named("template"); Variable <string> arg1 = Variable.Random(StringDistribution.Any(minLength: 1, maxLength: 15)).Named("arg1"); Variable <string> arg2 = Variable.Random(StringDistribution.Any(minLength: 1)).Named("arg2"); Variable <string> text = Variable.StringFormat(template, arg1, arg2).Named("text"); Variable <string> fullTextFormat = Variable.Random(StringDistribution.Any()).Named("fullTextFormat"); Variable <string> fullText = Variable.StringFormat(fullTextFormat, text).Named("fullText"); var engine = new InferenceEngine(); engine.Compiler.RecommendedQuality = QualityBand.Experimental; engine.ShowProgress = false; Action action = () => { // Generate random observed string string observedPattern = string.Empty; for (int j = 0; j < 5; ++j) { for (int k = 0; k < 5; ++k) { observedPattern += (char)Rand.Int('a', 'z' + 1); } observedPattern += ' '; } // Run inference fullText.ObservedValue = observedPattern; engine.Infer <StringDistribution>(arg1); engine.Infer <StringDistribution>(arg2); engine.Infer <StringDistribution>(text); engine.Infer <StringDistribution>(fullTextFormat); }; action(); // To exclude the compilation time from the profile ProfileAction(action, 100); }, 10000); }
public void Product1() { var hello = StringDistribution.String("hello"); var world = StringDistribution.String("world"); var helloworld = StringDistribution.String("hello world"); var subs = StringDistribution.OneOf("hello", "world", "hello world"); var any = StringDistribution.Any(); string[] interestingStrings = new[] { "hello", "world", "hello world", "hell", "ello", string.Empty }; StringInferenceTestUtilities.TestProduct(subs, subs, subs, interestingStrings); StringInferenceTestUtilities.TestProduct(hello, any, hello, interestingStrings); StringInferenceTestUtilities.TestProduct(any, hello, hello, interestingStrings); StringInferenceTestUtilities.TestProduct(world, any, world, interestingStrings); StringInferenceTestUtilities.TestProduct(any, world, world, interestingStrings); StringInferenceTestUtilities.TestProduct(any, any, any, interestingStrings); StringInferenceTestUtilities.TestProduct(hello, world, StringDistribution.Zero()); StringInferenceTestUtilities.TestProduct(helloworld, world, StringDistribution.Zero()); }
public void SemanticWebTest4() { var prop0 = Variable.Random(NamePrior()); var dateStrings = Variable.Observed(new[] { "6 May 1953", "May 6, 1953" }); var dateFormat = Variable.DiscreteUniform(dateStrings.Range); var prop1 = ArrayIndex(dateStrings, dateFormat); var template = Variable.Random(StringDistribution.String("{0}") + WordStrings.WordMiddle() + StringDistribution.String("{1}")); var text = Variable.StringFormat(template, prop0, prop1); var template2 = Variable.Random(StringDistribution.Any()); var fulltext = Variable.StringFormat(template2, text); fulltext.ObservedValue = "I believe Mr Tony Blair was born on May 6, 1953 in Edinburgh, UK."; 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); var textDist = engine.Infer <StringDistribution>(text); var dateFormatDist = engine.Infer <Discrete>(dateFormat); StringInferenceTestUtilities.TestProbability(prop0Dist, 0.5, "Mr Tony", "Tony Blair"); StringInferenceTestUtilities.TestProbability(prop1Dist, 1.0, "May 6, 1953"); StringInferenceTestUtilities.TestProbability(templateDist, 0.5, "{0} was born on {1}", "{0} Blair was born on {1}"); StringInferenceTestUtilities.TestProbability( textDist, 0.5, "Tony Blair was born on May 6, 1953", "Mr Tony Blair was born on May 6, 1953"); //Assert.AreEqual(1.0, dateFormatDist[1]); // TODO: fix me }
public void StringFormatTest3() { var T = new Range(2).Named("T"); var templates = Variable.Array <string>(T).Named("templates"); //templates[T] = Variable.Random(WordStrings.WordPrefix() + StringAutomaton.String("{0}") + WordStrings.WordSuffix()).ForEach(T); templates[T] = Variable.Random(StringDistribution.Any()).ForEach(T); var a = Variable.Random(StringDistribution.OneOf("John", "Tom")).Named("a"); var N = new Range(2).Named("N"); var templateNumber = Variable.Array <int>(N).Named("templateNumber"); var templateNumberPrior = Variable.Observed(new Vector[] { Vector.FromArray(0.4, 0.6), Vector.FromArray(0.6, 0.4) }, N); var b = Variable.Array <string>(N).Named("b"); using (Variable.ForEach(N)) { templateNumber[N] = Variable.Discrete(T, templateNumberPrior[N]); using (Variable.Switch(templateNumber[N])) { b[N] = Variable.StringFormat(templates[templateNumber[N]], a); } } Variable.ConstrainEqual(templateNumber[0], 0); var engine = new InferenceEngine(); engine.NumberOfIterations = 5; engine.Compiler.RecommendedQuality = QualityBand.Experimental; engine.Compiler.GivePriorityTo(typeof(ReplicateOp_NoDivide)); engine.Compiler.UnrollLoops = true; Test("a", engine.Infer <IDistribution <string> >(a), "John", "Tom"); Console.WriteLine(engine.Infer(b)); b.ObservedValue = new string[] { "My name is John.", "I'm John." }; Console.WriteLine(engine.Infer(templateNumber)); Console.WriteLine(engine.Infer(templates)); Test("a", engine.Infer <IDistribution <string> >(a), "John"); }
public void SampleImproper() { Rand.Restart(96); const double probChar = (double)1 / (char.MaxValue + 1); const double StoppingProbability = probChar * 0.99; var dist = StringDistribution.Any(); // The length of sequences sampled from this distribution will follow a geometric distribution var acc = new MeanVarianceAccumulator(); const int SampleCount = 30000; for (int i = 0; i < SampleCount; ++i) { string sample = dist.Sample(); acc.Add(sample.Length); } const double ExpectedMean = (1.0 - StoppingProbability) / StoppingProbability; const double ExpectedVariance = (1.0 - StoppingProbability) / (StoppingProbability * StoppingProbability); Assert.Equal(ExpectedMean, acc.Mean, 1e-2); Assert.Equal(ExpectedVariance, acc.Variance, 1e-2); }