def test_tol_memory(self, d, tol): window = window_from_tol_memory(d, tol) lost_memory_0 = np.abs(np.sum(fdiff_coef(d, self.LARGE)[window:])) lost_memory_1 = np.abs(np.sum(fdiff_coef(d, self.LARGE)[window - 1:])) assert lost_memory_0 < tol assert lost_memory_1 > tol
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 fit(self, X=None, y=None): """ Fit the model with `X`. Set `self.window_` and `self.coef_`. Parameters ---------- - X : array-like, shape (n_samples, n_features), default None Ignored. - y : array-like, shape (n_samples,), default None Ignored. Returns ------- self """ self.coef_ = fdiff_coef(self.d, self.window) return self
def window_from_tol_coef(n, tol_coef, max_window=2**12) -> int: """ Return length of window determined from tolerance to memory loss. Tolerance of smallness of coefficient to determine the length of window. That is, `window_` is chosen as the minimum integer that makes the absolute value of the `window`-th fracdiff coefficient is smaller than `tol_coef`. Parameters ---------- - n : int Order of fractional differentiation. - tol_coef : float in range (0, 1) ... Notes ----- The window for small `d` or `tol_(memory|coef)` can become extremely large. For instance, window grows with the order of `tol_coef ** (-1 / d)`. Returns ------- window : int Length of window Examples -------- >>> window_from_tol_coef(0.5, 0.1) 4 >>> fdiff_coef(0.5, 3)[-1] -0.125 >>> fdiff_coef(0.5, 4)[-1] -0.0625 """ coef = np.abs(fdiff_coef(n, max_window)) return np.searchsorted(-coef, -tol_coef) + 1 # index -> length
def window_from_tol_memory(n, tol_memory, max_window=2**12) -> int: """ Return length of window determined from tolerance to memory loss. Minimum lenght of window that makes the absolute value of the sum of fracdiff coefficients from `window_ + 1`-th term is smaller than `tol_memory`. If `window` is not None, ignored. Notes ----- The window for small `d` or `tol_(memory|coef)` can become extremely large. For instance, window grows with the order of `tol_coef ** (-1 / d)`. Parameters ---------- - n : int Order of fractional differentiation. - tol_memory : float in range (0, 1) Tolerance of lost memory. Returns ------- window : int Length of window Examples -------- >>> window_from_tol_memory(0.5, 0.2) 9 >>> np.sum(fdiff_coef(0.5, 10000)[9:]) -0.19073... >>> np.sum(fdiff_coef(0.5, 10000)[8:]) -0.20383... """ lost_memory = np.abs(np.cumsum(fdiff_coef(n, max_window))) return np.searchsorted(-lost_memory, -tol_memory) + 1 # index -> length
def test_tol_coef(self, d, tol): window = window_from_tol_coef(d, tol) assert np.abs(fdiff_coef(d, window - 1)[-1]) > tol assert np.abs(fdiff_coef(d, window)[-1]) < tol
def test_coef(self, n, window): out = self._coef(n, window) assert_allclose(fdiff_coef(n, window), out)