Java tutorial
//package com.java2s; public class Main { /** * result linear coef with coef[0]=a and coef[1]=b in a*x+b*/ static public double[] regress(double[] y, double[] x) { double[] coefs = new double[2]; // first pass: read in data, compute xbar and ybar double sumx = 0.0, sumy = 0.0, sumx2 = 0.0; int offset = 0; int length = x.length; double R2 = 0; while (R2 < 0.50) { for (int i = offset; i < length; i++) { if (x[i] != Integer.MIN_VALUE && y[i] != Integer.MIN_VALUE) { sumx += x[i]; sumx2 += x[i] * x[i]; sumy += y[i]; } } double xbar = sumx / length; double ybar = sumy / length; // second pass: compute summary statistics double xxbar = 0.0, yybar = 0.0, xybar = 0.0; for (int i = offset; i < length; i++) { if (x[i] != Integer.MIN_VALUE && y[i] != Integer.MIN_VALUE) { xxbar += (x[i] - xbar) * (x[i] - xbar); yybar += (y[i] - ybar) * (y[i] - ybar); xybar += (x[i] - xbar) * (y[i] - ybar); } } double a = xybar / xxbar; double b = ybar - a * xbar; coefs[0] = a; coefs[1] = b; // analyze results int df = length - 2; double rss = 0.0; // residual sum of squares double ssr = 0.0; // regression sum of squares for (int i = offset; i < length; i++) { if (x[i] != Integer.MIN_VALUE && y[i] != Integer.MIN_VALUE) { double fit = a * x[i] + b; rss += (fit - y[i]) * (fit - y[i]); ssr += (fit - ybar) * (fit - ybar); } } R2 = ssr / yybar; double svar = rss / df; double svar1 = svar / xxbar; double svar0 = svar / length + xbar * xbar * svar1; svar0 = svar * sumx2 / (length * xxbar); //Log.v("BORDER", "Coefficient of correlation : " + R2); offset++; } return coefs; } }