预测算法之 二次指数平滑算法 Java实现

Double modulus=0.5;    //系数
private static Long getExpect(List list, int year, int start, int end) {
		if (list.size() < 1 || modulus <= 0 || modulus >= 1) {
			return null;
		}
		Double modulusLeft = 1 - modulus;
		Double lastIndex = list.get(0) * 1.0;
		Double lastSecIndex = list.get(0) * 1.0;
		for (int i = start; i <= end; ++i) {
			if (list.get(i) == 0) {
				continue;
			}
			lastIndex = modulus * list.get(i) + modulusLeft * lastIndex;
			lastSecIndex = modulus * lastIndex + modulusLeft * lastSecIndex;
		}
		Double a = 2 * lastIndex - lastSecIndex;
		Double b = (modulus / modulusLeft) * (lastIndex - lastSecIndex);
		Double ret = a + b * year;
		if (ret < 0) {
			ret = 0d;
		}
		Long reLong = ret.longValue();
		if (ret - reLong >= 0.5) {
			reLong += 1;
		}
		return reLong;
	}

	private static Boolean adjust(List list, int start, int end) {
		if (start < 0 || end >= list.size()) {
			System.out.println("out of index");
			return false;
		}
		Long forecast = getExpect(list, 1, start, end);
		Integer real = list.get(end + 1);
		Long dif = real - forecast;
		Double rate = dif / 1.0 / real;
		if (real == 0 || rate > 0.7 || rate < -0.7) {
			//System.out.println("去噪\n\n");
			list.remove(end + 1);
			return false;
		}
		modulus += rate * 0.5;      //这个0.5可以采用二次系统adjust 但我没有实现
		if (modulus >= 1) {
			//System.out.println("重置极大");
			modulus = 0.999999999;
		}
		if (modulus < 0) {
			//System.out.println("重置极小");
			modulus = 0.000000001;
		}
		return true;
	}
 /* 
 华为软挑遇到预测的题目后用了此算法 但测试效果似乎并不理想
*/

你可能感兴趣的:(算法,算法,数据,预测)