//-------------------------------------------------------------- #region Creation & Cleanup //-------------------------------------------------------------- /// <summary> /// Creates the principal component analysis for the given list of points. /// </summary> /// <param name="points"> /// The list of data points. All points must have the same /// <see cref="VectorD.NumberOfElements"/>. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="points"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="points"/> is empty. /// </exception> public PrincipalComponentAnalysisD(IList <VectorD> points) { if (points == null) { throw new ArgumentNullException("points"); } if (points.Count == 0) { throw new ArgumentException("The list of points is empty."); } // Compute covariance matrix. MatrixD covarianceMatrix = StatisticsHelper.ComputeCovarianceMatrix(points); // Perform Eigenvalue decomposition. EigenvalueDecompositionD evd = new EigenvalueDecompositionD(covarianceMatrix); int numberOfElements = evd.RealEigenvalues.NumberOfElements; Variances = new VectorD(numberOfElements); V = new MatrixD(numberOfElements, numberOfElements); // Sort eigenvalues by decreasing value. // Since covarianceMatrix is symmetric, we have no imaginary eigenvalues. for (int i = 0; i < Variances.NumberOfElements; i++) { int index = evd.RealEigenvalues.IndexOfLargestElement; Variances[i] = evd.RealEigenvalues[index]; V.SetColumn(i, evd.V.GetColumn(index)); evd.RealEigenvalues[index] = double.NegativeInfinity; } }
public void Test2() { MatrixD a = new MatrixD(new double[,] {{ 0, 1, 2 }, { 1, 4, 3 }, { 2, 3, 5 }}); EigenvalueDecompositionD d = new EigenvalueDecompositionD(a); Assert.IsTrue(MatrixD.AreNumericallyEqual(a, d.V * d.D * d.V.Transposed)); }
public void Test1() { MatrixD a = new MatrixD(new double[,] {{ 1, -1, 4 }, { 3, 2, -1 }, { 2, 1, -1 }}); EigenvalueDecompositionD d = new EigenvalueDecompositionD(a); Assert.IsTrue(MatrixD.AreNumericallyEqual(a * d.V, d.V * d.D)); }
public void Test2() { MatrixD a = new MatrixD(new double[, ] { { 0, 1, 2 }, { 1, 4, 3 }, { 2, 3, 5 } }); EigenvalueDecompositionD d = new EigenvalueDecompositionD(a); Assert.IsTrue(MatrixD.AreNumericallyEqual(a, d.V * d.D * d.V.Transposed)); }
public void Test1() { MatrixD a = new MatrixD(new double[, ] { { 1, -1, 4 }, { 3, 2, -1 }, { 2, 1, -1 } }); EigenvalueDecompositionD d = new EigenvalueDecompositionD(a); Assert.IsTrue(MatrixD.AreNumericallyEqual(a * d.V, d.V * d.D)); }
public void TestWithNaNValues() { MatrixD a = new MatrixD(new[,] {{ 0, double.NaN, 2 }, { 1, 4, 3 }, { 2, 3, 5}}); var d = new EigenvalueDecompositionD(a); foreach (var element in d.RealEigenvalues.ToList()) Assert.IsNaN(element); foreach (var element in d.ImaginaryEigenvalues.ToList()) Assert.IsNaN(element); foreach (var element in d.V.ToList(MatrixOrder.RowMajor)) Assert.IsNaN(element); d = new EigenvalueDecompositionD(new MatrixD(4, 4, double.NaN)); foreach (var element in d.RealEigenvalues.ToList()) Assert.IsNaN(element); foreach (var element in d.ImaginaryEigenvalues.ToList()) Assert.IsNaN(element); foreach (var element in d.V.ToList(MatrixOrder.RowMajor)) Assert.IsNaN(element); }
public void TestWithNaNValues() { MatrixD a = new MatrixD(new[, ] { { 0, double.NaN, 2 }, { 1, 4, 3 }, { 2, 3, 5 } }); var d = new EigenvalueDecompositionD(a); foreach (var element in d.RealEigenvalues.ToList()) { Assert.IsNaN(element); } foreach (var element in d.ImaginaryEigenvalues.ToList()) { Assert.IsNaN(element); } foreach (var element in d.V.ToList(MatrixOrder.RowMajor)) { Assert.IsNaN(element); } d = new EigenvalueDecompositionD(new MatrixD(4, 4, double.NaN)); foreach (var element in d.RealEigenvalues.ToList()) { Assert.IsNaN(element); } foreach (var element in d.ImaginaryEigenvalues.ToList()) { Assert.IsNaN(element); } foreach (var element in d.V.ToList(MatrixOrder.RowMajor)) { Assert.IsNaN(element); } }