java - 趋势线(回归,曲线拟合)java库

标签 java math statistics regression

我正在尝试开发一个应用程序来计算与 excel 相同的趋势线,但适用于更大的数据集。

enter image description here

但我找不到任何计算此类回归的 Java 库。对于 linera 模型,我使用 Apache Commons 数学,对于另一个模型,Michael Thomas Flanagan 提供了一个很棒的数值库,但自 1 月以来它不再可用:

http://www.ee.ucl.ac.uk/~mflanaga/java/

你知道任何其他库、代码存储库来计算 java 中的这些回归吗?最好的,

最佳答案

由于它们都基于线性拟合,因此 OLSMultipleLinearRegression 是线性、多项式、指数、对数和幂趋势线所需的全部。

你的问题给了我下载和使用常用数学回归工具的借口,我整理了一些趋势线工具:

接口(interface):

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;}
}

指数和幂模型更简单:

(注意:我们现在预测的是 log y——这很重要。这两个都只适用于正 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,而不是 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;}
}

你可以这样使用它:

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<x.length; i++) { x[i] = 1000*rand.nextDouble(); }
    for (int i=0; i<x.length; i++) { err[i] = 100*rand.nextGaussian(); } 
    for (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
}

因为您只想要趋势线,所以我在完成 ols 模型后放弃了它们,但您可能想要保留一些关于拟合优度等的数据。

对于使用移动平均值、移动中位数等的实现,看起来您可以坚持使用通用数学。尝试 DescriptiveStatistics并指定一个窗口。您可能想按照另一个答案中的建议使用插值法进行一些平滑处理。

关于java - 趋势线(回归,曲线拟合)java库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17592139/

相关文章:

java - 在图形代码中处理除以零

windows - 查看 Windows 运行了多长时间以及其他指标

Java基础类的使用

math - 求解递推关系 T(n) = √n T(√n) + n

java - 如何使用 java 在 Google 云数据存储中一次放入/获取多个实体

math - 计算对角线的垂直偏移

python - 如何获得完整的协方差矩阵并在 GPflow 中找到它的熵

python - StatsModels 的置信区间和预测区间

java - 无法使用 EclipseLink 从 Oracle TIMESTAMPTZ 列获取日历对象

java - 如何将此插入排序从升序更改为降序 - java