forked from tsai/mathnet-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.
144 lines
5.0 KiB
144 lines
5.0 KiB
// <copyright file="CorrelationTests.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-2013 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
|
|
{
|
|
#if !PORTABLE
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using NUnit.Framework;
|
|
using Statistics;
|
|
|
|
/// <summary>
|
|
/// Correlation tests
|
|
/// </summary>
|
|
/// <remarks>NOTE: this class is not included into Silverlight version, because it uses data from local files.
|
|
/// In Silverlight access to local files is forbidden, except several cases.</remarks>
|
|
[TestFixture, Category("Statistics")]
|
|
public class CorrelationTests
|
|
{
|
|
/// <summary>
|
|
/// Statistics data.
|
|
/// </summary>
|
|
readonly IDictionary<string, StatTestData> _data = new Dictionary<string, StatTestData>();
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the CorrelationTests class.
|
|
/// </summary>
|
|
public CorrelationTests()
|
|
{
|
|
var lottery = new StatTestData("./data/NIST/Lottery.dat");
|
|
_data.Add("lottery", lottery);
|
|
var lew = new StatTestData("./data/NIST/Lew.dat");
|
|
_data.Add("lew", lew);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pearson correlation test.
|
|
/// </summary>
|
|
[Test]
|
|
public void PearsonCorrelationTest()
|
|
{
|
|
var dataA = _data["lottery"].Data.Take(200);
|
|
var dataB = _data["lew"].Data.Take(200);
|
|
|
|
var corr = Correlation.Pearson(dataA, dataB);
|
|
AssertHelpers.AlmostEqual(-0.029470861580726, corr, 14);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pearson correlation test.
|
|
/// </summary>
|
|
[Test]
|
|
public void PearsonCorrelationConsistentWithCovariance()
|
|
{
|
|
var dataA = _data["lottery"].Data.Take(200).ToArray();
|
|
var dataB = _data["lew"].Data.Take(200).ToArray();
|
|
|
|
var direct = Correlation.Pearson(dataA, dataB);
|
|
var covariance = dataA.Covariance(dataB)/(dataA.StandardDeviation()*dataB.StandardDeviation());
|
|
|
|
AssertHelpers.AlmostEqual(covariance, direct, 14);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Constant-weighted Pearson correlation test.
|
|
/// </summary>
|
|
[Test]
|
|
public void ConstantWeightedPearsonCorrelationTest()
|
|
{
|
|
var dataA = _data["lottery"].Data.Take(200);
|
|
var dataB = _data["lew"].Data.Take(200);
|
|
var weights = Generate.Repeat(200, 2.0);
|
|
|
|
var corr = Correlation.Pearson(dataA, dataB);
|
|
var corr2 = Correlation.WeightedPearson(dataA, dataB, weights);
|
|
AssertHelpers.AlmostEqual(corr, corr2, 14);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pearson correlation test fail.
|
|
/// </summary>
|
|
[Test]
|
|
public void PearsonCorrelationTestFail()
|
|
{
|
|
var dataA = _data["lottery"].Data;
|
|
var dataB = _data["lew"].Data;
|
|
Assert.That(() => Correlation.Pearson(dataA, dataB), Throws.TypeOf<ArgumentOutOfRangeException>());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Spearman correlation test.
|
|
/// </summary>
|
|
[Test]
|
|
public void SpearmanCorrelationTest()
|
|
{
|
|
var dataA = _data["lottery"].Data.Take(200);
|
|
var dataB = _data["lew"].Data.Take(200);
|
|
|
|
var corr = Correlation.Spearman(dataA, dataB);
|
|
AssertHelpers.AlmostEqual(-0.0382856977898528, corr, 14);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Spearman correlation test fail.
|
|
/// </summary>
|
|
[Test]
|
|
public void SpearmanCorrelationTestFail()
|
|
{
|
|
var dataA = _data["lottery"].Data;
|
|
var dataB = _data["lew"].Data;
|
|
Assert.That(() => Correlation.Spearman(dataA, dataB), Throws.TypeOf<ArgumentOutOfRangeException>());
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
|