示例#1
0
        public void QuantilePerformanceTest()
        {
            int       n    = 10;
            var       sw0  = new Stopwatch();
            var       sw1  = new Stopwatch();
            const int reps = 1000;

            while (n <= 1000000)
            {
                for (int i = 0; i < reps; i++)
                {
                    var xs = RandomEnumerable.SampleRandomNumbers(0, 10000, n + 1).Select(x => (double)x).ToArray();
                    sw0.Start();
                    var q0 = Median(xs); // sorting
                    sw0.Stop();


                    sw1.Start();
                    var q1 = xs.Median(); // selection
                    sw1.Stop();
                    Assert.AreEqual(q0, q1, 1E-9);
                }
                Console.WriteLine("{0,-10} {1,-10} {2,-10}", n, sw0.ElapsedMilliseconds, sw1.ElapsedMilliseconds);

                n = n * 10;
            }
        }
        protected IEnumerable <int> GenerateRowsToEvaluate(double percentageOfRows)
        {
            IEnumerable <int> rows;
            int samplesStart       = EvaluationPartitionParameter.ActualValue.Start;
            int samplesEnd         = EvaluationPartitionParameter.ActualValue.End;
            int testPartitionStart = ProblemDataParameter.ActualValue.TestPartition.Start;
            int testPartitionEnd   = ProblemDataParameter.ActualValue.TestPartition.End;

            if (samplesEnd < samplesStart)
            {
                throw new ArgumentException("Start value is larger than end value.");
            }

            if (percentageOfRows.IsAlmost(1.0))
            {
                rows = Enumerable.Range(samplesStart, samplesEnd - samplesStart);
            }
            else
            {
                int seed  = RandomParameter.ActualValue.Next();
                int count = (int)((samplesEnd - samplesStart) * percentageOfRows);
                if (count == 0)
                {
                    count = 1;
                }
                rows = RandomEnumerable.SampleRandomNumbers(seed, samplesStart, samplesEnd, count);
            }

            return(rows.Where(i => i < testPartitionStart || testPartitionEnd <= i));
        }
示例#3
0
        public void BeCompatibleWithImplicitEnumerableTypes()
        {
            var re = new RandomEnumerable();

            var count = 0;

            foreach (var r in re)
            {
                Assert.True(true);

                if (++count == 10)
                {
                    break;
                }
            }
        }
    static void Main(string[] args)
    {
        int[] numbers = new int[5] {
            32, 67, 88, 13, 50
        };
        var randomEnumerable = new RandomEnumerable(0, numbers.Length);
        int i = 0;

        while (i < numbers.Length)
        {
            var nextIndex = randomEnumerable.First();
            var number    = numbers[nextIndex];
            Console.WriteLine(number);
            i++;
        }
        Console.ReadLine();
    }
        protected IEnumerable <int> GenerateRowsToEvaluate()
        {
            int seed               = RandomParameter.ActualValue.Next();
            int samplesStart       = ValidationPartitionParameter.ActualValue.Start;
            int samplesEnd         = ValidationPartitionParameter.ActualValue.End;
            int testPartitionStart = ProblemDataParameter.ActualValue.TestPartition.Start;
            int testPartitionEnd   = ProblemDataParameter.ActualValue.TestPartition.End;

            if (samplesEnd < samplesStart)
            {
                throw new ArgumentException("Start value is larger than end value.");
            }
            int count = (int)((samplesEnd - samplesStart) * RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value);

            if (count == 0)
            {
                count = 1;
            }
            return(RandomEnumerable.SampleRandomNumbers(seed, samplesStart, samplesEnd, count)
                   .Where(i => i < testPartitionStart || testPartitionEnd <= i));
        }
        protected IEnumerable <int> GenerateRowsToEvaluate(double percentageOfRows)
        {
            IEnumerable <int> rows;
            int samplesStart       = EvaluationPartitionParameter.ActualValue.Start;
            int samplesEnd         = EvaluationPartitionParameter.ActualValue.End;
            int testPartitionStart = ProblemDataParameter.ActualValue.TestPartition.Start;
            int testPartitionEnd   = ProblemDataParameter.ActualValue.TestPartition.End;

            if (samplesEnd < samplesStart)
            {
                throw new ArgumentException("Start value is larger than end value.");
            }

            if (percentageOfRows.IsAlmost(1.0))
            {
                rows = Enumerable.Range(samplesStart, samplesEnd - samplesStart);
            }
            else
            {
                int seed  = RandomParameter.ActualValue.Next();
                int count = (int)((samplesEnd - samplesStart) * percentageOfRows);
                if (count == 0)
                {
                    count = 1;
                }
                rows = RandomEnumerable.SampleRandomNumbers(seed, samplesStart, samplesEnd, count);
            }

            rows = rows.Where(i => i < testPartitionStart || testPartitionEnd <= i);
            if (ValidRowIndicatorParameter.ActualValue != null)
            {
                string indicatorVar = ValidRowIndicatorParameter.ActualValue.Value;
                var    problemData  = ProblemDataParameter.ActualValue;
                var    indicatorRow = problemData.Dataset.GetReadOnlyDoubleValues(indicatorVar);
                rows = rows.Where(r => !indicatorRow[r].IsAlmost(0.0));
            }
            return(rows);
        }
示例#7
0
        static void Main()
        {
            RandomEnumerable rand = new RandomEnumerable();

            //IEnumerator<double> r = rand.GetEnumerator();
            //while (r.MoveNext()) {
            //  double d = r.Current;
            //  Console.WriteLine("-->"+d);
            //  }
            foreach (double d in rand.Where(x => x < 0.5).OrderByDescending(x => x))
            {
                Console.WriteLine("-->" + d);
            }

            Console.WriteLine(rand.Min());

            var q = from d in rand where d > 0.5 orderby d select d;

            foreach (double v in q)
            {
                Console.WriteLine(">>>" + v);
            }
        }
示例#8
0
        public void SampleProportionalWithoutRepetitionTest()
        {
            {
                // select 1 of 100 uniformly (weights = 0)
                var items   = Enumerable.Range(0, 100);
                var random  = new MersenneTwister(31415);
                var weights = Enumerable.Repeat(0.0, 100);
                for (int i = 0; i < 1000; i++)
                {
                    var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
                    Assert.AreEqual(sample.Count(), 1);
                }
            }
            {
                // select 1 of 1 uniformly (weights = 0)
                var items   = Enumerable.Range(0, 1);
                var random  = new MersenneTwister(31415);
                var weights = Enumerable.Repeat(0.0, 1);
                for (int i = 0; i < 1000; i++)
                {
                    var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
                    Assert.AreEqual(sample.Count(), 1);
                }
            }
            {
                // select 1 of 2 non-uniformly (weights = 1, 2)
                var items        = Enumerable.Range(0, 2);
                var random       = new MersenneTwister(31415);
                var weights      = new double[] { 1.0, 2.0 };
                var zeroSelected = 0;
                for (int i = 0; i < 1000; i++)
                {
                    var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
                    Assert.AreEqual(sample.Count(), 1);
                    if (sample[0] == 0)
                    {
                        zeroSelected++;
                    }
                }
                Assert.IsTrue(zeroSelected > 0 && zeroSelected < 1000);
            }
            {
                // select 2 of 2 non-uniformly (weights = 1, 1000)
                var items   = Enumerable.Range(0, 2);
                var random  = new MersenneTwister(31415);
                var weights = new double[] { 1.0, 1000.0 };
                for (int i = 0; i < 1000; i++)
                {
                    var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 2, weights, false, false).ToArray();
                    Assert.AreEqual(sample.Count(), 2);
                    Assert.AreEqual(sample.Distinct().Count(), 2);
                }
            }
            {
                // select 2 from 1 uniformly (weights = 0), this does not throw an exception but instead returns a sample with 1 element!
                var items   = Enumerable.Range(0, 1);
                var random  = new MersenneTwister(31415);
                var weights = Enumerable.Repeat(0.0, 1);
                var sample  = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 2, weights, false, false).ToArray();
                Assert.AreEqual(sample.Count(), 1);
            }

            {
                // select 10 of 100 uniformly (weights = 0)
                var items   = Enumerable.Range(0, 100);
                var random  = new MersenneTwister(31415);
                var weights = Enumerable.Repeat(0.0, 100);
                for (int i = 0; i < 1000; i++)
                {
                    var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, false, false).ToArray();
                    Assert.AreEqual(sample.Count(), 10);
                    Assert.AreEqual(sample.Distinct().Count(), 10);
                }
            }

            {
                // select 100 of 100 uniformly (weights = 0)
                var items   = Enumerable.Range(0, 100);
                var random  = new MersenneTwister(31415);
                var weights = Enumerable.Repeat(0.0, 100);
                for (int i = 0; i < 1000; i++)
                {
                    var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 100, weights, false, false).ToArray();
                    Assert.AreEqual(sample.Count(), 100);
                    Assert.AreEqual(sample.Distinct().Count(), 100);
                }
            }

            {
                // select 10 of 10 uniformly (weights = 1)
                var items   = Enumerable.Range(0, 10);
                var random  = new MersenneTwister(31415);
                var weights = Enumerable.Repeat(1.0, 10);
                for (int i = 0; i < 1000; i++)
                {
                    var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, false, false).ToArray();
                    Assert.AreEqual(sample.Count(), 10);
                    Assert.AreEqual(sample.Distinct().Count(), 10);
                }
            }

            {
                // select 10 of 10 uniformly (weights = 1)
                var items   = Enumerable.Range(0, 10);
                var random  = new MersenneTwister(31415);
                var weights = Enumerable.Repeat(1.0, 10);
                for (int i = 0; i < 1000; i++)
                {
                    var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, true, false).ToArray();
                    Assert.AreEqual(sample.Count(), 10);
                    Assert.AreEqual(sample.Distinct().Count(), 10);
                }
            }

            {
                // select 10 of 10 uniformly (weights = 1)
                var items   = Enumerable.Range(0, 10);
                var random  = new MersenneTwister(31415);
                var weights = Enumerable.Repeat(1.0, 10);
                for (int i = 0; i < 1000; i++)
                {
                    var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, true, true).ToArray();
                    Assert.AreEqual(sample.Count(), 10);
                    Assert.AreEqual(sample.Distinct().Count(), 10);
                }
            }

            {
                // select 5 of 10 uniformly (weights = 0..n)
                var items   = Enumerable.Range(0, 10);
                var random  = new MersenneTwister(31415);
                var weights = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                for (int i = 0; i < 1000; i++)
                {
                    var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 5, weights, false, false).ToArray();
                    Assert.AreEqual(sample.Count(), 5);
                    Assert.AreEqual(sample.Distinct().Count(), 5);
                }
            }

            {
                // select 5 of 10 uniformly (weights = 0..n)
                var items   = Enumerable.Range(0, 10);
                var random  = new MersenneTwister(31415);
                var weights = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                for (int i = 0; i < 1000; i++)
                {
                    var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 5, weights, true, false).ToArray();
                    Assert.AreEqual(sample.Count(), 5);
                    Assert.AreEqual(sample.Distinct().Count(), 5);
                }
            }

            {
                // select 5 of 10 uniformly (weights = 0..n)
                var items   = Enumerable.Range(0, 10);
                var random  = new MersenneTwister(31415);
                var weights = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                for (int i = 0; i < 1000; i++)
                {
                    var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 5, weights, true, true).ToArray();
                    Assert.AreEqual(sample.Count(), 5);
                    Assert.AreEqual(sample.Distinct().Count(), 5);
                }
            }

            {
                // select 10 of 100 uniformly (weights = 1)
                // repeat 1000000 times and calculate statistics
                var items          = Enumerable.Range(0, 100);
                var random         = new MersenneTwister(31415);
                var weights        = Enumerable.Repeat(1.0, 100);
                var selectionCount = new int[100, 100]; // frequency of selecting item at pos
                for (int i = 0; i < 1000000; i++)
                {
                    var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 100, weights, false, false).ToArray();
                    Assert.AreEqual(sample.Count(), 100);
                    Assert.AreEqual(sample.Distinct().Count(), 100);

                    int pos = 0;
                    foreach (var item in sample)
                    {
                        selectionCount[item, pos]++;
                        pos++;
                    }
                }
                var sb = new StringBuilder();
                for (int item = 0; item < 100; item++)
                {
                    for (int pos = 0; pos < 100; pos++)
                    {
                        sb.AppendFormat("{0} ", selectionCount[item, pos]);
                    }
                    sb.AppendLine();
                }
                Console.WriteLine(sb.ToString());
            }
        }