@Override public UpperSPDDenseMatrix copy() { return new UpperSPDDenseMatrix(this); }
@Override public Matrix solve(Matrix B, Matrix X) { return SPDsolve(B, X); }
/** * Computes the estimate of the covariance matrix. * * @param matrix A multi-dimensional array containing the matrix values (assumed to contain at least one row). * @param weights The observation weights, normalized to sum to 1. * @param mean The values' mean vector. * @return The covariance matrix, including the ridge. */ private UpperSPDDenseMatrix weightedCovariance(double[][] matrix, DenseVector weights, Vector mean) { int rows = matrix.length; int cols = matrix[0].length; if (mean.size() != cols) { throw new IllegalArgumentException("Length of the mean vector must match matrix."); } // Create matrix with centered transposed data, weighted appropriately DenseMatrix transposed = new DenseMatrix(cols, rows); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { transposed.set(j, i, Math.sqrt(weights.get(i)) * (matrix[i][j] - mean.get(j))); } } UpperSPDDenseMatrix covT = (UpperSPDDenseMatrix) new UpperSPDDenseMatrix(cols).rank1(transposed); for (int i = 0; i < cols; i++) { covT.add(i, i, m_Ridge); } return covT; }
Matrix aTa = new UpperSPDDenseMatrix(numAttributes).rank1(independentTransposed); independentTransposed = null; dependent = null;
/** * Returns the log of the density value for the given vector. * * @param valuePassed input vector * @return log density based on given distribution */ @Override public double logDensity(double[] valuePassed) { // calculate mean subtractions Vector x = new DenseVector(valuePassed); return lnconstant - 0.5 * x.dot(covarianceInverse.mult(x.add(-1.0, mean), new DenseVector(x.size()))); }
/** * Computes the estimate of the covariance matrix. * * @param matrix A multi-dimensional array containing the matrix values (assumed to contain at least one row). * @param weights The observation weights, normalized to sum to 1. * @param mean The values' mean vector. * @return The covariance matrix, including the ridge. */ private UpperSPDDenseMatrix weightedCovariance(double[][] matrix, DenseVector weights, Vector mean) { int rows = matrix.length; int cols = matrix[0].length; if (mean.size() != cols) { throw new IllegalArgumentException("Length of the mean vector must match matrix."); } // Create matrix with centered transposed data, weighted appropriately DenseMatrix transposed = new DenseMatrix(cols, rows); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { transposed.set(j, i, Math.sqrt(weights.get(i)) * (matrix[i][j] - mean.get(j))); } } UpperSPDDenseMatrix covT = (UpperSPDDenseMatrix) new UpperSPDDenseMatrix(cols).rank1(transposed); for (int i = 0; i < cols; i++) { covT.add(i, i, m_Ridge); } return covT; }
Matrix aTa = new UpperSPDDenseMatrix(numAttributes).rank1(independentTransposed); independentTransposed = null; dependent = null;
/** * Returns the log of the density value for the given vector. * * @param valuePassed input vector * @return log density based on given distribution */ @Override public double logDensity(double[] valuePassed) { // calculate mean subtractions Vector x = new DenseVector(valuePassed); return lnconstant - 0.5 * x.dot(covarianceInverse.mult(x.add(-1.0, mean), new DenseVector(x.size()))); }
@Override public UpperSPDDenseMatrix copy() { return new UpperSPDDenseMatrix(this); }
@Override public Matrix solve(Matrix B, Matrix X) { return SPDsolve(B, X); }
/** * Calculates a Cholesky decomposition * * @param A * Matrix to decompose. Not modified * @return The current decomposition */ public static DenseCholesky factorize(Matrix A) { return new DenseCholesky(A.numRows(), true) .factor(new UpperSPDDenseMatrix(A)); }
/** * Calculates a Cholesky decomposition * * @param A * Matrix to decompose. Not modified * @return The current decomposition */ public static DenseCholesky factorize(Matrix A) { return new DenseCholesky(A.numRows(), true) .factor(new UpperSPDDenseMatrix(A)); }
/** * Generates the estimator based on the given observations and weight vector. * Equal weights are assumed if the weight vector is null. */ @Override public void estimate(double[][] observations, double[] weights) { if (weights == null) { weights = new double[observations.length]; for (int i = 0; i < weights.length; i++) { weights[i] = 1.0; } } DenseVector weightVector = new DenseVector(weights); weightVector = weightVector.scale(1.0 / weightVector.norm(Vector.Norm.One)); mean = weightedMean(observations, weightVector); Matrix cov = weightedCovariance(observations, weightVector, mean); // Compute inverse of covariance matrix DenseCholesky chol = new DenseCholesky(observations[0].length, true).factor((UpperSPDDenseMatrix)cov); covarianceInverse = new UpperSPDDenseMatrix(chol.solve(Matrices.identity(observations[0].length))); double logDeterminant = 0; for (int i = 0; i < observations[0].length; i++) { logDeterminant += Math.log(chol.getU().get(i, i)); } logDeterminant *= 2; lnconstant = -(Log2PI * observations[0].length + logDeterminant) * 0.5; }
Matrix cov = new UpperSPDDenseMatrix(m); mean = new DenseVector(groupMean[0].size()); for (int i = 0; i < c; i++) { covarianceInverse = new UpperSPDDenseMatrix(chol.solve(Matrices.identity(m)));
/** * Generates the estimator based on the given observations and weight vector. * Equal weights are assumed if the weight vector is null. */ @Override public void estimate(double[][] observations, double[] weights) { if (weights == null) { weights = new double[observations.length]; for (int i = 0; i < weights.length; i++) { weights[i] = 1.0; } } DenseVector weightVector = new DenseVector(weights); weightVector = weightVector.scale(1.0 / weightVector.norm(Vector.Norm.One)); mean = weightedMean(observations, weightVector); Matrix cov = weightedCovariance(observations, weightVector, mean); // Compute inverse of covariance matrix DenseCholesky chol = new DenseCholesky(observations[0].length, true).factor((UpperSPDDenseMatrix)cov); covarianceInverse = new UpperSPDDenseMatrix(chol.solve(Matrices.identity(observations[0].length))); double logDeterminant = 0; for (int i = 0; i < observations[0].length; i++) { logDeterminant += Math.log(chol.getU().get(i, i)); } logDeterminant *= 2; lnconstant = -(Log2PI * observations[0].length + logDeterminant) * 0.5; }
Matrix cov = new UpperSPDDenseMatrix(m); mean = new DenseVector(groupMean[0].size()); for (int i = 0; i < c; i++) { covarianceInverse = new UpperSPDDenseMatrix(chol.solve(Matrices.identity(m)));
m_L = new UpperSPDDenseMatrix(n); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { m_L = new UpperSPDDenseMatrix(m_L); // Convert from DenseMatrix
m_L = new UpperSPDDenseMatrix(n); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { m_L = new UpperSPDDenseMatrix(m_L); // Convert from DenseMatrix