趋势线(回归,曲线拟合)java库
我正在尝试开发一个应用程序来计算与excel相同的趋势线,但是对于更大的数据集。
但是我找不到任何计算这种回归的java库。 对于linera模型,我使用的是Apache Commons数学,另一方面,有一个来自Michael Thomas Flanagan的伟大数值库,但自1月以来它已不再可用:
http://www.ee.ucl.ac.uk/~mflanaga/java/
您是否知道任何其他库,代码存储库来计算java中的这些回归。 最好,
由于它们都基于线性拟合,因此OLSMultipleLinearRegression是线性,多项式,指数,对数和功率趋势线所需的全部。
你的问题给了我一个借口下载和使用公共数学回归工具,我把一些趋势线工具放在一起:
界面:
public interface TrendLine { public void setValues(double[] y, double[] x); // y ~ f(x) public double predict(double x); // get a predicted y for a given x }
基于回归的趋势线的抽象类:
public abstract class OLSTrendLine implements TrendLine { RealMatrix coef = null; // will hold prediction coefs once we get values protected abstract double[] xVector(double x); // create vector of values from x protected abstract boolean logY(); // set true to predict log of y (note: y must be positive) @Override public void setValues(double[] y, double[] x) { if (x.length != y.length) { throw new IllegalArgumentException(String.format("The numbers of y and x values must be equal (%d != %d)",y.length,x.length)); } double[][] xData = new double[x.length][]; for (int i = 0; i < x.length; i++) { // the implementation determines how to produce a vector of predictors from a single x xData[i] = xVector(x[i]); } if(logY()) { // in some models we are predicting ln y, so we replace each y with ln y y = Arrays.copyOf(y, y.length); // user might not be finished with the array we were given for (int i = 0; i < x.length; i++) { y[i] = Math.log(y[i]); } } OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression(); ols.setNoIntercept(true); // let the implementation include a constant in xVector if desired ols.newSampleData(y, xData); // provide the data to the model coef = MatrixUtils.createColumnRealMatrix(ols.estimateRegressionParameters()); // get our coefs } @Override public double predict(double x) { double yhat = coef.preMultiply(xVector(x))[0]; // apply coefs to xVector if (logY()) yhat = (Math.exp(yhat)); // if we predicted ln y, we still need to get y return yhat; } }
多项式或线性模型的实现:
(对于线性模型,只需在调用构造函数时将度数设置为1.)
public class PolyTrendLine extends OLSTrendLine { final int degree; public PolyTrendLine(int degree) { if (degree < 0) throw new IllegalArgumentException("The degree of the polynomial must not be 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;} }
指数和功率模型更容易:
(注意:我们现在正在预测日志 - 这很重要。这两个都只适用于正y)
public class ExpTrendLine extends OLSTrendLine { @Override protected double[] xVector(double x) { return new double[]{1,x}; } @Override protected boolean logY() {return true;} }
和
public class PowerTrendLine extends OLSTrendLine { @Override protected double[] xVector(double x) { return new double[]{1,Math.log(x)}; } @Override protected boolean logY() {return true;} }
和日志模型:
(它取x的对数但是预测y,而不是yn 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;} }
你可以像这样使用它:
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]; for (int i=0; i
因为你只是想要趋势线,所以当我完成它们时我解开了ols模型,但是你可能想要保留一些合适的数据等等。
对于使用移动平均线,移动中位数等的实现,看起来你可以坚持使用公共数学。 尝试DescriptiveStatistics并指定一个窗口。 您可能希望使用另一个答案中建议的插值进行一些平滑处理。
您可以使用org.apache.commons.math3.analysis.interpolation
提供的不同类型的插值器 ,包括例如LinearInterpolator,LoessInterpolator和NevilleInterpolator。
除了可能是WeCouldStealAVa所说的;
commons-math3库也可以在maven存储库中使用 。
当前版本为3.2,依赖标记为:
org.apache.commons commons-math3 3.2
- Java中“private static final”和“public static final”类变量的最近Ruby表示?
- org.json.simple无法解析
- 在Blackberry4.6操作系统版本上加载html
- 如何在maven-shade-plugin创建的Jar中包含测试类?
- RSelenium UnknownError – 使用谷歌浏览器的java.lang.IllegalStateException
- 为什么等待,通知和notifyAll方法都在Object Class中?
- 有没有办法在Java中检测RTL语言?
- 在Java中提取两个字符串之间的差异
- 如何运行Python解释器并使用Java获取其输出?