/** * Returns the diagonal matrix Σ of the decomposition. * <p>Σ is a diagonal matrix. The singular values are provided in * non-increasing order, for compatibility with Jama.</p> * @return the Σ matrix */ public RealMatrix getS() { if (cachedS == null) { // cache the matrix for subsequent calls cachedS = MatrixUtils.createRealDiagonalMatrix(singularValues); } return cachedS; }
/** * Gets the block diagonal matrix D of the decomposition. * D is a block diagonal matrix. * Real eigenvalues are on the diagonal while complex values are on * 2x2 blocks { {real +imaginary}, {-imaginary, real} }. * * @return the D matrix. * * @see #getRealEigenvalues() * @see #getImagEigenvalues() */ public RealMatrix getD() { if (cachedD == null) { // cache the matrix for subsequent calls cachedD = MatrixUtils.createRealDiagonalMatrix(realEigenvalues); for (int i = 0; i < imagEigenvalues.length; i++) { if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) > 0) { cachedD.setEntry(i, i+1, imagEigenvalues[i]); } else if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) < 0) { cachedD.setEntry(i, i-1, imagEigenvalues[i]); } } } return cachedD; }
/** * Computes the square-root of the matrix. * This implementation assumes that the matrix is symmetric and positive * definite. * * @return the square-root of the matrix. * @throws MathUnsupportedOperationException if the matrix is not * symmetric or not positive definite. * @since 3.1 */ public RealMatrix getSquareRoot() { if (!isSymmetric) { throw new MathUnsupportedOperationException(); } final double[] sqrtEigenValues = new double[realEigenvalues.length]; for (int i = 0; i < realEigenvalues.length; i++) { final double eigen = realEigenvalues[i]; if (eigen <= 0) { throw new MathUnsupportedOperationException(); } sqrtEigenValues[i] = FastMath.sqrt(eigen); } final RealMatrix sqrtEigen = MatrixUtils.createRealDiagonalMatrix(sqrtEigenValues); final RealMatrix v = getV(); final RealMatrix vT = getVT(); return v.multiply(sqrtEigen).multiply(vT); }
/** * Returns the diagonal matrix Σ of the decomposition. * <p>Σ is a diagonal matrix. The singular values are provided in * non-increasing order, for compatibility with Jama.</p> * @return the Σ matrix */ public RealMatrix getS() { if (cachedS == null) { // cache the matrix for subsequent calls cachedS = MatrixUtils.createRealDiagonalMatrix(singularValues); } return cachedS; }
/** * Returns the diagonal matrix Σ of the decomposition. * <p>Σ is a diagonal matrix. The singular values are provided in * non-increasing order, for compatibility with Jama.</p> * @return the Σ matrix */ public RealMatrix getS() { if (cachedS == null) { // cache the matrix for subsequent calls cachedS = MatrixUtils.createRealDiagonalMatrix(singularValues); } return cachedS; }
/** * Gets the block diagonal matrix D of the decomposition. * D is a block diagonal matrix. * Real eigenvalues are on the diagonal while complex values are on * 2x2 blocks { {real +imaginary}, {-imaginary, real} }. * * @return the D matrix. * * @see #getRealEigenvalues() * @see #getImagEigenvalues() */ public RealMatrix getD() { if (cachedD == null) { // cache the matrix for subsequent calls cachedD = MatrixUtils.createRealDiagonalMatrix(realEigenvalues); for (int i = 0; i < imagEigenvalues.length; i++) { if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) > 0) { cachedD.setEntry(i, i+1, imagEigenvalues[i]); } else if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) < 0) { cachedD.setEntry(i, i-1, imagEigenvalues[i]); } } } return cachedD; }
/** * Gets the block diagonal matrix D of the decomposition. * D is a block diagonal matrix. * Real eigenvalues are on the diagonal while complex values are on * 2x2 blocks { {real +imaginary}, {-imaginary, real} }. * * @return the D matrix. * * @see #getRealEigenvalues() * @see #getImagEigenvalues() */ public RealMatrix getD() { if (cachedD == null) { // cache the matrix for subsequent calls cachedD = MatrixUtils.createRealDiagonalMatrix(realEigenvalues); for (int i = 0; i < imagEigenvalues.length; i++) { if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) > 0) { cachedD.setEntry(i, i+1, imagEigenvalues[i]); } else if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) < 0) { cachedD.setEntry(i, i-1, imagEigenvalues[i]); } } } return cachedD; }
/** * Gets the block diagonal matrix D of the decomposition. * D is a block diagonal matrix. * Real eigenvalues are on the diagonal while complex values are on * 2x2 blocks { {real +imaginary}, {-imaginary, real} }. * * @return the D matrix. * * @see #getRealEigenvalues() * @see #getImagEigenvalues() */ public RealMatrix getD() { if (cachedD == null) { // cache the matrix for subsequent calls cachedD = MatrixUtils.createRealDiagonalMatrix(realEigenvalues); for (int i = 0; i < imagEigenvalues.length; i++) { if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) > 0) { cachedD.setEntry(i, i+1, imagEigenvalues[i]); } else if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) < 0) { cachedD.setEntry(i, i-1, imagEigenvalues[i]); } } } return cachedD; }
/** * Computes the square-root of the matrix. * This implementation assumes that the matrix is symmetric and positive * definite. * * @return the square-root of the matrix. * @throws MathUnsupportedOperationException if the matrix is not * symmetric or not positive definite. * @since 3.1 */ public RealMatrix getSquareRoot() { if (!isSymmetric) { throw new MathUnsupportedOperationException(); } final double[] sqrtEigenValues = new double[realEigenvalues.length]; for (int i = 0; i < realEigenvalues.length; i++) { final double eigen = realEigenvalues[i]; if (eigen <= 0) { throw new MathUnsupportedOperationException(); } sqrtEigenValues[i] = Math.sqrt(eigen); } final RealMatrix sqrtEigen = MatrixUtils.createRealDiagonalMatrix(sqrtEigenValues); final RealMatrix v = getV(); final RealMatrix vT = getVT(); return v.multiply(sqrtEigen).multiply(vT); }
private double computeL(double mu) { double LPenalty = lpenalty * mu; SingularValueDecomposition svd = new SingularValueDecomposition(X.subtract(S)); double[] penalizedD = softThreshold(svd.getSingularValues(), LPenalty); RealMatrix D_matrix = MatrixUtils.createRealDiagonalMatrix(penalizedD); L = svd.getU().multiply(D_matrix).multiply(svd.getVT()); return sum(penalizedD) * LPenalty; }
private double computeL(double mu) { double LPenalty = lpenalty * mu; SingularValueDecomposition svd = new SingularValueDecomposition(X.subtract(S)); double[] penalizedD = softThreshold(svd.getSingularValues(), LPenalty); RealMatrix D_matrix = MatrixUtils.createRealDiagonalMatrix(penalizedD); L = svd.getU().multiply(D_matrix).multiply(svd.getVT()); return sum(penalizedD) * LPenalty; }
private double computeL(double mu) { double LPenalty = lpenalty * mu; SingularValueDecomposition svd = new SingularValueDecomposition(X.subtract(S)); double[] penalizedD = softThreshold(svd.getSingularValues(), LPenalty); RealMatrix D_matrix = MatrixUtils.createRealDiagonalMatrix(penalizedD); L = svd.getU().multiply(D_matrix).multiply(svd.getVT()); return sum(penalizedD) * LPenalty; }
private double computeL(double mu) { double LPenalty = lpenalty * mu; SingularValueDecomposition svd = new SingularValueDecomposition(X.subtract(S)); double[] penalizedD = softThreshold(svd.getSingularValues(), LPenalty); RealMatrix D_matrix = MatrixUtils.createRealDiagonalMatrix(penalizedD); L = svd.getU().multiply(D_matrix).multiply(svd.getVT()); return sum(penalizedD) * LPenalty; }
/** * Computes the square-root of the matrix. * This implementation assumes that the matrix is symmetric and positive * definite. * * @return the square-root of the matrix. * @throws MathUnsupportedOperationException if the matrix is not * symmetric or not positive definite. * @since 3.1 */ public RealMatrix getSquareRoot() { if (!isSymmetric) { throw new MathUnsupportedOperationException(); } final double[] sqrtEigenValues = new double[realEigenvalues.length]; for (int i = 0; i < realEigenvalues.length; i++) { final double eigen = realEigenvalues[i]; if (eigen <= 0) { throw new MathUnsupportedOperationException(); } sqrtEigenValues[i] = FastMath.sqrt(eigen); } final RealMatrix sqrtEigen = MatrixUtils.createRealDiagonalMatrix(sqrtEigenValues); final RealMatrix v = getV(); final RealMatrix vT = getVT(); return v.multiply(sqrtEigen).multiply(vT); }
/** * Computes the square-root of the matrix. * This implementation assumes that the matrix is symmetric and positive * definite. * * @return the square-root of the matrix. * @throws MathUnsupportedOperationException if the matrix is not * symmetric or not positive definite. * @since 3.1 */ public RealMatrix getSquareRoot() { if (!isSymmetric) { throw new MathUnsupportedOperationException(); } final double[] sqrtEigenValues = new double[realEigenvalues.length]; for (int i = 0; i < realEigenvalues.length; i++) { final double eigen = realEigenvalues[i]; if (eigen <= 0) { throw new MathUnsupportedOperationException(); } sqrtEigenValues[i] = FastMath.sqrt(eigen); } final RealMatrix sqrtEigen = MatrixUtils.createRealDiagonalMatrix(sqrtEigenValues); final RealMatrix v = getV(); final RealMatrix vT = getVT(); return v.multiply(sqrtEigen).multiply(vT); }
public void updateCoefficients(double l2penalty) { if (this.X_svd == null) { this.X_svd = new SingularValueDecomposition(X); } RealMatrix V = this.X_svd.getV(); double[] s = this.X_svd.getSingularValues(); RealMatrix U = this.X_svd.getU(); for (int i = 0; i < s.length; i++) { s[i] = s[i] / (s[i]*s[i] + l2penalty); } RealMatrix S = MatrixUtils.createRealDiagonalMatrix(s); RealMatrix Z = V.multiply(S).multiply(U.transpose()); this.coefficients = Z.operate(this.Y); this.fitted = this.X.operate(this.coefficients); double errorVariance = 0; for (int i = 0; i < residuals.length; i++) { this.residuals[i] = this.Y[i] - this.fitted[i]; errorVariance += this.residuals[i] * this.residuals[i]; } errorVariance = errorVariance / (X.getRowDimension() - X.getColumnDimension()); RealMatrix errorVarianceMatrix = MatrixUtils.createRealIdentityMatrix(this.Y.length).scalarMultiply(errorVariance); RealMatrix coefficientsCovarianceMatrix = Z.multiply(errorVarianceMatrix).multiply(Z.transpose()); this.standarderrors = getDiagonal(coefficientsCovarianceMatrix); }
public void updateCoefficients(double l2penalty) { if (this.X_svd == null) { this.X_svd = new SingularValueDecomposition(X); } RealMatrix V = this.X_svd.getV(); double[] s = this.X_svd.getSingularValues(); RealMatrix U = this.X_svd.getU(); for (int i = 0; i < s.length; i++) { s[i] = s[i] / (s[i]*s[i] + l2penalty); } RealMatrix S = MatrixUtils.createRealDiagonalMatrix(s); RealMatrix Z = V.multiply(S).multiply(U.transpose()); this.coefficients = Z.operate(this.Y); this.fitted = this.X.operate(this.coefficients); double errorVariance = 0; for (int i = 0; i < residuals.length; i++) { this.residuals[i] = this.Y[i] - this.fitted[i]; errorVariance += this.residuals[i] * this.residuals[i]; } errorVariance = errorVariance / (X.getRowDimension() - X.getColumnDimension()); RealMatrix errorVarianceMatrix = MatrixUtils.createRealIdentityMatrix(this.Y.length).scalarMultiply(errorVariance); RealMatrix coefficientsCovarianceMatrix = Z.multiply(errorVarianceMatrix).multiply(Z.transpose()); this.standarderrors = getDiagonal(coefficientsCovarianceMatrix); }