[英]Trend lines ( regression, curve fitting) java library
我正在嘗試開發一個應用程序,該應用程序可以計算與 excel 相同的趨勢線,但適用於更大的數據集。
但是我找不到任何計算此類回歸的 Java 庫。 對於 linera 模型,我使用的是 Apache Commons 數學,對於另一個模型,Michael Thomas Flanagan 提供了一個很棒的數值庫,但自 1 月以來它不再可用:
http://www.ee.ucl.ac.uk/~mflanaga/java/
你知道任何其他庫,代碼存儲庫來計算 java.lang 中的這些回歸嗎? 最好,
由於它們都基於線性擬合,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;}
}
指數和冪模型更簡單:
(注意:我們現在正在預測 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並指定一個窗口。 您可能想要使用另一個答案中建議的插值來進行一些平滑處理。
您可以使用org.apache.commons.math3.analysis.interpolation
可用的不同類型的插值器,包括例如 LinearInterpolator、LoessInterpolator 和 NevilleInterpolator。
除了也許WeCouldStealAVa所說的;
commons-math3 庫也在maven 存儲庫中可用。
當前版本是 3.2,依賴標簽是:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.2</version>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.