// // 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. // namespace MathNet.Numerics.UnitTests.StatisticsTests { #if !PORTABLE using System; using System.Collections.Generic; using System.Linq; using NUnit.Framework; using Statistics; /// /// Correlation tests /// /// 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. [TestFixture, Category("Statistics")] public class CorrelationTests { /// /// Statistics data. /// readonly IDictionary _data = new Dictionary(); /// /// Initializes a new instance of the CorrelationTests class. /// 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); } /// /// Pearson correlation test. /// [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); } /// /// Pearson correlation test. /// [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); } /// /// Constant-weighted Pearson correlation test. /// [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); } /// /// Pearson correlation test fail. /// [Test] public void PearsonCorrelationTestFail() { var dataA = _data["lottery"].Data; var dataB = _data["lew"].Data; Assert.That(() => Correlation.Pearson(dataA, dataB), Throws.TypeOf()); } /// /// Spearman correlation test. /// [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); } /// /// Spearman correlation test fail. /// [Test] public void SpearmanCorrelationTestFail() { var dataA = _data["lottery"].Data; var dataB = _data["lew"].Data; Assert.That(() => Correlation.Spearman(dataA, dataB), Throws.TypeOf()); } } #endif }