Esempio n. 1
0
def exp_smoothing(df):
    '''
    Function to calculate exponential smoothing
    Pandas functions are used to calculate moving average and standard deviation
    Upper and lower thresholds are calculated by
    Upper threshold = moving average + standard deviation
    Lower threshold = moving average - standard deviation
    '''
    #Calculate exp weighted moving average
    ExpWeightedMovingAvg = ewma(df, span=N)
    ExpWeightedMovingAvg.columns = ['Exp Weighted Moving Avg']

    #Calculate exp weighted moving std
    ExpWeightedMovingStd = ewmstd(df, span=N)
    ExpWeightedMovingStd.columns = ['Std Deviation']

    s1 = df['Original data']
    s2 = ExpWeightedMovingAvg['Exp Weighted Moving Avg']
    s3 = ExpWeightedMovingStd['Std Deviation']
    s4 = s2.add(s3, fill_value=0)
    s5 = s2.sub(s3, fill_value=0)

    df2 = DataFrame(s1, columns=['Original data'])
    df2['Exp Weighted Moving Avg'] = s2
    df2['Std Deviation'] = s3
    df2['Upper Threshold'] = s4
    df2['Lower Threshold'] = s5

    return df2
def exp_smoothing(df):
    '''
    Function to calculate exponential smoothing
    Pandas functions are used to calculate moving average and standard deviation
    Upper and lower thresholds are calculated by
    Upper threshold = moving average + standard deviation
    Lower threshold = moving average - standard deviation
    '''
    #Calculate exp weighted moving average
    ExpWeightedMovingAvg = ewma(df,span=N)
    ExpWeightedMovingAvg.columns=['Exp Weighted Moving Avg']

    #Calculate exp weighted moving std
    ExpWeightedMovingStd = ewmstd(df,span=N)
    ExpWeightedMovingStd.columns=['Std Deviation']

    s1=df['Original data']
    s2=ExpWeightedMovingAvg['Exp Weighted Moving Avg']
    s3=ExpWeightedMovingStd['Std Deviation']
    s4=s2.add(s3, fill_value=0)
    s5=s2.sub(s3, fill_value=0)

    df2=DataFrame(s1,columns=['Original data'])
    df2['Exp Weighted Moving Avg']=s2
    df2['Std Deviation']=s3
    df2['Upper Threshold']=s4
    df2['Lower Threshold']=s5

    return df2
Esempio n. 3
0
def stddev_from_moving_average(timeseries):
    """
    A timeseries is anomalous if the absolute value of the average of the latest
    three datapoint minus the moving average is greater than three standard
    deviations of the moving average. This is better for finding anomalies with
    respect to the short term trends.
    """
    # print('算法计算: stddev_from_moving_average')

    series = pandas.Series([x[1] for x in timeseries])
    expAverage = moments.ewma(series, com=50)
    stdDev = moments.ewmstd(series, com=50)

    return abs(series.iget(-1) - expAverage.iget(-1)) > 3 * stdDev.iget(-1)
Esempio n. 4
0
    def test_rolling_std_neg_sqrt(self):
        # unit test from Bottleneck

        # Test move_nanstd for neg sqrt.

        a = np.array([
            0.0011448196318903589, 0.00028718669878572767,
            0.00028718669878572767, 0.00028718669878572767,
            0.00028718669878572767
        ])
        b = mom.rolling_std(a, window=3)
        self.assert_(np.isfinite(b[2:]).all())

        b = mom.ewmstd(a, span=3)
        self.assert_(np.isfinite(b[2:]).all())
Esempio n. 5
0
    def test_rolling_std_neg_sqrt(self):
        # unit test from Bottleneck

        # Test move_nanstd for neg sqrt.

        a = np.array([0.0011448196318903589,
                      0.00028718669878572767,
                      0.00028718669878572767,
                      0.00028718669878572767,
                      0.00028718669878572767])
        b = mom.rolling_std(a, window=3)
        self.assertTrue(np.isfinite(b[2:]).all())

        b = mom.ewmstd(a, span=3)
        self.assertTrue(np.isfinite(b[2:]).all())
Esempio n. 6
0
def auto_envelope(s, nema=22, nsmooth=100, ndev=2.7):
    sema = ema(s.close, nema)
    mdiff = s[['high', 'low']].sub(sema, axis=0).abs().max(axis=1)
    csize = moments.ewmstd(mdiff, nsmooth) * ndev

    return DataFrame(dict(ema=sema, lenv=sema - csize, henv=sema + csize))
Esempio n. 7
0
def auto_envelope(s, nema=22, nsmooth=100, ndev=2.7):
    sema = ema(s.close, nema)
    mdiff = s[['high','low']].sub(sema, axis=0).abs().max(axis=1)
    csize = moments.ewmstd(mdiff, nsmooth)*ndev

    return DataFrame(dict(ema=sema, lenv=sema - csize, henv=sema + csize))