use of org.apache.ignite.ml.math.exceptions.math.SingularMatrixException in project ignite by apache.
the class LUDecomposition method solve.
/**
* @param b Vector to solve using this decomposition.
* @return Solution vector.
*/
public Vector solve(Vector b) {
final int m = pivot.size();
if (b.size() != m)
throw new CardinalityException(b.size(), m);
if (singular)
throw new SingularMatrixException();
final double[] bp = new double[m];
// Apply permutations to b
for (int row = 0; row < m; row++) bp[row] = b.get((int) pivot.get(row));
// Solve LY = b
for (int col = 0; col < m; col++) {
final double bpCol = bp[col];
for (int i = col + 1; i < m; i++) bp[i] -= bpCol * lu.get(i, col);
}
// Solve UX = Y
for (int col = m - 1; col >= 0; col--) {
bp[col] /= lu.get(col, col);
final double bpCol = bp[col];
for (int i = 0; i < col; i++) bp[i] -= bpCol * lu.get(i, col);
}
return b.like(m).assign(bp);
}
use of org.apache.ignite.ml.math.exceptions.math.SingularMatrixException in project ignite by apache.
the class GmmTrainer method updateModel.
/**
* Gets older model and returns updated model on given data.
*
* @param dataset Dataset.
* @param model Model.
* @return Updated model.
*/
@NotNull
private UpdateResult updateModel(Dataset<EmptyContext, GmmPartitionData> dataset, GmmModel model) {
boolean isConverged = false;
int countOfIterations = 0;
double maxProbInDataset = Double.NEGATIVE_INFINITY;
while (!isConverged) {
MeanWithClusterProbAggregator.AggregatedStats stats = MeanWithClusterProbAggregator.aggreateStats(dataset, countOfComponents);
Vector clusterProbs = stats.clusterProbabilities();
Vector[] newMeans = stats.means().toArray(new Vector[countOfComponents]);
A.ensure(newMeans.length == model.countOfComponents(), "newMeans.size() == count of components");
A.ensure(newMeans[0].size() == initialMeans[0].size(), "newMeans[0].size() == initialMeans[0].size()");
List<Matrix> newCovs = CovarianceMatricesAggregator.computeCovariances(dataset, clusterProbs, newMeans);
try {
List<MultivariateGaussianDistribution> components = buildComponents(newMeans, newCovs);
GmmModel newModel = new GmmModel(clusterProbs, components);
countOfIterations += 1;
isConverged = isConverged(model, newModel) || countOfIterations > maxCountOfIterations;
model = newModel;
maxProbInDataset = GmmPartitionData.updatePcxiAndComputeLikelihood(dataset, clusterProbs, components);
} catch (SingularMatrixException | IllegalArgumentException e) {
String msg = "Cannot construct non-singular covariance matrix by data. " + "Try to select other initial means or other model trainer. Iterations will stop.";
environment.logger().log(MLLogger.VerboseLevel.HIGH, msg);
isConverged = true;
}
}
return new UpdateResult(model, maxProbInDataset);
}
use of org.apache.ignite.ml.math.exceptions.math.SingularMatrixException in project ignite by apache.
the class LUDecomposition method solve.
/**
* @param b Matrix to solve using this decomposition.
* @return Solution matrix.
*/
public Matrix solve(Matrix b) {
final int m = pivot.size();
if (b.rowSize() != m)
throw new CardinalityException(b.rowSize(), m);
if (singular)
throw new SingularMatrixException();
final int nColB = b.columnSize();
// Apply permutations to b
final double[][] bp = new double[m][nColB];
for (int row = 0; row < m; row++) {
final double[] bpRow = bp[row];
final int pRow = (int) pivot.get(row);
for (int col = 0; col < nColB; col++) bpRow[col] = b.get(pRow, col);
}
// Solve LY = b
for (int col = 0; col < m; col++) {
final double[] bpCol = bp[col];
for (int i = col + 1; i < m; i++) {
final double[] bpI = bp[i];
final double luICol = lu.get(i, col);
for (int j = 0; j < nColB; j++) bpI[j] -= bpCol[j] * luICol;
}
}
// Solve UX = Y
for (int col = m - 1; col >= 0; col--) {
final double[] bpCol = bp[col];
final double luDiag = lu.getX(col, col);
for (int j = 0; j < nColB; j++) bpCol[j] /= luDiag;
for (int i = 0; i < col; i++) {
final double[] bpI = bp[i];
final double luICol = lu.get(i, col);
for (int j = 0; j < nColB; j++) bpI[j] -= bpCol[j] * luICol;
}
}
return b.like(b.rowSize(), b.columnSize()).assign(bp);
}
Aggregations