List of usage examples for org.apache.commons.math3.stat.regression SimpleRegression addData
public void addData(final double[][] data) throws ModelSpecificationException
data. From source file:com.idylwood.Statistics.java
public static SimpleRegression regress(List<YahooFinance.Pair> pairs) { SimpleRegression regress = new SimpleRegression(); regress.addData(data(pairs)); return regress; }
From source file:alignment.BinaryToCSV.java
/** * //from ww w . ja va2 s . c o m * * @param * @return */ public void computeLinearRegression() { double[][] data = new double[offsets.size()][2]; int i = 0; for (Double d : offsets.keySet()) { data[i][0] = d; data[i++][1] = offsets.get(d); } SimpleRegression regress = new SimpleRegression(true); regress.addData(data); // dummy data from wikipedia (linear regression) // used to check the regression library /*regress.addData(1.47,52.21); regress.addData(1.50,53.12); regress.addData(1.52,54.48); regress.addData(1.55,55.84); regress.addData(1.57,57.20); regress.addData(1.60,58.57); regress.addData(1.63,59.93); regress.addData(1.65,61.29); regress.addData(1.68,63.11); regress.addData(1.70,64.47); regress.addData(1.73,66.28); regress.addData(1.75,68.10); regress.addData(1.78,69.92); regress.addData(1.80,72.19); regress.addData(1.83,74.46);*/ slope = regress.getSlope(); intercept = regress.getIntercept(); System.out.println("Regression: y = " + slope + "x + " + intercept); }
From source file:com.medlog.webservice.vo.DiaryAnalysisSummaryVO.java
private void populateLineGuessPoints(SimpleRegression sg) throws ModelSpecificationException { sg.addData(new double[][] { { 0.5322920416, 3 }, { 0.4385508333, 8 }, { 0.3922372962, 9 }, { 0.4846032539, 7 }, { 0.4941322222, 7 }, { 0.2621152000, 8 }, { 0.7150910000, 5 }, { 0.5197160000, 5 }, { 0.4462330000, 5 }, { 0.7522432500, 10 }, { 0.2843740000, 1 }, { 0.3227760000, 4 }, { 0.6565395000, 8 }, { 0.4388810000, 10 } });// www .j a va 2s . c om }
From source file:org.spotter.ext.detection.highmessaging.analyze.LinearAnalyzer2.java
@Override public AnalyzeResult analyze() { result.addMessage("*************************************************"); result.addMessage("Testing data for linear progression.."); List<Double> smoothed = smooth(yValues, SMOOTH_WIDE); List<Double> normalized = normalize(smoothed, smoothed.get(0)); SimpleRegression base = new SimpleRegression(); base.addData(toDoubleArray(subList(xValues, 0, 5), subList(normalized, 0, 5))); SimpleRegression regression = new SimpleRegression(); regression.addData(toDoubleArray(xValues, normalized)); result.addMessage("Slope of data progression: " + regression.getSlope()); result.addMessage("Slope in first " + 5 + " values: " + base.getSlope()); result.addMessage("Slope threshold: " + base.getSlope() * threshold2); if (regression.getSlope() > base.getSlope() * threshold2) { result.addMessage("> detected"); return AnalyzeResult.POSITIVE; } else {/*from w w w. j a v a 2s.c o m*/ result.addMessage("> not detected"); return AnalyzeResult.NEGATIVE; } }
From source file:org.spotter.ext.detection.highmessaging.analyze.LogAnalyzer.java
@Override public AnalyzeResult analyze() { result.addMessage("*************************************************"); result.addMessage("Testing data for stagnating progression.."); TrendLine logTrend = new LogTrendLine(); logTrend.setValues(toDoubleArray(normalize(yValues, Collections.min(yValues))), toDoubleArray(xValues)); List<Double> smoothed = smooth(yValues, SMOOTH_WIDE); SimpleRegression first = new SimpleRegression(); first.addData( toDoubleArray(subList(xValues, 0, xValues.size() / 2), subList(smoothed, 0, xValues.size() / 2))); SimpleRegression second = new SimpleRegression(); second.addData(toDoubleArray(subList(xValues, xValues.size() / 2), subList(smoothed, xValues.size() / 2))); result.addMessage("> Slope 0%-50%: " + first.getSlope()); result.addMessage("> Slope 50%-100%: " + second.getSlope()); result.addMessage("> Threshold: " + maxSlopeFactor * first.getSlope()); if (Math.abs(second.getSlope()) < maxSlopeFactor * first.getSlope()) { result.addMessage("> detected"); return AnalyzeResult.POSITIVE; } else {/*from w w w.ja va 2 s . c om*/ result.addMessage("> not detected"); return AnalyzeResult.NEGATIVE; } }
From source file:org.spotter.ext.detection.highmessaging.analyze.UpAndDownAnalyzer.java
@Override public AnalyzeResult analyze() { result.addMessage("*************************************************"); result.addMessage("Testing data for increasing and decreasing progression.."); List<Double> valuesSmoothed = smooth(yValues, 3); List<Double> normalizedValues = normalize(valuesSmoothed, valuesSmoothed.get(0)); List<Integer> peakPoints = new ArrayList<Integer>(); int p = 0;// ww w . j a v a2 s . co m while ((p = nextPeak(normalizedValues, p)) != -1) { peakPoints.add(p); } double standardDeviation = standardDeviation(normalizedValues); // Find highest peak where the following doesnt exceed value+stdev int highestPeak = 0; for (; highestPeak < peakPoints.size();) { boolean found = false; for (int n = highestPeak + 1; n < peakPoints.size(); n++) { if (normalizedValues.get(peakPoints.get(highestPeak)) + standardDeviation < normalizedValues .get(peakPoints.get(n))) { highestPeak = n; found = true; } } if (!found) { break; } } // System.out.println("h: " + peakPoints.get(highestPeak)); // Bei keinem Peak => NEGATIVE if (peakPoints.isEmpty()) { return AnalyzeResult.NEGATIVE; } // Peak liegt zu nahe an messrand if (1D / normalizedValues.size() * peakPoints.get(highestPeak) > 1D - minBorderMarginForDetection) { return AnalyzeResult.UNKNOWN; } List<Double> subYValues = subList(normalizedValues, peakPoints.get(highestPeak)); List<Double> subXValues = subList(xValues, peakPoints.get(highestPeak)); SimpleRegression regression = new SimpleRegression(); regression.addData(toDoubleArray(subXValues, subYValues)); // System.out.println(regression.getSlope()); if (regression.getSlope() < 0) { result.addMessage("> detected"); return AnalyzeResult.POSITIVE; } else { result.addMessage("> not detected"); return AnalyzeResult.NEGATIVE; } }
From source file:saffranexperiment.Main.java
/** * Calculates the r<sup>2</sup> and RMSE values for each participant type * repeat.//from ww w. j a v a 2 s .com * * @param data A 4D {@link java.util.Arrays Array} with the following * structure (can be obtained from {@link * #calculateMeanSdTAndPValuesForEachExperimentRepeat(double[][][][][])}: * * <ul> * <li>First dimension: participant types</li> * <li>Second dimension: repeats</li> * <li>Third dimension: experiments</li> * <li>Fourth dimension:</li> * <ol type="1"> * <li>Familiar word presentation time mean</li> * <li>Familiar word presentation time standard deviation</li> * <li>Novel word presentation time mean</li> * <li>Novel word presentation time standard deviation</li> * <li>t value for familiar/novel word presentation time means</li> * <li>p value for familiar/novel word presentation time means</li> * </ol> * </ul> * * For example, invoking data[5][3][1][4] would return the t-value associated * with the second experiment of the fourth repeat for participant type 6. * * @return A 3D {@link java.util.Arrays Array}: * <ul> * <li>1st dimension: participant types</li> * <li>2nd dimension: repeats</li> * <li>3rd dimension</li> * <ol type="1"> * <li>R-square</li> * <li>RMSE</li> * </ol> * </ul> * * For example, assigning the result to a variable j and invoking j[5][3][1] * would return the RMSE value associated with the fourth repeat of * experiments for participant type 6. */ private static double[][][] calculateRSquareAndRmseForEachRepeat(double[][][][] data) { double[][][] rsquareAndRmseValues = new double[_totalParticipantTypes][_totalRepeats][2]; for (int participantType = 0; participantType < _totalParticipantTypes; participantType++) { for (int repeat = 0; repeat < _totalRepeats; repeat++) { double[][] humanModelFamilAndNovelRecTimesMeans = new double[4][2]; humanModelFamilAndNovelRecTimesMeans[0][0] = 7.97; humanModelFamilAndNovelRecTimesMeans[0][1] = data[participantType][repeat][0][0]; humanModelFamilAndNovelRecTimesMeans[1][0] = 8.85; humanModelFamilAndNovelRecTimesMeans[1][1] = data[participantType][repeat][0][2]; humanModelFamilAndNovelRecTimesMeans[2][0] = 6.77; humanModelFamilAndNovelRecTimesMeans[2][1] = data[participantType][repeat][1][0]; humanModelFamilAndNovelRecTimesMeans[3][0] = 7.60; humanModelFamilAndNovelRecTimesMeans[3][1] = data[participantType][repeat][0][2]; /****************************/ /**** Calculate R-square ****/ /****************************/ SimpleRegression repeatRegression = new SimpleRegression(); repeatRegression.addData(humanModelFamilAndNovelRecTimesMeans); rsquareAndRmseValues[participantType][repeat][0] = repeatRegression.getRSquare(); /************************/ /**** Calculate RMSE ****/ /************************/ //The Apache Stats library gives incorrect RMSEs if //Math.sqrt(repeatRegression.getMeanSquaredError()) is used. Therefore, //the RMSE for a repeat is calculated "by hand". //First, get the difference between the mean famil/novel times in each //experiment and square them. double expt1FamilWordTimeHumanModelDiffSquared = Math.pow( humanModelFamilAndNovelRecTimesMeans[0][0] - humanModelFamilAndNovelRecTimesMeans[0][1], 2.0); double expt1NovelWordTimeHumanModelDiffSquared = Math.pow( humanModelFamilAndNovelRecTimesMeans[1][0] - humanModelFamilAndNovelRecTimesMeans[1][1], 2.0); double expt2FamilWordTimeHumanModelDiffSquared = Math.pow( humanModelFamilAndNovelRecTimesMeans[2][0] - humanModelFamilAndNovelRecTimesMeans[2][1], 2.0); double expt2NovelWordTimeHumanModelDiffSquared = Math.pow( humanModelFamilAndNovelRecTimesMeans[3][0] - humanModelFamilAndNovelRecTimesMeans[3][1], 2.0); //Sum the differences calculated above. double sumHumanModelDiffSquares = expt1FamilWordTimeHumanModelDiffSquared + expt1NovelWordTimeHumanModelDiffSquared + expt2FamilWordTimeHumanModelDiffSquared + expt2NovelWordTimeHumanModelDiffSquared; //Divide the sum above by the number of data points. double meanSquaredError = sumHumanModelDiffSquares / 8; //Finally, square root the mean squared error. double rootMeanSquaredError = Math.sqrt(meanSquaredError); rsquareAndRmseValues[participantType][repeat][1] = rootMeanSquaredError; } } return rsquareAndRmseValues; }
From source file:sim.module.broker.bo.BrokerAgent.java
/** * Forecast future deficit using linear regression forecast. * //from ww w. ja va 2 s .c o m * Forecast: Using the last 3 years data (36 months): * 1. Use linear regression to estimate linear trend line. * 2. Calculate seasonal index (monthly residual from trend line) * 3. Use trend line to produce linear forecast * 4. Add seasonal (monthly) effects to forecast demand for each future month, t: forecast(t). * * @param - recent historical demand over the last 36 months (inclusive of *this* month) * @return demand forecast list for the next [learning period = 36] months */ public List<Double> getForecastDemandUsingLinearRegression(List<Double> historicDemand) { List<Double> forecastDemand = new ArrayList<Double>(); List<Double> trendLine = new ArrayList<Double>(); List<Double> residuals = new ArrayList<Double>(); List<Double> monthlyResiduals = new ArrayList<Double>(); if (historicDemand.size() != learningPeriod) { logger.warn("*** Historic data list is incorrect size. Returning zero forecast list. *** Size = " + historicDemand.size() + ", Size should be = " + learningPeriod + ", historicData: " + historicDemand); for (int i = 0; i < learningPeriod; i++) { forecastDemand.add(0.0); } return forecastDemand; } else { //convert List into 2d array double[][] historicDataArray = new double[historicDemand.size()][2]; int index = 0; for (double h : historicDemand) { historicDataArray[index][0] = index; historicDataArray[index][1] = h; index++; } logger.debug("Historic array: " + Arrays.deepToString(historicDataArray)); // now do the regression... SimpleRegression regression = new SimpleRegression(); regression.addData(historicDataArray); logger.debug("Regression: m=" + regression.getSlope() + ", c=" + regression.getIntercept() + ", cStdErr=" + regression.getInterceptStdErr()); // now get the trend line & residuals for (int i = 0; i < historicDemand.size(); i++) { trendLine.add((i * regression.getSlope()) + regression.getIntercept()); residuals.add(historicDemand.get(i) - trendLine.get(i)); } logger.debug("Trend line = " + trendLine); logger.debug("Residuals = " + residuals); // get mean residuals per month for (int i = 0; i < 12; i++) { logger.debug("We are assuming that learning period is 36 months. Make this generic code!!!"); //TODO use %12 and iterate through to make this generic monthlyResiduals.add((residuals.get(i) + residuals.get(i + 12) + residuals.get(i + 24)) / 3); } logger.debug("mean monthly residuals: " + monthlyResiduals); // now forecast using monthly residuals and trend line // first set forecast using trendline for (double t : trendLine) { forecastDemand.add(t); } // forecast using trend line plus monthly adjustment for (int i = 0; i < 12; i++) { //TODO: Make this code generic not fixed to 36 months forecastDemand.set(i, trendLine.get(i) + monthlyResiduals.get(i)); forecastDemand.set(i + 12, trendLine.get(i + 12) + monthlyResiduals.get(i)); forecastDemand.set(i + 24, trendLine.get(i + 24) + monthlyResiduals.get(i)); } // finally, multiply normalised demand by number of agents for (int i = 0; i < forecastDemand.size(); i++) { forecastDemand.set(i, Math.floor(forecastDemand.get(i) * BrokerModuleRunner.getInstance().getNumEachAgent())); } logger.debug("final forecast with seasonal adjustment: " + forecastDemand); logger.debug("RForecastDemand: " + forecastDemand); logger.debug("Previous demand: " + historicDemand); return forecastDemand; } }