Example #1
0
        public static double Median(
            PSObject values,
            bool alreadySorted  = false,
            QuantileMethod type = QuantileMethod.Default,
            bool inPlace        = false
            )
        {
            var array = Converter.ToDoubleArray(values);

            return(Accord.Statistics.Measures.Median(array, alreadySorted, type, inPlace));
        }
Example #2
0
        public static double Quartiles(
            PSObject values,
            out double q1,
            out double q3,
            bool alreadySorted  = false,
            QuantileMethod type = QuantileMethod.Default
            )
        {
            var array = Converter.ToDoubleArray(values);

            return(Accord.Statistics.Measures.Quartiles(array, out q1, out q3, alreadySorted, type, false));
        }
        /// <summary>
        ///   Computes multiple quantiles for the given sequence.
        /// </summary>
        ///
        /// <param name="values">The sequence of observations.</param>
        /// <param name="probabilities">The sequence of quantile probabilities.</param>
        /// <param name="type">The quantile type, 1...9.</param>
        /// <param name="alreadySorted">A boolean parameter informing if the given values have already been sorted.</param>
        /// <param name="inPlace">Pass true if the method is allowed to sort <paramref name="values"/> in place, overwriting the
        ///   its original order.</param>
        ///
        /// <returns>Quantile value.</returns>
        ///
        public static double[] Quantiles(this double[] values, double[] probabilities, bool alreadySorted = false, QuantileMethod type = QuantileMethod.Default, bool inPlace = false)
        {
            if (values == null)
            {
                throw new ArgumentNullException("Sequence of observations can't be null.", "values");
            }
            if (values.Length == 0)
            {
                throw new ArgumentException("Sequence of observations can't be empty.", "values");
            }

            if (probabilities == null)
            {
                throw new ArgumentNullException("Sequence of quantile probabilities can't be null.", "probabilities");
            }
            if (probabilities.Length == 0)
            {
                throw new ArgumentNullException("Sequence of quantile probabilities can't be empty.", "probabilities");
            }

            if (probabilities.Any(pv => pv < 0.0 || pv > 1.0))
            {
                throw new ArgumentException("There is invalid probability in the sequence of quantile probabilities.", "probabilities");
            }

            double[] result = new double[probabilities.Length];

            if (!inPlace && !alreadySorted)
            {
                values = values.Copy();
            }

            switch (type)
            {
            case QuantileMethod.Type1: return(Q1(values, probabilities, alreadySorted, result));

            case QuantileMethod.Type2: return(Q2(values, probabilities, alreadySorted, result));

            case QuantileMethod.Type3: return(Q3(values, probabilities, alreadySorted, result));

            case QuantileMethod.Type4: return(Q4(values, probabilities, alreadySorted, result));

            case QuantileMethod.Type5: return(Q5(values, probabilities, alreadySorted, result));

            case QuantileMethod.Type6: return(Q6(values, probabilities, alreadySorted, result));

            case QuantileMethod.Type7: return(Q7(values, probabilities, alreadySorted, result));

            case QuantileMethod.Type8: return(Q8(values, probabilities, alreadySorted, result));

            case QuantileMethod.Type9: return(Q9(values, probabilities, alreadySorted, result));

            default:
                throw new ArgumentException("Invalid quantile type, must be between 1 and 9 (inclusive)", "type");
            }
        }
 /// <summary>
 ///   Computes single quantile for the given sequence.
 /// </summary>
 ///
 /// <param name="values">The sequence of observations.</param>
 /// <param name="type">The quantile type, 1...9.</param>
 /// <param name="probabilities">The auantile probability.</param>
 /// <param name="alreadySorted">A boolean parameter informing if the given values have already been sorted.</param>
 /// <param name="inPlace">Pass true if the method is allowed to sort <paramref name="values"/> in place, overwriting the
 ///   its original order.</param>
 ///
 /// <returns>Quantile value.</returns>
 ///
 public static double Quantile(this double[] values, double probabilities, bool alreadySorted = false, QuantileMethod type = QuantileMethod.Default, bool inPlace = false)
 {
     return(Quantiles(values, new double[] { probabilities }, alreadySorted: alreadySorted, type: type, inPlace: inPlace)[0]);
 }
 /// <summary>
 ///   Computes the upper quartile (Q3) for the given data.
 /// </summary>
 ///
 /// <param name="values">An integer array containing the vector members.</param>
 /// <param name="alreadySorted">A boolean parameter informing if the given values have already been sorted.</param>
 /// <param name="type">The quartile definition that should be used. See <see cref="QuantileMethod"/> for datails.</param>
 /// <param name="inPlace">Pass true if the method is allowed to sort <paramref name="values"/> in place, overwriting the
 ///   its original order.</param>
 ///
 /// <returns>The third quartile of the given data.</returns>
 ///
 public static double UpperQuartile(this double[] values, bool alreadySorted = false, QuantileMethod type = QuantileMethod.Default, bool inPlace = false)
 {
     return(Quantile(values, type: type, probabilities: 0.75, alreadySorted: alreadySorted, inPlace: inPlace));
 }
 /// <summary>
 ///   Computes the Quartiles of the given values.
 /// </summary>
 ///
 /// <param name="values">An integer array containing the vector members.</param>
 /// <param name="q1">The first quartile.</param>
 /// <param name="q3">The third quartile.</param>
 /// <param name="alreadySorted">A boolean parameter informing if the given values have already been sorted.</param>
 /// <param name="type">The quartile definition that should be used. See <see cref="QuantileMethod"/> for datails.</param>
 /// <param name="inPlace">Pass true if the method is allowed to sort <paramref name="values"/> in place, overwriting the
 ///   its original order.</param>
 ///
 /// <returns>The second quartile, the median of the given data.</returns>
 ///
 public static double Quartiles(this double[] values, out double q1, out double q3, bool alreadySorted = false, QuantileMethod type = QuantileMethod.Default, bool inPlace = false)
 {
     double[] result = Quantiles(values, type: type, probabilities: new[] { 0.25, 0.5, 0.75 }, alreadySorted: alreadySorted, inPlace: inPlace);
     q1 = result[0];
     q3 = result[2];
     return(result[1]);
 }
 /// <summary>
 ///   Computes the Quartiles of the given values.
 /// </summary>
 ///
 /// <param name="values">An integer array containing the vector members.</param>
 /// <param name="alreadySorted">A boolean parameter informing if the given values have already been sorted.</param>
 /// <param name="range">The inter-quartile range for the values.</param>
 /// <param name="type">The quartile definition that should be used. See <see cref="QuantileMethod"/> for datails.</param>
 /// <param name="inPlace">Pass true if the method is allowed to sort <paramref name="values"/> in place, overwriting the
 ///   its original order.</param>
 ///
 /// <returns>The second quartile, the median of the given data.</returns>
 ///
 public static double Quartiles(this double[] values, out DoubleRange range, bool alreadySorted = false, QuantileMethod type = QuantileMethod.Default, bool inPlace = false)
 {
     double[] result = Quantiles(values, type: type, probabilities: new[] { 0.25, 0.5, 0.75 }, alreadySorted: alreadySorted, inPlace: inPlace);
     range = new DoubleRange(result[0], result[2]);
     return(result[1]);
 }
 /// <summary>
 ///   Computes the Median of the given values.
 /// </summary>
 ///
 /// <param name="values">An integer array containing the vector members.</param>
 /// <param name="alreadySorted">A boolean parameter informing if the given values have already been sorted.</param>
 /// <param name="type">The quartile definition that should be used. See <see cref="QuantileMethod"/> for datails.</param>
 /// <param name="inPlace">Pass true if the method is allowed to sort <paramref name="values"/> in place, overwriting the
 ///   its original order.</param>
 ///
 /// <returns>The median of the given data.</returns>
 ///
 public static double Median(this int[] values, bool alreadySorted = false, QuantileMethod type = QuantileMethod.Default, bool inPlace = false)
 {
     // TODO: Use T4 templates to generate separate implementations for each data type
     return(Quantile(values.ToDouble(), type: type, probabilities: 0.5, alreadySorted: alreadySorted, inPlace: inPlace));
 }
Example #9
0
        /// <summary>
        ///   Computes the circular quartiles of the given circular angles.
        /// </summary>
        ///
        /// <param name="angles">A double array containing the angles in radians.</param>
        /// <param name="q1">The first quartile, as an out parameter.</param>
        /// <param name="q3">The third quartile, as an out parameter.</param>
        /// <param name="median">The angular median, if already known.</param>
        /// <param name="wrap">
        ///   Whether range values should be wrapped to be contained in the circle. If
        ///   set to false, range values could be returned outside the [+pi;-pi] range.
        /// </param>
        /// <param name="type">The quartile definition that should be used. See <see cref="QuantileMethod"/> for datails.</param>
        ///
        /// <returns>The median of the given angles.</returns>
        ///
        public static double Quartiles(double[] angles, out double q1, out double q3, double median, bool wrap = true, QuantileMethod type = QuantileMethod.Default)
        {
            double[] x = new double[angles.Length];
            for (int i = 0; i < angles.Length; i++)
            {
                x[i] = Accord.Math.Tools.Mod(angles[i] - median, 2 * Math.PI);
            }

            for (int i = 0; i < x.Length; i++)
            {
                x[i] = (x[i] < -Math.PI) ? (x[i] + (2 * Math.PI)) : (x[i]);
                x[i] = (x[i] > +Math.PI) ? (x[i] - (2 * Math.PI)) : (x[i]);
            }

            x.Quartiles(out q1, out q3, alreadySorted: false, type: type, inPlace: true);

            q1 = q1 + median;
            q3 = q3 + median;

            if (wrap)
            {
                q1 = Accord.Math.Tools.Mod(q1, 2 * Math.PI);
                q3 = Accord.Math.Tools.Mod(q3, 2 * Math.PI);
            }

            return(median);
        }
Example #10
0
        /// <summary>
        ///   Computes the circular quartiles of the given circular angles.
        /// </summary>
        ///
        /// <param name="angles">A double array containing the angles in radians.</param>
        /// <param name="range">The sample quartiles, as an out parameter.</param>
        /// <param name="median">The angular median, if already known.</param>
        /// <param name="wrap">
        ///   Whether range values should be wrapped to be contained in the circle. If
        ///   set to false, range values could be returned outside the [+pi;-pi] range.
        /// </param>
        /// <param name="type">The quartile definition that should be used. See <see cref="QuantileMethod"/> for datails.</param>
        ///
        /// <returns>The median of the given angles.</returns>
        ///
        public static double Quartiles(double[] angles, out DoubleRange range, double median, bool wrap = true, QuantileMethod type = QuantileMethod.Default)
        {
            double q1, q3;
            double q2 = Quartiles(angles, out q1, out q3, median, wrap, type: type);

            Accord.Diagnostics.Debug.Assert(q2 == median);

            range = new DoubleRange(q1, q3);
            return(median);
        }
Example #11
0
 /// <summary>
 ///   Computes the circular quartiles of the given circular angles.
 /// </summary>
 ///
 /// <param name="angles">A double array containing the angles in radians.</param>
 /// <param name="range">The sample quartiles, as an out parameter.</param>
 /// <param name="wrap">
 ///   Whether range values should be wrapped to be contained in the circle. If
 ///   set to false, range values could be returned outside the [+pi;-pi] range.
 /// </param>
 /// <param name="type">The quartile definition that should be used. See <see cref="QuantileMethod"/> for datails.</param>
 ///
 /// <returns>The median of the given angles.</returns>
 ///
 public static double Quartiles(double[] angles, out DoubleRange range, bool wrap = true, QuantileMethod type = QuantileMethod.Default)
 {
     return(Quartiles(angles, out range, Median(angles), wrap, type: type));
 }
Example #12
0
        /// <summary>
        ///   Computes the circular quartiles of the given circular samples.
        ///   The minimum possible value for a sample must be zero and the maximum must
        ///   be indicated in the parameter <paramref name="length"/>.
        /// </summary>
        ///
        /// <param name="samples">A double array containing the circular samples.</param>
        /// <param name="length">The maximum possible value of the samples.</param>
        /// <param name="range">The sample quartiles, as an out parameter.</param>
        /// <param name="median">The median value of the <paramref name="samples"/>, if already known.</param>
        /// <param name="wrap">
        ///   Whether range values should be wrapped to be contained in the circle. If
        ///   set to false, range values could be returned outside the [+pi;-pi] range.
        /// </param>
        /// <param name="type">The quartile definition that should be used. See <see cref="QuantileMethod"/> for datails.</param>
        ///
        /// <returns>The median of the given samples.</returns>
        ///
        public static double Quartiles(double[] samples, double length, out DoubleRange range, double median, bool wrap = true, QuantileMethod type = QuantileMethod.Default)
        {
            double angleMedian = Circular.ToRadians(median, length);
            double q2          = Quartiles(ToRadians(samples, length), out range, angleMedian, wrap, type: type);

            range.Min = ToCircular(range.Min, length, wrap);
            range.Max = ToCircular(range.Max, length, wrap);
            return(ToCircular(q2, length));
        }
Example #13
0
 /// <summary>
 ///   Computes the circular quartiles of the given circular angles.
 /// </summary>
 ///
 /// <param name="angles">A double array containing the angles in radians.</param>
 /// <param name="q1">The first quartile, as an out parameter.</param>
 /// <param name="q3">The third quartile, as an out parameter.</param>
 /// <param name="wrap">
 ///   Whether range values should be wrapped to be contained in the circle. If
 ///   set to false, range values could be returned outside the [+pi;-pi] range.
 /// </param>
 /// <param name="type">The quartile definition that should be used. See <see cref="QuantileMethod"/> for datails.</param>
 ///
 /// <returns>The median of the given angles.</returns>
 ///
 public static double Quartiles(double[] angles, out double q1, out double q3, bool wrap = true, QuantileMethod type = QuantileMethod.Default)
 {
     return(Quartiles(angles, out q1, out q3, Median(angles), wrap, type: type));
 }
Example #14
0
        /// <summary>
        ///   Computes the circular quartiles of the given circular samples.
        ///   The minimum possible value for a sample must be zero and the maximum must
        ///   be indicated in the parameter <paramref name="length"/>.
        /// </summary>
        ///
        /// <param name="samples">A double array containing the circular samples.</param>
        /// <param name="length">The maximum possible value of the samples.</param>
        /// <param name="q1">The first quartile, as an out parameter.</param>
        /// <param name="q3">The third quartile, as an out parameter.</param>
        /// <param name="median">The median value of the <paramref name="samples"/>, if already known.</param>
        /// <param name="wrap">
        ///   Whether range values should be wrapped to be contained in the circle. If
        ///   set to false, range values could be returned outside the [+pi;-pi] range.
        /// </param>
        /// <param name="type">The quartile definition that should be used. See <see cref="QuantileMethod"/> for datails.</param>
        ///
        /// <returns>The median of the given samples.</returns>
        ///
        public static double Quartiles(double[] samples, double length, out double q1, out double q3, double median, bool wrap = true, QuantileMethod type = QuantileMethod.Default)
        {
            double angleMedian = Circular.ToRadians(median, length);
            double q2          = Quartiles(ToRadians(samples, length), out q1, out q3, angleMedian, wrap, type: type);

            q1 = ToCircular(q1, length, wrap);
            q3 = ToCircular(q3, length, wrap);
            return(ToCircular(q2, length));
        }