Esempio n. 1
0
        /// <summary>
        /// Evaluates the root mean square (RMS) also known as quadratic mean.
        /// Returns NaN if data is empty or if any entry is NaN.
        /// </summary>
        /// <param name="data">The data to calculate the RMS of.</param>
        public static double RootMeanSquare(this IEnumerable <double> data)
        {
            var array = data as double[];

            return(array != null
                ? ArrayStatistics.RootMeanSquare(array)
                : StreamingStatistics.RootMeanSquare(data));
        }
Esempio n. 2
0
        /// <summary>
        /// Estimates the unbiased population standard deviation from the provided samples.
        /// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
        /// Returns NaN if data has less than two entries or if any entry is NaN.
        /// </summary>
        /// <param name="samples">A subset of samples, sampled from the full population.</param>
        public static double StandardDeviation(this IEnumerable <double> samples)
        {
            var array = samples as double[];

            return(array != null
                ? ArrayStatistics.StandardDeviation(array)
                : StreamingStatistics.StandardDeviation(samples));
        }
Esempio n. 3
0
        /// <summary>
        /// Estimates the sample mean and the unbiased population variance from the provided samples.
        /// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
        /// Returns NaN for mean if data is empty or if any entry is NaN and NaN for variance if data has less than two entries or if any entry is NaN.
        /// </summary>
        /// <param name="data">The data to calculate the mean of.</param>
        /// <returns>The mean of the sample.</returns>
        public static Tuple <double, double> MeanVariance(this IEnumerable <double> data)
        {
            var array = data as double[];

            return(array != null
                ? ArrayStatistics.MeanVariance(array)
                : StreamingStatistics.MeanVariance(data));
        }
Esempio n. 4
0
        /// <summary>
        /// Evaluates the harmonic mean.
        /// Returns NaN if data is empty or if any entry is NaN.
        /// </summary>
        /// <param name="data">The data to calculate the harmonic mean of.</param>
        /// <returns>The harmonic mean of the sample.</returns>
        public static double HarmonicMean(this IEnumerable <double> data)
        {
            var array = data as double[];

            return(array != null
                ? ArrayStatistics.HarmonicMean(array)
                : StreamingStatistics.HarmonicMean(data));
        }
Esempio n. 5
0
        /// <summary>
        /// Estimates the sample mean and the unbiased population variance from the provided samples.
        /// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
        /// Returns NaN for mean if data is empty or if any entry is NaN and NaN for variance if data has less than two entries or if any entry is NaN.
        /// </summary>
        /// <param name="samples">The data to calculate the mean of.</param>
        /// <returns>The mean of the sample.</returns>
        public static Tuple <double, double> MeanVariance(this IEnumerable <double> samples)
        {
            var array = samples as double[];

            return(array != null
                ? ArrayStatistics.MeanVariance(array)
                : StreamingStatistics.MeanVariance(samples));
        }
Esempio n. 6
0
        /// <summary>
        /// Evaluates the population variance from the provided full population.
        /// On a dataset of size N will use an N normalizer and would thus be biased if applied to a subset.
        /// Returns NaN if data is empty or if any entry is NaN.
        /// </summary>
        /// <param name="population">The full population data.</param>
        public static double PopulationVariance(this IEnumerable <double> population)
        {
            var array = population as double[];

            return(array != null
                ? ArrayStatistics.PopulationVariance(array)
                : StreamingStatistics.PopulationVariance(population));
        }
Esempio n. 7
0
        /// <summary>
        /// Returns the maximum value in the sample data.
        /// Returns NaN if data is empty or if any entry is NaN.
        /// </summary>
        /// <param name="data">The sample data.</param>
        /// <returns>The maximum value in the sample data.</returns>
        public static double Maximum(this IEnumerable <double> data)
        {
            var array = data as double[];

            return(array != null
                ? ArrayStatistics.Maximum(array)
                : StreamingStatistics.Maximum(data));
        }
Esempio n. 8
0
        /// <summary>
        /// Evaluates the population standard deviation from the provided full population.
        /// On a dataset of size N will use an N normalizer and would thus be biased if applied to a subset.
        /// Returns NaN if data is empty or if any entry is NaN.
        /// </summary>
        /// <param name="population">The full population data.</param>
        public static double PopulationStandardDeviation(this IEnumerable <double> population)
        {
            var array = population as double[];

            return(array != null
                ? ArrayStatistics.PopulationStandardDeviation(array)
                : StreamingStatistics.PopulationStandardDeviation(population));
        }
Esempio n. 9
0
        /// <summary>
        /// The internal method for calculating the auto-correlation.
        /// </summary>
        /// <param name="x">The data array to calculate auto-correlation for</param>
        /// <param name="kLow">Min lag to calculate ACF for (0 = no shift with acf=1) must be zero or positive and smaller than x.Length</param>
        /// <param name="kHigh">Max lag (EXCLUSIVE) to calculate ACF for must be positive and smaller than x.Length</param>
        /// <returns>An array with the ACF as a function of the lags k.</returns>
        private static double[] AutoCorrelationFft(double[] x, int kLow, int kHigh)
        {
            if (x == null)
            {
                throw new ArgumentNullException(nameof(x));
            }

            int N = x.Length;    // Sample size

            if (kLow < 0 || kLow >= N)
            {
                throw new ArgumentOutOfRangeException(nameof(kLow), "kMin must be zero or positive and smaller than x.Length");
            }
            if (kHigh < 0 || kHigh >= N)
            {
                throw new ArgumentOutOfRangeException(nameof(kHigh), "kMax must be positive and smaller than x.Length");
            }
            if (N < 1)
            {
                return(new double[0]);
            }

            int nFFT = Euclid.CeilingToPowerOfTwo(N) * 2;

            Complex[] xFFT  = new Complex[nFFT];
            Complex[] xFFT2 = new Complex[nFFT];

            double xDash   = ArrayStatistics.Mean(x);
            double xArrNow = 0.0d;

            // copy values in range and substract mean - all the remaining parts are padded with zero.
            for (int i = 0; i < x.Length; i++)
            {
                xFFT[i] = new Complex(x[i] - xDash, 0.0);    // copy values in range and substract mean
            }

            Fourier.Forward(xFFT, FourierOptions.Matlab);

            // maybe a Vector<Complex> implementation here would be faster
            for (int i = 0; i < xFFT.Length; i++)
            {
                xFFT2[i] = Complex.Multiply(xFFT[i], Complex.Conjugate(xFFT[i]));
            }

            Fourier.Inverse(xFFT2, FourierOptions.Matlab);

            double dc = xFFT2[0].Real;

            double[] result = new double[kHigh - kLow + 1];

            // normalize such that acf[0] would be 1.0
            for (int i = 0; i < (kHigh - kLow + 1); i++)
            {
                result[i] = xFFT2[kLow + i].Real / dc;
            }

            return(result);
        }
Esempio n. 10
0
        /// <summary>
        /// Estimates the unbiased population covariance from the provided samples.
        /// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
        /// Returns NaN if data has less than two entries or if any entry is NaN.
        /// </summary>
        /// <param name="samples1">A subset of samples, sampled from the full population.</param>
        /// <param name="samples2">A subset of samples, sampled from the full population.</param>
        public static double Covariance(this IEnumerable <double> samples1, IEnumerable <double> samples2)
        {
            var array1 = samples1 as double[];
            var array2 = samples2 as double[];

            return(array1 != null && array2 != null
                ? ArrayStatistics.Covariance(array1, array2)
                : StreamingStatistics.Covariance(samples1, samples2));
        }
Esempio n. 11
0
        /// <summary>
        /// Evaluates the population covariance from the provided full populations.
        /// On a dataset of size N will use an N normalizer and would thus be biased if applied to a subset.
        /// Returns NaN if data is empty or if any entry is NaN.
        /// </summary>
        /// <param name="population1">The full population data.</param>
        /// <param name="population2">The full population data.</param>
        public static double PopulationCovariance(this IEnumerable <double> population1, IEnumerable <double> population2)
        {
            var array1 = population1 as double[];
            var array2 = population2 as double[];

            return(array1 != null && array2 != null
                ? ArrayStatistics.PopulationCovariance(array1, array2)
                : StreamingStatistics.PopulationCovariance(population1, population2));
        }
Esempio n. 12
0
        /// <summary>
        /// Estimates the empirical inverse CDF at tau from the provided samples.
        /// </summary>
        /// <param name="data">The data sample sequence.</param>
        /// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive).</param>
        public static double InverseCDF(this IEnumerable <double> data, double tau)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            var array = data.ToArray();

            return(ArrayStatistics.QuantileCustomInplace(array, tau, QuantileDefinition.InverseCDF));
        }
Esempio n. 13
0
        /// <summary>
        /// stimates the tau-th quantile from the provided samples.
        /// The tau-th quantile is the data value where the cumulative distribution
        /// function crosses tau. The quantile definition can be specificed to be compatible
        /// with an existing system.
        /// </summary>
        /// <param name="data">The data sample sequence.</param>
        /// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive).</param>
        /// <param name="definition">Quantile definition, to choose what product/definition it should be consistent with</param>
        public static double QuantileCustom(this IEnumerable <double?> data, double tau, QuantileDefinition definition)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            var array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();

            return(ArrayStatistics.QuantileCustomInplace(array, tau, definition));
        }
Esempio n. 14
0
        /// <summary>
        /// Estimates the p-Percentile value from the provided samples.
        /// If a non-integer Percentile is needed, use Quantile instead.
        /// Approximately median-unbiased regardless of the sample distribution (R8).
        /// </summary>
        /// <param name="data">The data sample sequence.</param>
        /// <param name="p">Percentile selector, between 0 and 100 (inclusive).</param>
        public static double Percentile(this IEnumerable <double?> data, int p)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            var array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();

            return(ArrayStatistics.PercentileInplace(array, p));
        }
Esempio n. 15
0
        /// <summary>
        /// Estimates the third quartile value from the provided samples.
        /// Approximately median-unbiased regardless of the sample distribution (R8).
        /// </summary>
        /// <param name="data">The data sample sequence.</param>
        public static double UpperQuartile(this IEnumerable <double> data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            var array = data.ToArray();

            return(ArrayStatistics.UpperQuartileInplace(array));
        }
Esempio n. 16
0
        /// <summary>
        /// Estimates the inter-quartile range from the provided samples.
        /// Approximately median-unbiased regardless of the sample distribution (R8).
        /// </summary>
        /// <param name="data">The data sample sequence.</param>
        public static double InterquartileRange(this IEnumerable <double?> data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            var array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();

            return(ArrayStatistics.InterquartileRangeInplace(array));
        }
Esempio n. 17
0
        /// <summary>
        /// Estimates {min, lower-quantile, median, upper-quantile, max} from the provided samples.
        /// Approximately median-unbiased regardless of the sample distribution (R8).
        /// </summary>
        /// <param name="data">The data sample sequence.</param>
        public static double[] FiveNumberSummary(this IEnumerable <double?> data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            var array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();

            return(ArrayStatistics.FiveNumberSummaryInplace(array));
        }
Esempio n. 18
0
        /// <summary>
        /// Returns the order statistic (order 1..N) from the provided samples.
        /// </summary>
        /// <param name="data">The data sample sequence.</param>
        /// <param name="order">One-based order of the statistic, must be between 1 and N (inclusive).</param>
        public static double OrderStatistic(IEnumerable <double> data, int order)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            var array = data.ToArray();

            return(ArrayStatistics.OrderStatisticInplace(array, order));
        }
Esempio n. 19
0
        /// <summary>
        /// Estimates the tau-th quantile from the provided samples.
        /// The tau-th quantile is the data value where the cumulative distribution
        /// function crosses tau.
        /// Approximately median-unbiased regardless of the sample distribution (R8).
        /// </summary>
        /// <param name="data">The data sample sequence.</param>
        /// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive).</param>
        public static double Quantile(this IEnumerable <double> data, double tau)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            var array = data.ToArray();

            return(ArrayStatistics.QuantileInplace(array, tau));
        }
Esempio n. 20
0
        static double[] Rank(IEnumerable <double> series)
        {
            if (series == null)
            {
                return(new double[0]);
            }

            // WARNING: do not try to cast series to an array and use it directly,
            // as we need to sort it (inplace operation)

            var data = series.ToArray();

            return(ArrayStatistics.RanksInplace(data, RankDefinition.Average));
        }
Esempio n. 21
0
        /// <summary>
        /// Estimates {min, lower-quantile, median, upper-quantile, max} from the provided samples.
        /// Approximately median-unbiased regardless of the sample distribution (R8).
        /// </summary>
        /// <param name="data">The data sample sequence.</param>
        public static double[] FiveNumberSummary(this IEnumerable <double> data)
        {
            var array = data.ToArray();

            return(ArrayStatistics.FiveNumberSummaryInplace(array));
        }
Esempio n. 22
0
        /// <summary>
        /// Evaluates the rank of each entry of the provided samples.
        /// The rank definition can be specificed to be compatible
        /// with an existing system.
        /// </summary>
        /// <param name="data">The data sample sequence.</param>
        /// <param name="definition">Rank definition, to choose how ties should be handled and what product/definition it should be consistent with</param>
        public static double[] Ranks(this IEnumerable <double> data, RankDefinition definition = RankDefinition.Default)
        {
            var array = data.ToArray();

            return(ArrayStatistics.RanksInplace(array, definition));
        }
Esempio n. 23
0
        /// <summary>
        /// Returns the order statistic (order 1..N) from the provided samples.
        /// </summary>
        /// <param name="data">The data sample sequence.</param>
        /// <param name="order">One-based order of the statistic, must be between 1 and N (inclusive).</param>
        public static double OrderStatistic(IEnumerable <double> data, int order)
        {
            var array = data.ToArray();

            return(ArrayStatistics.OrderStatisticInplace(array, order));
        }
Esempio n. 24
0
        /// <summary>
        /// Estimates the empirical inverse CDF at tau from the provided samples.
        /// </summary>
        /// <param name="data">The data sample sequence.</param>
        /// <param name="tau">Quantile selector, between 0.0 and 1.0 (inclusive).</param>
        public static double EmpiricalInvCDF(this IEnumerable <double> data, double tau)
        {
            var array = data.ToArray();

            return(ArrayStatistics.QuantileCustomInplace(array, tau, QuantileDefinition.EmpiricalInvCDF));
        }
Esempio n. 25
0
        /// <summary>
        /// Estimates the p-Percentile value from the provided samples.
        /// If a non-integer Percentile is needed, use Quantile instead.
        /// Approximately median-unbiased regardless of the sample distribution (R8).
        /// </summary>
        /// <param name="data">The data sample sequence.</param>
        /// <param name="p">Percentile selector, between 0 and 100 (inclusive).</param>
        public static double Percentile(this IEnumerable <double?> data, int p)
        {
            var array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();

            return(ArrayStatistics.PercentileInplace(array, p));
        }
Esempio n. 26
0
        /// <summary>
        /// Estimates the inter-quartile range from the provided samples.
        /// Approximately median-unbiased regardless of the sample distribution (R8).
        /// </summary>
        /// <param name="data">The data sample sequence.</param>
        public static double InterquartileRange(this IEnumerable <double?> data)
        {
            var array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();

            return(ArrayStatistics.InterquartileRangeInplace(array));
        }
Esempio n. 27
0
        /// <summary>
        /// Estimates the inter-quartile range from the provided samples.
        /// Approximately median-unbiased regardless of the sample distribution (R8).
        /// </summary>
        /// <param name="data">The data sample sequence.</param>
        public static double InterquartileRange(this IEnumerable <double> data)
        {
            var array = data.ToArray();

            return(ArrayStatistics.InterquartileRangeInplace(array));
        }
Esempio n. 28
0
        /// <summary>
        /// Estimates the p-Percentile value from the provided samples.
        /// If a non-integer Percentile is needed, use Quantile instead.
        /// Approximately median-unbiased regardless of the sample distribution (R8).
        /// </summary>
        /// <param name="data">The data sample sequence.</param>
        /// <param name="p">Percentile selector, between 0 and 100 (inclusive).</param>
        public static double Percentile(this IEnumerable <double> data, int p)
        {
            var array = data.ToArray();

            return(ArrayStatistics.PercentileInplace(array, p));
        }
Esempio n. 29
0
        /// <summary>
        /// Estimates the third quartile value from the provided samples.
        /// Approximately median-unbiased regardless of the sample distribution (R8).
        /// </summary>
        /// <param name="data">The data sample sequence.</param>
        public static double UpperQuartile(this IEnumerable <double> data)
        {
            var array = data.ToArray();

            return(ArrayStatistics.UpperQuartileInplace(array));
        }
Esempio n. 30
0
        /// <summary>
        /// Estimates {min, lower-quantile, median, upper-quantile, max} from the provided samples.
        /// Approximately median-unbiased regardless of the sample distribution (R8).
        /// </summary>
        /// <param name="data">The data sample sequence.</param>
        public static double[] FiveNumberSummary(this IEnumerable <double?> data)
        {
            var array = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();

            return(ArrayStatistics.FiveNumberSummaryInplace(array));
        }