/// <summary>
        /// Calculates the median of the given numbers.
        /// </summary>
        /// <param name="numbers">The numbers whose median is to be calculated.</param>
        /// <returns>The median of the given numbers.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// The specified collection must not be null.
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// The arguments must not be empty.
        /// </exception>
        public static double Median(IEnumerable <double> numbers)
        {
            if (numbers == null)
            {
                throw ArgumentNullException;
            }

            if (!numbers.Any())
            {
                throw EmptyNumbersCollectionException;
            }

            List <double> sortedNumbers = numbers.OrderBy(number => number).ToList();

            double median = 0;

            if (sortedNumbers.Count % 2 == 0)
            {
                double lowerMedian = sortedNumbers[sortedNumbers.Count / 2 - 1];
                double upperMedian = sortedNumbers[sortedNumbers.Count / 2];

                median = ArithmeticMeanCalculator.ArithmeticMean(lowerMedian, upperMedian);
            }
            else
            {
                median = sortedNumbers[sortedNumbers.Count / 2];
            }

            return(median);
        }
Example #2
0
        /// <summary>
        /// Calculates the midrange of the specified collection of numbers.
        /// </summary>
        /// <param name="numbers">The numbers whose midrange is to be calculated.</param>
        /// <returns>
        /// The midrange of the specified collection of numbers.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// The specified collection must not be null.
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// The specified collection must not be empty.
        /// </exception>
        public static double Midrange(IEnumerable <double> numbers)
        {
            if (numbers == null)
            {
                throw ArgumentNullException;
            }

            if (!numbers.Any())
            {
                throw EmptyNumbersCollectionException;
            }

            double minValue = numbers.Min();
            double maxValue = numbers.Max();

            double midrange = ArithmeticMeanCalculator.ArithmeticMean(minValue, maxValue);

            return(midrange);
        }
        /// <summary>
        /// Calculates the arithmetic-geometric mean of the specified two numbers.
        /// </summary>
        /// <param name="x">The first number.</param>
        /// <param name="y">The second number.</param>
        /// <returns>
        /// The arithmetic-geometric mean of the specified two numbers.
        /// </returns>
        public static double ArithmeticGeometricMean(double x, double y)
        {
            if (x < 0 || y < 0)
            {
                throw new InvalidOperationException("Both arguments must be non-negative!");
            }

            double a_n = 0;
            double g_n = 0;

            for (int n = 0; n < ApproximationIterations; n++)
            {
                a_n = ArithmeticMeanCalculator.ArithmeticMean(x, y);
                g_n = GeometricMeanCalculator.GeometricMean(x, y);

                x = a_n;
                y = g_n;
            }

            return(a_n);
        }