Example #1
0
def test_explicit_period():
    period = 200
    s = sequences.sine(1.0, period, CYCLES, period // 3)
    # wrong but plausible
    seasons, _ = seasonal.fit_seasons(s, period=period - 2)
    assert seasons is not None and len(seasons) == period - 2
    # flat out wrong
    seasons, _ = seasonal.fit_seasons(s, period=period // 2)
    assert seasons is None
Example #2
0
def test_explicit_period():
    period = 200
    s = sequences.sine(1.0, period, CYCLES, period / 3)
    # wrong but plausible
    seasons, _ = seasonal.fit_seasons(s, period=period - 2)
    assert seasons is not None and len(seasons) == period - 2
    # flat out wrong
    seasons, _ = seasonal.fit_seasons(s, period=period / 2)
    assert seasons is None
Example #3
0
def test_sine_trend():
    """a linear trend with sine wave superimposed"""
    for kind in ["mean", "median", "spline", "line"]:
        for period in PERIODS:
            for slope in SLOPES:
                s = sequences.sine(AMP, period, CYCLES, period / 3)
                trend = np.arange(len(s)) * slope / len(s)
                s += trend
                a = fit_trend(s, kind=kind)
                rmse = np.sqrt(np.mean((a - trend) ** 2))
                assert rmse <= s.std() / 2.0
Example #4
0
def test_sine_trend():
    """a linear trend with sine wave superimposed"""
    for kind in ["mean", "median", "spline", "line"]:
        for period in PERIODS:
            for slope in SLOPES:
                s = sequences.sine(AMP, period, CYCLES, period // 3)
                trend = np.arange(len(s)) * slope / len(s)
                s += trend
                a = fit_trend(s, kind=kind)
                rmse = np.sqrt(np.mean((a - trend) ** 2))
                assert rmse <= s.std() / 2.0
Example #5
0
def try_sine(period, noise=0.0, reps=1):
    npass = 0
    s = sequences.sine(1.0, period, CYCLES, period / 3)
    for _ in range(reps):
        ns = sequences.add_noise(sequences.mix(s, 0, noise), noise)
        ns = ns - ns.mean()
        p = fit_period(ns)
        if period - EPS <= p and p <= period + EPS:
            npass += 1
    print "try_sine noise {0} period {1} -> {2}%".format(
        noise, period, (100 * npass) / reps)
    return npass
Example #6
0
def try_sine(period, noise=0.0, reps=1):
    npass = 0
    s = sequences.sine(1.0, period, CYCLES, period // 3)
    for _ in range(reps):
        ns = sequences.add_noise(sequences.mix(s, 0, noise), noise)
        ns = ns - ns.mean()
        p = fit_period(ns)
        if period - EPS <= p and p <= period + EPS:
            npass += 1
    print("try_sine noise {0} period {1} -> {2}%".format(
        noise, period, (100 * npass) / reps))
    return npass
Example #7
0
def test_sine_bend():
    """trend up then down, with sine wave superimposed"""
    for kind in ["mean", "median", "spline"]:
        for period in PERIODS:
            for slope in SLOPES:
                s = sequences.sine(AMP, period, CYCLES, period / 3)
                trend = np.arange(len(s)+1) * slope / len(s)
                trend = np.concatenate((trend[::2], trend[::-2]))[:len(s)]
                s += trend
                a = fit_trend(s, kind=kind)
                rmse = np.sqrt(np.mean((a - trend) ** 2))
                print kind, period, slope, rmse, s.std()
                assert rmse <= s.std() / 2.0
Example #8
0
def test_sine_bend():
    """trend up then down, with sine wave superimposed"""
    for kind in ["mean", "median", "spline"]:
        for period in PERIODS:
            for slope in SLOPES:
                s = sequences.sine(AMP, period, CYCLES, period // 3)
                trend = np.arange(len(s)+1) * slope / len(s)
                trend = np.concatenate((trend[::2], trend[::-2]))[:len(s)]
                s += trend
                a = fit_trend(s, kind=kind)
                rmse = np.sqrt(np.mean((a - trend) ** 2))
                print(kind, period, slope, rmse, s.std())
                assert rmse <= s.std() / 2.0
Example #9
0
# adjust_seasons() handles a variety of optional arguments.
# verify that adjust_trend() correctly calledfor different option combinations.
#
# No noise in this test set.
#
from __future__ import division
import numpy as np
from seasonal import fit_trend, adjust_seasons # pylint:disable=import-error
from seasonal.sequences import sine # pylint:disable=import-error

PERIOD = 25
CYCLES = 4
AMP = 1.0
TREND = AMP / PERIOD
LEVEL = 1000.0
SEASONS = sine(AMP, PERIOD, 1)
DATA = LEVEL + np.arange(PERIOD * CYCLES) * TREND + np.tile(SEASONS, CYCLES)
ZEROS = np.zeros(PERIOD * CYCLES)

def iszero(a):
    return np.all(np.isclose(a, ZEROS))

def isseasons(a):
    return np.all(np.isclose(a, SEASONS))

def test_auto():
    adjusted = adjust_seasons(DATA)
    assert adjusted.std() < DATA.std()

def test_trend_line():
    adjusted = adjust_seasons(DATA, trend="line")
Example #10
0
def test_sine():
    for period in PERIODS:
        s = sequences.sine(1.0, period, CYCLES, period / 3)
        p = periodogram_peaks(s, thresh=1.0)
        assert len(p) == 1 and p[0][2] <= period and p[0][3] >= period
Example #11
0
def test_sine():
    for period in PERIODS:
        s = sequences.sine(1.0, period, CYCLES, period // 3)
        p = periodogram_peaks(s, thresh=1.0)
        assert len(p) == 1 and p[0][2] <= period and p[0][3] >= period