Example #1
0
        public void NaiveBayesConstructorTest4()
        {
            int classes    = 2;
            int inputCount = 3;

            double[] priors = { 0.4, 0.6 };
            UniformDiscreteDistribution initial = new UniformDiscreteDistribution(0, 3);

            var target = new NaiveBayes <UniformDiscreteDistribution>(classes, inputCount, initial, priors);

            Assert.AreEqual(classes, target.ClassCount);
            Assert.AreEqual(inputCount, target.InputCount);
            Assert.AreEqual(priors.Length, target.Priors.Length);
            Assert.AreEqual(0.4, target.Priors[0]);
            Assert.AreEqual(0.6, target.Priors[1]);

            Assert.AreEqual(2, target.Distributions.GetLength(0));
            Assert.AreEqual(3, target.Distributions.GetLength(1));

            for (int i = 0; i < classes; i++)
            {
                for (int j = 0; j < inputCount; j++)
                {
                    Assert.AreNotSame(initial, target.Distributions[i, j]);
                    Assert.AreEqual(0, target.Distributions[i, j].Minimum);
                    Assert.AreEqual(3, target.Distributions[i, j].Maximum);
                }
            }
        }
Example #2
0
        protected override void EndProcessing()
        {
            var dist = new UniformDiscreteDistribution(Minimum, Maximum);
            var obj  = DistributionHelper.AddConvinienceMethods(dist);

            WriteObject(obj);
        }
Example #3
0
        public void insertion_sort_success()
        {
            for (int n = 1; n < 100; n += 10)
            {
                for (int i = 0; i < 100; i++)
                {
                    int[]    random   = UniformDiscreteDistribution.Random(n);
                    double[] expected = random.ToDouble();
                    double[] a        = random.ToDouble();
                    double[] b        = random.ToDouble();
                    double[] c        = random.ToDouble();
                    double[] d        = random.ToDouble();
                    double[] e        = random.ToDouble();

                    Array.Sort(expected);
                    Sort.Insertion(a);
                    Sort.Insertion(b, asc: false);
                    b = b.Reversed();
                    Sort.Insertion(c, asc: true);

                    Func <double, double, int> greater = (x, y) => - x.CompareTo(y);
                    Sort.Insertion(d, comparer: greater, asc: false);
                    Sort.Insertion(e, comparer: greater, asc: true);
                    e = e.Reversed();

                    Assert.IsTrue(expected.IsEqual(a));
                    Assert.IsTrue(expected.IsEqual(b));
                    Assert.IsTrue(expected.IsEqual(c));
                    Assert.IsTrue(expected.IsEqual(d));
                    Assert.IsTrue(expected.IsEqual(e));
                }
            }
        }
Example #4
0
        public void bottom_1()
        {
            Accord.Math.Random.Generator.Seed = 0;

            for (int size = 1; size < 1000; size *= 10)
            {
                for (int n = 1; n < size; n += 10)
                {
                    for (int i = 0; i < 100; i++)
                    {
                        int[] random = UniformDiscreteDistribution.Random(size);
                        int[] sorted = random.Sorted();

                        int[] expectedBottom = sorted.First(n);
                        int[] bottomIdx      = random.Bottom(n);
                        int[] actualBottom   = random.Submatrix(bottomIdx);
                        Assert.IsTrue(actualBottom.IsEqual(expectedBottom));

                        int[] expectedTop = sorted.Last(n).Reversed();
                        int[] topIdx      = random.Top(n);
                        int[] actualTop   = random.Submatrix(topIdx);
                        Assert.IsTrue(actualTop.IsEqual(expectedTop));
                    }
                }
            }
        }
        public void ConstructorTest()
        {
            // Create an uniform (discrete) distribution in [2, 6]
            var dist = new UniformDiscreteDistribution(a: 2, b: 6);

            // Common measures
            double mean   = dist.Mean;     // 4.0
            double median = dist.Median;   // 4.0
            double var    = dist.Variance; // 1.3333333333333333

            // Cumulative distribution functions
            double cdf  = dist.DistributionFunction(k: 2);              // 0.2
            double ccdf = dist.ComplementaryDistributionFunction(k: 2); // 0.8

            // Probability mass functions
            double pmf1 = dist.ProbabilityMassFunction(k: 4);    // 0.2
            double pmf2 = dist.ProbabilityMassFunction(k: 5);    // 0.2
            double pmf3 = dist.ProbabilityMassFunction(k: 6);    // 0.2
            double lpmf = dist.LogProbabilityMassFunction(k: 2); // -1.6094379124341003

            // Quantile function
            int icdf1 = dist.InverseDistributionFunction(p: 0.17); // 2
            int icdf2 = dist.InverseDistributionFunction(p: 0.46); // 4
            int icdf3 = dist.InverseDistributionFunction(p: 0.87); // 6

            // Hazard (failure rate) functions
            double hf  = dist.HazardFunction(x: 4);           // 0.5
            double chf = dist.CumulativeHazardFunction(x: 4); // 0.916290731874155

            // String representation
            string str = dist.ToString(CultureInfo.InvariantCulture); // "U(x; a = 2, b = 6)"

            Assert.AreEqual(4.0, mean);
            Assert.AreEqual(4.0, median);
            Assert.AreEqual(1.3333333333333333, var);
            Assert.AreEqual(0.916290731874155, chf, 1e-10);
            Assert.AreEqual(0.2, cdf);
            Assert.AreEqual(0.2, pmf1);
            Assert.AreEqual(0.2, pmf2);
            Assert.AreEqual(0.2, pmf3);
            Assert.AreEqual(-1.6094379124341003, lpmf);
            Assert.AreEqual(0.5, hf);
            Assert.AreEqual(0.8, ccdf);
            Assert.AreEqual(2, icdf1);
            Assert.AreEqual(4, icdf2);
            Assert.AreEqual(6, icdf3);
            Assert.AreEqual("U(x; a = 2, b = 6)", str);

            var range1 = dist.GetRange(0.95);
            var range2 = dist.GetRange(0.99);
            var range3 = dist.GetRange(0.01);

            Assert.AreEqual(2, range1.Min);
            Assert.AreEqual(6, range1.Max);
            Assert.AreEqual(2.0, range2.Min);
            Assert.AreEqual(6, range2.Max);
            Assert.AreEqual(2.0, range3.Min);
            Assert.AreEqual(6, range3.Max);
        }
        private static void generator9(GeneratorSeed seed)
        {
            //ZLOZITA OPRAVA
            //diskretne rovnomerne Tmin = 120, Tmax = 260
            var gen9 = new UniformDiscreteDistribution(seed.GetRandomSeed(), 120, 260);

            TestDistributionsToFileInt(gen9, "generator_9_diskretne_uniform_min_120_max_260.dst");
        }
        public void GetRangeTest()
        {
            var u = new UniformDiscreteDistribution(-8, 7);

            var range = u.GetRange(0.99);

            Assert.AreEqual(-8, range.Min);
            Assert.AreEqual(7, range.Max);
        }
        private static void generator7(GeneratorSeed seed)
        {
            ///////////////////////////////////////////////////////////////////////////////////////////////
            //pravdepodobnosti jednotlivych oprav a ich trvanie
            // JEDNODUCHA OPRAVA
            //diskretne rovnomerne = Tmin = 2, Tmax = 20
            var gen7 = new UniformDiscreteDistribution(seed.GetRandomSeed(), 2, 20);

            TestDistributionsToFileInt(gen7, "generator_7_diskretne_uniform_min_2_max_20.dst");
        }
        private static void generatorPriemerDiskretny(GeneratorSeed seed)
        {
            UniformDiscreteDistribution gen = new UniformDiscreteDistribution(seed.GetRandomSeed(), 0, 10);
            int sucet = 0;
            int count = 100000000;

            for (int i = 0; i < count; i++)
            {
                sucet += gen.GenerateInt();
            }
            Console.WriteLine((double)sucet / (count));
        }
Example #10
0
        public void insertion_sort_with_items_success()
        {
            for (int n = 1; n < 100; n += 10)
            {
                for (int i = 0; i < 100; i++)
                {
                    int[]    random   = UniformDiscreteDistribution.Random(n);
                    double[] expected = random.ToDouble();
                    double[] a        = random.ToDouble();
                    long[]   ai       = random.ToInt64();
                    double[] b        = random.ToDouble();
                    long[]   bi       = random.ToInt64();
                    double[] c        = random.ToDouble();
                    long[]   ci       = random.ToInt64();
                    double[] d        = random.ToDouble();
                    long[]   di       = random.ToInt64();
                    double[] e        = random.ToDouble();
                    long[]   ei       = random.ToInt64();

                    Array.Sort(expected);
                    Sort.Insertion(a, ai);
                    Sort.Insertion(b, bi, asc: false);
                    b  = b.Reversed();
                    bi = bi.Reversed();
                    Sort.Insertion(c, ci, asc: true);

                    Func <double, double, int> greater = (x, y) => - x.CompareTo(y);
                    Sort.Insertion(d, di, comparer: greater, asc: false);
                    Sort.Insertion(e, ei, comparer: greater, asc: true);
                    e  = e.Reversed();
                    ei = ei.Reversed();

                    Assert.IsTrue(a.IsSorted());
                    Assert.IsTrue(a.IsSorted(ComparerDirection.Ascending));
                    Assert.IsTrue(expected.IsEqual(a));
                    Assert.IsTrue(expected.IsEqual(b));
                    Assert.IsTrue(expected.IsEqual(c));
                    Assert.IsTrue(expected.IsEqual(d));
                    Assert.IsTrue(expected.IsEqual(e));

                    Assert.IsTrue(expected.IsEqual(ai));
                    Assert.IsTrue(expected.IsEqual(bi));
                    Assert.IsTrue(expected.IsEqual(ci));
                    Assert.IsTrue(expected.IsEqual(di));
                    Assert.IsTrue(expected.IsEqual(ei));
                }
            }
        }
        public void IntervalTest()
        {
            var target = new UniformDiscreteDistribution(-10, 10);

            for (int k = -15; k < 15; k++)
            {
                double expected = target.ProbabilityMassFunction(k);

                double a = target.DistributionFunction(k);
                double b = target.DistributionFunction(k - 1);
                double c = a - b;

                Assert.AreEqual(expected, c, 1e-15);
                Assert.AreEqual(c, target.DistributionFunction(k - 1, k), 1e-15);
            }
        }
Example #12
0
        public void nth_element_1()
        {
            for (int size = 1; size < 1000; size += 10)
            {
                for (int nth = 1; nth < size; nth += 10)
                {
                    int[]    random = UniformDiscreteDistribution.Random(size);
                    double[] a      = random.ToDouble();
                    double[] b      = random.ToDouble();
                    int[]    bk     = (int[])random.Clone();
                    double[] c      = random.ToDouble();
                    double[] d      = random.ToDouble();
                    int[]    dk     = (int[])random.Clone();
                    double[] e      = random.ToDouble();
                    double[] f      = random.ToDouble();
                    int[]    fk     = (int[])random.Clone();

                    Sort.NthElement(a, 0, a.Length - 1, nth);
                    Sort.NthElement(b, bk, 0, b.Length - 1, nth);
                    Assert.IsTrue(a.IsEqual(b));
                    Assert.IsTrue(a.IsEqual(bk));

                    Func <double, double, int> comparer = (x, y) => x.CompareTo(y);
                    Sort.NthElement(c, 0, a.Length - 1, nth, comparer);
                    Sort.NthElement(d, dk, 0, b.Length - 1, nth, comparer);
                    var str = random.ToString(CSharpArrayFormatProvider.InvariantCulture);
                    Assert.IsTrue(a.IsEqual(c));
                    Assert.IsTrue(c.IsEqual(d));
                    Assert.IsTrue(d.IsEqual(dk));

                    Func <double, double, int> comparer2 = (x, y) => - x.CompareTo(y);
                    Sort.NthElement(e, 0, a.Length - 1, nth, comparer2, asc: false);
                    Sort.NthElement(f, fk, 0, b.Length - 1, nth, comparer2, asc: false);
                    Assert.IsTrue(a.IsEqual(e));
                    Assert.IsTrue(e.IsEqual(f));
                    Assert.IsTrue(f.IsEqual(fk));

                    Assert.IsTrue(a.IsEqual(c));
                    Assert.IsTrue(a.IsEqual(d));
                    Assert.IsTrue(a.IsEqual(e));
                    Assert.IsTrue(a.IsEqual(f));
                    Assert.IsTrue(a.IsEqual(fk));
                }
            }
        }
Example #13
0
        public void PosteriorTest1()
        {
            // Example from http://ai.stanford.edu/~serafim/CS262_2007/notes/lecture5.pdf


            double[,] A =
            {
                { 0.95, 0.05 }, // fair dice state
                { 0.05, 0.95 }, // loaded dice state
            };

            double[,] B =
            {
                {  1 / 6.0,  1 / 6.0,  1 / 6.0,  1 / 6.0,  1 / 6.0, 1 / 6.0 }, // fair dice probabilities
                { 1 / 10.0, 1 / 10.0, 1 / 10.0, 1 / 10.0, 1 / 10.0, 1 / 2.0 }, // loaded probabilities
            };

            double[] pi = { 0.5, 0.5 };

            HiddenMarkovModel hmm = new HiddenMarkovModel(A, B, pi);

            int[]  x = new int[] { 1, 2, 1, 5, 6, 2, 1, 5, 2, 4 }.Subtract(1);
            int[]  y  = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            double py = Math.Exp(hmm.Evaluate(x, y));

            Assert.AreEqual(0.00000000521158647211, py, 1e-16);

            x  = new int[] { 1, 2, 1, 5, 6, 2, 1, 5, 2, 4 }.Subtract(1);
            y  = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
            py = Math.Exp(hmm.Evaluate(x, y));

            Assert.AreEqual(0.00000000015756235243, py, 1e-16);


            Accord.Math.Tools.SetupGenerator(0);
            var u = new UniformDiscreteDistribution(0, 6);

            int[] sequence = u.Generate(1000);
            int   start    = 120;
            int   end      = 150;

            for (int i = start; i < end; i += 2)
            {
                sequence[i] = 5;
            }


            // Predict the next observation in sequence
            int[]      path;
            double[][] p = hmm.Posterior(sequence, out path);

            for (int i = 0; i < path.Length; i++)
            {
                Assert.AreEqual(1, p[i][0] + p[i][1], 1e-10);
            }


            int loaded = 0;

            for (int i = 0; i < start; i++)
            {
                if (p[i][1] > 0.95)
                {
                    loaded++;
                }
            }

            Assert.AreEqual(0, loaded);

            loaded = 0;
            for (int i = start; i < end; i++)
            {
                if (p[i][1] > 0.95)
                {
                    loaded++;
                }
            }

            Assert.IsTrue(loaded > 15);

            loaded = 0;
            for (int i = end; i < p.Length; i++)
            {
                if (p[i][1] > 0.95)
                {
                    loaded++;
                }
            }

            Assert.AreEqual(0, loaded);
        }