public MatrixValue Function(MatrixValue M) { var ev = new Eigenvalues(M as MatrixValue); var m = new MatrixValue(ev.RealEigenvalues.Length, 1); for (var i = 1; i <= ev.RealEigenvalues.Length; i++) { m[i, 1] = new ScalarValue(ev.RealEigenvalues[i - 1], ev.ImagEigenvalues[i - 1]); } return(m); }
public ArgumentsValue Function(MatrixValue M) { var ev = new Eigenvalues(M as MatrixValue); var m = new MatrixValue(ev.RealEigenvalues.Length, 1); var n = ev.GetV(); for (var i = 1; i <= ev.RealEigenvalues.Length; i++) { m[i, 1] = new ScalarValue(ev.RealEigenvalues[i - 1], ev.ImagEigenvalues[i - 1]); } return(new ArgumentsValue(m, n)); }
public virtual void Compute() { if (!onlyCovarianceMatrixAvailable) { int rows; if (this.array != null) { rows = array.Length; // Center and standardize the source matrix double[][] matrix = Adjust(array, Overwrite); // Perform the Singular Value Decomposition (SVD) of the matrix var svd = new JaggedSingularValueDecomposition(matrix, computeLeftSingularVectors: true, computeRightSingularVectors: true, autoTranspose: true, inPlace: true); SingularValues = svd.Diagonal; // The principal components of 'Source' are the eigenvectors of Cov(Source). Thus if we // calculate the SVD of 'matrix' (which is Source standardized), the columns of matrix V // (right side of SVD) will be the principal components of Source. // The right singular vectors contains the principal components of the data matrix ComponentVectors = svd.RightSingularVectors.Transpose(); } else { rows = source.GetLength(0); // Center and standardize the source matrix #pragma warning disable 612, 618 double[,] matrix = Adjust(source, Overwrite); #pragma warning restore 612, 618 // Perform the Singular Value Decomposition (SVD) of the matrix var svd = new SingularValueDecomposition(matrix, computeLeftSingularVectors: true, computeRightSingularVectors: true, autoTranspose: true, inPlace: true); SingularValues = svd.Diagonal; // The principal components of 'Source' are the eigenvectors of Cov(Source). Thus if we // calculate the SVD of 'matrix' (which is Source standardized), the columns of matrix V // (right side of SVD) will be the principal components of Source. // The right singular vectors contains the principal components of the data matrix ComponentVectors = svd.RightSingularVectors.ToArray().Transpose(); // The left singular vectors contains the factor scores for the principal components } // Eigenvalues are the square of the singular values Eigenvalues = new double[SingularValues.Length]; for (int i = 0; i < SingularValues.Length; i++) { Eigenvalues[i] = SingularValues[i] * SingularValues[i] / (rows - 1); } } else { // We only have the covariance matrix. Compute the Eigenvalue decomposition var evd = new EigenvalueDecomposition(covarianceMatrix, assumeSymmetric: true, sort: true); // Gets the Eigenvalues and corresponding Eigenvectors Eigenvalues = evd.RealEigenvalues; var eigenvectors = evd.Eigenvectors.ToJagged(); SingularValues = Eigenvalues.Sqrt(); ComponentVectors = eigenvectors.Transpose(); } if (Whiten) { ComponentVectors = ComponentVectors.Transpose().Divide(Eigenvalues, dimension: 0).Transpose(); } CreateComponents(); if (!onlyCovarianceMatrixAvailable) { if (array != null) { result = Transform(array).ToMatrix(); } else if (source != null) { result = Transform(source.ToJagged()).ToMatrix(); } } }
/// <summary> /// Learns a model that can map the given inputs to the desired outputs. /// </summary> /// /// <param name="x">The model inputs.</param> /// <param name="weights">The weight of importance for each input sample.</param> /// /// <returns> /// A model that has learned how to produce suitable outputs /// given the input data <paramref name="x" />. /// </returns> /// public MultivariateLinearRegression Learn(double[][] x, double[] weights = null) { this.NumberOfInputs = x.Columns(); if (Method == PrincipalComponentMethod.Center || Method == PrincipalComponentMethod.Standardize) { this.Means = x.Mean(dimension: 0); double[][] matrix = Overwrite ? x : Jagged.CreateAs(x); x.Subtract(Means, dimension: 0, result: matrix); if (Method == PrincipalComponentMethod.Standardize) { this.StandardDeviations = x.StandardDeviation(Means); matrix.Divide(StandardDeviations, dimension: 0, result: matrix); } // The principal components of 'Source' are the eigenvectors of Cov(Source). Thus if we // calculate the SVD of 'matrix' (which is Source standardized), the columns of matrix V // (right side of SVD) will be the principal components of Source. // Perform the Singular Value Decomposition (SVD) of the matrix var svd = new JaggedSingularValueDecomposition(matrix, computeLeftSingularVectors: false, computeRightSingularVectors: true, autoTranspose: true, inPlace: true); SingularValues = svd.Diagonal; Eigenvalues = SingularValues.Pow(2); Eigenvalues.Divide(x.Rows() - 1, result: Eigenvalues); ComponentVectors = svd.RightSingularVectors.Transpose(); } else if (Method == PrincipalComponentMethod.CovarianceMatrix || Method == PrincipalComponentMethod.CorrelationMatrix) { // We only have the covariance matrix. Compute the Eigenvalue decomposition var evd = new JaggedEigenvalueDecomposition(x, assumeSymmetric: true, sort: true); // Gets the Eigenvalues and corresponding Eigenvectors Eigenvalues = evd.RealEigenvalues; SingularValues = Eigenvalues.Sqrt(); ComponentVectors = evd.Eigenvectors.Transpose(); } else { // The method type should have been validated before we even entered this section throw new InvalidOperationException("Invalid method, this should never happen: {0}".Format(Method)); } if (Whiten) { ComponentVectors.Divide(SingularValues, dimension: 1, result: ComponentVectors); } // Computes additional information about the analysis and creates the // object-oriented structure to hold the principal components found. CreateComponents(); return(CreateRegression()); }
public MultivariateLinearRegression Learn(double[][] x, double[] weights = null) { this.NumberOfInputs = x.Columns(); if (Method == PrincipalComponentMethod.Center || Method == PrincipalComponentMethod.Standardize) { if (weights == null) { this.Means = x.Mean(dimension: 0); double[][] matrix = Overwrite ? x : Jagged.CreateAs(x); x.Subtract(Means, dimension: (VectorType)0, result: matrix); if (Method == PrincipalComponentMethod.Standardize) { this.StandardDeviations = x.StandardDeviation(Means); matrix.Divide(StandardDeviations, dimension: (VectorType)0, result: matrix); } var svd = new JaggedSingularValueDecomposition(matrix, computeLeftSingularVectors: false, computeRightSingularVectors: true, autoTranspose: true, inPlace: true); SingularValues = svd.Diagonal; Eigenvalues = SingularValues.Pow(2); Eigenvalues.Divide(x.Rows() - 1, result: Eigenvalues); ComponentVectors = svd.RightSingularVectors.Transpose(); } else { this.Means = x.WeightedMean(weights: weights); double[][] matrix = Overwrite ? x : Jagged.CreateAs(x); x.Subtract(Means, dimension: (VectorType)0, result: matrix); if (Method == PrincipalComponentMethod.Standardize) { this.StandardDeviations = x.WeightedStandardDeviation(weights, Means); matrix.Divide(StandardDeviations, dimension: (VectorType)0, result: matrix); } double[,] cov = x.WeightedCovariance(weights, Means); var evd = new EigenvalueDecomposition(cov, assumeSymmetric: true, sort: true); Eigenvalues = evd.RealEigenvalues; SingularValues = Eigenvalues.Sqrt(); ComponentVectors = Jagged.Transpose(evd.Eigenvectors); } } else if (Method == PrincipalComponentMethod.CovarianceMatrix || Method == PrincipalComponentMethod.CorrelationMatrix) { if (weights != null) { throw new Exception(); } var evd = new JaggedEigenvalueDecomposition(x, assumeSymmetric: true, sort: true); Eigenvalues = evd.RealEigenvalues; SingularValues = Eigenvalues.Sqrt(); ComponentVectors = evd.Eigenvectors.Transpose(); } else { throw new InvalidOperationException("Invalid method, this should never happen: {0}".Format(Method)); } if (Whiten) { ComponentVectors.Divide(SingularValues, dimension: (VectorType)1, result: ComponentVectors); } CreateComponents(); return(CreateRegression()); }
public virtual void Compute() { if (!onlyCovarianceMatrixAvailable) { int rows; if (this.array != null) { rows = array.Length; double[][] matrix = Adjust(array, Overwrite); var svd = new JaggedSingularValueDecomposition(matrix, computeLeftSingularVectors: true, computeRightSingularVectors: true, autoTranspose: true, inPlace: true); SingularValues = svd.Diagonal; ComponentVectors = svd.RightSingularVectors.Transpose(); } else { rows = source.GetLength(0); #pragma warning disable 612, 618 double[,] matrix = Adjust(source, Overwrite); #pragma warning restore 612, 618 var svd = new SingularValueDecomposition(matrix, computeLeftSingularVectors: true, computeRightSingularVectors: true, autoTranspose: true, inPlace: true); SingularValues = svd.Diagonal; ComponentVectors = svd.RightSingularVectors.ToArray().Transpose(); } Eigenvalues = new double[SingularValues.Length]; for (int i = 0; i < SingularValues.Length; i++) { Eigenvalues[i] = SingularValues[i] * SingularValues[i] / (rows - 1); } } else { var evd = new EigenvalueDecomposition(covarianceMatrix, assumeSymmetric: true, sort: true); Eigenvalues = evd.RealEigenvalues; var eigenvectors = evd.Eigenvectors.ToJagged(); SingularValues = Eigenvalues.Sqrt(); ComponentVectors = eigenvectors.Transpose(); } if (Whiten) { ComponentVectors = ComponentVectors.Transpose().Divide(Eigenvalues, dimension: 0).Transpose(); } CreateComponents(); if (!onlyCovarianceMatrixAvailable) { if (array != null) { result = Transform(array).ToMatrix(); } else if (source != null) { result = Transform(source.ToJagged()).ToMatrix(); } } }
public MatrixValue Function(MatrixValue M) { var ev = new Eigenvalues(M as MatrixValue); return(ev.GetV()); }