Beispiel #1
0
        public void QuantileBin1DTest()
        {
            var path = NUnit.Framework.TestContext.CurrentContext.TestDirectory + "\\TestResult\\QuantileBin1DTest\\";

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            var filename = path + "QuantileBin1DTest.log";

            try
            {
                using (StreamWriter writer = new StreamWriter(filename))
                {
                    var argv = new String[] { "100", "L" };

                    /*
                     * Get the number of examples from the first argument
                     */
                    int numExamples = 0;
                    try
                    {
                        numExamples = int.Parse(argv[0]);
                    }
                    catch (Exception e)
                    {
                        Assert.Inconclusive("Unable to parse input line count argument");
                        Assert.Inconclusive(e.Message);
                    }
                    writer.WriteLine("Got numExamples=" + numExamples);

                    /*
                     * Get N from the second argument
                     */
                    long N = 0;
                    try
                    {
                        if (argv[1].Equals("L"))
                        {
                            N = long.MaxValue;
                        }
                        else if (argv[1].Equals("I"))
                        {
                            N = (long)int.MaxValue;
                        }
                        else
                        {
                            N = long.Parse(argv[1]);
                        }
                    }
                    catch (Exception e)
                    {
                        Assert.Inconclusive("Error parsing flag for N");
                        Assert.Inconclusive(e.Message);
                    }
                    writer.WriteLine("Got N=" + N);

                    /*
                     * Set up the QuantileBin1D object
                     */
                    DRand         rand   = new DRand(new DateTime());
                    QuantileBin1D qAccum = new QuantileBin1D(false,
                                                             N,
                                                             1e-4,
                                                             1e-3,
                                                             200,
                                                             rand,
                                                             false,
                                                             false,
                                                             2);

                    DynamicBin1D dbin = new DynamicBin1D();

                    /*
                     * Use a new random number generator to generate numExamples
                     * random gaussians, and add them to the QuantileBin1D
                     */
                    Uniform dataRand = new Uniform(new DRand(7757));
                    for (int i = 1; i <= numExamples; i++)
                    {
                        double gauss = dataRand.NextDouble();
                        qAccum.Add(gauss);
                        dbin.Add(gauss);
                    }

                    /*
                     * print out the percentiles
                     */
                    //DecimalFormat fmt = new DecimalFormat("0.00");
                    writer.WriteLine();
                    //int step = 1;
                    int step = 10;
                    for (int i = 1; i < 100;)
                    {
                        double percent  = ((double)i) * 0.01;
                        double quantile = qAccum.Quantile(percent);

                        writer.WriteLine(percent.ToString("0.00") + "  " + quantile + ",  " + dbin.Quantile(percent) + ",  " + (dbin.Quantile(percent) - quantile));
                        i = i + step;
                    }
                }
            }
            catch (IOException x)
            {
                using (StreamWriter writer = new StreamWriter(filename))
                {
                    writer.Write(x.StackTrace);
                }
            }
        }