public void CalculatePearsonsRSquaredTest() { System.Random random = new System.Random(31415); int n = testData.GetLength(0); int cols = testData.GetLength(1); for (int c1 = 0; c1 < cols; c1++) { for (int c2 = c1 + 1; c2 < cols; c2++) { { double c1Scale = random.NextDouble() * 1E7; double c2Scale = random.NextDouble() * 1E7; IEnumerable <double> x = from rows in Enumerable.Range(0, n) select testData[rows, c1] * c1Scale; IEnumerable <double> y = from rows in Enumerable.Range(0, n) select testData[rows, c2] * c2Scale; double[] xs = x.ToArray(); double[] ys = y.ToArray(); double r2_alglib = alglib.pearsoncorrelation(xs, ys, n); r2_alglib *= r2_alglib; var r2Calculator = new OnlinePearsonsRSquaredCalculator(); for (int i = 0; i < n; i++) { r2Calculator.Add(xs[i], ys[i]); } double r2 = r2Calculator.RSquared; Assert.IsTrue(r2_alglib.IsAlmost(r2)); } } } }
public void CalculatePearsonsRSquaredOfConstantTest() { System.Random random = new System.Random(31415); int n = 12; int cols = testData.GetLength(1); for (int c1 = 0; c1 < cols; c1++) { double c1Scale = random.NextDouble() * 1E7; IEnumerable <double> x = from rows in Enumerable.Range(0, n) select testData[rows, c1] * c1Scale; IEnumerable <double> y = (new List <double>() { 150494407424305.47 }) .Concat(Enumerable.Repeat(150494407424305.47, n - 1)); double[] xs = x.ToArray(); double[] ys = y.ToArray(); double r2_alglib = alglib.pearsoncorrelation(xs, ys, n); r2_alglib *= r2_alglib; var r2Calculator = new OnlinePearsonsRSquaredCalculator(); for (int i = 0; i < n; i++) { r2Calculator.Add(xs[i], ys[i]); } double r2 = r2Calculator.RSquared; Assert.AreEqual(r2_alglib.ToString(), r2.ToString()); } }
public void CalculatePearsonsRSquaredTest() { System.Random random = new System.Random(31415); int n = testData.GetLength(0); int cols = testData.GetLength(1); for (int c1 = 0; c1 < cols; c1++) { for (int c2 = c1 + 1; c2 < cols; c2++) { { double c1Scale = random.NextDouble() * 1E7; double c2Scale = random.NextDouble() * 1E7; IEnumerable<double> x = from rows in Enumerable.Range(0, n) select testData[rows, c1] * c1Scale; IEnumerable<double> y = from rows in Enumerable.Range(0, n) select testData[rows, c2] * c2Scale; double[] xs = x.ToArray(); double[] ys = y.ToArray(); double r2_alglib = alglib.pearsoncorrelation(xs, ys, n); r2_alglib *= r2_alglib; var r2Calculator = new OnlinePearsonsRSquaredCalculator(); for (int i = 0; i < n; i++) { r2Calculator.Add(xs[i], ys[i]); } double r2 = r2Calculator.RSquared; Assert.IsTrue(r2_alglib.IsAlmost(r2)); } } } }
public void CalculatePearsonsRSquaredOfConstantTest() { System.Random random = new System.Random(31415); int n = 12; int cols = testData.GetLength(1); for (int c1 = 0; c1 < cols; c1++) { double c1Scale = random.NextDouble() * 1E7; IEnumerable<double> x = from rows in Enumerable.Range(0, n) select testData[rows, c1] * c1Scale; IEnumerable<double> y = (new List<double>() { 150494407424305.47 }) .Concat(Enumerable.Repeat(150494407424305.47, n - 1)); double[] xs = x.ToArray(); double[] ys = y.ToArray(); double r2_alglib = alglib.pearsoncorrelation(xs, ys, n); r2_alglib *= r2_alglib; var r2Calculator = new OnlinePearsonsRSquaredCalculator(); for (int i = 0; i < n; i++) { r2Calculator.Add(xs[i], ys[i]); } double r2 = r2Calculator.RSquared; Assert.AreEqual(r2_alglib.ToString(), r2.ToString()); } }