Java Code Examples for org.apache.commons.math3.stat.regression.SimpleRegression#predict()

The following examples show how to use org.apache.commons.math3.stat.regression.SimpleRegression#predict() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PredictionData.java    From OpenLibre with GNU General Public License v3.0 6 votes vote down vote up
private void makePrediction(List<GlucoseData> trendList) {
    if (trendList.size() == 0) {
        return;
    }
    regression = new SimpleRegression();
    for (int i = 0; i < trendList.size(); i++) {
        regression.addData(i, (trendList.get(i)).getGlucoseLevelRaw());
    }
    int glucoseLevelRaw =
            (int) regression.predict(regression.getN() - 1 + PREDICTION_TIME);
    glucoseSlopeRaw = regression.getSlope();
    confidenceInterval = regression.getSlopeConfidenceInterval();
    int ageInSensorMinutes =
            trendList.get(trendList.size() - 1).getAgeInSensorMinutes() + PREDICTION_TIME;
    glucoseData = new GlucoseData(trendList.get(0).getSensor(), ageInSensorMinutes, trendList.get(0).getTimezoneOffsetInMinutes(), glucoseLevelRaw, true);
}
 
Example 2
Source File: AlgorithmUtil.java    From LibreAlarm with GNU General Public License v3.0 6 votes vote down vote up
@NonNull
private static PredictionData getPredictionData(int attempt, String tagId, ArrayList<GlucoseData> trendList) {
    PredictionData predictedGlucose = new PredictionData();
    SimpleRegression regression = new SimpleRegression();
    for (int i = 0; i < trendList.size(); i++) {
        regression.addData(trendList.size() - i, (trendList.get(i)).glucoseLevel);
    }
    predictedGlucose.glucoseLevel = (int)regression.predict(15 + PREDICTION_TIME);
    predictedGlucose.trend = regression.getSlope();
    predictedGlucose.confidence = regression.getSlopeConfidenceInterval();
    predictedGlucose.errorCode = PredictionData.Result.OK;
    predictedGlucose.realDate = trendList.get(0).realDate;
    predictedGlucose.sensorId = tagId;
    predictedGlucose.attempt = attempt;
    predictedGlucose.sensorTime = trendList.get(0).sensorTime;
    return predictedGlucose;
}
 
Example 3
Source File: IMSLinearRegression.java    From blue-marlin with Apache License 2.0 5 votes vote down vote up
public static double predict(double[] input)
{
    SimpleRegression simpleRegression = new SimpleRegression();
    double[][] data = new double[input.length][2];
    for (int i = 0; i < input.length; i++)
    {
        data[i][0] = i;
        data[i][1] = input[i];
    }
    simpleRegression.addData(data);
    double r = simpleRegression.predict(input.length);
    return r;
}
 
Example 4
Source File: UserFaceAligner.java    From server_face_recognition with GNU General Public License v3.0 5 votes vote down vote up
private void addAlignedEyesPos(List<Point2d> landmarks) {
    SimpleRegression linearRegression = new SimpleRegression();

    linearRegression.addData(
            landmarks.get(landmark_pos.LEFT_EYE_OUTER.pos).x(),
            landmarks.get(landmark_pos.LEFT_EYE_OUTER.pos).y()
    );

    linearRegression.addData(
            landmarks.get(landmark_pos.LEFT_EYE_INNER.pos).x(),
            landmarks.get(landmark_pos.LEFT_EYE_INNER.pos).y()
    );

    linearRegression.addData(
            landmarks.get(landmark_pos.RIGHT_EYE_INNER.pos).x(),
            landmarks.get(landmark_pos.RIGHT_EYE_INNER.pos).y()
    );

    linearRegression.addData(
            landmarks.get(landmark_pos.RIGHT_EYE_OUTER.pos).x(),
            landmarks.get(landmark_pos.RIGHT_EYE_OUTER.pos).y()
    );

    Point2d alignedLeftEyePos = new Point2d(
            landmarks.get(landmark_pos.LEFT_EYE_OUTER.pos).x(),
            linearRegression.predict(landmarks.get(landmark_pos.LEFT_EYE_OUTER.pos).x())
    );

    Point2d alignedRightEyePos = new Point2d(
            landmarks.get(landmark_pos.RIGHT_EYE_OUTER.pos).x(),
            linearRegression.predict(landmarks.get(landmark_pos.RIGHT_EYE_OUTER.pos).x())
    );

    landmarks.add(alignedLeftEyePos);
    landmarks.add(alignedRightEyePos);
}
 
Example 5
Source File: ListGrid.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@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 )
    {
        // 0 omitted from regression

        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 );

        // Enough values must exist for regression

        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.getType(), header.isHidden(), header.isMeta() );

            addHeader( regressionHeader );
        }
    }

    return this;
}