Example #1
0
        public void RunningStat_Stdev_Test2()
        {
            var stat = new RunningStat();

            stat.Push(0);
            stat.Push(0);
            stat.Push(0);

            //Assert.AreEqual()
        }
Example #2
0
        public static void Evaluate(Action action)
        {
            var measure = new Func <int, RunningStat>(samples =>
            {
                var runningStat = new RunningStat();
                var sw          = new Stopwatch();

                while (samples-- > 0)
                {
                    sw.Restart();
                    action();
                    runningStat.Push((decimal)sw.Elapsed.TotalMilliseconds);
                }

                return(runningStat);
            });

            var initialTime = measure(1);
            var baseTime    = measure(1);

            var warmupSamples = Math.Max(1, (int)Math.Round(WarmupTargetTime.TotalMilliseconds / (double)baseTime.Mean));
            var warmupTime    = measure(warmupSamples);

            var testSamples = Math.Max(30, (int)Math.Round(TestTargetTime.TotalMilliseconds / (double)warmupTime.Mean));
            var testTime    = measure(testSamples);

            PublishResults(initialTime.Mean, baseTime.Mean, warmupSamples, warmupTime.Mean, warmupTime.StandardDeviation, testSamples, testTime.Mean, testTime.StandardDeviation);
        }
Example #3
0
 public void RunningStat_Average_Test1()
 {
     foreach (var n in new[] { 1, 2, 3, 10, 100, 1000 })
     {
         var xs   = Randomness.NextRandom().NextSequence(n).Select(x => (double)x).Take(n).ToList();
         var stat = new RunningStat();
         foreach (var x in xs)
         {
             stat.Push(x);
         }
         Assert.AreEqual(xs.Average(), stat.Average, 1d / 1000000);
     }
 }
Example #4
0
 public void RunningStat_Stdev_Test1()
 {
     foreach (var n in new[] { 10, 100, 1000 })
     {
         var xs   = Randomness.NextRandom().NextSequence(n).Select(x => (double)x).Take(n).ToList();
         var stat = new RunningStat();
         foreach (var x in xs)
         {
             stat.Push(x);
         }
         var stdev = xs.Stdev(xs.Average());
         var error = stdev - stat.Stdev;
         Assert.AreEqual(stdev, stat.Stdev, 0.25, $"e={error}");
     }
 }
Example #5
0
        public void TestMeanAndVariacneConsistency()
        {
            const int   numSamples = 100000;
            double      mean, stdev;
            RunningStat rs        = new RunningStat();
            Random      defaultrs = new Random();
            Poisson     poisson   = new Poisson();

            rs.Clear();
            mean           = 2000; stdev = Math.Sqrt(2000);
            poisson.Lambda = 2000;
            for (int i = 0; i < numSamples; ++i)
            {
                rs.Push(poisson.Sample(defaultrs));
            }
            PrintResult.CompareMeanAndVariance("Poisson Discrete", mean, stdev * stdev, rs.Mean(), rs.Variance());
        }
Example #6
0
        public void TestMeanAndVariacneConsistency()
        {
            const int   numSamples = 100000;
            double      mean, stdev;
            RunningStat rs        = new RunningStat();
            Random      defaultrs = new Random();
            Triangular  tri       = new Triangular();

            rs.Clear();
            var a = tri.LowerBound; var b = tri.UpperBound; var c = tri.Mode;

            mean = (a + b + c) / 3; stdev = Math.Sqrt((a * a + b * b + c * c - a * b - a * c - b * c) / 18);
            for (int i = 0; i < numSamples; ++i)
            {
                rs.Push(tri.Sample(defaultrs));
            }
            PrintResult.CompareMeanAndVariance("Triangular", mean, stdev * stdev, rs.Mean(), rs.Variance());
        }
Example #7
0
        public void TestMeanAndVariacneConsistency()
        {
            const int   numSamples = 100000;
            double      mean, stdev;
            RunningStat rs        = new RunningStat();
            Random      defaultrs = new Random();
            Gamma       gamma     = new Gamma();

            rs.Clear();
            mean = 2; stdev = 5;
            for (int i = 0; i < numSamples; ++i)
            {
                gamma.Mean = mean;
                gamma.StandardDeviation = stdev;
                rs.Push(gamma.Sample(defaultrs));                                                       //yy
            }
            PrintResult.CompareMeanAndVariance("gamma", mean, stdev * stdev, rs.Mean(), rs.Variance()); // TODO: result not consistent need to fix the bug
        }
Example #8
0
        public void TestMeanAndVariacneConsistency_Std()
        {
            const int   numSamples = 100000;
            double      mean, stdev;
            RunningStat rs        = new RunningStat();
            Random      defaultrs = new Random();
            Normal      normal    = new Normal();

            rs.Clear();
            mean = 2; stdev = 5;
            for (int i = 0; i < numSamples; ++i)
            {
                normal.Mean = mean;
                normal.StandardDeviation = stdev;
                rs.Push(normal.Sample(defaultrs));
            }
            PrintResult.CompareMeanAndVariance("normal", mean, stdev * stdev, rs.Mean(), rs.Variance());
        }
Example #9
0
        public void TestMeanAndVariacneConsistency()
        {
            const int   numSamples = 100000;
            double      mean, stdev;
            RunningStat rs          = new RunningStat();
            Random      defaultrs   = new Random();
            Exponential exponential = new Exponential();

            rs.Clear();
            mean = 2; stdev = 2;
            for (int i = 0; i < numSamples; ++i)
            {
                exponential.StandardDeviation = 2;
                //exponential.Mean = mean;
                rs.Push(exponential.Sample(defaultrs));
            }
            PrintResult.CompareMeanAndVariance("exponential", mean, stdev * stdev, rs.Mean(), rs.Variance());
        }
Example #10
0
        public void TestMeanAndVariacneConsistency()
        {
            const int   numSamples = 100000;
            double      mean, stdev;
            RunningStat rs        = new RunningStat();
            Random      defaultrs = new Random();
            Uniform     uniform   = new Uniform();

            rs.Clear();
            var a = uniform.UpperBound;
            var b = uniform.LowerBound;

            mean = (a + b) / 2; stdev = Math.Sqrt((b - a) * (b - a) / 12);
            for (int i = 0; i < numSamples; ++i)
            {
                rs.Push(uniform.Sample(defaultrs));
            }
            PrintResult.CompareMeanAndVariance("uniform", mean, stdev * stdev, rs.Mean(), rs.Variance());
        }
Example #11
0
        public void TestMeanAndVariacneConsistency()
        {
            List <int> numList = new List <int>()
            {
                1, 2, 3, 4, 5, 6, 7, 8, 9
            };
            const int     numSamples = 100000;
            double        mean, stdev;
            RunningStat   rs        = new RunningStat();
            Random        defaultrs = new Random();
            Uniform <int> uniform   = new Uniform <int>();

            uniform.Candidates = numList;
            rs.Clear();
            mean = 50; stdev = 0;
            for (int i = 0; i < numSamples; ++i)
            {
                rs.Push(uniform.Sample(defaultrs));
            }
            PrintResult.CompareMeanAndVariance("uniform categorical", mean, stdev * stdev, rs.Mean(), rs.Variance());
        }
Example #12
0
        public void TestMeanAndVariacneConsistency()
        {
            const int   numSamples = 100000;
            double      mean, stdev;
            RunningStat rs        = new RunningStat();
            Random      defaultrs = new Random();
            Uniform     uniform   = new Uniform();

            rs.Clear();
            var a = Convert.ToDouble(uniform.UpperBound);
            var b = Convert.ToDouble(uniform.LowerBound);

            mean = (a + b) / 2; stdev = Math.Sqrt(0.25);
            for (int i = 0; i < numSamples; ++i)
            {
                rs.Push(uniform.Sample(defaultrs));
            }
            PrintResult.CompareMeanAndVariance("uniform", mean, stdev * stdev, rs.Mean(), rs.Variance());
            Assert.IsTrue(Math.Abs(mean - rs.Mean()) < 0.1);
            Assert.IsTrue(Math.Abs(stdev * stdev - rs.Variance()) < 0.1);
        }
Example #13
0
        public void TestMeanAndVariacneConsistency_MuSigma()
        {
            const int   numSamples = 100000;
            double      mean, stdev;
            RunningStat rs        = new RunningStat();
            Random      defaultrs = new Random();
            LogNormal   logNormal = new LogNormal();

            rs.Clear();
            mean = 2; stdev = 5;
            var muTemp    = Math.Log(mean) - 0.5 * Math.Log(1 + stdev * stdev / mean / mean);
            var sigmaTemp = Math.Sqrt(Math.Log(1 + stdev * stdev / mean / mean));

            for (int i = 0; i < numSamples; ++i)
            {
                logNormal.Mu    = muTemp;
                logNormal.Sigma = sigmaTemp;

                rs.Push(logNormal.Sample(defaultrs));
            }
            PrintResult.CompareMeanAndVariance("logNormal", mean, stdev * stdev, rs.Mean(), rs.Variance());
        }
Example #14
0
        public void TestMeanAndVariacneConsistency_Mean()
        {
            const int numSamples = 100000;
            double    mean, variance;

            RunningStat rs        = new RunningStat();
            Random      defaultrs = new Random();
            Beta        beta      = new Beta();

            rs.Clear();
            //double a = 2, b = 70;

            //mean = a / (a + b);
            //variance = mean * (1 - mean) / (a + b + 1);
            mean = 0.1; variance = 0.1 * 0.1;
            for (int i = 0; i < numSamples; ++i)
            {
                beta.Mean = mean;
                beta.StandardDeviation = Math.Sqrt(variance);
                rs.Push(beta.Sample(defaultrs));
            }
            PrintResult.CompareMeanAndVariance("Beta", mean, variance, rs.Mean(), rs.Variance());
        }
        public void Evaluate(MethodInfo method)
        {
            var action = MakeAction(this, method);

            var measure = new Func<int, RunningStat>(samples =>
            {
                var runningStat = new RunningStat();
                var sw = new Stopwatch();

                while (samples-- > 0)
                {
                    sw.Restart();
                    action();
                    runningStat.Push((decimal)sw.Elapsed.TotalMilliseconds);
                }

                return runningStat;
            });

            var initialTime = measure(1);
            var baseTime = measure(1);

            var warmupSamples = Math.Max(1, (int)Math.Round(this.warmupTargetTime.TotalMilliseconds / (double)baseTime.Mean));
            var warmupTime = measure(warmupSamples);

            var testSamples = Math.Max(30, (int)Math.Round(this.testTargetTime.TotalMilliseconds / (double)warmupTime.Mean));
            var testTime = measure(testSamples);

            PublishResults(initialTime.Mean, baseTime.Mean, warmupSamples, warmupTime.Mean, warmupTime.StandardDeviation, testSamples, testTime.Mean, testTime.StandardDeviation);
        }