Accord.Statistics.Measures.GetHistogramRange C# (CSharp) Method

GetHistogramRange() public static method

Get range around median of an histogram containing specified percentage of values.

The input array is treated as histogram, i.e. its indexes are treated as values of stochastic function, but array values are treated as "probabilities" (total amount of hits).

The method calculates range of stochastic variable, which summary probability comprises the specified percentage of histogram's hits.

public static GetHistogramRange ( this values, double percent ) : IntRange
values this Histogram array.
percent double Values percentage around median.
return IntRange
        public static IntRange GetHistogramRange(this int[] values, double percent)
        {
            int total = 0;

            // for all values
            for (int i = 0; i < values.Length; i++)
            {
                // accumulate total
                total += values[i];
            }

            int min, max, hits;
            int h = (int)(total * (percent + (1 - percent) / 2));

            // get range min value
            for (min = 0, hits = total; min < values.Length; min++)
            {
                hits -= values[min];
                if (hits < h)
                    break;
            }

            // get range max value
            for (max = values.Length - 1, hits = total; max >= 0; max--)
            {
                hits -= values[max];
                if (hits < h)
                    break;
            }

            return new IntRange(min, max);
        }