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);
        }
        /// <summary>
        /// /// Fills all cell values of the given vector into a bin from which statistics measures can be retrieved efficiently.
        /// Cells values are copied.
        /// </summary>
        /// <param name="vector">the vector to analyze.</param>
        /// <returns>a bin holding the statistics measures of the vector.</returns>
        /// <example>
        /// Tip: Use <i>Console.WriteLine(bin(vector))</i> to print most measures computed by the bind Example:
        /// <table>
        /// <td class="PRE">
        /// <pre>
        /// Size: 20000
        /// Sum: 299858.02350278624
        /// SumOfSquares: 5399184.154095971
        /// Min: 0.8639113139711261
        /// Max: 59.75331890541892
        /// Mean: 14.992901175139313
        /// RMS: 16.43043540825375
        /// Variance: 45.17438077634358
        /// Standard deviation: 6.721188940681818
        /// Standard error: 0.04752598277592142
        /// Geometric mean: 13.516615397064466
        /// Product: Infinity
        /// Harmonic mean: 11.995174297952191
        /// Sum of inversions: 1667.337172700724
        /// Skew: 0.8922838940067878
        /// Kurtosis: 1.1915828121825598
        /// Sum of powers(3): 1.1345828465808412E8
        /// Sum of powers(4): 2.7251055344494686E9
        /// Sum of powers(5): 7.367125643433887E10
        /// Sum of powers(6): 2.215370909100143E12
        /// Moment(0,0): 1.0
        /// Moment(1,0): 14.992901175139313
        /// Moment(2,0): 269.95920770479853
        /// Moment(3,0): 5672.914232904206
        /// Moment(4,0): 136255.27672247344
        /// Moment(5,0): 3683562.8217169433
        /// Moment(6,0): 1.1076854545500715E8
        /// Moment(0,mean()): 1.0
        /// Moment(1,mean()): -2.0806734113421045E-14
        /// Moment(2,mean()): 45.172122057305664
        /// Moment(3,mean()): 270.92018671421
        /// Moment(4,mean()): 8553.8664869067
        /// Moment(5,mean()): 153357.41712233616
        /// Moment(6,mean()): 4273757.570142922
        /// 25%, 50% and 75% Quantiles: 10.030074811938091, 13.977982089912224,
        /// 18.86124362967137
        /// quantileInverse(mean): 0.559163335012079
        /// Distinct elements & frequencies not printed (too many).
        /// </pre>
        /// </td>
        /// </table>
        /// </example>
        public static DynamicBin1D Bin(DoubleMatrix1D vector)
        {
            DynamicBin1D bin = new DynamicBin1D();

            bin.AddAllOf(new DoubleArrayList(DoubleFactory1D.Dense.ToList(vector).ToArray()));
            return(bin);
        }
        public static void test2(int size, AbstractDistribution a, AbstractDistribution b)
        {
            DynamicBin1D binA = new DynamicBin1D();
            DynamicBin1D binB = new DynamicBin1D();

            for (int j = 0, i = size; --i >= 0; j++)
            {
                binA.Add(a.NextDouble());
                binB.Add(b.NextDouble());
            }
        }
        public static void test2(int size, AbstractDistribution distribution)
        {
            DynamicBin1D bin = new DynamicBin1D();

            for (int j = 0, i = size; --i >= 0; j++)
            {
                bin.Add(distribution.NextDouble());
            }
            Console.WriteLine(bin);
            Console.WriteLine("\n\nGood bye.\n");
        }
        /// <summary>
        /// Applies the given aggregation functions to each column and stores the results in a the result matrix.
        /// If matrix has shape <i>m x n</i>, then result must have shape <i>aggr.Length x n</i>.
        /// Tip: To do aggregations on rows use dice views (transpositions), as in <i>aggregate(matrix.viewDice(),aggr,result.viewDice())</i>.
        /// </summary>
        /// <param name="matrix">any matrix; a column holds the values of a given variable.</param>
        /// <param name="aggr">the aggregation functions to be applied to each column.</param>
        /// <param name="result">the matrix to hold the aggregation results.</param>
        /// <returns><i>result</i> (for convenience only).</returns>
        /// <see cref="Formatter"/>
        /// <see cref="Hep.Aida.Bin.BinFunction1D"/>
        /// <see cref="Hep.Aida.Bin.BinFunctions1D"/>
        public static DoubleMatrix2D Aggregate(DoubleMatrix2D matrix, Hep.Aida.Bin.BinFunction1D[] aggr, DoubleMatrix2D result)
        {
            var bin      = new DynamicBin1D();
            var elements = new double[matrix.Rows];
            var values   = elements;

            for (int column = matrix.Columns; --column >= 0;)
            {
                matrix.ViewColumn(column).ToArray(ref elements); // copy column into values
                bin.Clear();
                bin.AddAllOf(new DoubleArrayList(values));
                for (int i = aggr.Length; --i >= 0;)
                {
                    result[i, column] = aggr[i](bin);
                }
            }
            return(result);
        }
Exemple #6
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);
                }
            }
        }