Example #1
0
 /// <summary>Evidence message for EP.</summary>
 /// <param name="str">Constant value for <c>str</c>.</param>
 /// <param name="format">Incoming message from <c>format</c>.</param>
 /// <param name="args">Incoming message from <c>args</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_(format,args) p(format,args) factor(str,format,args))</c>. Adding up these values across all factors and variables gives the log-evidence estimate for EP.</para>
 /// </remarks>
 public static double LogEvidenceRatio(string str, StringDistribution format, IList <string> args)
 {
     return(LogEvidenceRatio(str, format, ToAutomatonArray(args)));
 }
Example #2
0
 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);
 }
Example #3
0
        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");
        }
Example #4
0
            public void Initialize()
            {
                // DO NOT make this a constructor, because it makes the test not notice complete lack of serialization as an empty object is set up exactly as the thing
                // you are trying to deserialize.
                this.pareto  = new Pareto(1.2, 3.5);
                this.poisson = new Poisson(2.3);
                this.wishart = new Wishart(20, new PositiveDefiniteMatrix(new double[, ] {
                    { 22, 21 }, { 21, 23 }
                }));
                this.vectorGaussian = new VectorGaussian(Vector.FromArray(13, 14), new PositiveDefiniteMatrix(new double[, ] {
                    { 16, 15 }, { 15, 17 }
                }));
                this.unnormalizedDiscrete = UnnormalizedDiscrete.FromLogProbs(DenseVector.FromArray(5.1, 5.2, 5.3));
                this.pointMass            = PointMass <double> .Create(1.1);

                this.gaussian             = new Gaussian(11.0, 12.0);
                this.nonconjugateGaussian = new NonconjugateGaussian(1.2, 2.3, 3.4, 4.5);
                this.gamma              = new Gamma(9.0, 10.0);
                this.gammaPower         = new GammaPower(5.6, 2.8, 3.4);
                this.discrete           = new Discrete(6.0, 7.0, 8.0);
                this.conjugateDirichlet = new ConjugateDirichlet(1.2, 2.3, 3.4, 4.5);
                this.dirichlet          = new Dirichlet(3.0, 4.0, 5.0);
                this.beta      = new Beta(2.0, 1.0);
                this.binomial  = new Binomial(5, 0.8);
                this.bernoulli = new Bernoulli(0.6);

                this.sparseBernoulliList    = SparseBernoulliList.Constant(4, new Bernoulli(0.1));
                this.sparseBernoulliList[1] = new Bernoulli(0.9);
                this.sparseBernoulliList[3] = new Bernoulli(0.7);

                this.sparseBetaList    = SparseBetaList.Constant(5, new Beta(2.0, 2.0));
                this.sparseBetaList[0] = new Beta(3.0, 4.0);
                this.sparseBetaList[1] = new Beta(5.0, 6.0);

                this.sparseGaussianList    = SparseGaussianList.Constant(6, Gaussian.FromMeanAndPrecision(0.1, 0.2));
                this.sparseGaussianList[4] = Gaussian.FromMeanAndPrecision(0.3, 0.4);
                this.sparseGaussianList[5] = Gaussian.FromMeanAndPrecision(0.5, 0.6);

                this.sparseGammaList = SparseGammaList.Constant(1, Gamma.FromShapeAndRate(1.0, 2.0));

                this.truncatedGamma    = new TruncatedGamma(1.2, 2.3, 3.4, 4.5);
                this.truncatedGaussian = new TruncatedGaussian(1.2, 3.4, 5.6, 7.8);
                this.wrappedGaussian   = new WrappedGaussian(1.2, 2.3, 3.4);

                ga = Distribution <double> .Array(new[] { this.gaussian, this.gaussian });

                vga = Distribution <Vector> .Array(new[] { this.vectorGaussian, this.vectorGaussian });

                ga2D = Distribution <double> .Array(new[, ] {
                    { this.gaussian, this.gaussian }, { this.gaussian, this.gaussian }
                });

                vga2D = Distribution <Vector> .Array(new[, ] {
                    { this.vectorGaussian, this.vectorGaussian }, { this.vectorGaussian, this.vectorGaussian }
                });

                gaJ = Distribution <double> .Array(new[] { new[] { this.gaussian, this.gaussian }, new[] { this.gaussian, this.gaussian } });

                vgaJ = Distribution <Vector> .Array(new[] { new[] { this.vectorGaussian, this.vectorGaussian }, new[] { this.vectorGaussian, this.vectorGaussian } });

                var gp    = new GaussianProcess(new ConstantFunction(0), new SquaredExponential(0));
                var basis = Util.ArrayInit(2, i => Vector.FromArray(1.0 * i));

                this.sparseGp = new SparseGP(new SparseGPFixed(gp, basis));

                this.quantileEstimator = new QuantileEstimator(0.01);
                this.quantileEstimator.Add(5);

                this.stringDistribution1 = StringDistribution.String("aa").Append(StringDistribution.OneOf("b", "ccc")).Append("dddd");
                this.stringDistribution2 = new StringDistribution();
                this.stringDistribution2.SetToProduct(StringDistribution.OneOf("a", "b"), StringDistribution.OneOf("b", "c"));
            }
        public void RegexpBuildingPerformanceTest2()
        {
            Assert.Timeout(() =>
            {
                StringDistribution dist = StringDistribution.OneOf(StringDistribution.Lower(), StringDistribution.Upper());
                for (int i = 0; i < 3; ++i)
                {
                    dist = StringDistribution.OneOf(dist, dist);
                }

                Console.WriteLine(dist.ToString());

                ProfileAction(() => RegexpTreeBuilder.BuildRegexp(dist.GetWorkspaceOrPoint()), 6000);
            }, 20000);
        }
Example #6
0
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="StringsAreEqualOp"]/message_doc[@name="LogAverageFactor(bool, StringDistribution, StringDistribution)"]/*'/>
        public static double LogAverageFactor(bool areEqual, StringDistribution str1, StringDistribution str2)
        {
            Bernoulli messageToAreEqual = AreEqualAverageConditional(str1, str2);

            return(messageToAreEqual.GetLogProb(areEqual));
        }
 public static double LogEvidenceRatio(DiscreteChar allowedChars, Discrete length, StringDistribution str)
 {
     return(0);
 }
Example #8
0
        /// <summary>
        /// An implementation of <see cref="StrAverageConditional(StringDistribution, IList{StringDistribution}, IList{string})"/>
        /// specialized for some cases for performance reasons.
        /// </summary>
        /// <param name="format">The message from <c>format</c>.</param>
        /// <param name="allowedArgs">The message from <c>args</c>, truncated to allowed values and converted to automata.</param>
        /// <param name="argNames">The names of the arguments.</param>
        /// <param name="withGroups">Whether the result should mark different arguments with groups.</param>
        /// <returns>
        /// Result distribution if there is an optimized implementation available for the provided parameters.
        /// <see langword="null"/> otherwise.
        /// </returns>
        /// <remarks>
        /// Supports the case of point mass <paramref name="format"/>.
        /// </remarks>
        private static StringDistribution TryOptimizedStrAverageConditionalImpl(
            StringDistribution format, IList <StringAutomaton> allowedArgs, IList <string> argNames, bool withGroups)
        {
            if (!format.IsPointMass)
            {
                // Fall back to the general case
                return(null);
            }

            // Check braces for correctness & replace placeholders with arguments simultaneously
            var result = StringAutomaton.Builder.ConstantOn(Weight.One, string.Empty);

            bool[] argumentSeen = new bool[allowedArgs.Count];
            int    openingBraceIndex = format.Point.IndexOf("{", StringComparison.Ordinal), closingBraceIndex = -1;

            while (openingBraceIndex != -1)
            {
                // Add the part of the format before the placeholder
                result.Append(StringAutomaton.ConstantOn(1.0, format.Point.Substring(closingBraceIndex + 1, openingBraceIndex - closingBraceIndex - 1)));

                // Find next opening and closing braces
                closingBraceIndex = format.Point.IndexOf("}", openingBraceIndex + 1, StringComparison.Ordinal);
                int nextOpeningBraceIndex = format.Point.IndexOf("{", openingBraceIndex + 1, StringComparison.Ordinal);

                // Opening brace must be followed by a closing brace
                if (closingBraceIndex == -1 || (nextOpeningBraceIndex != -1 && nextOpeningBraceIndex < closingBraceIndex))
                {
                    return(StringDistribution.Zero());
                }

                string argumentName  = format.Point.Substring(openingBraceIndex + 1, closingBraceIndex - openingBraceIndex - 1);
                int    argumentIndex = argNames.IndexOf(argumentName);

                // Unknown or previously seen argument found
                if (argumentIndex == -1 || argumentSeen[argumentIndex])
                {
                    return(StringDistribution.Zero());
                }

                // Replace the placeholder by the argument
                result.Append(allowedArgs[argumentIndex], withGroups ? argumentIndex + 1 : 0);

                // Mark the argument as 'seen'
                argumentSeen[argumentIndex] = true;

                openingBraceIndex = nextOpeningBraceIndex;
            }

            // There should be no closing braces after the last opening brace
            if (format.Point.IndexOf('}', closingBraceIndex + 1) != -1)
            {
                return(StringDistribution.Zero());
            }

            if (RequirePlaceholderForEveryArgument && argumentSeen.Any(seen => !seen))
            {
                // Some argument wasn't present although it was required
                return(StringDistribution.Zero());
            }

            // Append the part of the format after the last placeholder
            result.Append(StringAutomaton.ConstantOn(1.0, format.Point.Substring(closingBraceIndex + 1, format.Point.Length - closingBraceIndex - 1)));

            return(StringDistribution.FromWorkspace(result.GetAutomaton()));
        }
Example #9
0
        /// <summary>
        /// Converts a list of string to an array of point mass automata.
        /// </summary>
        /// <param name="strings">The list of strings.</param>
        /// <returns>The created automata array.</returns>
        private static StringDistribution[] ToAutomatonArray(IList <string> strings)
        {
            Debug.Assert(strings != null, "A valid string list must be provided.");

            return(Util.ArrayInit(strings.Count, i => StringDistribution.PointMass(strings[i])));
        }
Example #10
0
        public static double LogEvidenceRatio(string str, StringDistribution format, IList <string> args, IList <string> argNames)
        {
            Argument.CheckIfNotNull(args, "args");

            return(LogEvidenceRatio(str, format, ToAutomatonArray(args), argNames));
        }
Example #11
0
        /// <summary>
        /// An implementation of <see cref="FormatAverageConditional(StringDistribution, IList{StringDistribution}, IList{string})"/>
        /// specialized for some cases for performance reasons.
        /// </summary>
        /// <param name="str">The message from <c>str</c>.</param>
        /// <param name="allowedArgs">The message from <c>args</c>, truncated to allowed values and converted to automata.</param>
        /// <param name="argNames">The names of the arguments.</param>
        /// <param name="resultDist">The computed result.</param>
        /// <returns>
        /// <see langword="true"/> if there is an optimized implementation available for the provided parameters,
        /// and <paramref name="resultDist"/> has been computed using it.
        /// <see langword="false"/> otherwise.
        /// </returns>
        /// <remarks>
        /// Supports the case of point mass <paramref name="str"/> and <paramref name="allowedArgs"/>,
        /// where each of the arguments is present in <paramref name="str"/> at most once and the occurrences
        /// are non-overlapping.
        /// </remarks>
        private static bool TryOptimizedFormatAverageConditionalImpl(
            StringDistribution str, IList <StringAutomaton> allowedArgs, IList <string> argNames, out StringDistribution resultDist)
        {
            resultDist = null;

            string[] allowedArgPoints = Util.ArrayInit(allowedArgs.Count, i => allowedArgs[i].TryComputePoint());
            if (!str.IsPointMass || !allowedArgPoints.All(argPoint => argPoint != null && SubstringOccurrencesCount(str.Point, argPoint) <= 1))
            {
                // Fall back to the general case
                return(false);
            }

            // Obtain arguments present in 'str' (ordered by position)
            var argPositions =
                allowedArgPoints.Select((arg, argIndex) => Tuple.Create(argIndex, str.Point.IndexOf(arg, StringComparison.Ordinal)))
                .Where(t => t.Item2 != -1)
                .OrderBy(t => t.Item2)
                .ToList();

            if (RequirePlaceholderForEveryArgument && argPositions.Count != allowedArgs.Count)
            {
                // Some argument is not in 'str'
                resultDist = StringDistribution.Zero();
                return(true);
            }

            StringAutomaton result            = StringAutomaton.ConstantOn(1.0, string.Empty);
            int             curArgumentIndex  = -1;
            int             curArgumentPos    = -1;
            int             curArgumentLength = 1;

            for (int i = 0; i < argPositions.Count; ++i)
            {
                int prevArgumentIndex  = curArgumentIndex;
                int prevArgumentPos    = curArgumentPos;
                int prevArgumentLength = curArgumentLength;
                curArgumentIndex  = argPositions[i].Item1;
                curArgumentPos    = argPositions[i].Item2;
                curArgumentLength = allowedArgPoints[curArgumentIndex].Length;

                if (prevArgumentIndex != -1 && curArgumentPos < prevArgumentPos + prevArgumentLength)
                {
                    // It's easier to fall back to the general case in case of overlapping arguments
                    return(false);
                }

                // Append the contents of 'str' preceeding the current argument
                result.AppendInPlace(str.Point.Substring(prevArgumentPos + prevArgumentLength, curArgumentPos - prevArgumentPos - prevArgumentLength));

                // The format may have included either the text ot the placeholder
                string argName = "{" + argNames[curArgumentIndex] + "}";
                if (RequirePlaceholderForEveryArgument)
                {
                    result.AppendInPlace(StringAutomaton.ConstantOn(1.0, argName));
                }
                else
                {
                    result.AppendInPlace(StringAutomaton.ConstantOn(1.0, argName, allowedArgPoints[curArgumentIndex]));
                }
            }

            // Append the rest of 'str'
            result.AppendInPlace(str.Point.Substring(curArgumentPos + curArgumentLength, str.Point.Length - curArgumentPos - curArgumentLength));

            resultDist = StringDistribution.FromWorkspace(result);
            return(true);
        }
Example #12
0
 public static StringDistribution ToStrReverseMessage(StringDistribution format, IList <StringDistribution> args, IList <string> argNames)
 {
     return(ComputeToStrReverseMessage(format, args, argNames, true));
 }
Example #13
0
 /// <summary>EP message to <c>str</c>.</summary>
 /// <param name="format">Incoming message from <c>format</c>.</param>
 /// <param name="args">Incoming message from <c>args</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_(format,args) p(format,args) factor(str,format,args)]/p(str)</c>.</para>
 /// </remarks>
 public static StringDistribution StrAverageConditional(StringDistribution format, IList <string> args)
 {
     return(StrAverageConditional(format, ToAutomatonArray(args)));
 }
Example #14
0
 /// <summary>
 /// Converts a list of string to an array of point mass automata.
 /// </summary>
 /// <param name="strings">The list of strings.</param>
 /// <returns>The created automata array.</returns>
 private static StringDistribution[] ToAutomatonArray(IList <string> strings)
 {
     return(Util.ArrayInit(strings.Count, i => StringDistribution.PointMass(strings[i])));
 }
Example #15
0
 /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="StringsAreEqualOp"]/message_doc[@name="Str2AverageConditional(StringDistribution, Bernoulli, StringDistribution)"]/*'/>
 public static StringDistribution Str2AverageConditional(
     StringDistribution str1, [SkipIfUniform] Bernoulli areEqual, StringDistribution result)
 {
     return(Str1AverageConditional(str1, areEqual, result));
 }
Example #16
0
        public static StringDistribution StrAverageConditional(StringDistribution format, IList <string> args)
        {
            Argument.CheckIfNotNull(args, "args");

            return(StringFormatOpBase <TStringFormatOp> .StrAverageConditional(format, args, GetArgumentNames(args.Count)));
        }
Example #17
0
 /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="StringsAreEqualOp"]/message_doc[@name="LogEvidenceRatio(bool, StringDistribution, StringDistribution)"]/*'/>
 public static double LogEvidenceRatio(bool areEqual, StringDistribution str1, StringDistribution str2)
 {
     return(LogAverageFactor(areEqual, str1, str2));
 }
Example #18
0
        public static StringDistribution FormatAverageConditional(StringDistribution str, IList <StringDistribution> args)
        {
            Argument.CheckIfNotNull(args, "args");

            return(StringFormatOpBase <TStringFormatOp> .FormatAverageConditional(str, args, GetArgumentNames(args.Count)));
        }
Example #19
0
        public void MessageOperatorsTest()
        {
            //// Messages to concatenation results

            StringDistribution concat1 = StringConcatOp.ConcatAverageConditional(StringDistribution.String("ab"), StringDistribution.String("cd"));

            Assert.True(concat1.IsPointMass && concat1.Point == "abcd");

            StringDistribution concat2 = StringConcatOp.ConcatAverageConditional(StringDistribution.Upper(), StringDistribution.String("cd"));

            StringInferenceTestUtilities.TestIfIncludes(concat2, "Acd", "ABcd");
            StringInferenceTestUtilities.TestIfExcludes(concat2, "cd", "Abcd", "ABc");

            StringDistribution concat3 = StringConcatOp.ConcatAverageConditional(StringDistribution.OneOf("a", "ab"), StringDistribution.OneOf("b", string.Empty));

            StringInferenceTestUtilities.TestProbability(concat3, 0.5, "ab");
            StringInferenceTestUtilities.TestProbability(concat3, 0.25, "a", "abb");

            //// Messages to the first argument

            StringDistribution str11 = StringConcatOp.Str1AverageConditional(StringDistribution.String("abc"), StringDistribution.OneOf("abc", "bc"));

            StringInferenceTestUtilities.TestProbability(str11, 0.5, string.Empty, "a");

            StringDistribution str12 = StringConcatOp.Str1AverageConditional(StringDistribution.String("abc"), StringDistribution.OneOf("abc", "bc", "a", "b"));

            StringInferenceTestUtilities.TestProbability(str12, 0.5, string.Empty, "a");

            StringDistribution str13 = StringConcatOp.Str1AverageConditional(StringDistribution.OneOf("aa", "aaa"), StringDistribution.OneOf("a", "aa"));

            StringInferenceTestUtilities.TestProbability(str13, 0.5, "a");
            StringInferenceTestUtilities.TestProbability(str13, 0.25, string.Empty, "aa");

            StringDistribution str14 = StringConcatOp.Str1AverageConditional(StringDistribution.OneOf("abc", "abd"), StringDistribution.OneOf("bc", "d"));

            StringInferenceTestUtilities.TestProbability(str14, 0.5, "a", "ab");

            StringDistribution str15 = StringConcatOp.Str1AverageConditional(StringDistribution.OneOf("abc", "abd"), StringDistribution.OneOf("bc", "bd"));

            Assert.True(str15.IsPointMass && str15.Point == "a");

            //// Messages to the second argument

            StringDistribution str21 = StringConcatOp.Str2AverageConditional(StringDistribution.String("abc"), StringDistribution.OneOf("abc", "ab"));

            StringInferenceTestUtilities.TestProbability(str21, 0.5, string.Empty, "c");

            StringDistribution str22 = StringConcatOp.Str2AverageConditional(StringDistribution.String("abc"), StringDistribution.OneOf("abc", "ab", "c", "b"));

            StringInferenceTestUtilities.TestProbability(str22, 0.5, string.Empty, "c");

            StringDistribution str23 = StringConcatOp.Str2AverageConditional(StringDistribution.OneOf("aa", "aaa"), StringDistribution.OneOf("a", "aa"));

            StringInferenceTestUtilities.TestProbability(str23, 0.5, "a");
            StringInferenceTestUtilities.TestProbability(str23, 0.25, string.Empty, "aa");

            StringDistribution str24 = StringConcatOp.Str2AverageConditional(StringDistribution.OneOf("abc", "dbc"), StringDistribution.OneOf("ab", "d"));

            StringInferenceTestUtilities.TestProbability(str24, 0.5, "c", "bc");

            StringDistribution str25 = StringConcatOp.Str2AverageConditional(StringDistribution.OneOf("abc", "dbc"), StringDistribution.OneOf("ab", "db"));

            Assert.True(str25.IsPointMass && str25.Point == "c");

            //// Evidence messages

            const double EvidenceEps = 1e-6;

            Assert.Equal(0.0, StringConcatOp.LogEvidenceRatio(StringDistribution.Any(minLength: 1, maxLength: 5), StringDistribution.Capitalized(minLength: 2, maxLength: 2), StringDistribution.Upper()), EvidenceEps);
            Assert.Equal(
                Math.Log(1.0 / 3.0),
                StringConcatOp.LogEvidenceRatio("aaba", StringDistribution.OneOf("a", "aa"), StringDistribution.OneOf("a", "ba", "aba")),
                EvidenceEps);
            Assert.True(double.IsNegativeInfinity(
                            StringConcatOp.LogEvidenceRatio("aaba", StringDistribution.OneOf("a", "aa"), StringDistribution.OneOf("a", "bd", "abd"))));

            //// Incompatible message parameters

            Assert.True(StringConcatOp.Str1AverageConditional(StringDistribution.OneOf("abc", "abd"), StringDistribution.OneOf("ab", "b")).IsZero());
            Assert.True(StringConcatOp.Str2AverageConditional(StringDistribution.OneOf("abc", "abd"), StringDistribution.OneOf("bc", "b")).IsZero());
        }
Example #20
0
        public static StringDistribution ToStrReverseMessage(StringDistribution format, IList <StringDistribution> args)
        {
            Argument.CheckIfNotNull(args, "args");

            return(StringFormatOpBase <TStringFormatOp> .ToStrReverseMessage(format, args, GetArgumentNames(args.Count)));
        }
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="StringOfLengthOp"]/message_doc[@name="LogEvidenceRatio(DiscreteChar, Discrete, String)"]/*'/>
        public static double LogEvidenceRatio(DiscreteChar allowedChars, Discrete length, string str)
        {
            StringDistribution toStr = StrAverageConditional(allowedChars, length);

            return(toStr.GetLogProb(str));
        }
Example #22
0
 public static double LogEvidenceRatio(StringDistribution str)
 {
     return(0);
 }
        public void RegexpBuildingPerformanceTest1()
        {
            Assert.Timeout(() =>
            {
                StringDistribution dist =
                    StringDistribution.OneOf(
                        StringDistribution.Lower() + StringDistribution.Upper() + StringDistribution.Optional(StringDistribution.Upper(2)),
                        StringDistribution.Digits(3) + StringDistribution.String("XXX") + StringDistribution.Letters(3, 5));

                Console.WriteLine(dist.ToString());

                ProfileAction(() => RegexpTreeBuilder.BuildRegexp(dist.GetWorkspaceOrPoint()), 6000);
            }, 10000);
        }
Example #24
0
        public static double LogEvidenceRatio(string str, StringDistribution format, IList <string> args)
        {
            Argument.CheckIfNotNull(args, "args");

            return(StringFormatOpBase <TStringFormatOp> .LogEvidenceRatio(str, format, args, GetArgumentNames(args.Count)));
        }
        [Trait("Category", "OpenBug")] // Test failing with AutomatonTooLarge due to determinization added to SetToProduct in change 47614.  Increasing max states to 1M does not fix the issue
        public void PropertyInferencePerformanceTest()
        {
            Rand.Restart(777);

            var namesData     = new[] { "Alice", "Bob", "Charlie", "Eve", "Boris", "John" };
            var valueData     = new[] { "sender", "receiver", "attacker", "eavesdropper", "developer", "researcher" };
            var templatesData = new[] { "{0} is {1}", "{0} is known as {1}", "{1} is a role of {0}", "{0} -- {1}", "{0} aka {1}" };

            var textsData = new string[10];

            for (int i = 0; i < textsData.Length; ++i)
            {
                int entityIndex   = Rand.Int(namesData.Length);
                int templateIndex = Rand.Int(templatesData.Length);
                textsData[i] = string.Format(templatesData[templateIndex], namesData[entityIndex], valueData[entityIndex]);
            }

            var entity   = new Range(namesData.Length).Named("entity");
            var template = new Range(templatesData.Length).Named("template");
            var text     = new Range(textsData.Length).Named("text");

            var entityNames = Variable.Array <string>(entity).Named("entityNames");

            entityNames[entity] = Variable.Random(StringDistribution.Capitalized()).ForEach(entity);
            var entityValues = Variable.Array <string>(entity).Named("entityValues");

            entityValues[entity] = Variable.Random(StringDistribution.Lower()).ForEach(entity);

            StringDistribution templatePriorMiddle = StringDistribution.ZeroOrMore(DiscreteChar.OneOf('{', '}').Complement());
            StringDistribution templatePrior       =
                StringDistribution.OneOf(
                    StringDistribution.String("{0} ") + templatePriorMiddle + StringDistribution.String(" {1}"),
                    StringDistribution.String("{1} ") + templatePriorMiddle + StringDistribution.String(" {0}"));
            var templates = Variable.Array <string>(template).Named("templates");

            templates[template] = Variable.Random(templatePrior).ForEach(template);

            var texts = Variable.Array <string>(text).Named("texts");

            using (Variable.ForEach(text))
            {
                var entityIndex   = Variable.DiscreteUniform(entity).Named("entityIndex");
                var templateIndex = Variable.DiscreteUniform(template).Named("templateIndex");
                using (Variable.Switch(entityIndex))
                    using (Variable.Switch(templateIndex))
                    {
                        texts[text] = Variable.StringFormat(templates[templateIndex], entityNames[entityIndex], entityValues[entityIndex]);
                    }
            }

            texts.ObservedValue = textsData;

            var engine = new InferenceEngine();

            engine.ShowProgress                = false;
            engine.OptimiseForVariables        = new[] { entityNames, entityValues };
            engine.Compiler.RecommendedQuality = QualityBand.Experimental;
            // TODO: get this test to work with parallel for loops.
            engine.Compiler.UseParallelForLoops = false;
            engine.NumberOfIterations           = 1;

            ProfileAction(
                () =>
            {
                Console.WriteLine(engine.Infer <StringDistribution[]>(entityNames)[0]);
                Console.WriteLine(engine.Infer <StringDistribution[]>(entityValues)[0]);
            },
                1);
        }
Example #26
0
        public static StringDistribution StrAverageConditional(StringDistribution format, IList <string> args, IList <string> argNames)
        {
            Argument.CheckIfNotNull(args, "args");

            return(StrAverageConditional(format, ToAutomatonArray(args), argNames));
        }
Example #27
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);
        }
Example #28
0
 /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="StringsAreEqualOp"]/message_doc[@name="AreEqualAverageConditional(StringDistribution, StringDistribution)"]/*'/>
 public static Bernoulli AreEqualAverageConditional(StringDistribution str1, StringDistribution str2)
 {
     return(Bernoulli.FromLogOdds(MMath.LogitFromLog(str1.GetLogAverageOf(str2))));
 }
Example #29
0
        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");
        }
Example #30
0
        /// <summary>Evidence message for EP.</summary>
        /// <param name="str">Constant value for <c>str</c>.</param>
        /// <param name="format">Incoming message from <c>format</c>.</param>
        /// <param name="args">Incoming message from <c>args</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_(format,args) p(format,args) factor(str,format,args))</c>. Adding up these values across all factors and variables gives the log-evidence estimate for EP.</para>
        /// </remarks>
        public static double LogEvidenceRatio(string str, StringDistribution format, IList <StringDistribution> args)
        {
            StringDistribution toStr = StrAverageConditional(format, args);

            return(toStr.GetLogProb(str));
        }