Exemple #1
0
 public void NormalCdf2Test4()
 {
     for (int j = 8; j <= 17; j++)
     {
         double r        = -1 + System.Math.Pow(10, -j);
         double expected = System.Math.Log(NormalCdfZero(r));
         for (int i = 5; i < 100; i++)
         {
             double x      = -System.Math.Pow(10, -i);
             double y      = -x;
             double actual = MMath.NormalCdfLn(x, y, r);
             //double actual = Math.Log(NormalCdfConFrac3(x, y, r));
             double error;
             if (actual == expected)
             {
                 error = 0;
             }
             else
             {
                 error = System.Math.Abs(actual - expected);
             }
             //Console.WriteLine($"NormalCdfLn({x},{y},{r}) = {actual}, error = {error}");
             Assert.True(error < 1e-9);
         }
     }
 }
        public void InferArgumentsFromConcatenationTest2()
        {
            var str1 = Variable.StringUpper(minLength: 0);
            var str2 = Variable.StringLower(minLength: 0);
            var str3 = Variable.StringUpper(minLength: 0);
            var s    = str1 + str2 + str3;

            s.ObservedValue = "ABC";
            var engine = new InferenceEngine();

            var posteriorOverStr1 = engine.Infer <StringDistribution>(str1);

            StringInferenceTestUtilities.TestIfIncludes(posteriorOverStr1, string.Empty, "A", "AB", "ABC");
            StringInferenceTestUtilities.TestIfExcludes(posteriorOverStr1, "B", "BC", "C");

            var posteriorOverStr2 = engine.Infer <StringDistribution>(str2);

            Assert.True(posteriorOverStr2.IsPointMass);
            Assert.Equal(string.Empty, posteriorOverStr2.Point);

            var posteriorOverStr3 = engine.Infer <StringDistribution>(str3);

            StringInferenceTestUtilities.TestIfIncludes(posteriorOverStr3, "ABC", "BC", "C", string.Empty);
            StringInferenceTestUtilities.TestIfExcludes(posteriorOverStr3, "A", "AB", "B");
        }
        public void ImpossibleBranchTest3()
        {
            Variable <string> str1 = Variable.StringLower().Named("str1");
            Variable <string> str2 = Variable.StringLower().Named("str2");
            Variable <string> text = Variable.New <string>().Named("text");

            Variable <int> selector = Variable.DiscreteUniform(2).Named("selector");

            using (Variable.Case(selector, 0))
            {
                text.SetTo(str1 + Variable.Constant(" ") + str2);
            }

            using (Variable.Case(selector, 1))
            {
                text.SetTo(str1);
            }

            text.ObservedValue = "abc";

            var engine            = new InferenceEngine();
            var selectorPosterior = engine.Infer <Discrete>(selector);
            var str1Posterior     = engine.Infer <StringDistribution>(str1);
            var str2Posterior     = engine.Infer <StringDistribution>(str2);

            Assert.True(selectorPosterior.IsPointMass && selectorPosterior.Point == 1);
            Assert.True(str1Posterior.IsPointMass && str1Posterior.Point == "abc");
            Assert.Equal(StringDistribution.Lower(), str2Posterior);
        }
Exemple #4
0
        public void NormalCdfIntegralTest()
        {
            Assert.True(0 <= NormalCdfIntegral(190187183095334850882507750944849586799124505055478568794871547478488387682304.0, -190187183095334850882507750944849586799124505055478568794871547478488387682304.0, -1, 0.817880416082724044547388352452631856079457366800004151664125953519049673808376291470533145141236089924006896061006277409614237094627499958581030715374379576478204968748786874650796450332240045653919846557755590765736997127532958984375e-78).Mantissa);
            Assert.True(0 <= NormalCdfIntegral(213393529.2046706974506378173828125, -213393529.2046706974506378173828125, -1, 0.72893668811495072384656764856902984306419313043079455383121967315673828125e-9).Mantissa);
            Assert.True(0 < NormalCdfIntegral(-0.421468532207607216033551367218024097383022308349609375, 0.42146843802130329326161017888807691633701324462890625, -0.99999999999999989, 0.62292398855983019004972723654291189010479001808562316000461578369140625e-8).Mantissa);

            // Checking all cases takes a long time.
            const int limit = 2_000_000;
            int       count = 0;

            Parallel.ForEach(OperatorTests.Doubles(), x =>
            {
                if (count > limit)
                {
                    return;
                }
                foreach (var y in OperatorTests.Doubles())
                {
                    if (count > limit)
                    {
                        break;
                    }
                    foreach (var r in OperatorTests.Doubles().Where(d => d >= -1 && d <= 1))
                    {
                        if (count > limit)
                        {
                            break;
                        }
                        MMath.NormalCdfIntegral(x, y, r);
                        Interlocked.Add(ref count, 1);
                    }
                }
            });
        }
Exemple #5
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");
        }
        public void NormalCdf2Test()
        {
            double x = 1.2, y = 1.3;
            double xy1 = System.Math.Max(0.0, MMath.NormalCdf(x) + MMath.NormalCdf(y) - 1);

            Assert.True(0 < MMath.NormalCdf(6.8419544775976187E-08, -5.2647906596206016E-08, -1, 3.1873689658872377E-10).Mantissa);

            // In sage:
            // integral(1/(2*pi*sqrt(1-t*t))*exp(-(x*x+y*y-2*t*x*y)/(2*(1-t*t))),t,-1,r).n(digits=200);
            double[,] normalcdf2_pairs = ReadPairs(Path.Combine(TestUtils.DataFolderPath, "SpecialFunctionsValues", "NormalCdf2.csv"));
            CheckFunctionValues("NormalCdf2", new MathFcn3(MMath.NormalCdf), normalcdf2_pairs);

            // using wolfram alpha:  (for cases where r=-1 returns zero)
            // log(integrate(1/(2*pi*sqrt(1-t*t))*exp(-(2*0.1*0.1 - 2*t*0.1*0.1)/(2*(1-t*t))),t,-1,-0.9999))
            // log(integrate(1/(2*pi*sqrt(1-t*t))*exp(-(2*1.5*1.5 + 2*t*1.5*1.5)/(2*(1-t*t))),t,-1,-0.5))
            double[,] normalcdfln2_pairs = ReadPairs(Path.Combine(TestUtils.DataFolderPath, "SpecialFunctionsValues", "NormalCdfLn2.csv"));
            CheckFunctionValues("NormalCdfLn2", new MathFcn3(MMath.NormalCdfLn), normalcdfln2_pairs);

            double[,] normalcdfRatioLn2_pairs = ReadPairs(Path.Combine(TestUtils.DataFolderPath, "SpecialFunctionsValues", "NormalCdfRatioLn2.csv"));
            CheckFunctionValues("NormalCdfRatioLn2", new MathFcn4(NormalCdfRatioLn), normalcdfRatioLn2_pairs);

            // The true values are computed using
            // x * MMath.NormalCdf(x, y, r) + System.Math.Exp(Gaussian.GetLogProb(x, 0, 1) + MMath.NormalCdfLn(ymrx)) + r * System.Math.Exp(Gaussian.GetLogProb(y, 0, 1) + MMath.NormalCdfLn(xmry))
            // where
            // ymrx = (y - r * x) / sqrt(1-r*r)
            // xmry = (x - r * y) / sqrt(1-r*r)
            double[,] normalcdfIntegral_pairs = ReadPairs(Path.Combine(TestUtils.DataFolderPath, "SpecialFunctionsValues", "NormalCdfIntegral.csv"));
            CheckFunctionValues("NormalCdfIntegral", new MathFcn3(MMath.NormalCdfIntegral), normalcdfIntegral_pairs);

            double[,] normalcdfIntegralRatio_pairs = ReadPairs(Path.Combine(TestUtils.DataFolderPath, "SpecialFunctionsValues", "NormalCdfIntegralRatio.csv"));
            CheckFunctionValues("NormalCdfIntegralRatio", new MathFcn3(MMath.NormalCdfIntegralRatio), normalcdfIntegralRatio_pairs);
        }
Exemple #7
0
 public void AssertEqualTo(MyClass that)
 {
     Assert.Equal(0, this.bernoulli.MaxDiff(that.bernoulli));
     Assert.Equal(0, this.beta.MaxDiff(that.beta));
     Assert.Equal(0, this.binomial.MaxDiff(that.binomial));
     Assert.Equal(0, this.conjugateDirichlet.MaxDiff(that.conjugateDirichlet));
     Assert.Equal(0, this.dirichlet.MaxDiff(that.dirichlet));
     Assert.Equal(0, this.discrete.MaxDiff(that.discrete));
     Assert.Equal(0, this.gamma.MaxDiff(that.gamma));
     Assert.Equal(0, this.gammaPower.MaxDiff(that.gammaPower));
     Assert.Equal(0, this.gaussian.MaxDiff(that.gaussian));
     Assert.Equal(0, this.nonconjugateGaussian.MaxDiff(that.nonconjugateGaussian));
     Assert.Equal(0, this.pointMass.MaxDiff(that.pointMass));
     Assert.Equal(0, this.sparseBernoulliList.MaxDiff(that.sparseBernoulliList));
     Assert.Equal(0, this.sparseBetaList.MaxDiff(that.sparseBetaList));
     Assert.Equal(0, this.sparseGammaList.MaxDiff(that.sparseGammaList));
     Assert.Equal(0, this.truncatedGamma.MaxDiff(that.truncatedGamma));
     Assert.Equal(0, this.truncatedGaussian.MaxDiff(that.truncatedGaussian));
     Assert.Equal(0, this.wrappedGaussian.MaxDiff(that.wrappedGaussian));
     Assert.Equal(0, this.sparseGaussianList.MaxDiff(that.sparseGaussianList));
     Assert.Equal(0, this.unnormalizedDiscrete.MaxDiff(that.unnormalizedDiscrete));
     Assert.Equal(0, this.vectorGaussian.MaxDiff(that.vectorGaussian));
     Assert.Equal(0, this.wishart.MaxDiff(that.wishart));
     Assert.Equal(0, this.pareto.MaxDiff(that.pareto));
     Assert.Equal(0, this.poisson.MaxDiff(that.poisson));
     Assert.Equal(0, ga.MaxDiff(that.ga));
     Assert.Equal(0, vga.MaxDiff(that.vga));
     Assert.Equal(0, ga2D.MaxDiff(that.ga2D));
     Assert.Equal(0, vga2D.MaxDiff(that.vga2D));
     Assert.Equal(0, gaJ.MaxDiff(that.gaJ));
     Assert.Equal(0, vgaJ.MaxDiff(that.vgaJ));
     Assert.Equal(0, this.sparseGp.MaxDiff(that.sparseGp));
     Assert.True(this.quantileEstimator.ValueEquals(that.quantileEstimator));
 }
Exemple #8
0
        public void Repeat1()
        {
            var dist = StringDistribution.Repeat(StringDistribution.OneOf("ab", "cd"), minTimes: 1, maxTimes: 3);

            Assert.True(dist.IsProper());
            StringInferenceTestUtilities.TestProbability(dist, StringInferenceTestUtilities.StringUniformProbability(1, 3, 2), "ab", "cd", "abab", "abcd", "cdabcd", "cdcdcd");
        }
Exemple #9
0
        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");
        }
Exemple #10
0
        public void DataContractSerializerMatrixTest()
        {
            for (int matrixType = 0; matrixType < 2; matrixType++)
            {
                MemoryStream stream = new MemoryStream();
                //FileStream stream = new FileStream("temp.txt", FileMode.Create);
                Matrix m;
                if (matrixType == 0)
                {
                    m = new Matrix(new double[, ] {
                        { 2, 3, 4 }, { 5, 6, 7 }
                    });
                }
                else
                {
                    m = new PositiveDefiniteMatrix(new double[, ] {
                        { 2, 1 }, { 1, 2 }
                    });
                }

                var m2 = CloneDataContract(m);
                Assert.True(m.MaxDiff(m2) < 1e-10);
                stream.Close();
            }
        }
        public void ZeroDetectionWithEpsilonLoop1()
        {
            StringAutomaton f = StringAutomaton.Zero();

            AddEpsilonLoop(f.Start, 5, 0);
            Assert.False(f.IsCanonicZero());
            Assert.True(f.IsZero());
        }
Exemple #12
0
        public void CaseInvariant()
        {
            var caseInvariantDist = StringDistribution.CaseInvariant("0aBC");

            Assert.True(caseInvariantDist.IsProper());
            StringInferenceTestUtilities.TestProbability(caseInvariantDist, 1.0 / 8.0, "0aBC", "0abc", "0Abc");
            StringInferenceTestUtilities.TestProbability(caseInvariantDist, 0.0, "0aB", string.Empty);
        }
Exemple #13
0
        public void PointMassToUniform()
        {
            var dist = StringDistribution.String("1337");

            Assert.False(dist.IsUniform());
            dist.SetToUniform();
            Assert.True(dist.IsUniform());
        }
        public void ZeroDetectionWithDeadSelfLoop()
        {
            StringAutomaton f = StringAutomaton.Zero();

            f.Start.AddSelfTransition('x', Weight.Zero);
            f.Start.AddTransition('y', Weight.Zero).EndWeight = Weight.One;
            Assert.True(f.IsZero());
        }
Exemple #15
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);
        }
        public void ZeroDetectionWithEpsilonLoop2()
        {
            StringAutomaton f = StringAutomaton.Zero();

            AddEpsilonLoop(f.Start, 5, 2.0);
            f.Start.AddTransition('a', Weight.One);
            Assert.False(f.IsCanonicZero());
            Assert.True(f.IsZero());
        }
Exemple #17
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);
        }
        private static void CheckFunctionValues(string name, Delegate fcn, double[,] pairs, double assertTolerance)
        {
            Vector x = Vector.Zero(pairs.GetLength(1) - 1);

            object[] args = new object[x.Count];
            for (int i = 0; i < pairs.GetLength(0); i++)
            {
                for (int k = 0; k < x.Count; k++)
                {
                    x[k]    = pairs[i, k];
                    args[k] = x[k];
                }
                bool showTiming = false;
                if (showTiming)
                {
                    Stopwatch watch           = Stopwatch.StartNew();
                    int       repetitionCount = 100000;
                    for (int repetition = 0; repetition < repetitionCount; repetition++)
                    {
                        Util.DynamicInvoke(fcn, args);
                    }
                    watch.Stop();
                    Trace.WriteLine($"  ({watch.ElapsedTicks} ticks for {repetitionCount} calls)");
                }
                double fx     = pairs[i, x.Count];
                double result = (double)Util.DynamicInvoke(fcn, args);
                if (!double.IsNaN(result) && System.Math.Sign(result) != System.Math.Sign(fx) && fx != 0 && result != 0)
                {
                    string strMsg = $"{name}({x:g17})\t has wrong sign (result = {result:g17})";
                    Trace.WriteLine(strMsg);
                    Assert.True(false, strMsg);
                }
                double err = MMath.AbsDiff(result, fx, 1e14 * double.Epsilon);
                if (Double.IsInfinity(fx))
                {
                    err = (result == fx) ? 0 : Double.PositiveInfinity;
                }
                if (Double.IsNaN(fx))
                {
                    err = Double.IsNaN(result) ? 0 : Double.NaN;
                }
                if (!IsErrorSignificant(TOLERANCE, err))
                {
                    Trace.WriteLine($"{name}({x:g17})\t ok");
                }
                else
                {
                    string strMsg = $"{name}({x:g17})\t wrong by {err.ToString("g2")} (result = {result:g17})";
                    Trace.WriteLine(strMsg);
                    if (IsErrorSignificant(assertTolerance, err) || double.IsNaN(err))
                    {
                        Assert.True(false, strMsg);
                    }
                }
            }
        }
Exemple #19
0
 /// <summary>
 /// Tests if a transducer projects each of given strings to a zero function.
 /// </summary>
 /// <param name="transducer">The transducer.</param>
 /// <param name="strings">The strings to test.</param>
 public static void TestIfTransducerRejects(StringTransducer transducer, params string[] strings)
 {
     foreach (string str in strings)
     {
         var res = transducer.ProjectSource(StringAutomaton.ConstantOn(1.0, str));
         Assert.True(res.IsZero());
         var res2 = transducer.ProjectSource(str);
         Assert.True(res2.IsZero());
     }
 }
        public void NormalCdfTest()
        {
            /* In python mpmath:
             * from mpmath import *
             * mp.dps = 500
             * mp.pretty = True
             * ncdf(-12.2)
             */
            // In wolfram alpha: (not always accurate)
            // http://www.wolframalpha.com/input/?i=erfc%2813%2Fsqrt%282%29%29%2F2
            // In xcas: (not always accurate)
            // Digits := 30
            // phi(x) := evalf(erfc(-x/sqrt(2))/2);
            double[,] normcdf_pairs = ReadPairs(Path.Combine(TestUtils.DataFolderPath, "SpecialFunctionsValues", "NormalCdf.csv"));
            CheckFunctionValues("NormalCdf", new MathFcn(MMath.NormalCdf), normcdf_pairs);

            Assert.Equal(0.5, MMath.NormalCdf(0));
            Assert.True(MMath.NormalCdf(7.9) <= 1);
            Assert.True(MMath.NormalCdf(-40) >= 0);
            Assert.True(MMath.NormalCdf(-80) >= 0);

            double[,] erfc_pairs = new double[normcdf_pairs.GetLength(0), normcdf_pairs.GetLength(1)];
            for (int i = 0; i < normcdf_pairs.GetLength(0); i++)
            {
                double input  = normcdf_pairs[i, 0];
                double output = normcdf_pairs[i, 1];
                erfc_pairs[i, 0] = -input / MMath.Sqrt2;
                erfc_pairs[i, 1] = 2 * output;
            }
            CheckFunctionValues("Erfc", new MathFcn(MMath.Erfc), erfc_pairs);

            double[,] normcdfinv_pairs = new double[normcdf_pairs.GetLength(0), 2];
            for (int i = 0; i < normcdfinv_pairs.GetLength(0); i++)
            {
                double input  = normcdf_pairs[i, 0];
                double output = normcdf_pairs[i, 1];
                if (!(Double.IsPositiveInfinity(input) || Double.IsNegativeInfinity(input)) && (input <= -10 || input >= 6))
                {
                    normcdfinv_pairs[i, 0] = 0.5;
                    normcdfinv_pairs[i, 1] = 0;
                }
                else
                {
                    normcdfinv_pairs[i, 0] = output;
                    normcdfinv_pairs[i, 1] = input;
                }
            }
            CheckFunctionValues("NormalCdfInv", new MathFcn(MMath.NormalCdfInv), normcdfinv_pairs);

            double[,] normcdfln_pairs = ReadPairs(Path.Combine(TestUtils.DataFolderPath, "SpecialFunctionsValues", "NormalCdfLn.csv"));
            CheckFunctionValues("NormalCdfLn", new MathFcn(MMath.NormalCdfLn), normcdfln_pairs);

            double[,] normcdflogit_pairs = ReadPairs(Path.Combine(TestUtils.DataFolderPath, "SpecialFunctionsValues", "NormalCdfLogit.csv"));
            CheckFunctionValues("NormalCdfLogit", new MathFcn(MMath.NormalCdfLogit), normcdflogit_pairs);
        }
Exemple #21
0
 public void DigammaInvTest()
 {
     for (int i = 0; i < 1000; i++)
     {
         double y     = -3 + i * 0.01;
         double x     = MMath.DigammaInv(y);
         double y2    = MMath.Digamma(x);
         double error = MMath.AbsDiff(y, y2, 1e-8);
         Assert.True(error < 1e-8);
     }
 }
Exemple #22
0
 public void LogisticGaussianTest2()
 {
     for (int i = 4; i < 100; i++)
     {
         double v   = System.Math.Pow(10, i);
         double f   = MMath.LogisticGaussian(1, v);
         double err = System.Math.Abs(f - 0.5);
         //Console.WriteLine("{0}: {1} {2}", i, f, err);
         Assert.True(err < 2e-2 / i);
     }
 }
Exemple #23
0
 /// <summary>
 /// Tests whether the support of a given distribution includes or excludes given values.
 /// </summary>
 /// <param name="distribution">The distribution.</param>
 /// <param name="values">The values to test.</param>
 /// <param name="testInclusion">Specifies whether the inclusion test must be performed instead of the exclusion test.</param>
 private static void TestInclusionExclusion <T>(IDistribution <T> distribution, T[] values, bool testInclusion)
 {
     Console.WriteLine("Testing distribution: " + distribution.ToString());
     foreach (var val in values)
     {
         var excluded = double.IsNegativeInfinity(distribution.GetLogProb(val));
         var msg      = val + (excluded ? " was not in " : " was in ");
         Assert.True(excluded != testInclusion, msg + " " + distribution.ToString());
         Console.WriteLine(msg + "distribution");
     }
 }
        public void AsAutomatonCompression()
        {
            var normalizedAutomaton = StringDictionaryWeightFunction.FromValues(NormalizedComplexDistributionTable).AsAutomaton();

            Assert.True(normalizedAutomaton.IsDeterministic());
            Assert.True(normalizedAutomaton.States.Count <= 23);
            var nonNormalizedAutomaton = StringDictionaryWeightFunction.FromValues(NonNormalizedComplexDistributionTable).AsAutomaton();

            Assert.True(nonNormalizedAutomaton.IsDeterministic());
            Assert.True(nonNormalizedAutomaton.States.Count <= 31);
        }
Exemple #25
0
        public void WeightedAverageTest()
        {
            Assert.Equal(Environment.Is64BitProcess ? 3.86361619394904E-311 : 3.86361619394162E-311, MMath.WeightedAverage(0.82912896852490248, 2.5484859206000203E-311, 3.50752234977395E-313, 31.087830618727477));
            Assert.Equal(MMath.WeightedAverage(0.1, double.MinValue, 0.01, double.MinValue), double.MinValue);
            Assert.Equal(MMath.WeightedAverage(0.1, -double.Epsilon, double.MaxValue, -double.Epsilon), -double.Epsilon);
            Assert.Equal(MMath.WeightedAverage(1e-100, 2e-250, 1e-100, 4e-250), MMath.Average(2e-250, 4e-250));
            Assert.Equal(MMath.WeightedAverage(1e100, 2e250, 1e100, 4e250), MMath.Average(2e250, 4e250));
            Assert.Equal(MMath.WeightedAverage(0, 0, 0.1, -double.Epsilon), -double.Epsilon);
            Assert.Equal(MMath.WeightedAverage(0.1, -double.Epsilon, 0, double.NegativeInfinity), -double.Epsilon);
            Assert.False(double.IsNaN(MMath.WeightedAverage(1.7976931348623157E+308, double.NegativeInfinity, 4.94065645841247E-324, double.NegativeInfinity)));
            Assert.False(double.IsNaN(MMath.WeightedAverage(0.01, double.NegativeInfinity, double.MaxValue, double.MaxValue)));
            Assert.False(double.IsNaN(MMath.WeightedAverage(0.01, double.NegativeInfinity, double.Epsilon, double.NegativeInfinity)));
            Assert.Equal(double.MaxValue, MMath.WeightedAverage(double.MaxValue, double.MaxValue, double.MaxValue, double.MaxValue));
            const int limit = 2_000_000;
            int       count = 0;

            Parallel.ForEach(OperatorTests.DoublesAtLeastZero(), wa =>
            {
                Parallel.ForEach(OperatorTests.DoublesAtLeastZero(), wb =>
                {
                    if (count > limit)
                    {
                        return;
                    }
                    Trace.WriteLine($"wa = {wa}, wb = {wb}");
                    foreach (var a in OperatorTests.Doubles())
                    {
                        if (count > limit)
                        {
                            break;
                        }
                        foreach (var b in OperatorTests.Doubles())
                        {
                            if (count > limit)
                            {
                                break;
                            }
                            if (double.IsNaN(a + b))
                            {
                                continue;
                            }
                            double midpoint = MMath.WeightedAverage(wa, a, wb, b);
                            Assert.True(midpoint >= System.Math.Min(a, b), $"Failed assertion: MMath.WeightedAverage({wa:r}, {a:r}, {wb:r}, {b:r}) {midpoint} >= {System.Math.Min(a, b)}");
                            Assert.True(midpoint <= System.Math.Max(a, b), $"Failed assertion: MMath.WeightedAverage({wa:r}, {a:r}, {wb:r}, {b:r}) {midpoint} <= {System.Math.Max(a, b)}");
                            if (wa == wb)
                            {
                                Assert.Equal(MMath.Average(a, b), midpoint);
                            }
                            Interlocked.Add(ref count, 1);
                        }
                    }
                });
            });
        }
        public void NormalizeWithInfiniteEpsilon1()
        {
            StringAutomaton automaton = StringAutomaton.Zero();

            automaton.Start.AddTransition('a', Weight.One).AddSelfTransition(null, Weight.FromValue(3)).EndWeight = Weight.One;

            // The automaton takes an infinite value on "a", and yet the normalization must work
            Assert.True(automaton.TryNormalizeValues());
            StringInferenceTestUtilities.TestValue(automaton, 1, "a");
            StringInferenceTestUtilities.TestValue(automaton, 0, "b");
        }
Exemple #27
0
        public void WeightedAverageTest()
        {
            Assert.Equal(MMath.WeightedAverage(0.1, double.MinValue, 0.01, double.MinValue), double.MinValue);
            Assert.Equal(MMath.WeightedAverage(0.1, -double.Epsilon, double.MaxValue, -double.Epsilon), -double.Epsilon);
            Assert.Equal(MMath.WeightedAverage(1e-100, 2e-250, 1e-100, 4e-250), MMath.Average(2e-250, 4e-250));
            Assert.Equal(MMath.WeightedAverage(1e100, 2e250, 1e100, 4e250), MMath.Average(2e250, 4e250));
            Assert.Equal(MMath.WeightedAverage(0, 0, 0.1, -double.Epsilon), -double.Epsilon);
            Assert.Equal(MMath.WeightedAverage(0.1, -double.Epsilon, 0, double.NegativeInfinity), -double.Epsilon);
            Assert.False(double.IsNaN(MMath.WeightedAverage(1.7976931348623157E+308, double.NegativeInfinity, 4.94065645841247E-324, double.NegativeInfinity)));
            Assert.False(double.IsNaN(MMath.WeightedAverage(0.01, double.NegativeInfinity, double.MaxValue, double.MaxValue)));
            Assert.False(double.IsNaN(MMath.WeightedAverage(0.01, double.NegativeInfinity, double.Epsilon, double.NegativeInfinity)));
            Assert.Equal(double.MaxValue, MMath.WeightedAverage(double.MaxValue, double.MaxValue, double.MaxValue, double.MaxValue));
            const int limit = 2_000_000;
            int       count = 0;

            Parallel.ForEach(OperatorTests.DoublesAtLeastZero(), wa =>
            {
                Parallel.ForEach(OperatorTests.DoublesAtLeastZero(), wb =>
                {
                    if (count > limit)
                    {
                        return;
                    }
                    Trace.WriteLine($"wa = {wa}, wb = {wb}");
                    foreach (var a in OperatorTests.Doubles())
                    {
                        if (count > limit)
                        {
                            break;
                        }
                        foreach (var b in OperatorTests.Doubles())
                        {
                            if (count > limit)
                            {
                                break;
                            }
                            if (double.IsNaN(a + b))
                            {
                                continue;
                            }
                            double midpoint = MMath.WeightedAverage(wa, a, wb, b);
                            Assert.True(midpoint >= System.Math.Min(a, b), $"Failed assertion: {midpoint} >= {System.Math.Min(a, b)}, wa={wa:r}, a={a:r}, wb={wb:r}, b={b:r}");
                            Assert.True(midpoint <= System.Math.Max(a, b), $"Failed assertion: {midpoint} <= {System.Math.Max(a, b)}, wa={wa:r}, a={a:r}, wb={wb:r}, b={b:r}");
                            if (wa == wb)
                            {
                                Assert.Equal(MMath.Average(a, b), midpoint);
                            }
                            Interlocked.Add(ref count, 1);
                        }
                    }
                });
            });
        }
        public void NormalizeWithInfiniteEpsilon2()
        {
            StringAutomaton automaton = StringAutomaton.Zero();

            automaton.Start.AddTransition('a', Weight.One).AddSelfTransition(null, Weight.FromValue(2)).EndWeight = Weight.One;
            automaton.Start.AddTransition('b', Weight.One).AddSelfTransition(null, Weight.FromValue(1)).EndWeight = Weight.One;

            // "a" branch infinitely dominates over the "b" branch
            Assert.True(automaton.TryNormalizeValues());
            StringInferenceTestUtilities.TestValue(automaton, 1, "a");
            Assert.True(automaton.GetValue("b") < 1e-50);
        }
Exemple #29
0
        /// <summary>
        /// Tests whether the product of given distributions is equal to another distribution on a specified strings.
        /// </summary>
        /// <param name="argument1">The first argument of the product.</param>
        /// <param name="argument2">The second argument of the product.</param>
        /// <param name="trueProduct">The true product.</param>
        /// <param name="stringsToCheckOn">The strings to test.</param>
        public static void TestProduct(
            StringDistribution argument1, StringDistribution argument2, StringDistribution trueProduct, params string[] stringsToCheckOn)
        {
            var    product = new StringDistribution();
            double productLogNormalizer = product.SetToProductAndReturnLogNormalizer(argument1, argument2);
            double logAverageOf         = argument1.GetLogAverageOf(argument2);

            Assert.Equal(productLogNormalizer, logAverageOf);
            Assert.Equal(logAverageOf, Clone(argument1).GetLogAverageOf(argument2));

            if (trueProduct.IsZero())
            {
                Assert.True(product.IsZero());
                Assert.True(double.IsNegativeInfinity(productLogNormalizer));
            }
            else if (trueProduct.IsPointMass)
            {
                Assert.True(product.IsPointMass);
                Assert.Equal(product.Point, trueProduct.Point);
            }
            else if (trueProduct.IsUniform())
            {
                Assert.True(product.IsUniform());
            }
            else
            {
                Assert.False(product.IsZero());
                Assert.False(product.IsPointMass);
                Assert.False(product.IsUniform());

                Assert.Equal(trueProduct.IsProper(), product.IsProper());

                foreach (var str in stringsToCheckOn)
                {
                    double logProb1       = argument1.GetLogProb(str);
                    double logProb2       = argument2.GetLogProb(str);
                    double logProbProduct = trueProduct.GetLogProb(str);

                    if (double.IsNegativeInfinity(logProb1) || double.IsNegativeInfinity(logProb2))
                    {
                        Assert.True(double.IsNegativeInfinity(logProbProduct));
                    }
                    else if (double.IsNegativeInfinity(logProbProduct))
                    {
                        Assert.True(double.IsNegativeInfinity(logProb1) || double.IsNegativeInfinity(logProb2));
                    }
                    else
                    {
                        Assert.Equal(logProb1 + logProb2, logProbProduct + productLogNormalizer, LogValueEps);
                    }
                }
            }
        }
Exemple #30
0
        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);
        }