def test_linearity_mul(self, n, window, mode, const): np.random.seed(42) a0 = np.random.randn(10, 20) a1 = const * a0 diff0 = fdiff(a0, n, window=window, mode=mode) diff1 = fdiff(a1, n, window=window, mode=mode) assert_allclose(const * diff0, diff1)
def test_linearity_add(self, n, window, mode): np.random.seed(42) a0 = np.random.randn(10, 20) a1 = np.random.randn(10, 20) a2 = a0 + a1 diff0 = fdiff(a0, n, window=window, mode=mode) diff1 = fdiff(a1, n, window=window, mode=mode) diff2 = fdiff(a2, n, window=window, mode=mode) assert_allclose(diff0 + diff1, diff2)
def test_mode(self, n, window): np.random.seed(42) a = np.random.randn(10, 20) out_f = fdiff(a, n, axis=0, window=window, mode="full") out_v = fdiff(a, n, axis=0, window=window, mode="valid") assert_array_equal(out_f[window - 1:, :], out_v) out_f = fdiff(a, n, axis=1, window=window, mode="full") out_v = fdiff(a, n, axis=1, window=window, mode="valid") assert_array_equal(out_f[:, window - 1:], out_v)
def test_integer_n(self, n, ndim): """ fdiff with integer n is equal to numpy.diff """ np.random.seed(42) a = np.random.randn(*([100] * ndim)) for axis in range(ndim): out = np.diff(a, n=int(n), axis=axis) assert_array_equal(fdiff(a, n=n, axis=axis), out)
def test_one(self, n, window, n_blanks_1, n_blanks_2, n_features): a = np.concatenate(( np.zeros((window + n_blanks_1, n_features)), np.ones((1, n_features)), np.zeros((window + n_blanks_2, n_features)), )) coef = fdiff_coef(n, window) diff = fdiff(a, n=n, window=window, axis=0) for i in range(n_features): assert_array_equal(diff[window + n_blanks_1:, i][:window], coef) assert_equal(diff[:window + n_blanks_1], 0) assert_equal(diff[window + n_blanks_1 + window:], 0)
def test_append(self, n, window, mode): a = np.random.randn(10, 20) append = np.random.randn(5, 20) cat = np.concatenate((a, append), axis=0) out = fdiff(cat, n, axis=0, window=window, mode=mode) result = fdiff(a, n, axis=0, window=window, append=append, mode=mode) assert_array_equal(result, out) append = np.random.randn(10, 5) cat = np.concatenate((a, append), axis=1) out = fdiff(cat, n, axis=1, window=window, mode=mode) result = fdiff(a, n, axis=1, window=window, append=append, mode=mode) assert_array_equal(result, out) cat = np.concatenate((a, np.zeros((1, 20))), axis=0) out = fdiff(cat, n, axis=0, window=window, mode=mode) result = fdiff(a, n, axis=0, window=window, append=0, mode=mode) assert_array_equal(result, out) cat = np.concatenate((a, np.zeros((10, 1))), axis=1) out = fdiff(cat, n, axis=1, window=window, mode=mode) result = fdiff(a, n, axis=1, window=window, append=0, mode=mode) assert_array_equal(result, out)
def _add_diff(self, _df, _diff_d, _name, _add=True): """ 为 数据列 添加 差分数据列 :param _add: :param _df: 原数据 DataFrame :param _d_list: 差分阶数 [0.3, 0.5, 0.7] :param _name: 需要添加 差分值 的数据列 名称 :param _agg_dict: :param _agg_type: :param _add: :return: """ if _add: if len(_df) >= 12: # 数据行数大于等于12才进行差分操作 _diff_ar = fdiff(_df[_name], n=_diff_d, window=10, mode="valid") # 列差分,不使用未来数据 _paddings = len(_df) - len(_diff_ar) # 差分后数据长度变短,需要在前面填充多少数据 _diff = np.nan_to_num(np.concatenate((np.full(_paddings, 0), _diff_ar)), nan=0) # 将所有nan替换为0 _df[_name + f'_diff_{_diff_d}'] = _diff # 将差分数据记录到 DataFrame else: _df[_name + f'_diff_{_diff_d}'] = np.nan # 数据行数不足12的填充为空数据
def test_transform(self, d, window, mode): np.random.seed(42) X = np.random.randn(100, 200) fracdiff = Fracdiff(d=d, window=window, mode=mode) out = fdiff(X, n=d, axis=0, window=window, mode=mode) assert_array_equal(fracdiff.fit_transform(X), out)
def test_axis(self, n): np.random.seed(42) a = np.random.randn(10, 20, 30, 40) out1 = np.swapaxes(fdiff(np.swapaxes(a, 0, 1), n, axis=0), 0, 1) out2 = np.swapaxes(fdiff(np.swapaxes(a, 0, 2), n, axis=0), 0, 2) out3 = np.swapaxes(fdiff(np.swapaxes(a, 0, 3), n, axis=0), 0, 3) assert_array_equal(fdiff(a, n, axis=1), out1) assert_array_equal(fdiff(a, n, axis=2), out2) assert_array_equal(fdiff(a, n, axis=3), out3) assert_array_equal(fdiff(a, n, axis=0), fdiff(a, n, axis=-4)) assert_array_equal(fdiff(a, n, axis=1), fdiff(a, n, axis=-3)) assert_array_equal(fdiff(a, n, axis=2), fdiff(a, n, axis=-2)) assert_array_equal(fdiff(a, n, axis=3), fdiff(a, n, axis=-1))