【发布时间】:2015-04-13 08:17:41
【问题描述】:
我在使用 Apache Commons 数学库的 OLS 方面遇到了一些问题。我有一个时间序列 y,我想将最小二乘趋势线拟合到前 26 个观察值。这是我的代码:
List<Double> y = new ArrayList<>(Arrays.asList(206.0, 245.0,
185.0, 169.0, 162.0, 177.0, 207.0, 216.0, 193.0, 230.0, 212.0,
192.0, 162.0, 189.0, 244.0, 209.0, 207.0, 211.0, 210.0, 173.0,
194.0, 234.0, 156.0, 206.0, 188.0, 162.0, 172.0, 210.0, 205.0,
244.0, 218.0, 182.0, 206.0, 211.0, 273.0, 248.0, 262.0, 258.0,
233.0, 255.0, 303.0, 282.0, 291.0, 280.0, 255.0, 312.0, 296.0,
307.0, 281.0, 308.0, 280.0, 345.0));
OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
int obs = y.size()/2;
int vars = 1;
double data[] = new Utils().toArray(y);
try {
ols.newSampleData(data, obs, vars); // 3
}
catch(IllegalArgumentException e) {
System.out.print("Can't sample data: ");
e.printStackTrace();
}
double[] coe = null;
try {
coe = ols.estimateRegressionParameters(); // 4
} catch(Exception e) { // 5
System.out.print("Can't estimate parameters: ");
e.printStackTrace();
}
我得到的结果是:
coe[0] = 58.3379729430363
coe[1] = 0.7075495794353521
但是,结果应该是(参见屏幕截图):
coe[0] = 202.6246154
coe[1] = -0.368205128
谁能帮我解决这个问题?
【问题讨论】:
标签: java time-series regression mathematical-optimization least-squares