public virtual double Moment(int k, double c)
        {
            if (k < 0)
            {
                throw new ArgumentException(Cern.LocalizedResources.Instance().Exception_KMustBePositive);
            }
            //checkOrder(k);
            if (!HasSumOfPowers(k))
            {
                return(Double.NaN);
            }

            int             maxOrder  = System.Math.Min(k, GetMaxOrderForSumOfPowers());
            DoubleArrayList sumOfPows = new DoubleArrayList(maxOrder + 1);

            sumOfPows.Add(Size);
            sumOfPows.Add(Sum);
            sumOfPows.Add(SumOfSquares);
            for (int i = 3; i <= maxOrder; i++)
            {
                sumOfPows.Add(GetSumOfPowers(i));
            }

            return(Descriptive.Moment(k, c, Size, sumOfPows.ToArray()));
        }
예제 #2
0
        /// <summary>
        /// 3-d OLAP cube operator; Fills all cells of the given vectors into the given histogram.
        /// If you use Hep.Aida.Ref.Converter.ToString(histo) on the result, the OLAP cube of x-"column" vsd y-"column" vsd z-"column", summing the weights "column" will be printed.
        /// For example, aggregate sales by product by region by time.
        /// <p>
        /// Computes the distinct values of x and y and z, yielding histogram axes that capture one distinct value per bin.
        /// Then fills the histogram.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="z"></param>
        /// <param name="weights"></param>
        /// <returns>the histogram containing the cube.</returns>
        /// <excption cref="ArgumentException">if <i>x.Count != y.Count || x.Count != z.Count || x.Count != weights.Count</i>.</excption>
        public static Hep.Aida.IHistogram3D cube(DoubleMatrix1D x, DoubleMatrix1D y, DoubleMatrix1D z, DoubleMatrix1D weights)
        {
            if (x.Size != y.Size || x.Size != z.Size || x.Size != weights.Size)
            {
                throw new ArgumentException("vectors must have same size");
            }

            var epsilon  = 1.0E-9;
            var distinct = new DoubleArrayList();
            var vals     = new double[x.Size];
            var sorted   = new DoubleArrayList(vals);

            // compute distinct values of x
            vals = x.ToArray(); // copy x into vals
            sorted.Sort();
            Cern.Jet.Stat.Descriptive.Frequencies(sorted, distinct, null);
            // since bins are right-open [from,to) we need an additional dummy bin so that the last distinct value does not fall into the overflow bin
            if (distinct.Count > 0)
            {
                distinct.Add(distinct[distinct.Count - 1] + epsilon);
            }
            distinct.TrimToSize();
            Hep.Aida.IAxis xaxis = new Hep.Aida.Ref.VariableAxis(distinct.ToArray());

            // compute distinct values of y
            vals = y.ToArray();
            sorted.Sort();
            Cern.Jet.Stat.Descriptive.Frequencies(sorted, distinct, null);
            // since bins are right-open [from,to) we need an additional dummy bin so that the last distinct value does not fall into the overflow bin
            if (distinct.Count > 0)
            {
                distinct.Add(distinct[distinct.Count - 1] + epsilon);
            }
            distinct.TrimToSize();
            Hep.Aida.IAxis yaxis = new Hep.Aida.Ref.VariableAxis(distinct.ToArray());

            // compute distinct values of z
            vals = z.ToArray();
            sorted.Sort();
            Cern.Jet.Stat.Descriptive.Frequencies(sorted, distinct, null);
            // since bins are right-open [from,to) we need an additional dummy bin so that the last distinct value does not fall into the overflow bin
            if (distinct.Count > 0)
            {
                distinct.Add(distinct[distinct.Count - 1] + epsilon);
            }
            distinct.TrimToSize();
            Hep.Aida.IAxis zaxis = new Hep.Aida.Ref.VariableAxis(distinct.ToArray());

            Hep.Aida.IHistogram3D histo = new Hep.Aida.Ref.Histogram3D("Cube", xaxis, yaxis, zaxis);
            return(Histogram(histo, x, y, z, weights));
        }
예제 #3
0
        public static void demo1()
        {
            // Gamma distribution

            // define distribution parameters
            double mean     = 5;
            double variance = 1.5;
            double alpha    = mean * mean / variance;
            double lambda   = 1 / (variance / mean);

            // for tests and debugging use a random engine with CONSTANT seed --> deterministic and reproducible results
            RandomEngine engine = new MersenneTwister();

            // your favourite distribution goes here
            AbstractDistribution dist = new Gamma(alpha, lambda, engine);

            // collect random numbers and print statistics
            int size    = 100000;
            var numbers = new DoubleArrayList(size);

            for (int i = 0; i < size; i++)
            {
                numbers.Add(dist.NextDouble());
            }

            DynamicBin1D bin = new DynamicBin1D();

            bin.AddAllOf(numbers);
            Console.WriteLine(bin);
        }
예제 #4
0
 /// <summary>
 /// Adds a value to the receiver.
 /// </summary>
 /// <param name="value"></param>
 public void Add(double value)
 {
     if (!isAllocated)
     {
         Allocate();               // lazy buffer allocation can safe memory.
     }
     values.Add(value);
     this.isSorted = false;
 }
예제 #5
0
        /// <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 DoubleArrayList ObservedEpsilonsAtPhis(DoubleArrayList phis, ExactDoubleQuantileFinder exactFinder, IDoubleQuantileFinder approxFinder, double desiredEpsilon)
        {
            DoubleArrayList epsilons = new DoubleArrayList(phis.Count);

            for (int i = phis.Count; --i >= 0;)
            {
                double epsilon = ObservedEpsilonAtPhi(phis[i], exactFinder, approxFinder);
                epsilons.Add(epsilon);
                if (epsilon > desiredEpsilon) Console.WriteLine("Real epsilon = " + epsilon + " is larger than desired by " + (epsilon - desiredEpsilon));
            }
            return epsilons;
        }
예제 #6
0
 public override void Add(double element)
 {
     _elements.Add(element);
     InvalidateAll();
 }
예제 #7
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) })));
            }
        }