Browse Source

stats: added Percentile class and missing complex MatlabWriter tests

pull/36/head
Marcus Cuda 16 years ago
parent
commit
efbb270556
  1. 5
      src/MathNet.Numerics.5.1.ReSharper
  2. 1
      src/Numerics/Numerics.csproj
  3. 9
      src/Numerics/Properties/Resources.Designer.cs
  4. 3
      src/Numerics/Properties/Resources.resx
  5. 210
      src/Numerics/Statistics/Percentile.cs
  6. 3
      src/Silverlight/Silverlight.csproj
  7. 116
      src/UnitTests/LinearAlgebraTests/Complex/IO/MatlabWriterTests.cs
  8. 116
      src/UnitTests/LinearAlgebraTests/Complex32/IO/MatlabWriterTests.cs
  9. 90
      src/UnitTests/StatisticsTests/PercentileTests.cs
  10. 1
      src/UnitTests/UnitTests.csproj

5
src/MathNet.Numerics.5.1.ReSharper

@ -29,7 +29,10 @@ kronecker
Cholesky
Eigen
mxn
nxn</UserWords>
nxn
Nist
NIST's
Excel's</UserWords>
</CustomDictionary>
</Dictionaries>
</CustomDictionaries>

1
src/Numerics/Numerics.csproj

@ -389,6 +389,7 @@
<Compile Include="Statistics\MCMC\MetropolisHastingsSampler.cs" />
<Compile Include="Statistics\MCMC\MetropolisSampler.cs" />
<Compile Include="Statistics\MCMC\RejectionSampler.cs" />
<Compile Include="Statistics\Percentile.cs" />
<Compile Include="Statistics\Statistics.cs" />
<Compile Include="Statistics\MCMC\UnivariateSliceSampler.cs" />
<Compile Include="Threading\CommonParallel.cs" />

9
src/Numerics/Properties/Resources.Designer.cs

@ -627,6 +627,15 @@ namespace MathNet.Numerics.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Data must contain at least {0} values..
/// </summary>
internal static string MustContainAtLeast {
get {
return ResourceManager.GetString("MustContainAtLeast", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Name cannot contain a space. name: {0}.
/// </summary>

3
src/Numerics/Properties/Resources.resx

@ -345,4 +345,7 @@
<data name="MatlabDateHeaderFormat" xml:space="preserve">
<value>ddd MMM dd HH:mm:ss yyyy</value>
</data>
<data name="MustContainAtLeast" xml:space="preserve">
<value>Data must contain at least {0} values.</value>
</data>
</root>

210
src/Numerics/Statistics/Percentile.cs

@ -0,0 +1,210 @@
// <copyright file="Percentile.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-2010 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>
namespace MathNet.Numerics.Statistics
{
using System;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// Methods to calculate the percentiles.
/// </summary>
public enum PercentileMethod
{
/// <summary>
/// Using the method recommened my NIST,
/// http://www.itl.nist.gov/div898/handbook/prc/section2/prc252.htm
/// </summary>
Nist = 0,
/// <summary>
/// Using the nearest rank, http://en.wikipedia.org/wiki/Percentile#Nearest_Rank
/// </summary>
Nearest,
/// <summary>
/// Using the same method as Excel does,
/// http://www.itl.nist.gov/div898/handbook/prc/section2/prc252.htm
/// </summary>
Excel,
/// <summary>
/// Use linear interpolation between the two nearest ranks,
/// http://en.wikipedia.org/wiki/Percentile#Linear_Interpolation_Between_Closest_Ranks
/// </summary>
Interpolation
}
/// <summary>
/// Class to calculate percentiles.
/// </summary>
public class Percentile
{
/// <summary>
/// Holds the data.
/// </summary>
private readonly List<double> _data;
/// <summary>
/// Gets or sets the method used to calculate the percentiles.
/// </summary>
/// <value>The calculation method.</value>
/// <remarks>defaults to <see cref="PercentileMethod.Nist"/>.</remarks>
public PercentileMethod Method
{
get;
set;
}
/// <summary>
/// Initializes a new instance of the <see cref="Percentile"/> class.
/// </summary>
/// <param name="data">The data to calculate the percentiles of.</param>
public Percentile(IEnumerable<double> data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
if (data.Count() < 3)
{
throw new ArgumentException(string.Format(Properties.Resources.MustContainAtLeast, 3), "data");
}
_data = new List<double>(data);
_data.Sort();
}
/// <summary>
/// Computes the percentile.
/// </summary>
/// <param name="percentile">The percentile, must be between 0.0 and 1.0 (inclusive).</param>
/// <returns>the requested percentile.</returns>
public double Compute(double percentile)
{
if (percentile < 0 || percentile > 100)
{
throw new ArgumentException("Percentile value must be between 0 and 100.");
}
if (percentile == 0.0)
{
return _data[0];
}
if (percentile == 1.0)
{
return _data[_data.Count - 1];
}
var result = double.NaN;
switch (Method)
{
case PercentileMethod.Nist:
result = Nist(percentile);
break;
case PercentileMethod.Nearest:
result = Nearest(percentile);
break;
case PercentileMethod.Interpolation:
result = Interpolation(percentile);
break;
case PercentileMethod.Excel:
result = Excel(percentile);
break;
}
return result;
}
/// <summary>
/// Computes the percentiles for the given list.
/// </summary>
/// <param name="percentiles">The percentiles, must be between 0.0 and 1.0 (inclusive)</param>
/// <returns>the values that correspond to the given percentiles.</returns>
public IList<double> Compute(IEnumerable<double> percentiles)
{
if (percentiles == null)
{
throw new ArgumentNullException("percentiles");
}
return percentiles.Select(Compute).ToList();
}
/// <summary>
/// Computes the percentile using the nearest value.
/// </summary>
/// <param name="percentile">The percentile.</param>
/// <returns>the percentile using the nearest value.</returns>
private double Nearest(double percentile)
{
var n = (int)Math.Round((_data.Count * percentile) + 0.5, 0);
return _data[n - 1];
}
/// <summary>
/// Computes the percentile using Excel's method.
/// </summary>
/// <param name="percentile">The percentile.</param>
/// <returns>the percentile using Excel's method.</returns>
private double Excel(double percentile)
{
var tmp = 1 + (percentile * (_data.Count - 1.0));
var k = (int)tmp;
var d = tmp - k;
return _data[k - 1] + (d * (_data[k] - _data[k - 1]));
}
/// <summary>
/// Computes the percentile using interpolation.
/// </summary>
/// <param name="percentile">The percentile.</param>
/// <returns>the percentile using the interpolation.</returns>
private double Interpolation(double percentile)
{
var k = (int)(_data.Count * percentile);
var pk = (k - 0.5) / _data.Count;
return _data[k - 1] + (_data.Count * (percentile - pk) * (_data[k] - _data[k - 1]));
}
/// <summary>
/// Computes the percentile using NIST's method.
/// </summary>
/// <param name="percentile">The percentile.</param>
/// <returns>the percentile using NIST's method.</returns>
private double Nist(double percentile)
{
var tmp = percentile * (_data.Count + 1.0);
var k = (int)tmp;
var d = tmp - k;
return _data[k - 1] + (d * (_data[k] - _data[k - 1]));
}
}
}

3
src/Silverlight/Silverlight.csproj

@ -929,6 +929,9 @@
<Compile Include="..\Numerics\Statistics\MCMC\UnivariateSliceSampler.cs">
<Link>Statistics\MCMC\UnivariateSliceSampler.cs</Link>
</Compile>
<Compile Include="..\Numerics\Statistics\Percentile.cs">
<Link>Statistics\Percentile.cs</Link>
</Compile>
<Compile Include="..\Numerics\Statistics\Statistics.cs">
<Link>Statistics\Statistics.cs</Link>
</Compile>

116
src/UnitTests/LinearAlgebraTests/Complex/IO/MatlabWriterTests.cs

@ -0,0 +1,116 @@
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.IO
{
using System;
using System.IO;
using System.Numerics;
using LinearAlgebra.Complex;
using LinearAlgebra.Complex.IO;
using LinearAlgebra.IO;
using MbUnit.Framework;
[TestFixture]
public class MatlabMatrixWriterTests
{
[Test]
public void Constructor_ThrowsArgumentException()
{
Assert.Throws<ArgumentException>(() => new MatlabMatrixWriter(string.Empty));
Assert.Throws<ArgumentException>(() => new MatlabMatrixWriter(null));
}
[Test]
public void WriteMatrices_ThrowsArgumentException()
{
Matrix matrix = new DenseMatrix(1, 1);
var writer = new MatlabMatrixWriter("somefile3");
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix }, new[] { string.Empty }));
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix }, new string[] { null }));
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix, matrix }, new[] { "matrix" }));
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix }, new[] { "some matrix" }));
writer.Dispose();
}
[Test]
public void WriteMatrices_ThrowsArgumentNullException()
{
var writer = new MatlabMatrixWriter("somefile4");
Assert.Throws<ArgumentNullException>(() => writer.WriteMatrices(new Matrix[] { null }, new[] { "matrix" }));
Matrix matrix = new DenseMatrix(1, 1);
Assert.Throws<ArgumentNullException>(() => writer.WriteMatrices(new[] { matrix }, null));
writer.Dispose();
}
[Test]
public void WriteMatricesTest()
{
Matrix mat1 = new DenseMatrix(5, 3);
for (var i = 0; i < mat1.ColumnCount; i++)
{
mat1[i, i] = new Complex(i + .1, i + .1);
}
Matrix mat2 = new DenseMatrix(4, 5);
for (var i = 0; i < mat2.RowCount; i++)
{
mat2[i, i] = new Complex(i + .1, i + .1);
}
Matrix mat3 = new SparseMatrix(5, 4);
for (var i = 0; i < mat3.ColumnCount; i++)
{
mat3[i, i] = new Complex(i + .1, i + .1);
}
Matrix mat4 = new SparseMatrix(3, 5);
for (var i = 0; i < mat4.RowCount; i++)
{
mat4[i, i] = new Complex(i + .1, i + .1);
}
var write = new[] { mat1, mat2, mat3, mat4 };
var names = new[] { "mat1", "dense_matrix_2", "s1", "sparse2" };
if (File.Exists("test.mat"))
{
File.Delete("test.mat");
}
var writer = new MatlabMatrixWriter("test.mat");
writer.WriteMatrices(write, names);
writer.Dispose();
var reader = new MatlabMatrixReader("test.mat");
var read = reader.ReadMatrices(names);
Assert.AreEqual(write.Length, read.Count);
for (var i = 0; i < write.Length; i++ )
{
var w = write[i];
var r = read[names[i]];
Assert.AreEqual(w.RowCount, r.RowCount);
Assert.AreEqual(w.ColumnCount, r.ColumnCount);
Assert.IsTrue(w.Equals(r));
}
}
[Test]
public void WriteMatrix_ThrowsArgumentException()
{
Matrix matrix = new DenseMatrix(1, 1);
var writer = new MatlabMatrixWriter("somefile1");
Assert.Throws<ArgumentException>(() => writer.WriteMatrix(matrix, string.Empty));
Assert.Throws<ArgumentException>(() => writer.WriteMatrix(matrix, null));
writer.Dispose();
}
[Test]
public void WriteMatrix_ThrowsArgumentNullException()
{
var writer = new MatlabMatrixWriter("somefile2");
Assert.Throws<ArgumentNullException>(() => writer.WriteMatrix<double>(null, "matrix"));
writer.Dispose();
}
}
}

116
src/UnitTests/LinearAlgebraTests/Complex32/IO/MatlabWriterTests.cs

@ -0,0 +1,116 @@
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.IO
{
using System;
using System.IO;
using LinearAlgebra.Complex32;
using LinearAlgebra.Complex32.IO;
using LinearAlgebra.IO;
using MbUnit.Framework;
using Numerics;
[TestFixture]
public class MatlabMatrixWriterTests
{
[Test]
public void Constructor_ThrowsArgumentException()
{
Assert.Throws<ArgumentException>(() => new MatlabMatrixWriter(string.Empty));
Assert.Throws<ArgumentException>(() => new MatlabMatrixWriter(null));
}
[Test]
public void WriteMatrices_ThrowsArgumentException()
{
Matrix matrix = new DenseMatrix(1, 1);
var writer = new MatlabMatrixWriter("somefile3");
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix }, new[] { string.Empty }));
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix }, new string[] { null }));
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix, matrix }, new[] { "matrix" }));
Assert.Throws<ArgumentException>(() => writer.WriteMatrices(new[] { matrix }, new[] { "some matrix" }));
writer.Dispose();
}
[Test]
public void WriteMatrices_ThrowsArgumentNullException()
{
var writer = new MatlabMatrixWriter("somefile4");
Assert.Throws<ArgumentNullException>(() => writer.WriteMatrices(new Matrix[] { null }, new[] { "matrix" }));
Matrix matrix = new DenseMatrix(1, 1);
Assert.Throws<ArgumentNullException>(() => writer.WriteMatrices(new[] { matrix }, null));
writer.Dispose();
}
[Test]
public void WriteMatricesTest()
{
Matrix mat1 = new DenseMatrix(5, 3);
for (var i = 0; i < mat1.ColumnCount; i++)
{
mat1[i, i] = new Complex32(i + .1f, i + .1f);
}
Matrix mat2 = new DenseMatrix(4, 5);
for (var i = 0; i < mat2.RowCount; i++)
{
mat2[i, i] = new Complex32(i + .1f, i + .1f);
}
Matrix mat3 = new SparseMatrix(5, 4);
for (var i = 0; i < mat3.ColumnCount; i++)
{
mat3[i, i] = new Complex32(i + .1f, i + .1f);
}
Matrix mat4 = new SparseMatrix(3, 5);
for (var i = 0; i < mat4.RowCount; i++)
{
mat4[i, i] = new Complex32(i + .1f, i + .1f);
}
var write = new[] { mat1, mat2, mat3, mat4 };
var names = new[] { "mat1", "dense_matrix_2", "s1", "sparse2" };
if (File.Exists("test.mat"))
{
File.Delete("test.mat");
}
var writer = new MatlabMatrixWriter("test.mat");
writer.WriteMatrices(write, names);
writer.Dispose();
var reader = new MatlabMatrixReader("test.mat");
var read = reader.ReadMatrices(names);
Assert.AreEqual(write.Length, read.Count);
for (var i = 0; i < write.Length; i++ )
{
var w = write[i];
var r = read[names[i]];
Assert.AreEqual(w.RowCount, r.RowCount);
Assert.AreEqual(w.ColumnCount, r.ColumnCount);
Assert.IsTrue(w.Equals(r));
}
}
[Test]
public void WriteMatrix_ThrowsArgumentException()
{
Matrix matrix = new DenseMatrix(1, 1);
var writer = new MatlabMatrixWriter("somefile1");
Assert.Throws<ArgumentException>(() => writer.WriteMatrix(matrix, string.Empty));
Assert.Throws<ArgumentException>(() => writer.WriteMatrix(matrix, null));
writer.Dispose();
}
[Test]
public void WriteMatrix_ThrowsArgumentNullException()
{
var writer = new MatlabMatrixWriter("somefile2");
Assert.Throws<ArgumentNullException>(() => writer.WriteMatrix<double>(null, "matrix"));
writer.Dispose();
}
}
}

90
src/UnitTests/StatisticsTests/PercentileTests.cs

@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Text;
using Gallio.Framework;
using MbUnit.Framework;
using MbUnit.Framework.ContractVerifiers;
namespace MathNet.Numerics.UnitTests.StatisticsTests
{
using Statistics;
[TestFixture]
public class PercentileTests
{
private static readonly double[] _data = {
95.1772,
95.1567,
95.1937,
95.1959,
95.1442,
95.061,
95.1591,
95.1195,
95.1065,
95.0925,
95.199,
95.1682
};
[Test]
public void CanComputePercentileUsingNistMethod()
{
var percentile = new Percentile(_data);
percentile.Method = PercentileMethod.Nist;
Assert.AreEqual(95.19807, percentile.Compute(.9));
}
[Test]
public void CanComputePercentileUsingExcelMethod()
{
var percentile = new Percentile(_data);
percentile.Method = PercentileMethod.Excel;
Assert.AreEqual(95.19568, percentile.Compute(.9));
}
[Test]
public void CanComputePercentileUsingNearestMethod()
{
var percentile = new Percentile(_data);
percentile.Method = PercentileMethod.Nearest;
Assert.AreEqual(95.1959, percentile.Compute(.9));
}
[Test]
public void CanComputePercentileUsingInterpolationMethod()
{
var data = new double[] { 1, 2, 3, 4, 5 };
var percentile = new Percentile(data);
percentile.Method = PercentileMethod.Interpolation;
var values = new[] { .25, .5, .75 };
var percentiles = percentile.Compute(values);
Assert.AreEqual(1.75, percentiles[0]);
Assert.AreEqual(3.0, percentiles[1]);
Assert.AreEqual(4.25, percentiles[2]);
}
[Test]
public void SmallDataSetThrowArgumentException()
{
var data = new double[] { };
Assert.Throws<ArgumentException>(() => new Percentile(data));
data = new double[] { 1 };
Assert.Throws<ArgumentException>(() => new Percentile(data));
data = new double[] { 1, 2 };
Assert.Throws<ArgumentException>(() => new Percentile(data));
}
[Test]
public void InvalidPercentileValuesThrowArgumentExecption()
{
var percentile = new Percentile(_data);
Assert.Throws<ArgumentException>(() => percentile.Compute(-0.1));
Assert.Throws<ArgumentException>(() => percentile.Compute(100.1));
}
}
}

1
src/UnitTests/UnitTests.csproj

@ -362,6 +362,7 @@
<Compile Include="StatisticsTests\MCMCTests\MetropolisHastingsSamplerTests.cs" />
<Compile Include="StatisticsTests\MCMCTests\MetropolisSamplerTests.cs" />
<Compile Include="StatisticsTests\MCMCTests\RejectionSamplerTests.cs" />
<Compile Include="StatisticsTests\PercentileTests.cs" />
<Compile Include="StatisticsTests\StatisticsTests.cs" />
<Compile Include="StatisticsTests\StatTestData.cs" />
<Compile Include="StatisticsTests\MCMCTests\UnivariateSliceSamplerTests.cs" />

Loading…
Cancel
Save