import json import numpy as np x = np.array([2., 3., 4.5, 6.75]) accumulated = np.cumsum(x) background = (accumulated[:-1] + accumulated[1:]) / 2 # GM(1,1) 的离散回归式为 x(k) + a·z(k) = b,未知量只有 a 与 b。 design = np.column_stack((-background, np.ones_like(background))) a, b = np.linalg.lstsq(design, x[1:], rcond=None)[0] # 响应从 k=1 起算。下一期原尺度值取相邻累加预测之差。 forecast = float((x[0] - b / a) * (np.exp(-4 * a) - np.exp(-3 * a))) assert np.allclose([a, b], [-.4, 1.6]) and 9.7 < forecast < 10.0 result = {"a": float(a), "b": float(b), "background": background.tolist(), "forecast": forecast} print("LAB_RESULT " + json.dumps(result, ensure_ascii=False))