Skip to content

Commit

Permalink
NIFI-7516: Catch and log SingularMatrixExceptions in OrdinaryLeastSqu…
Browse files Browse the repository at this point in the history
…ares model (apache#4323)
  • Loading branch information
mattyb149 authored and agentdalecoper committed Jun 25, 2020
1 parent ab1ccdb commit 7c9776f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
Expand Up @@ -16,17 +16,17 @@
*/
package org.apache.nifi.controller.status.analytics.models;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.math3.linear.SingularMatrixException;
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression;
import org.apache.nifi.controller.status.analytics.StatusAnalyticsModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

/**
* <p>
* An implementation of the {@link StatusAnalyticsModel} that uses Ordinary Least Squares computation for regression.
Expand All @@ -46,9 +46,14 @@ public OrdinaryLeastSquares() {
@Override
public void learn(Stream<Double[]> features, Stream<Double> labels) {
double[] labelArray = ArrayUtils.toPrimitive(labels.toArray(Double[]::new));
double[][] featuresMatrix = features.map(feature -> ArrayUtils.toPrimitive(feature)).toArray(double[][]::new);
double[][] featuresMatrix = features.map(ArrayUtils::toPrimitive).toArray(double[][]::new);
this.olsModel.newSampleData(labelArray, featuresMatrix);
this.coefficients = olsModel.estimateRegressionParameters();
try {
this.coefficients = olsModel.estimateRegressionParameters();
} catch (SingularMatrixException sme) {
LOG.debug("The OLSMultipleLinearRegression model's matrix has no inverse (i.e. it is singular) so regression parameters can not be estimated at this time.");

}
}

@Override
Expand Down Expand Up @@ -76,8 +81,7 @@ public Double predictVariable(Integer predictVariableIndex, Map<Integer, Double>
double sumX = 0;
if (knownVariablesWithIndex.size() > 0) {
sumX = knownVariablesWithIndex.entrySet().stream().map(featureTuple -> coefficients[olsModel.isNoIntercept()
? featureTuple.getKey() : featureTuple.getKey() + 1] * featureTuple.getValue())
.collect(Collectors.summingDouble(Double::doubleValue));
? featureTuple.getKey() : featureTuple.getKey() + 1] * featureTuple.getValue()).mapToDouble(Double::doubleValue).sum();
}
return (label - intercept - sumX) / predictorCoeff;
}
Expand All @@ -89,10 +93,13 @@ public Map<String, Double> getScores() {
return null;
} else {
Map<String, Double> scores = new HashMap<>();
scores.put("rSquared", olsModel.calculateRSquared());
scores.put("totalSumOfSquares", olsModel.calculateTotalSumOfSquares());
try {
scores.put("rSquared", olsModel.calculateRSquared());
scores.put("totalSumOfSquares", olsModel.calculateTotalSumOfSquares());
} catch (SingularMatrixException sme) {
LOG.debug("The OLSMultipleLinearRegression model's matrix has no inverse (i.e. it is singular) so no scores can be calculated at this time.");
}
return scores;

}
}

Expand Down
Expand Up @@ -16,7 +16,7 @@
*/
package org.apache.nifi.controller.status.analytics.models;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

import java.util.Date;
import java.util.HashMap;
Expand All @@ -27,7 +27,7 @@
import org.apache.commons.math3.linear.SingularMatrixException;
import org.junit.Test;

public class TestOrdinaryLeastSqaures {
public class TestOrdinaryLeastSquares {


@Test
Expand All @@ -53,7 +53,8 @@ public void testConstantPrediction(){
} catch (SingularMatrixException sme){
exOccurred = true;
}
assertTrue(exOccurred);
// SingularMatrixException should not be thrown, it will instead be logged
assertFalse(exOccurred);

}

Expand Down Expand Up @@ -149,8 +150,4 @@ public void comparePredictions(){
assert(olsR2 > srR2);

}




}

0 comments on commit 7c9776f

Please sign in to comment.