def test_emwmv_unbiased(): # bias: True v = [np.nan, 2, np.nan, 6, 8, 5] s = Stream(v, "s") specs = [{ "alpha": 0.68, "adjust": True, "ignore_na": True, "min_periods": 3 }, { "alpha": 0.68, "adjust": True, "ignore_na": False, "min_periods": 3 }, { "alpha": 0.68, "adjust": False, "ignore_na": True, "min_periods": 3 }, { "alpha": 0.68, "adjust": False, "ignore_na": False, "min_periods": 3 }] for spec in specs: d = spec.copy() d["warmup"] = d["min_periods"] del d["min_periods"] var = s.ewm(**d).var(bias=False).rename("var") feed = DataFeed([var]) feed.compile() expected = list(pd.Series(v).ewm(**spec).var(bias=False)) actual = [] while feed.has_next(): actual += [feed.next()["var"]] assert all(np.isclose(actual, expected))
def test_ewa_beginning_na(): # adjust: True, ignore_na: False v = [np.nan, 2, np.nan, 6, 8, 5] s = Stream(v).rename("s") specs = [{ "alpha": 0.68, "adjust": True, "ignore_na": True, "min_periods": 3 }, { "alpha": 0.68, "adjust": True, "ignore_na": False, "min_periods": 3 }, { "alpha": 0.68, "adjust": False, "ignore_na": True, "min_periods": 3 }, { "alpha": 0.68, "adjust": False, "ignore_na": False, "min_periods": 3 }] for spec in specs: d = spec.copy() d["warmup"] = d["min_periods"] del d["min_periods"] mean = s.ewm(**d).mean().rename("mean") feed = DataFeed([mean]) feed.compile() expected = list(pd.Series(v).ewm(**spec).mean()) actual = [] while feed.has_next(): actual += [feed.next()["mean"]] assert all(np.isclose(actual, expected))
def test_ewma(): # adjust: True, ignore_na: True v = [5, 2, 4, 6] s = Stream(v, "s") mean = s.ewm(alpha=0.68, adjust=True, ignore_na=True).mean().rename("mean") feed = DataFeed([mean]) feed.compile() expected = list( pd.Series(v).ewm(alpha=0.68, adjust=True, ignore_na=True).mean()) actual = [] while feed.has_next(): actual += [feed.next()["mean"]] assert all(np.isclose(actual, expected)) # adjust: True, ignore_na: False v = [5, 2, np.nan, 6] s = Stream(v, "s") mean = s.ewm(alpha=0.68, adjust=True, ignore_na=False).mean().rename("mean") feed = DataFeed([mean]) feed.compile() expected = list( pd.Series(v).ewm(alpha=0.68, adjust=True, ignore_na=False).mean()) actual = [] while feed.has_next(): actual += [feed.next()["mean"]] assert all(np.isclose(actual, expected)) # adjust: True, ignore_na: False v = [5, 2, np.nan, 6] s = Stream(v, "s") mean = s.ewm(alpha=0.68, adjust=False, ignore_na=True).mean().rename("mean") feed = DataFeed([mean]) feed.compile() expected = list( pd.Series(v).ewm(alpha=0.68, adjust=False, ignore_na=True).mean()) actual = [] while feed.has_next(): actual += [feed.next()["mean"]] assert all(np.isclose(actual, expected)) # adjust: True, ignore_na: False v = [5, 2, np.nan, 6] s = Stream(v, "s") mean = s.ewm(alpha=0.68, adjust=False, ignore_na=False).mean().rename("mean") feed = DataFeed([mean]) feed.compile() expected = list( pd.Series(v).ewm(alpha=0.68, adjust=False, ignore_na=False).mean()) actual = [] while feed.has_next(): actual += [feed.next()["mean"]] assert all(np.isclose(actual, expected))