def test_index_mass_quantile_relative(x): result = IndexMassQuantile(q=0.5, rel=True).transform(x) for i, a in series(x): actual = result.values[i] a = np.abs(a) expected = (np.argmax((np.cumsum(a) / np.sum(a)) >= 0.5) + 1) / len(a) np.testing.assert_almost_equal(actual, expected)
def test_entropy(x): x = Collection.from_array(np.abs(x.values) + 1) # ensure that values > 0 result = Entropy().transform(x) for i, a in series(x): actual = result.values[i] expected = stats.entropy(a) np.testing.assert_almost_equal(actual, expected)
def test_ratio_absolute(x, y): result = Ratio().transform(x, y) for i, a, b in series(x, y): actual = result.values[i] with np.errstate(divide='ignore', invalid='ignore'): expected = a / b np.testing.assert_almost_equal(actual, expected)
def test_high_standard_deviation_one(x): result = HighStandardDeviation(r=1).transform(x) assert np.issubdtype(result.dtype, np.bool_) for i, a in series(x): actual = result.values[i] expected = np.std(a) > 1 * (np.max(a) - np.min(a)) np.testing.assert_almost_equal(actual, expected)
def test_number_of_peaks_cwt(x): result = NumberPeaksCWT(max_width=5).transform(x) for i, a in series(x): actual = result.values[i] expected = len( signal.find_peaks_cwt(a, np.arange(1, 6), wavelet=signal.ricker)) np.testing.assert_almost_equal(actual, expected)
def test_high_variance_1(x): result = HighVariance(threshold=1).transform(x) assert np.issubdtype(result.dtype, np.bool_) for i, a in series(x): actual = result.values[i] expected = np.var(a) > 1 np.testing.assert_almost_equal(actual, expected)
def test_difference_relative(x, y): result = Difference(rel=True).transform(x, y) for i, a, b in series(x, y): actual = result.values[i] with np.errstate(divide='ignore', invalid='ignore'): expected = np.abs((b - a) / a) np.testing.assert_almost_equal(actual, expected)
def test_resample_down(x): num = int(x.shape[1] / 2) result = Resample(num=num).transform(x) for i, a in series(x): actual = result.values[i] expected = signal.resample(a, num=num) np.testing.assert_almost_equal(actual, expected)
def test_time_reversal_asymmetry_statistic(x): result = TimeReversalAsymmetryStatistic(lag=1).transform(x) for i, a in series(x): actual = result.values[i] n = len(a) - 2 expected = np.mean(a[2:][:n] * a[2:][:n] * a[1:][:n] - a[1:][:n] * a[:n] * a[:n]) np.testing.assert_almost_equal(actual, expected)
def test_cross_correlation(x, y): result = CrossCorrelation().transform(x, y) for i, a, b in series(x, y): actual = result.values[i] np.testing.assert_almost_equal(actual[0], np.correlate(a, b)[0]) for lag in range(1, len(actual)): np.testing.assert_almost_equal(actual[lag], np.correlate(a[lag:], b[:-lag])[0])
def test_symmetry_looking_1(x): result = SymmetryLooking(r=1).transform(x) assert np.issubdtype(result.dtype, np.bool_) for i, a in series(x): actual = result.values[i] expected = np.abs(np.mean(a) - np.median(a)) < 1 * (np.max(a) - np.min(a)) np.testing.assert_almost_equal(actual, expected)
def test_fft_moments(x): def moment(a, moment): return a.dot(np.arange(len(a))**moment) / np.sum(a) def mean(a): return moment(a, 1) def variance(a): return moment(a, 2) - mean(a)**2 def skewness(a): return (moment(a, 3) - 3 * mean(a) * variance(a) - mean(a)**3) / variance(a)**(1.5) def kurtosis(a): return ((moment(a, 4) - 4 * mean(a) * moment(a, 3) + 6 * moment(a, 2) * mean(a)**2 - 3 * mean(a)) / variance(a)**2) fft = FFT().transform(x) result_mean = SpectralMean().transform(x) result_variance = SpectralVariance().transform(x) result_skewness = SpectralSkewness().transform(x) result_kurtosis = SpectralKurtosis().transform(x) with warnings.catch_warnings(): warnings.simplefilter("ignore") for i, a in series(x): np.testing.assert_allclose(result_mean.values[i], mean(a)) np.testing.assert_allclose(result_variance.values[i], variance(a)) np.testing.assert_allclose(result_skewness.values[i], skewness(a)) np.testing.assert_allclose(result_kurtosis.values[i], kurtosis(a))
def test_auto_correlation(x): result = AutoCorrelation().transform(x) for i, a in series(x): actual = result.values[i] mu = np.mean(a) var = np.var(a) np.testing.assert_almost_equal(actual[0], np.mean((a - mu) * (a - mu)) / var) for lag in range(1, len(actual)): expected = np.mean((a[:-lag] - mu) * (a[lag:] - mu)) / var np.testing.assert_almost_equal(actual[lag], expected)
def test_friedrich_coefficients_m_3(x): result = FriedrichCoefficients(m=3, r=10).transform(x) for i, a in series(x): actual = result.values[i] df = pd.DataFrame({'signal': a[:-1], 'delta': np.diff(a)}) df['quantiles'] = pd.qcut(df.signal, 10) q = df.groupby('quantiles') mean = pd.DataFrame({'x': q.signal.mean(), 'y': q.delta.mean()}) expected = np.polyfit(mean.x, mean.y, deg=3) np.testing.assert_almost_equal(actual, expected)
def test_max_langevin_fixed_point_m_3(x): result = MaxLangevinFixedPoint(m=3, r=10).transform(x) for i, a in series(x): actual = result.values[i] df = pd.DataFrame({'signal': a[:-1], 'delta': np.diff(a)}) df['quantiles'] = pd.qcut(df.signal, 10) q = df.groupby('quantiles') mean = pd.DataFrame({'x': q.signal.mean(), 'y': q.delta.mean()}) coef = np.polyfit(mean.x, mean.y, deg=3) expected = np.max(np.roots(coef).real) np.testing.assert_almost_equal(actual, expected)
def test_single_pass_statistics(x): result = SinglePassStatistics().transform(x) for i, a in series(x): actual = result.values[i] np.testing.assert_almost_equal(actual[0], len(a)) np.testing.assert_almost_equal(actual[1], np.sum(a)) np.testing.assert_almost_equal(actual[2], np.min(a)) np.testing.assert_almost_equal(actual[3], np.max(a)) np.testing.assert_almost_equal(actual[4], np.mean(a)) np.testing.assert_almost_equal(actual[5], np.var(a)) np.testing.assert_almost_equal(actual[6], stats.skew(a)) np.testing.assert_almost_equal(actual[7], stats.kurtosis(a))
def test_binned_distribution(x): result = BinnedDistribution(bins=10).transform(x) for i, a in series(x): actual = result.values[i] r = (np.min(a), np.max(a)) s = (r[1] - r[0]) / 10 for bin in range(10): lower = r[0] + bin * s upper = r[0] + (bin + 1) * s if bin < 10 - 1: count = len(np.where((a >= lower) & (a < upper))[0]) else: count = len(np.where(a >= lower)[0]) ratio = count / len(a) np.testing.assert_almost_equal(actual[bin], ratio)
def test_binned_entropy(x): result = BinnedEntropy(bins=10).transform(x) for i, a in series(x): actual = result.values[i] r = (np.min(a), np.max(a)) s = (r[1] - r[0]) / 10 bd = [] for bin in range(10): lower = r[0] + bin * s upper = r[0] + (bin + 1) * s if bin < 10 - 1: count = len(np.where((a >= lower) & (a < upper))[0]) else: count = len(np.where(a >= lower)[0]) bd.append(count / len(a)) np.testing.assert_almost_equal(actual, stats.entropy(bd))
def test_linear_trend(x): result = LinearTrend().transform(x) for i, a in series(x): actual = result.values[i] expected = stats.linregress(np.arange(len(a)), a) np.testing.assert_almost_equal(actual, expected)
def test_count_below_mean(x): result = CountBelowMean().transform(x) for i, a in series(x): actual = result.values[i] expected = np.sum(a < np.mean(a)) np.testing.assert_almost_equal(actual, expected)
def test_kurtosis(x): result = Kurtosis().transform(x) for i, a in series(x): actual = result.values[i] expected = stats.kurtosis(a) np.testing.assert_almost_equal(actual, expected)
def test_skewness(x): result = Skewness().transform(x) for i, a in series(x): actual = result.values[i] expected = stats.skew(a) np.testing.assert_almost_equal(actual, expected)
def test_variance(x): result = Variance().transform(x) for i, a in series(x): actual = result.values[i] expected = np.var(a) np.testing.assert_almost_equal(actual, expected)
def test_median(x): result = Median().transform(x) for i, a in series(x): actual = result.values[i] expected = np.median(a) np.testing.assert_almost_equal(actual, expected)
def test_range_count(x): result = RangeCount(min=0, max=1).transform(x) for i, a in series(x): actual = result.values[i] expected = np.sum((a >= 0) & (a < 1)) np.testing.assert_almost_equal(actual, expected)
def test_outliers_rel(x): result = Outliers(r=1.5, rel=True).transform(x) for i, a in series(x): actual = result.values[i] expected = np.sum(np.abs(a - np.mean(a)) > 1.5 * np.std(a)) / len(a) np.testing.assert_almost_equal(actual, expected)
def test_auto_regressive_coefficients(x): result = AutoRegressiveCoefficients().transform(x) for i, a in series(x): actual = result.values[i] expected = statsmodels.tsa.ar_model.AR(a).fit().params np.testing.assert_almost_equal(actual, expected)
def test_mean_second_derivative_central(x): result = MeanSecondDerivativeCentral().transform(x) for i, a in series(x): actual = result.values[i] expected = np.mean(a[2:] - 2 * a[1:-1] + a[:-2]) np.testing.assert_almost_equal(actual, expected)
def test_mean_change_abs(x): result = MeanChange(abs=True).transform(x) for i, a in series(x): actual = result.values[i] expected = np.mean(np.abs(np.diff(a))) np.testing.assert_almost_equal(actual, expected)
def test_sum_change(x): result = SumChange().transform(x) for i, a in series(x): actual = result.values[i] expected = np.sum(np.diff(a)) np.testing.assert_almost_equal(actual, expected)