Math.NET Numerics
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

364 lines
11 KiB

// <copyright file="HistogramTests.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.UnitTests.StatisticsTests
{
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Statistics;
/// <summary>
/// Histogram tests.
/// </summary>
[TestFixture]
public class HistogramTests
{
/// <summary>
/// Datatset array.
/// </summary>
private readonly double[] _smallDataset = { 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5 };
/// <summary>
/// Can create empty bucket.
/// </summary>
[Test]
public void CanCreateEmptyBucket()
{
new Bucket(0.0, 1.0);
}
/// <summary>
/// Can create filled bucket.
/// </summary>
[Test]
public void CanCreateFilledBucket()
{
new Bucket(0.0, 1.0, 10.0);
}
/// <summary>
/// Empty bucket with bad bounds fails.
/// </summary>
[Test]
public void EmptyBucketWithBadBoundsFails()
{
Assert.Throws<ArgumentOutOfRangeException>(() => new Bucket(1.0, 0.5));
}
/// <summary>
/// Empty bucket with bad count fails.
/// </summary>
[Test]
public void EmptyBucketWithBadCountFails()
{
Assert.Throws<ArgumentOutOfRangeException>(() => new Bucket(1.0, 0.5, -1.0));
}
/// <summary>
/// Can get bucket width.
/// </summary>
[Test]
public void CanGetBucketWidth()
{
var b = new Bucket(0.0, 1.0, 10.0);
Assert.AreEqual(1.0, b.Width);
}
/// <summary>
/// Can get bucket count.
/// </summary>
[Test]
public void CanGetBucketCount()
{
var b = new Bucket(0.0, 1.0, 10.0);
Assert.AreEqual(10.0, b.Count);
}
/// <summary>
/// Can get bucket lower bound.
/// </summary>
[Test]
public void CanGetBucketLowerBound()
{
var b = new Bucket(0.0, 1.0, 10.0);
Assert.AreEqual(0.0, b.LowerBound);
}
/// <summary>
/// Can get bucket upper bound.
/// </summary>
[Test]
public void CanGetBucketUpperBound()
{
var b = new Bucket(0.0, 1.0, 10.0);
Assert.AreEqual(1.0, b.UpperBound);
}
/// <summary>
/// Validate contains.
/// </summary>
/// <param name="x">Point values.</param>
/// <param name="r">Expected result.</param>
[Test, Sequential]
public void ValidateContains([Values(0.0, 1.0, 1.05, 2.0, -1.0)] double x, [Values(-1, 0, 0, 1, 0 - 1)] int r)
{
var b = new Bucket(0.0, 1.5, 10.0);
Assert.AreEqual(r, b.Contains(x));
}
/// <summary>
/// Can create empty histogram.
/// </summary>
[Test]
public void CanCreateEmptyHistogram()
{
new Histogram();
}
/// <summary>
/// Can add bucket.
/// </summary>
[Test]
public void CanAddBucket()
{
var h = new Histogram();
h.AddBucket(new Bucket(0.0, 1.0));
}
/// <summary>
/// Can get bucket index of.
/// </summary>
/// <param name="x">Point to check.</param>
/// <param name="i">Bucket index.</param>
[Test, Sequential]
public void CanGetBucketIndexOf([Values(0.5, 1.0, 10.0, 10000.0)] double x, [Values(0, 0, 3, 4)] double i)
{
var h = new Histogram();
h.AddBucket(new Bucket(0.0, 1.0));
h.AddBucket(new Bucket(1.0, 2.0));
h.AddBucket(new Bucket(2.0, 3.0));
h.AddBucket(new Bucket(3.0, 20.0));
h.AddBucket(new Bucket(20.0, Double.PositiveInfinity));
Assert.AreEqual(i, h.GetBucketIndexOf(x));
}
/// <summary>
/// Can get bucket index of fails when bucket doesn't exist.
/// </summary>
[Test]
public void CanGetBucketIndexOfFailsWhenBucketDoesNotExist()
{
var h = new Histogram();
h.AddBucket(new Bucket(0.0, 1.0));
h.AddBucket(new Bucket(1.0, 2.0));
h.AddBucket(new Bucket(2.0, 3.0));
h.AddBucket(new Bucket(3.0, 20.0));
h.AddBucket(new Bucket(20.0, Double.PositiveInfinity));
Assert.Throws<ArgumentException>(() => { var i = h.GetBucketIndexOf(0.0); });
Assert.Throws<ArgumentException>(() => { var i = h.GetBucketIndexOf(-1.0); });
}
/// <summary>
/// Can get bucket of.
/// </summary>
[Test]
public void CanGetBucketOf()
{
var h = new Histogram();
var b = new Bucket(0.0, 1.0);
h.AddBucket(b);
h.AddBucket(new Bucket(1.0, 2.0));
h.AddBucket(new Bucket(2.0, 3.0));
h.AddBucket(new Bucket(3.0, 20.0));
h.AddBucket(new Bucket(20.0, Double.PositiveInfinity));
Assert.AreEqual(b, h.GetBucketOf(0.1));
}
/// <summary>
/// Validate item.
/// </summary>
[Test]
public void ValidateItem()
{
var h = new Histogram();
var b = new Bucket(0.0, 1.0);
var c = new Bucket(3.0, 20.0);
h.AddBucket(b);
h.AddBucket(c);
h.AddBucket(new Bucket(1.0, 2.0));
h.AddBucket(new Bucket(2.0, 3.0));
h.AddBucket(new Bucket(20.0, Double.PositiveInfinity));
Assert.AreEqual(b, h[0]);
Assert.AreEqual(c, h[3]);
}
/// <summary>
/// Can get bucket count in histogram.
/// </summary>
[Test]
public void CanGetBucketCountInHistogram()
{
var h = new Histogram();
h.AddBucket(new Bucket(0.0, 1.0));
h.AddBucket(new Bucket(1.0, 2.0));
h.AddBucket(new Bucket(2.0, 3.0));
h.AddBucket(new Bucket(3.0, 20.0));
h.AddBucket(new Bucket(20.0, Double.PositiveInfinity));
Assert.AreEqual(5, h.BucketCount);
}
/// <summary>
/// Can get total count.
/// </summary>
[Test]
public void CanGetTotalCount()
{
var h = new Histogram();
h.AddBucket(new Bucket(0.0, 1.0, 1));
h.AddBucket(new Bucket(1.0, 2.0, 1));
h.AddBucket(new Bucket(2.0, 3.0, 1));
h.AddBucket(new Bucket(3.0, 20.0, 1));
h.AddBucket(new Bucket(20.0, Double.PositiveInfinity, 1));
Assert.AreEqual(5, h.DataCount);
}
/// <summary>
/// Can create equal spaced histogram.
/// </summary>
[Test]
public void CanCreateEqualSpacedHistogram()
{
new Histogram(new[] { 1.0, 5.0, 10.0 }, 2);
}
/// <summary>
/// Fail create equal spaced histogram with no data.
/// </summary>
[Test]
public void FailCreateEqualSpacedHistogramWithNoData()
{
Assert.Throws<ArgumentException>(() => new Histogram(new List<double>(), 10));
}
/// <summary>
/// Can create equal spaced histogram with given lower and upper bounds.
/// </summary>
[Test]
public void CanCreateEqualSpacedHistogramWithGivenLowerAndUpperBound()
{
new Histogram(new[] { 1.0, 5.0, 10.0 }, 2, 0.0, 20.0);
}
/// <summary>
/// Can add data single.
/// </summary>
[Test]
public void CanAddDataSingle()
{
var h = new Histogram(new[] { 1.0, 5.0, 10.0 }, 2);
h.AddData(7.0);
Assert.AreEqual(2, h[1].Count);
}
/// <summary>
/// Can add data list.
/// </summary>
[Test]
public void CanAddDataList()
{
var h = new Histogram(new[] { 1.0, 5.0, 10.0 }, 2);
h.AddData(new[] { 7.0, 8.0 });
Assert.AreEqual(3, h[1].Count);
}
/// <summary>
/// Add data increase upper bound.
/// </summary>
[Test]
public void AddDataIncreasesUpperBound()
{
var h = new Histogram(new[] { 1.0, 5.0, 10.0 }, 2);
h.AddData(20.0);
Assert.AreEqual(2, h[1].Count);
}
/// <summary>
/// Add data decrease lower bound.
/// </summary>
[Test]
public void AddDataDecreasesLowerBound()
{
var h = new Histogram(new[] { 1.0, 5.0, 10.0 }, 2);
h.AddData(0.0);
Assert.AreEqual(3, h[0].Count);
}
/// <summary>
/// Small dataset histogram without bounds.
/// </summary>
[Test]
public void SmallDatasetHistogramWithoutBounds()
{
var hist = new Histogram(_smallDataset, 9);
Assert.AreEqual(9, hist.BucketCount);
for (var i = 1; i < 9; i++)
{
Assert.AreEqual(1.0, hist[i].Count);
}
Assert.AreEqual(2.0, hist[0].Count);
Assert.AreEqual(0.5.Decrement(), hist.LowerBound);
Assert.AreEqual(9.5, hist.UpperBound);
}
/// <summary>
/// Small dataset histogram with bounds.
/// </summary>
[Test]
public void SmallDatasetHistogramWithBounds()
{
var hist = new Histogram(_smallDataset, 10, 0.0, 10.0);
Assert.AreEqual(10, hist.BucketCount);
for (var i = 0; i < 10; i++)
{
Assert.AreEqual(1.0, hist[i].Count);
}
Assert.AreEqual(0.0, hist.LowerBound);
Assert.AreEqual(10.0, hist.UpperBound);
}
}
}