jmquintana79
5/9/2017 - 5:17 AM

timeseries analysis

Time series analysis

"""
TIMESERIES ANALYSIS

Input: Pandas Dataframe Series
Output:
- test stationarity
- correlation / auto-correlation
- decomposition

"""
def timeserie_analysis(y):

    from statsmodels.tsa.stattools import adfuller
    import statsmodels.api as sm

    ## TEST STATIONARITY
    def test_stationarity(timeseries):
        import pandas as pd
        import matplotlib.pyplot as plt

        #Determing rolling statistics
        rolmean = pd.rolling_mean(timeseries, window=12)
        rolstd = pd.rolling_std(timeseries, window=12)

        #Plot rolling statistics:
        fig = plt.figure(figsize=(12, 8))
        orig = plt.plot(timeseries, color='blue',label='Original')
        mean = plt.plot(rolmean, color='red', label='Rolling Mean')
        std = plt.plot(rolstd, color='black', label = 'Rolling Std')
        plt.legend(loc='best')
        plt.title('Rolling Mean & Standard Deviation')
        plt.show()

        #Perform Dickey-Fuller test:
        print('Results of Dickey-Fuller Test:')
        dftest = adfuller(timeseries, autolag='AIC')
        dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])
        for key,value in dftest[4].items():
            dfoutput['Critical Value (%s)'%key] = value
        print(dfoutput) 


    ## STATIONARITY TEST
    try:
        test_stationarity(y)
    except:
        print('ERROR: stationary test analysis')


    ## CORRELATION / AUTO-CORRELATION
    try:
        import matplotlib.pyplot as plt
        fig = plt.figure(figsize=(12,8))
        ax1 = fig.add_subplot(211)
        fig = sm.graphics.tsa.plot_acf(y.iloc[:], lags=40, ax=ax1)
        ax2 = fig.add_subplot(212)
        fig = sm.graphics.tsa.plot_pacf(y.iloc[:], lags=40, ax=ax2)
        plt.show()
    except:
        print('ERROR: correlation analysis')


    ## DECOMPOSITION

    try:
        from statsmodels.tsa.seasonal import seasonal_decompose
        import matplotlib.pyplot as plt

        # calculate decomposition
        decomposition = seasonal_decompose(y)
        trend = decomposition.trend
        seasonal = decomposition.seasonal
        residual = decomposition.resid

        # build chart object
        fig= plt.figure(figsize=(15,8))
        # plot original data
        ax1 = plt.subplot2grid((4,4),(0,0),colspan=4) 
        ax1.plot(y, label='Original')
        plt.legend(loc='best')
        # title
        plt.title('DECOMPOSITION',fontsize=18)
        # plot trend data
        ax2 = plt.subplot2grid((4,4),(1,0),colspan=4)
        ax2.plot(trend, label='Trend')
        plt.legend(loc='best')
        # plot seasonality
        ax3 = plt.subplot2grid((4,4),(2,0),colspan=4)
        ax3.plot(seasonal,label='Seasonality')
        plt.legend(loc='best')
        # plot residuals
        ax4 = plt.subplot2grid((4,4),(3,0),colspan=4)
        ax4.plot(residual, label='Residuals')
        plt.legend(loc='best')
        # display
        plt.tight_layout()
        plt.show()
    except:
        print('ERROR: decomposition analysis')

    return None