Browse Source

Minor cleanup, contributors, tests

cuda
Christoph Ruegg 12 years ago
parent
commit
401b594aa4
  1. 1
      CONTRIBUTORS.md
  2. 28
      src/Numerics/Statistics/Correlation.cs
  3. 16
      src/UnitTests/StatisticsTests/CorrelationTests.cs

1
CONTRIBUTORS.md

@ -53,6 +53,7 @@ Feel free to add a link to your personal site/blog and/or twitter handle.*
- Sunny Ahuwanya - Sunny Ahuwanya
- Till Hoffmann - Till Hoffmann
- Tomas Petricek - Tomas Petricek
- ViK
- VicPara - VicPara
- bstrausser - bstrausser
- mjmckp - mjmckp

28
src/Numerics/Statistics/Correlation.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2013 Math.NET // Copyright (c) 2009-2014 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -74,17 +74,17 @@ namespace MathNet.Numerics.Statistics
double currentB = ieB.Current; double currentB = ieB.Current;
double deltaA = currentA - meanA; double deltaA = currentA - meanA;
double scaleDeltaA = deltaA / ++n; double scaleDeltaA = deltaA/++n;
double deltaB = currentB - meanB; double deltaB = currentB - meanB;
double scaleDeltaB = deltaB / n; double scaleDeltaB = deltaB/n;
meanA += scaleDeltaA; meanA += scaleDeltaA;
meanB += scaleDeltaB; meanB += scaleDeltaB;
varA += scaleDeltaA * deltaA * (n - 1); varA += scaleDeltaA*deltaA*(n - 1);
varB += scaleDeltaB * deltaB * (n - 1); varB += scaleDeltaB*deltaB*(n - 1);
r += (deltaA * deltaB * (n - 1)) / n; r += (deltaA*deltaB*(n - 1))/n;
} }
if (ieB.MoveNext()) if (ieB.MoveNext())
@ -93,7 +93,7 @@ namespace MathNet.Numerics.Statistics
} }
} }
return r / Math.Sqrt(varA * varB); return r/Math.Sqrt(varA*varB);
} }
/// <summary> /// <summary>
@ -138,18 +138,18 @@ namespace MathNet.Numerics.Statistics
double temp = sumWeight + wi; double temp = sumWeight + wi;
double deltaX = xi - meanA; double deltaX = xi - meanA;
double rX = deltaX * wi / temp; double rX = deltaX*wi/temp;
meanA += rX; meanA += rX;
varA += sumWeight * deltaX * rX; varA += sumWeight*deltaX*rX;
double deltaY = yi - meanB; double deltaY = yi - meanB;
double rY = deltaY * wi / temp; double rY = deltaY*wi/temp;
meanB += rY; meanB += rY;
varB += sumWeight * deltaY * rY; varB += sumWeight*deltaY*rY;
sumWeight = temp; sumWeight = temp;
covariance += deltaX * deltaY * (n - 1) * wi / n; covariance += deltaX*deltaY*(n - 1)*wi/n;
} }
if (ieB.MoveNext()) if (ieB.MoveNext())
{ {
@ -160,7 +160,7 @@ namespace MathNet.Numerics.Statistics
throw new ArgumentOutOfRangeException("weights", Resources.ArgumentArraysSameLength); throw new ArgumentOutOfRangeException("weights", Resources.ArgumentArraysSameLength);
} }
} }
return covariance / Math.Sqrt(varA * varB); return covariance/Math.Sqrt(varA*varB);
} }
/// <summary> /// <summary>
@ -225,7 +225,7 @@ namespace MathNet.Numerics.Statistics
return PearsonMatrix(vectors.Select(Rank).ToArray()); return PearsonMatrix(vectors.Select(Rank).ToArray());
} }
private static double[] Rank(IEnumerable<double> series) static double[] Rank(IEnumerable<double> series)
{ {
if (series == null) if (series == null)
{ {

16
src/UnitTests/StatisticsTests/CorrelationTests.cs

@ -89,6 +89,22 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests
AssertHelpers.AlmostEqual(covariance, direct, 14); 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> /// <summary>
/// Pearson correlation test fail. /// Pearson correlation test fail.
/// </summary> /// </summary>

Loading…
Cancel
Save