i'm trying develop application compute same trend lines excel does, larger datasets.
but i'm not able find java library calculates such regressions. linera model i'm using apache commons math, , other there great numerical library michael thomas flanagan since january no longer available:
http://www.ee.ucl.ac.uk/~mflanaga/java/
do know other libraries, code repositories calculate these regressions in java. best,
since they're based on linear fits, olsmultiplelinearregression need linear, polynomial, exponential, logarithmic, , power trend lines.
your question gave me excuse download , play commons math regression tools, , put trend line tools:
an interface:
public interface trendline { public void setvalues(double[] y, double[] x); // y ~ f(x) public double predict(double x); // predicted y given x }
an abstract class regression-based trendlines:
public abstract class olstrendline implements trendline { realmatrix coef = null; // hold prediction coefs once values protected abstract double[] xvector(double x); // create vector of values x protected abstract boolean logy(); // set true predict log of y (note: y must positive) @override public void setvalues(double[] y, double[] x) { if (x.length != y.length) { throw new illegalargumentexception(string.format("the numbers of y , x values must equal (%d != %d)",y.length,x.length)); } double[][] xdata = new double[x.length][]; (int = 0; < x.length; i++) { // implementation determines how produce vector of predictors single x xdata[i] = xvector(x[i]); } if(logy()) { // in models predicting ln y, replace each y ln y y = arrays.copyof(y, y.length); // user might not finished array given (int = 0; < x.length; i++) { y[i] = math.log(y[i]); } } olsmultiplelinearregression ols = new olsmultiplelinearregression(); ols.setnointercept(true); // let implementation include constant in xvector if desired ols.newsampledata(y, xdata); // provide data model coef = matrixutils.createcolumnrealmatrix(ols.estimateregressionparameters()); // our coefs } @override public double predict(double x) { double yhat = coef.premultiply(xvector(x))[0]; // apply coefs xvector if (logy()) yhat = (math.exp(yhat)); // if predicted ln y, still need y return yhat; } }
an implementation polynomial or linear models:
(for linear models, set degree 1 when calling constructor.)
public class polytrendline extends olstrendline { final int degree; public polytrendline(int degree) { if (degree < 0) throw new illegalargumentexception("the degree of polynomial must not negative"); this.degree = degree; } protected double[] xvector(double x) { // {1, x, x*x, x*x*x, ...} double[] poly = new double[degree+1]; double xi=1; for(int i=0; i<=degree; i++) { poly[i]=xi; xi*=x; } return poly; } @override protected boolean logy() {return false;} }
exponential , power models easier:
(note: we're predicting log y -- that's important. both of these suitable positive y)
public class exptrendline extends olstrendline { @override protected double[] xvector(double x) { return new double[]{1,x}; } @override protected boolean logy() {return true;} }
and
public class powertrendline extends olstrendline { @override protected double[] xvector(double x) { return new double[]{1,math.log(x)}; } @override protected boolean logy() {return true;} }
and log model:
(which takes log of x predicts y, not ln y)
public class logtrendline extends olstrendline { @override protected double[] xvector(double x) { return new double[]{1,math.log(x)}; } @override protected boolean logy() {return false;} }
and can use this:
public static void main(string[] args) { trendline t = new polytrendline(2); random rand = new random(); double[] x = new double[1000*1000]; double[] err = new double[x.length]; double[] y = new double[x.length]; (int i=0; i<x.length; i++) { x[i] = 1000*rand.nextdouble(); } (int i=0; i<x.length; i++) { err[i] = 100*rand.nextgaussian(); } (int i=0; i<x.length; i++) { y[i] = x[i]*x[i]+err[i]; } // quadratic model t.setvalues(y,x); system.out.println(t.predict(12)); // when x=12, y should be... , eg 143.61380202745192 }
since wanted trend lines, dismissed ols models when done them, might want keep data on goodness of fit, etc.
for implementations using moving average, moving median, etc, looks can stick commons math. try descriptivestatistics , specify window. might want smoothing, using interpolation suggested in answer.
Comments
Post a Comment