use of org.apache.commons.math3.stat.regression.SimpleRegression in project ta4j by ta4j.
the class SimpleLinearRegressionIndicatorTest method calculateLinearRegressionOn4Observations.
@Test
public void calculateLinearRegressionOn4Observations() {
SimpleLinearRegressionIndicator reg = new SimpleLinearRegressionIndicator(closePrice, 4);
assertDecimalEquals(reg.getValue(1), 20);
assertDecimalEquals(reg.getValue(2), 30);
SimpleRegression origReg = buildSimpleRegression(10, 20, 30, 40);
assertDecimalEquals(reg.getValue(3), 40);
assertDecimalEquals(reg.getValue(3), origReg.predict(3));
origReg = buildSimpleRegression(30, 40, 30, 40);
assertDecimalEquals(reg.getValue(5), origReg.predict(3));
origReg = buildSimpleRegression(30, 20, 30, 50);
assertDecimalEquals(reg.getValue(9), origReg.predict(3));
}
use of org.apache.commons.math3.stat.regression.SimpleRegression in project dhis2-core by dhis2.
the class ListGrid method addRegressionColumn.
@Override
public Grid addRegressionColumn(int columnIndex, boolean addHeader) {
verifyGridState();
SimpleRegression regression = new SimpleRegression();
List<Object> column = getColumn(columnIndex);
int index = 0;
for (Object value : column) {
if (value != null && !MathUtils.isEqual(Double.parseDouble(String.valueOf(value)), 0d)) {
regression.addData(index++, Double.parseDouble(String.valueOf(value)));
}
}
List<Object> regressionColumn = new ArrayList<>();
for (int i = 0; i < column.size(); i++) {
final double predicted = regression.predict(i);
if (!Double.isNaN(predicted)) {
regressionColumn.add(Precision.round(predicted, 1));
} else {
regressionColumn.add(null);
}
}
addColumn(regressionColumn);
if (addHeader && columnIndex < headers.size()) {
GridHeader header = headers.get(columnIndex);
if (header != null) {
GridHeader regressionHeader = new GridHeader(header.getName() + REGRESSION_SUFFIX, header.getColumn() + REGRESSION_SUFFIX, header.getValueType(), header.isHidden(), header.isMeta());
addHeader(regressionHeader);
}
}
return this;
}
use of org.apache.commons.math3.stat.regression.SimpleRegression in project cloudsim-plus by manoelcampos.
the class Regression method getRobustLoessParameterEstimates.
/**
* Gets the robust LOESS parameter estimates.
*
* @param y the dependent variable
* @return the robust loess parameter estimates
*/
public static double[] getRobustLoessParameterEstimates(final double... y) {
final double[] x = createIndependentArray(y.length);
final SimpleRegression tricubeRegression = newWeightedLinearRegression(x, y, getTricubeWeights(y.length));
final double[] residuals = new double[y.length];
for (int i = 0; i < y.length; i++) {
residuals[i] = y[i] - tricubeRegression.predict(x[i]);
}
final SimpleRegression tricubeBySqrRegression = newWeightedLinearRegression(x, y, getTricubeBisquareWeights(residuals));
final double[] estimates = tricubeBySqrRegression.regress().getParameterEstimates();
if (Double.isNaN(estimates[0]) || Double.isNaN(estimates[1])) {
return tricubeRegression.regress().getParameterEstimates();
}
return estimates;
}
Aggregations