Exemple #1
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
Exemple #2
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
Exemple #3
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
Exemple #4
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
Exemple #5
0
def filler(ts):

    tsC = ts.copy()

    filled = tsC.interpolate().fillna(0)

    # Find the trend inside the series
    trend = fit_trend(filled, kind='spline')

    trend = trend - trend.min()

    trendSeries = pd.Series(trend, tsC.index)

    detrended = tsC - trendSeries

    stdseason = detrended.groupby([detrended.index.month, detrended.index.day]).median()

    # Create fake yrs
    reshape = np.tile(stdseason, 3)
    reindex = np.tile(stdseason.index, 3)
    t = pd.Series(reshape, reindex)

    # TODO decide which filter it's the best one
    # Smooth by Savinsky Golet
    tSV = savgol_filter(t, 5, 2)  # TODO change to parametric

    # # Smooth by boxcar
    # tSV = t.rolling(5, win_type='bartlett', center=True).mean()  #parzen

    tsv = tSV[stdseason.count(): 2 * stdseason.count()]
    ps = pd.Series(tsv, stdseason.index)

    nanlist = tsC[tsC.isnull()]

    for index, value in nanlist.iteritems():

        nanlist.loc[index] = stdseason.loc[index.month, index.day]

    tsC.update(nanlist)

    return tsC
def test_explicit_trend():
    trend = fit_trend(DATA, kind="line")
    adjusted = adjust_seasons(DATA, trend=trend)
    assert adjusted.std() < DATA.std()
def test_trend_median():
    # median filter doesn't *quite* cancel a straight line
    adjusted = DATA - fit_trend(DATA, kind="median")
    adjusted = adjusted - adjusted.mean()
    assert iszero(adjusted)
def test_trend_mean():
    adjusted = DATA - fit_trend(DATA, kind="mean")
    adjusted = adjusted - adjusted.mean()
    assert iszero(adjusted)
def test_trend_spline():
    adjusted = DATA - fit_trend(DATA, kind="spline")
    adjusted = adjusted - adjusted.mean()
    assert iszero(adjusted)
def test_none():
    adjusted = DATA - fit_trend(DATA, kind=None)
    assert iszero(DATA - adjusted)
def test_trend_median():
    # median filter doesn't *quite* cancel a straight line
    fitted = fit_trend(DATA, kind="median")
    assert np.isclose(fitted.mean(), MID)
    assert iszero(DATA - fitted)
def test_trend_mean():
    fitted = fit_trend(DATA, kind="mean")
    assert np.isclose(fitted.mean(), MID)
    assert iszero(DATA - fitted)
def test_none():
    fitted = fit_trend(DATA, kind=None)
    assert np.isclose(fitted.mean(), MID)
def test_zero():
    adjusted = fit_trend(ZEROS + MID, kind="line")
    assert adjusted.mean() == MID