/// <summary>
        /// This method was created in VisualAge.
        /// <summary>
        /// <returns>double[]</returns>
        /// <param name="values">cern.it.hepodbms.primitivearray.DoubleArrayList</param>
        /// <param name="phis">double[]</param>
        public static double ObservedEpsilonAtPhi(double phi, ExactDoubleQuantileFinder exactFinder, IDoubleQuantileFinder approxFinder)
        {
            int N = (int)exactFinder.Size;

            int exactRank = (int)Utils.EpsilonCeiling(phi * N) - 1;
            //Console.WriteLine("exactRank="+exactRank);
            var    tmp           = exactFinder.QuantileElements(new DoubleArrayList(new double[] { phi }))[0]; // just to ensure exactFinder is sorted
            double approxElement = approxFinder.QuantileElements(new DoubleArrayList(new double[] { phi }))[0];
            //Console.WriteLine("approxElem="+approxElement);
            IntArrayList approxRanks = BinaryMultiSearch(exactFinder.Buffer, approxElement);
            int          from        = approxRanks[0];
            int          to          = approxRanks[1];

            int distance;

            if (from <= exactRank && exactRank <= to)
            {
                distance = 0;
            }
            else
            {
                if (from > exactRank)
                {
                    distance = System.Math.Abs(from - exactRank);
                }
                else
                {
                    distance = System.Math.Abs(exactRank - to);
                }
            }

            double epsilon = (double)distance / (double)N;

            return(epsilon);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// <summary>
        public static void TestQuantileCalculation(String[] args)
        {
            int size = int.Parse(args[0]);
            int b = int.Parse(args[1]);
            int k = int.Parse(args[2]);
            //cern.it.util.Log.enableLogging(args[3].Equals("log"));
            int chunks = int.Parse(args[4]);
            Boolean computeExactQuantilesAlso = args[5].Equals("exact");
            Boolean doShuffle = args[6].Equals("shuffle");
            double epsilon = Double.Parse(args[7]);
            double delta = Double.Parse(args[8]);
            int quantiles = int.Parse(args[9]);
            long max_N = long.Parse(args[10]);



            Console.WriteLine("free=" + Cern.Colt.Karnel.FreePhysicalMemorySize);
            Console.WriteLine("total=" + Cern.Colt.Karnel.TotalVisibleMemorySize);

            double[] phis = { 0.001, 0.01, 0.1, 0.5, 0.9, 0.99, 0.999, 1.0 };
            //int quantiles = phis.Length;

            Timer timer = new Timer();
            Timer timer2 = new Timer();
            IDoubleQuantileFinder approxFinder;

            approxFinder = QuantileFinderFactory.NewDoubleQuantileFinder(false, max_N, epsilon, delta, quantiles, null);
            Console.WriteLine(approxFinder);
            //new UnknownApproximateDoubleQuantileFinder(b,k);
            //approxFinder = new ApproximateDoubleQuantileFinder(b,k);
            /*
            double[] returnSamplingRate = new double[1];
            long[] result = ApproximateQuantileFinder.computeBestBandK(size*chunks, epsilon, delta, quantiles, returnSamplingRate);
            approxFinder = new ApproximateQuantileFinder((int) result[0], (int) result[1]);
            Console.WriteLine("epsilon="+epsilon);
            Console.WriteLine("delta="+delta);
            Console.WriteLine("samplingRate="+returnSamplingRate[0]);
            */


            IDoubleQuantileFinder exactFinder = QuantileFinderFactory.NewDoubleQuantileFinder(false, -1, 0.0, delta, quantiles, null);
            Console.WriteLine(exactFinder);

            DoubleArrayList list = new DoubleArrayList(size);

            for (int chunk = 0; chunk < chunks; chunk++)
            {
                list.Clear();
                int d = chunk * size;
                timer2.Start();
                for (int i = 0; i < size; i++)
                {
                    list.Add((double)(i + d));
                }
                timer2.Stop();



                //Console.WriteLine("unshuffled="+list);
                if (doShuffle)
                {
                    Timer timer3 = new Timer();
                    timer3.Start();

                    list.Shuffle();
                    Console.WriteLine("shuffling took " + timer3.Interval.ToString());

                    timer3.Stop();
                }
                //Console.WriteLine("shuffled="+list);
                //list.sort();
                //Console.WriteLine("sorted="+list);

                timer.Start();
                approxFinder.AddAllOf(list);
                timer.Stop();

                if (computeExactQuantilesAlso)
                {
                    exactFinder.AddAllOf(list);
                }

            }
            Console.WriteLine("list.Add() took" + timer2);
            Console.WriteLine("approxFinder.Add() took" + timer);

            //Console.WriteLine("free="+Cern.Colt.Karnel.FreePhysicalMemorySize);
            //Console.WriteLine("total="+Cern.Colt.Karnel.TotalVisibleMemorySize);

            timer.Stop();
            timer.Start();

            //approxFinder.close();
            DoubleArrayList approxQuantiles = approxFinder.QuantileElements(new DoubleArrayList(phis));

            Console.WriteLine(timer.Display);
            timer.Stop();

            Console.WriteLine("Phis=" + new DoubleArrayList(phis));
            Console.WriteLine("ApproxQuantiles=" + approxQuantiles);

            //Console.WriteLine("MaxLevel of full buffers="+maxLevelOfFullBuffers(approxFinder.bufferSet));

            //Console.WriteLine("total buffers filled="+ approxFinder.totalBuffersFilled);
            //Console.WriteLine("free="+Cern.Colt.Karnel.FreePhysicalMemorySize);
            //Console.WriteLine("total="+Cern.Colt.Karnel.TotalVisibleMemorySize);


            if (computeExactQuantilesAlso)
            {
                Console.WriteLine("Comparing with exact quantile computation...");

                timer.Reset();

                //exactFinder.close();
                DoubleArrayList exactQuantiles = exactFinder.QuantileElements(new DoubleArrayList(phis));
                Console.WriteLine(timer.Display);

                timer.Stop();

                Console.WriteLine("ExactQuantiles=" + exactQuantiles);


                //double[] errors1 = errors1(exactQuantiles.ToArray(), approxQuantiles.ToArray());
                //Console.WriteLine("Error1="+new DoubleArrayList(errors1));

                /*
                DoubleArrayList buffer = new DoubleArrayList((int)exactFinder.Count);
                exactFinder.forEach(
                    new cern.colt.function.DoubleFunction() {
                        public void apply(double element) {
                            buffer.Add(element);
                        }
                    }
                );
                */


                DoubleArrayList observedEpsilons = ObservedEpsilonsAtPhis(new DoubleArrayList(phis), (ExactDoubleQuantileFinder)exactFinder, approxFinder, epsilon);
                Console.WriteLine("observedEpsilons=" + observedEpsilons);

                double element = 1000.0f;


                Console.WriteLine("exact phi(" + element + ")=" + exactFinder.Phi(element));
                Console.WriteLine("apprx phi(" + element + ")=" + approxFinder.Phi(element));

                Console.WriteLine("exact elem(phi(" + element + "))=" + exactFinder.QuantileElements(new DoubleArrayList(new double[] { exactFinder.Phi(element) })));
                Console.WriteLine("apprx elem(phi(" + element + "))=" + approxFinder.QuantileElements(new DoubleArrayList(new double[] { approxFinder.Phi(element) })));
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Observed epsilon
 /// <summary>
 public static double Epsilon(DoubleArrayList sortedList, IDoubleQuantileFinder finder, double phi)
 {
     double element = finder.QuantileElements(new DoubleArrayList(new double[] { phi }))[0];
     return Epsilon(sortedList, phi, element);
 }