Browse Source

Statistics: refactoring: move single precision implementations to partial class

pull/362/head
Christoph Ruegg 11 years ago
parent
commit
442c6d39ef
  1. 4
      MathNet.Numerics.sln
  2. 1
      src/Numerics/Numerics.csproj
  3. 326
      src/Numerics/Statistics/ArrayStatistics.Single.cs
  4. 288
      src/Numerics/Statistics/ArrayStatistics.cs

4
MathNet.Numerics.sln

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Readme", "Readme", "{C2F37492-38AE-4186-8A7F-17B0B080942C}"
ProjectSection(SolutionItems) = preProject

1
src/Numerics/Numerics.csproj

@ -229,6 +229,7 @@
<Compile Include="SpecialFunctions\TestFunctions.cs" />
<Compile Include="Statistics\ArrayStatistics.cs" />
<Compile Include="Statistics\ArrayStatistics.Int32.cs" />
<Compile Include="Statistics\ArrayStatistics.Single.cs" />
<Compile Include="Statistics\MovingStatistics.cs" />
<Compile Include="Statistics\RunningStatistics.cs" />
<Compile Include="Statistics\QuantileDefinition.cs" />

326
src/Numerics/Statistics/ArrayStatistics.Single.cs

@ -0,0 +1,326 @@
// <copyright file="ArrayStatistics.Single.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2015 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Statistics
{
public static partial class ArrayStatistics
{
/// <summary>
/// Returns the smallest value from the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed.</param>
public static float Minimum(float[] data)
{
if (data.Length == 0)
{
return float.NaN;
}
var min = float.PositiveInfinity;
for (int i = 0; i < data.Length; i++)
{
if (data[i] < min || float.IsNaN(data[i]))
{
min = data[i];
}
}
return min;
}
/// <summary>
/// Returns the smallest value from the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed.</param>
public static float Maximum(float[] data)
{
if (data.Length == 0)
{
return float.NaN;
}
var max = float.NegativeInfinity;
for (int i = 0; i < data.Length; i++)
{
if (data[i] > max || float.IsNaN(data[i]))
{
max = data[i];
}
}
return max;
}
/// <summary>
/// Estimates the arithmetic sample mean from the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed.</param>
public static double Mean(float[] data)
{
if (data.Length == 0)
{
return double.NaN;
}
double mean = 0;
ulong m = 0;
for (int i = 0; i < data.Length; i++)
{
mean += (data[i] - mean) / ++m;
}
return mean;
}
/// <summary>
/// Evaluates the geometric mean of the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed.</param>
public static double GeometricMean(float[] data)
{
if (data.Length == 0)
{
return double.NaN;
}
double sum = 0;
for (int i = 0; i < data.Length; i++)
{
sum += Math.Log(data[i]);
}
return Math.Exp(sum / data.Length);
}
/// <summary>
/// Evaluates the harmonic mean of the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed.</param>
public static double HarmonicMean(float[] data)
{
if (data.Length == 0)
{
return double.NaN;
}
double sum = 0;
for (int i = 0; i < data.Length; i++)
{
sum += 1.0 / data[i];
}
return data.Length / sum;
}
/// <summary>
/// Estimates the unbiased population variance from the provided samples as unsorted array.
/// 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">Sample array, no sorting is assumed.</param>
public static double Variance(float[] samples)
{
if (samples.Length <= 1)
{
return double.NaN;
}
double variance = 0;
double t = samples[0];
for (int i = 1; i < samples.Length; i++)
{
t += samples[i];
double diff = ((i + 1) * samples[i]) - t;
variance += (diff * diff) / ((i + 1.0) * i);
}
return variance / (samples.Length - 1);
}
/// <summary>
/// Evaluates the population variance from the full population provided as unsorted array.
/// 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">Sample array, no sorting is assumed.</param>
public static double PopulationVariance(float[] population)
{
if (population.Length == 0)
{
return double.NaN;
}
double variance = 0;
double t = population[0];
for (int i = 1; i < population.Length; i++)
{
t += population[i];
double diff = ((i + 1) * population[i]) - t;
variance += (diff * diff) / ((i + 1.0) * i);
}
return variance / population.Length;
}
/// <summary>
/// Estimates the unbiased population standard deviation from the provided samples as unsorted array.
/// 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">Sample array, no sorting is assumed.</param>
public static double StandardDeviation(float[] samples)
{
return Math.Sqrt(Variance(samples));
}
/// <summary>
/// Evaluates the population standard deviation from the full population provided as unsorted array.
/// 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">Sample array, no sorting is assumed.</param>
public static double PopulationStandardDeviation(float[] population)
{
return Math.Sqrt(PopulationVariance(population));
}
/// <summary>
/// Estimates the arithmetic sample mean and the unbiased population variance from the provided samples as unsorted array.
/// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
/// Returns NaN for mean if data is empty or 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">Sample array, no sorting is assumed.</param>
public static Tuple<double, double> MeanVariance(float[] samples)
{
return new Tuple<double, double>(Mean(samples), Variance(samples));
}
/// <summary>
/// Estimates the arithmetic sample mean and the unbiased population standard deviation from the provided samples as unsorted array.
/// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
/// Returns NaN for mean if data is empty or any entry is NaN and NaN for standard deviation if data has less than two entries or if any entry is NaN.
/// </summary>
/// <param name="samples">Sample array, no sorting is assumed.</param>
public static Tuple<double, double> MeanStandardDeviation(float[] samples)
{
return new Tuple<double, double>(Mean(samples), StandardDeviation(samples));
}
/// <summary>
/// Estimates the unbiased population covariance from the provided two sample arrays.
/// 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">First sample array.</param>
/// <param name="samples2">Second sample array.</param>
public static double Covariance(float[] samples1, float[] samples2)
{
if (samples1.Length != samples2.Length)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength);
}
if (samples1.Length <= 1)
{
return double.NaN;
}
double mean1 = Mean(samples1);
double mean2 = Mean(samples2);
double covariance = 0.0;
for (int i = 0; i < samples1.Length; i++)
{
covariance += (samples1[i] - mean1) * (samples2[i] - mean2);
}
return covariance / (samples1.Length - 1);
}
/// <summary>
/// Evaluates the population covariance from the full population provided as two arrays.
/// 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">First population array.</param>
/// <param name="population2">Second population array.</param>
public static double PopulationCovariance(float[] population1, float[] population2)
{
if (population1.Length != population2.Length)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength);
}
if (population1.Length == 0)
{
return double.NaN;
}
double mean1 = Mean(population1);
double mean2 = Mean(population2);
double covariance = 0.0;
for (int i = 0; i < population1.Length; i++)
{
covariance += (population1[i] - mean1) * (population2[i] - mean2);
}
return covariance / population1.Length;
}
/// <summary>
/// Estimates the root mean square (RMS) also known as quadratic mean from the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed.</param>
public static double RootMeanSquare(float[] data)
{
if (data.Length == 0)
{
return double.NaN;
}
double mean = 0;
ulong m = 0;
for (int i = 0; i < data.Length; i++)
{
mean += (data[i] * data[i] - mean) / ++m;
}
return Math.Sqrt(mean);
}
}
}

288
src/Numerics/Statistics/ArrayStatistics.cs

@ -69,30 +69,6 @@ namespace MathNet.Numerics.Statistics
return min;
}
/// <summary>
/// Returns the smallest value from the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed.</param>
public static float Minimum(float[] data)
{
if (data.Length == 0)
{
return float.NaN;
}
var min = float.PositiveInfinity;
for (int i = 0; i < data.Length; i++)
{
if (data[i] < min || float.IsNaN(data[i]))
{
min = data[i];
}
}
return min;
}
/// <summary>
/// Returns the largest value from the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
@ -117,30 +93,6 @@ namespace MathNet.Numerics.Statistics
return max;
}
/// <summary>
/// Returns the smallest value from the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed.</param>
public static float Maximum(float[] data)
{
if (data.Length == 0)
{
return float.NaN;
}
var max = float.NegativeInfinity;
for (int i = 0; i < data.Length; i++)
{
if (data[i] > max || float.IsNaN(data[i]))
{
max = data[i];
}
}
return max;
}
/// <summary>
/// Estimates the arithmetic sample mean from the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
@ -163,28 +115,6 @@ namespace MathNet.Numerics.Statistics
return mean;
}
/// <summary>
/// Estimates the arithmetic sample mean from the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed.</param>
public static double Mean(float[] data)
{
if (data.Length == 0)
{
return double.NaN;
}
double mean = 0;
ulong m = 0;
for (int i = 0; i < data.Length; i++)
{
mean += (data[i] - mean) / ++m;
}
return mean;
}
/// <summary>
/// Evaluates the geometric mean of the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
@ -206,27 +136,6 @@ namespace MathNet.Numerics.Statistics
return Math.Exp(sum/data.Length);
}
/// <summary>
/// Evaluates the geometric mean of the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed.</param>
public static double GeometricMean(float[] data)
{
if (data.Length == 0)
{
return double.NaN;
}
double sum = 0;
for (int i = 0; i < data.Length; i++)
{
sum += Math.Log(data[i]);
}
return Math.Exp(sum / data.Length);
}
/// <summary>
/// Evaluates the harmonic mean of the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
@ -248,27 +157,6 @@ namespace MathNet.Numerics.Statistics
return data.Length/sum;
}
/// <summary>
/// Evaluates the harmonic mean of the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed.</param>
public static double HarmonicMean(float[] data)
{
if (data.Length == 0)
{
return double.NaN;
}
double sum = 0;
for (int i = 0; i < data.Length; i++)
{
sum += 1.0 / data[i];
}
return data.Length / sum;
}
/// <summary>
/// Estimates the unbiased population variance from the provided samples as unsorted array.
/// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
@ -294,31 +182,6 @@ namespace MathNet.Numerics.Statistics
return variance/(samples.Length - 1);
}
/// <summary>
/// Estimates the unbiased population variance from the provided samples as unsorted array.
/// 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">Sample array, no sorting is assumed.</param>
public static double Variance(float[] samples)
{
if (samples.Length <= 1)
{
return double.NaN;
}
double variance = 0;
double t = samples[0];
for (int i = 1; i < samples.Length; i++)
{
t += samples[i];
double diff = ((i + 1) * samples[i]) - t;
variance += (diff * diff) / ((i + 1.0) * i);
}
return variance / (samples.Length - 1);
}
/// <summary>
/// Evaluates the population variance from the full population provided as unsorted array.
/// On a dataset of size N will use an N normalizer and would thus be biased if applied to a subset.
@ -344,31 +207,6 @@ namespace MathNet.Numerics.Statistics
return variance/population.Length;
}
/// <summary>
/// Evaluates the population variance from the full population provided as unsorted array.
/// 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">Sample array, no sorting is assumed.</param>
public static double PopulationVariance(float[] population)
{
if (population.Length == 0)
{
return double.NaN;
}
double variance = 0;
double t = population[0];
for (int i = 1; i < population.Length; i++)
{
t += population[i];
double diff = ((i + 1) * population[i]) - t;
variance += (diff * diff) / ((i + 1.0) * i);
}
return variance / population.Length;
}
/// <summary>
/// Estimates the unbiased population standard deviation from the provided samples as unsorted array.
/// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
@ -380,17 +218,6 @@ namespace MathNet.Numerics.Statistics
return Math.Sqrt(Variance(samples));
}
/// <summary>
/// Estimates the unbiased population standard deviation from the provided samples as unsorted array.
/// 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">Sample array, no sorting is assumed.</param>
public static double StandardDeviation(float[] samples)
{
return Math.Sqrt(Variance(samples));
}
/// <summary>
/// Evaluates the population standard deviation from the full population provided as unsorted array.
/// On a dataset of size N will use an N normalizer and would thus be biased if applied to a subset.
@ -402,17 +229,6 @@ namespace MathNet.Numerics.Statistics
return Math.Sqrt(PopulationVariance(population));
}
/// <summary>
/// Evaluates the population standard deviation from the full population provided as unsorted array.
/// 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">Sample array, no sorting is assumed.</param>
public static double PopulationStandardDeviation(float[] population)
{
return Math.Sqrt(PopulationVariance(population));
}
/// <summary>
/// Estimates the arithmetic sample mean and the unbiased population variance from the provided samples as unsorted array.
/// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
@ -424,17 +240,6 @@ namespace MathNet.Numerics.Statistics
return new Tuple<double, double>(Mean(samples), Variance(samples));
}
/// <summary>
/// Estimates the arithmetic sample mean and the unbiased population variance from the provided samples as unsorted array.
/// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
/// Returns NaN for mean if data is empty or 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">Sample array, no sorting is assumed.</param>
public static Tuple<double, double> MeanVariance(float[] samples)
{
return new Tuple<double, double>(Mean(samples), Variance(samples));
}
/// <summary>
/// Estimates the arithmetic sample mean and the unbiased population standard deviation from the provided samples as unsorted array.
/// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
@ -446,17 +251,6 @@ namespace MathNet.Numerics.Statistics
return new Tuple<double, double>(Mean(samples), StandardDeviation(samples));
}
/// <summary>
/// Estimates the arithmetic sample mean and the unbiased population standard deviation from the provided samples as unsorted array.
/// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
/// Returns NaN for mean if data is empty or any entry is NaN and NaN for standard deviation if data has less than two entries or if any entry is NaN.
/// </summary>
/// <param name="samples">Sample array, no sorting is assumed.</param>
public static Tuple<double, double> MeanStandardDeviation(float[] samples)
{
return new Tuple<double, double>(Mean(samples), StandardDeviation(samples));
}
/// <summary>
/// Estimates the unbiased population covariance from the provided two sample arrays.
/// On a dataset of size N will use an N-1 normalizer (Bessel's correction).
@ -487,36 +281,6 @@ namespace MathNet.Numerics.Statistics
return covariance/(samples1.Length - 1);
}
/// <summary>
/// Estimates the unbiased population covariance from the provided two sample arrays.
/// 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">First sample array.</param>
/// <param name="samples2">Second sample array.</param>
public static double Covariance(float[] samples1, float[] samples2)
{
if (samples1.Length != samples2.Length)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength);
}
if (samples1.Length <= 1)
{
return double.NaN;
}
double mean1 = Mean(samples1);
double mean2 = Mean(samples2);
double covariance = 0.0;
for (int i = 0; i < samples1.Length; i++)
{
covariance += (samples1[i] - mean1) * (samples2[i] - mean2);
}
return covariance / (samples1.Length - 1);
}
/// <summary>
/// Evaluates the population covariance from the full population provided as two arrays.
/// On a dataset of size N will use an N normalizer and would thus be biased if applied to a subset.
@ -547,36 +311,6 @@ namespace MathNet.Numerics.Statistics
return covariance/population1.Length;
}
/// <summary>
/// Evaluates the population covariance from the full population provided as two arrays.
/// 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">First population array.</param>
/// <param name="population2">Second population array.</param>
public static double PopulationCovariance(float[] population1, float[] population2)
{
if (population1.Length != population2.Length)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength);
}
if (population1.Length == 0)
{
return double.NaN;
}
double mean1 = Mean(population1);
double mean2 = Mean(population2);
double covariance = 0.0;
for (int i = 0; i < population1.Length; i++)
{
covariance += (population1[i] - mean1) * (population2[i] - mean2);
}
return covariance / population1.Length;
}
/// <summary>
/// Estimates the root mean square (RMS) also known as quadratic mean from the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
@ -599,28 +333,6 @@ namespace MathNet.Numerics.Statistics
return Math.Sqrt(mean);
}
/// <summary>
/// Estimates the root mean square (RMS) also known as quadratic mean from the unsorted data array.
/// Returns NaN if data is empty or any entry is NaN.
/// </summary>
/// <param name="data">Sample array, no sorting is assumed.</param>
public static double RootMeanSquare(float[] data)
{
if (data.Length == 0)
{
return double.NaN;
}
double mean = 0;
ulong m = 0;
for (int i = 0; i < data.Length; i++)
{
mean += (data[i] * data[i] - mean) / ++m;
}
return Math.Sqrt(mean);
}
/// <summary>
/// Returns the order statistic (order 1..N) from the unsorted data array.
/// WARNING: Works inplace and can thus causes the data array to be reordered.

Loading…
Cancel
Save