List of usage examples for org.apache.commons.math3.stat.regression SimpleRegression SimpleRegression
public SimpleRegression(boolean includeIntercept)
From source file:edu.washington.gs.skyline.model.quantification.WeightedRegressionTest.java
public void testWeighted() { Random random = new Random((int) new Date().getTime()); SimpleRegression simpleRegressionWithIntercept = new SimpleRegression(true); SimpleRegression simpleRegressionWithoutIntercept = new SimpleRegression(false); final int nPoints = 10; double[][] xValues = new double[nPoints][]; double[] yValues = new double[nPoints]; double[] weights = new double[nPoints]; for (int i = 0; i < nPoints; i++) { int weight = random.nextInt(10) + 1; weights[i] = weight;/*from ww w . j ava 2 s . c om*/ double x = random.nextDouble(); double y = random.nextDouble(); xValues[i] = new double[] { x }; yValues[i] = y; for (int w = 0; w < weight; w++) { simpleRegressionWithIntercept.addData(x, y); simpleRegressionWithoutIntercept.addData(x, y); } } final double epsilon = 1E-12; double repeatedIntercept = simpleRegressionWithIntercept.getIntercept(); double repeatedSlope = simpleRegressionWithIntercept.getSlope(); double[] weightedRegression = WeightedRegression.weighted(xValues, yValues, weights, true); assertEquals(repeatedIntercept, weightedRegression[0], epsilon); assertEquals(repeatedSlope, weightedRegression[1], epsilon); double[] weightedRegressionWithoutIntercept = WeightedRegression.weighted(xValues, yValues, weights, false); double repeatedSlopeWithoutIntercept = simpleRegressionWithoutIntercept.getSlope(); assertEquals(repeatedSlopeWithoutIntercept, weightedRegressionWithoutIntercept[0], epsilon); }
From source file:com.dataartisans.flinktraining.exercises.datastream_java.utils.TravelTimePredictionModel.java
public TravelTimePredictionModel() { models = new SimpleRegression[NUM_DIRECTION_BUCKETS]; for (int i = 0; i < NUM_DIRECTION_BUCKETS; i++) { models[i] = new SimpleRegression(false); }/*from w ww . j a va2 s .c om*/ }
From source file:de.biomedical_imaging.ij.nanotrackj.RegressionEstimator.java
@Override public double getDiffusionCoefficient(Track track, double driftx, double drifty) { if (track.size() == 1) { return 0; }//from ww w . j a v a2s.c o m SimpleRegression reg = new SimpleRegression(true); double msdhelp = 0; if (tau.length == 1) { reg.addData(0, 0); } for (int i = 0; i < tau.length; i++) { msdhelp = track.getMeanSquareDisplacement(driftx, drifty, tau[i]); reg.addData(tau[i] * (1.0 / getFramesPerSecond()), msdhelp); } double D = reg.getSlope() / 4; return D; }
From source file:com.twitter.heron.healthmgr.common.ComponentMetricsHelper.java
public void computeBufferSizeTrend() { for (InstanceMetrics instanceMetrics : componentMetrics.getMetrics().values()) { Map<Instant, Double> bufferMetrics = instanceMetrics.getMetrics().get(METRIC_BUFFER_SIZE.text()); if (bufferMetrics == null || bufferMetrics.size() < 3) { // missing of insufficient data for creating a trend line continue; }/* w w w. ja va 2s . c o m*/ SimpleRegression simpleRegression = new SimpleRegression(true); for (Instant timestamp : bufferMetrics.keySet()) { simpleRegression.addData(timestamp.getEpochSecond(), bufferMetrics.get(timestamp)); } double slope = simpleRegression.getSlope(); instanceMetrics.addMetric(METRIC_WAIT_Q_GROWTH_RATE.text(), slope); if (maxBufferChangeRate < slope) { maxBufferChangeRate = slope; } } }
From source file:dynetica.algorithm.DataFit.java
public void runExponentialFit() { //want to fit equation y = alpha*e^(Beta*x) double[] xvals; double[] yvals; if (independent == "Time") xvals = system.getTimer().getTimePoints(); else {// w w w . j a va2s .c o m if (independent.indexOf("Rate of") < 0) { xvals = system.getSubstance(independent).getValues(); } else { xvals = system.getSubstance(independent.split("\\s+")[independent.split("\\s+").length - 1]) .getRates(); } } if (dependent.indexOf("Rate of") < 0) { yvals = system.getSubstance(dependent).getValues(); } else { yvals = system.getSubstance(dependent.split("\\s+")[dependent.split("\\s+").length - 1]).getRates(); } SimpleRegression model = new SimpleRegression(true); double x, y; //need to use arraylist since we may not be using all indexed values in xvals ArrayList<Double> xValuesList = new ArrayList<Double>(); for (int i = 0; i < xvals.length; i++) { if (yvals[i] == 0) { continue; } else { y = log(yvals[i]); model.addData(xvals[i], y); xValuesList.add(xvals[i]); } } double alpha = exp(model.getIntercept()); double Beta = exp(model.getSlope()); fitParameters.put("alpha", alpha); fitParameters.put("Beta", Beta); fitParameters.put("r-squared", model.getRSquare()); // now create best fit line xValues = new double[xValuesList.size()]; yValues = new double[xValuesList.size()]; for (int i = 0; i < xValuesList.size(); i++) { xValues[i] = xValuesList.get(i); yValues[i] = alpha * exp(Beta * xValuesList.get(i)); } }
From source file:IBDMUT.Tools.java
public static double[] ApacheRegression(double[][] x, double[] y) { if (x.length < 2) { // Tools.warning("******************************************************"); // Tools.warning("******************************************************"); // Tools.warning("******************************************************"); // Tools.warning("Trying to run regression with " + x.length + " points."); // Tools.warning("******************************************************"); // Tools.warning("******************************************************"); // Tools.warning("******************************************************"); exit("Trying to run regression with " + x.length + " points."); }/*from ww w . j a v a 2s .c om*/ if (Tools.loggerIsOn()) { Tools.writeLog("Regression"); Tools.writeLog("\tx\ty"); for (int i = 0; i < x.length; i++) { Tools.writeLog("\t" + x[i][0] + "\t" + y[i]); } } SimpleRegression reg = new SimpleRegression(true); reg.addObservations(x, y); RegressionResults regRes = reg.regress(); double[] regResValues = regRes.getParameterEstimates(); double intercept = regResValues[0]; double slope = regResValues[1]; return new double[] { intercept, slope, regRes.getRSquared() }; }
From source file:edu.stanford.cfuller.imageanalysistools.fitting.BisquareLinearFit.java
/** * Performs a weighted least squares fit with supplied weights to the supplied data. * * @param indVarValues A RealVector containing the values of the independent variable. * @param depVarValues A RealVector containing the values of the dependent variable. * @param weights A RealVector containing the weights for the data points. * @return a RealVector containing two elements: the slope of the fit and the y-intercept of the fit. */// ww w .j a v a 2s . c o m public RealVector wlsFit(RealVector indVarValues, RealVector depVarValues, RealVector weights) { //initial guess for the fit: unweighted regression. SimpleRegression unweighted = new SimpleRegression(!this.noIntercept); for (int i = 0; i < indVarValues.getDimension(); i++) { unweighted.addData(indVarValues.getEntry(i), depVarValues.getEntry(i)); } RealVector parameters = null; if (this.noIntercept) { parameters = new ArrayRealVector(1, 0.0); } else { parameters = new ArrayRealVector(2, 0.0); } parameters.setEntry(0, unweighted.getSlope()); if (!this.noIntercept) { parameters.setEntry(1, unweighted.getIntercept()); } NelderMeadMinimizer nmm = new NelderMeadMinimizer(1e-12); WlsObjectiveFunction wof = new WlsObjectiveFunction(); wof.setIndVar(indVarValues); wof.setDepVar(depVarValues); wof.setWeights(weights); wof.setShouldFitIntercept(!this.noIntercept); parameters = nmm.optimize(wof, parameters); if (this.noIntercept) { RealVector output = new ArrayRealVector(2, 0.0); output.setEntry(0, parameters.getEntry(0)); return output; } return parameters; }
From source file:dynetica.algorithm.DataFit.java
public void runHillFit() { double[] xvals; double[] yvals; if (independent == "Time") xvals = system.getTimer().getTimePoints(); else {/*from w w w .jav a 2 s .c o m*/ if (independent.indexOf("Rate of") < 0) { xvals = system.getSubstance(independent).getValues(); } else { xvals = system.getSubstance(independent.split("\\s+")[independent.split("\\s+").length - 1]) .getRates(); } } if (dependent.indexOf("Rate of") < 0) { yvals = system.getSubstance(dependent).getValues(); } else { yvals = system.getSubstance(dependent.split("\\s+")[dependent.split("\\s+").length - 1]).getRates(); } double maxRate = 0; for (int i = 0; i < yvals.length; i++) { if (yvals[i] > maxRate) maxRate = yvals[i]; } SimpleRegression model = new SimpleRegression(true); double x, y; //need to use arraylist since we may not be using all indexed values in xvals ArrayList<Double> xValuesList = new ArrayList<Double>(); for (int i = 0; i < xvals.length; i++) { double v = yvals[i] / maxRate; if (v == 0) { //dont include this x and y continue; } else { x = log(xvals[i]); y = log(v / (1 - v)); model.addData(x, y); //add x to xResults xValuesList.add(xvals[i]); } } double hillCoefficient = model.getSlope(); double Km = exp(-(model.getIntercept()) / model.getSlope()); System.out.println(Km); System.out.println(hillCoefficient); //add parameters of best fit to hashmap, including r-squared value fitParameters.put("n", hillCoefficient); fitParameters.put("Km", Km); fitParameters.put("r-squared", model.getRSquare()); // now create best fit line xValues = new double[xValuesList.size()]; yValues = new double[xValuesList.size()]; for (int i = 0; i < xValuesList.size(); i++) { xValues[i] = xValuesList.get(i); yValues[i] = Math.pow(xValues[i], hillCoefficient) / (Math.pow(xValues[i], hillCoefficient) + Math.pow(Km, hillCoefficient)); } }
From source file:mase.mason.generic.systematic.SystematicEvaluator.java
@Override protected void postSimulation(MasonSimState sim) { if (timeMode == TimeMode.mean) { // Make averages double[] res = new double[size + 1]; for (int i = 0; i < size; i++) { List<Double> featureSample = features.get(i); double sum = 0; int count = 0; for (Double d : featureSample) { if (!Double.isNaN(d)) { sum += d;//from ww w. j a v a 2 s . com count++; } } res[i] = (double) (count == 0 ? 0 : sum / count); } res[size] = sim.schedule.getSteps(); vbr = new SystematicResult(res); } else if (timeMode == TimeMode.last) { double[] res = new double[size + 1]; for (int i = 0; i < size; i++) { List<Double> featureSample = features.get(i); double last = 0; for (Double d : featureSample) { if (!Double.isNaN(d)) { last = d; } } res[i] = (double) last; } res[size] = sim.schedule.getSteps(); vbr = new SystematicResult(res); } else if (timeMode == TimeMode.meanlast) { double[] res = new double[size * 2 + 1]; for (int i = 0; i < size; i++) { List<Double> featureSample = features.get(i); double last = 0; double sum = 0; int count = 0; for (Double d : featureSample) { if (!Double.isNaN(d)) { sum += d; count++; last = d; } } res[i * 2] = (double) (count == 0 ? 0 : sum / count); res[i * 2 + 1] = (double) last; } res[size * 2] = sim.schedule.getSteps(); vbr = new SystematicResult(res); } else if (timeMode == TimeMode.simplereg || timeMode == TimeMode.meanslope) { SimpleRegression reg = new SimpleRegression(true); double[] res = new double[size * 2 + 1]; for (int i = 0; i < size; i++) { reg.clear(); double sum = 0; List<Double> featureSample = features.get(i); for (int j = 0; j < featureSample.size(); j++) { double d = featureSample.get(j); if (!Double.isNaN(d)) { reg.addData(j, d); sum += d; } } if (timeMode == TimeMode.simplereg) { res[i * 2] = (double) (reg.getN() >= 2 ? reg.getIntercept() : 0); } else if (timeMode == TimeMode.meanslope) { res[i * 2] = (double) (reg.getN() > 0 ? sum / reg.getN() : 0); } res[i * 2 + 1] = (double) (reg.getN() >= 2 ? reg.getSlope() : 0); } res[size * 2] = sim.schedule.getSteps(); vbr = new SystematicResult(res); } else if (timeMode == TimeMode.frames) { int len = features.get(0).size(); int frameLen = (int) Math.ceil((double) len / timeFrames); // DEBUG for (int i = 0; i < size; i++) { if (features.get(i).size() != len) { System.out.println( "DIFFERENT FEATURE LENGTHS. Expected: " + len + " Got: " + features.get(i).size()); for (List<Double> sample : features) { System.out.print(sample.size() + " "); } System.out.println(); } } double[] res = new double[size * timeFrames + 1]; for (int t = 0; t < timeFrames; t++) { for (int i = 0; i < size; i++) { double sum = 0; int count = 0; for (int x = t * frameLen; x < Math.min(len, (t + 1) * frameLen); x++) { double d = features.get(i).get(x); if (!Double.isNaN(d)) { sum += d; count++; } } res[t * size + i] = (double) (count == 0 ? 0 : sum / count); } } res[size * timeFrames] = features.get(0).size(); vbr = new SystematicResult(res); } }
From source file:com.scottjjohnson.finance.analysis.WeeksTightCalculator.java
/** * Determines if a stock's weekly closing prices are trending up. * /*ww w.j a v a2 s .c o m*/ * @param quotes * list of stock quote beans * @return true if the weekly closing prices are trending up, otherwise false */ private boolean isStockInUptrend(List<WeeklyQuoteBean> quotes) { SimpleRegression r = new SimpleRegression(true); int numberOfQuotes = quotes.size(); // Normalize all of the prices relative to the first quote's closing price. double firstClosingPrice = quotes.get(0).getAdj_Close(); LOGGER.debug("Uptrend check: date = {}, first closing price = {}.", quotes.get(0).getWeekEndingDate(), firstClosingPrice); double d = 0; for (WeeklyQuoteBean quote : quotes) { LOGGER.debug("Uptrend check: date = {}, d = {}, data = {}, {}.", quote.getWeekEndingDate(), d, d / numberOfQuotes, quote.getAdj_Close() / firstClosingPrice); r.addData(d / numberOfQuotes, quote.getAdj_Close() / firstClosingPrice); d++; } LOGGER.debug("Slope = {}, intercept = {}, min slope = {}.", r.getSlope(), r.getIntercept(), MINIMUM_SLOPE_OF_TRENDLINE_UPTREND); if (r.getSlope() >= MINIMUM_SLOPE_OF_TRENDLINE_UPTREND) return true; else return false; }