コード例 #1
0
        private static void MinMax_Inner(UniformDistributionSampler sampler, int len)
        {
            // Alloc arrays and fill with uniform random noise.
            float[] a = new float[len];
            sampler.Sample(a);

            // Calc results and compare.
            PointwiseMinMax(a, out float expectedMin, out float expectedMax);
            MathSpan.MinMax(a, out float actualMin, out float actualMax);

            Assert.Equal(expectedMin, actualMin, 10);
            Assert.Equal(expectedMax, actualMax, 10);
        }
コード例 #2
0
        private static void MinMax_Inner(ISampler <int> sampler, int len)
        {
            // Alloc arrays and fill with uniform random noise.
            int[] a = new int[len];
            sampler.Sample(a);

            // Calc results and compare.
            PointwiseMinMax(a, out int expectedMin, out int expectedMax);
            MathSpan.MinMax(a, out int actualMin, out int actualMax);

            Assert.Equal(expectedMin, actualMin);
            Assert.Equal(expectedMax, actualMax);
        }
コード例 #3
0
ファイル: NumericsUtils.cs プロジェクト: lanicon/Redzen
        /// <summary>
        /// Calculate a histogram for the provided span of values.
        /// 1) The minimum and maximum values are found.
        /// 2) The resulting value range is divided into equal sized sub-ranges or bins.
        /// 3) The number of values that fall into each bin is determined.
        /// </summary>
        /// <param name="vals">The values to calculate a histogram for.</param>
        /// <param name="binCount">The number of histogram bins to use.</param>
        /// <returns>A new instance of <see cref="HistogramData"/>.</returns>
        public static HistogramData BuildHistogramData(Span <double> vals, int binCount)
        {
            // Determine min/max.
            MathSpan.MinMax(vals, out double min, out double max);

            // Note. each bin's range has interval [low,high), i.e. samples exactly equal to 'high' will fall
            // into the next highest bin. Except for the last bin which has interval [low, high].
            double range = max - min;

            // Handle special case where the data series contains a single value.
            if (range == 0.0)
            {
                return(new HistogramData(min, max, 0.0, new int[] { vals.Length }));
            }

            // Loop values, and for each one increment the relevant category's frequency count.
            double incr = range / binCount;

            int[] frequencyArr = new int[binCount];

            for (int i = 0; i < vals.Length; i++)
            {
                // Determine which bin the value falls within.
                int binIdx = (int)((vals[i] - min) / incr);

                // Values that equal max, are placed into the last bin.
                if (binIdx == binCount)
                {
                    binIdx--;
                }

                frequencyArr[binIdx]++;
            }

            return(new HistogramData(min, max, incr, frequencyArr));
        }