1 .二次指數平滑法求預測值
/**
* 二次指數平滑法求預測值
* @param list 基礎數據集合
* @param year 未來第幾期
* @param modulus 平滑系數
* @return 預測值
*/
private static Double getExpect(List<Double> 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<Double> polynomial(List<Double> 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<Double> 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;
}
