1 .二次指数平滑法求预测值
/**
* 二次指数平滑法求预测值
* @param list 基础数据集合
* @param year 未来第几期
* @param modulus 平滑系数
* @return 预测值
*/
private static Double getExpect(List list, int year, Double modulus ) {
if (list.size() < 10 || modulus <= 0 || modulus >= 1) {
return null;
}
Double modulusLeft = 1 - modulus;
Double lastIndex = list.get(0);
Double lastSecIndex = list.get(0);
for (Double data :list) {
lastIndex = modulus * data + modulusLeft * lastIndex;
lastSecIndex = modulus * lastIndex + modulusLeft * lastSecIndex;
}
Double a = 2 * lastIndex - lastSecIndex;
Double b = (modulus / modulusLeft) * (lastIndex - lastSecIndex);
return a + b * year;
}
2.最小二乘法曲线拟合
/**
* 最小二乘法曲线拟合
* @param data
* @return
*/
public static List polynomial(List data,int degree){
final WeightedObservedPoints obs = new WeightedObservedPoints();
for (int i = 0; i < data.size(); i++) {
obs.add(i, data.get(i));
}
/**
* 实例化一个2次多项式拟合器
*/
final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(degree);//degree 阶数,一般为 3
/**
* 实例化检索拟合参数(多项式函数的系数)
*/
final double[] coeff = fitter.fit(obs.toList());//size 0-3 阶数
List result = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
double tmp=0.0;
/**
* 多项式函数f(x) = a0 * x + a1 * pow(x, 2) + .. + an * pow(x, n).
*/
for (int j = 0; j<= degree; j++) {
tmp+= coeff[j]* Math.pow(i,j);
}
result.add(tmp);
}
return result;
}
3.5点3次平滑曲线
public static Double[] Mean5_3(Double[] a, int m) {
Double[] b = new Double[a.length];
int n = a.length;
for (int k = 0; k < m; k++) {
b[0] = (69 * a[0] + 4 * (a[1] + a[3]) - 6 * a[2] - a[4]) / 70;
b[1] = (2 * (a[0] + a[4]) + 27 * a[1] + 12 * a[2] - 8 * a[3]) / 35;
for (int j = 2; j < n - 2; j++) {
b[j] = (-3 * (a[j - 2] + a[j + 2]) + 12 * (a[j - 1] + a[j + 1]) + 17 * a[j]) / 35;
}
b[n - 2] = (2 * (a[n - 1] + a[n - 5]) + 27 * a[n - 2] + 12 * a[n - 3] - 8 * a[n - 4]) / 35;
b[n - 1] = (69 * a[n - 1] + 4 * (a[n - 2] + a[n - 4]) - 6 * a[n - 3] - a[n - 5]) / 70;
}
return b;
}