Code-SPA/dataset_property_new.py
def dynamic_assess(x, plot=1, y = None, round_number = 0, alpha = 0.01, freq = 1):
    """
    x: time series data of interests of size Nx1
    y: another time series for calculating corss-correlation of size Nx1
    plot: flag for plotting
    alpha: significance level
    freq: sampling frequency of the time series Hz
    
    Output:
        significant lags for ACF, PACF and CCF
    """
    
    #Dickey-Fuller Tests for statinonary, below alpha is staionary
    x=x.flatten()
    xdf = sm.tsa.stattools.adfuller(x,1)
    if xdf[1]> alpha:
        print('x is not stationary')
        
    if y is not None:
        ydf = sm.tsa.stattools.adfuller(y, 1)
        if ydf[1] < alpha:
            print('y is not stationary')        
    #ACF
    [acf, confint, qstat, acf_pvalues] = sm.tsa.stattools.acf(x, qstat = True, alpha = alpha)
    acf_detection = acf_pvalues < alpha #Ljung-Box Q-Statistic
    acf_lag = [i for i,u in enumerate(acf_detection) if u == True] 

    #PACF
    [pacf, confint_pacf] = sm.tsa.stattools.pacf(x, alpha = alpha)
    pacf_lag = [i for i,u in enumerate(pacf) if abs(u)>1.96/np.sqrt(x.shape[0])]
   
    #CCF
    if y is not None:
        plt.figure(figsize=(5,3))
        plt.xcorr(x,y, normed = True, usevlines=True, maxlags=20)
        plt.axhline(y=2.575*1/np.sqrt(x.shape[0]), color='blue', linestyle='--',alpha=0.9) #99% confidence interval
        plt.axhline(y=-2.575*1/np.sqrt(x.shape[0]), color='blue', linestyle='--',alpha=0.9) #99% confidence interval
        font = 15
        plt.title('Cross-correlation plot',fontsize=font)
        plt.xlabel('Lag',fontsize=font)
        plt.tick_params(labelsize=font-1)
        plt.tight_layout()
        plt.savefig('CCF_' + str(round_number)+'.png', dpi = 600,bbox_inches='tight')
    
    #FFT
    x=x.squeeze()
    if x.shape[0] % 2 != 0:
        x = x[:-1]
    #T = 1/freq
    L = x.shape[0]
    #t = np.linspace(0, L-1, num=L,endpoint=True)*T
    x_fft = np.fft.fft(x)
    P2 = abs(x_fft/L)
    P1 = P2[0:int(L/2+1)]
    P11 = P1[:]
    P1[1:-1] = 2*P11[1:-1]
    f = freq*np.linspace(0, int(L/2), num=int(L/2)+1,endpoint = True)/ L
    plt.figure(figsize=(5,3))
    plt.plot(f,P1)
    font=15
    plt.title('Single-sided amplitude spectrum',fontsize = font)
    plt.xlabel('frequncy (Hz)', fontsize = font)
    plt.ylabel('|P1(f)|', fontsize = font)
    plt.tight_layout()
    plt.savefig('FFT_' + str(round_number)+'.png', dpi = 600,bbox_inches='tight')
    
    return (acf_lag, pacf_lag)

    x: time series data of interests of size Nx1
    y: another time series for calculating corss-correlation of size Nx1
    plot: flag for plotting
    alpha: significance level
    freq: sampling frequency of the time series Hz
    
    Output:
        significant lags for ACF, PACF and CCF
    
time series data of interests of size Nx1
y: another time series for calculating corss-correlation of size Nx1
plot: flag for plotting
alpha: significance level
freq: sampling frequency of the time series Hz

significant lags for ACF, PACF and CCF
time series data of interests of size Nx1 y : another time series for calculating corss - correlation of size Nx1 plot : flag for plotting alpha : significance level freq : sampling frequency of the time series Hz significant lags for ACF PACF and CCF
def dynamic_assess(x, plot=1, y = None, round_number = 0, alpha = 0.01, freq = 1):
    x=x.flatten()
    xdf = sm.tsa.stattools.adfuller(x,1)
    if xdf[1]> alpha:
        print('x is not stationary')
    if y is not None:
        ydf = sm.tsa.stattools.adfuller(y, 1)
        if ydf[1] < alpha:
            print('y is not stationary')        
    [acf, confint, qstat, acf_pvalues] = sm.tsa.stattools.acf(x, qstat = True, alpha = alpha)
    acf_detection = acf_pvalues < alpha 
    acf_lag = [i for i,u in enumerate(acf_detection) if u == True] 
    [pacf, confint_pacf] = sm.tsa.stattools.pacf(x, alpha = alpha)
    pacf_lag = [i for i,u in enumerate(pacf) if abs(u)>1.96/np.sqrt(x.shape[0])]
    if y is not None:
        plt.figure(figsize=(5,3))
        plt.xcorr(x,y, normed = True, usevlines=True, maxlags=20)
        plt.axhline(y=2.575*1/np.sqrt(x.shape[0]), color='blue', linestyle='--',alpha=0.9) 
        plt.axhline(y=-2.575*1/np.sqrt(x.shape[0]), color='blue', linestyle='--',alpha=0.9) 
        font = 15
        plt.title('Cross-correlation plot',fontsize=font)
        plt.xlabel('Lag',fontsize=font)
        plt.tick_params(labelsize=font-1)
        plt.tight_layout()
        plt.savefig('CCF_' + str(round_number)+'.png', dpi = 600,bbox_inches='tight')
    x=x.squeeze()
    if x.shape[0] % 2 != 0:
        x = x[:-1]
    L = x.shape[0]
    x_fft = np.fft.fft(x)
    P2 = abs(x_fft/L)
    P1 = P2[0:int(L/2+1)]
    P11 = P1[:]
    P1[1:-1] = 2*P11[1:-1]
    f = freq*np.linspace(0, int(L/2), num=int(L/2)+1,endpoint = True)/ L
    plt.figure(figsize=(5,3))
    plt.plot(f,P1)
    font=15
    plt.title('Single-sided amplitude spectrum',fontsize = font)
    plt.xlabel('frequncy (Hz)', fontsize = font)
    plt.ylabel('|P1(f)|', fontsize = font)
    plt.tight_layout()
    plt.savefig('FFT_' + str(round_number)+'.png', dpi = 600,bbox_inches='tight')
    return (acf_lag, pacf_lag)
def dynamic_assess ( x , plot = 1 , y = None , round_number = 0 , alpha = 0.01 , freq = 1 ) : x = x . flatten ( ) xdf = sm . tsa . stattools . adfuller ( x , 1 ) if xdf [ 1 ] > alpha : print ( 'x is not stationary' ) if y is not None : ydf = sm . tsa . stattools . adfuller ( y , 1 ) if ydf [ 1 ] < alpha : print ( 'y is not stationary' ) [ acf , confint , qstat , acf_pvalues ] = sm . tsa . stattools . acf ( x , qstat = True , alpha = alpha ) acf_detection = acf_pvalues < alpha acf_lag = [ i for i , u in enumerate ( acf_detection ) if u == True ] [ pacf , confint_pacf ] = sm . tsa . stattools . pacf ( x , alpha = alpha ) pacf_lag = [ i for i , u in enumerate ( pacf ) if abs ( u ) > 1.96 / np . sqrt ( x . shape [ 0 ] ) ] if y is not None : plt . figure ( figsize = ( 5 , 3 ) ) plt . xcorr ( x , y , normed = True , usevlines = True , maxlags = 20 ) plt . axhline ( y = 2.575 * 1 / np . sqrt ( x . shape [ 0 ] ) , color = 'blue' , linestyle = '--' , alpha = 0.9 ) plt . axhline ( y = - 2.575 * 1 / np . sqrt ( x . shape [ 0 ] ) , color = 'blue' , linestyle = '--' , alpha = 0.9 ) font = 15 plt . title ( 'Cross-correlation plot' , fontsize = font ) plt . xlabel ( 'Lag' , fontsize = font ) plt . tick_params ( labelsize = font - 1 ) plt . tight_layout ( ) plt . savefig ( 'CCF_' + str ( round_number ) + '.png' , dpi = 600 , bbox_inches = 'tight' ) x = x . squeeze ( ) if x . shape [ 0 ] % 2 != 0 : x = x [ : - 1 ] L = x . shape [ 0 ] x_fft = np . fft . fft ( x ) P2 = abs ( x_fft / L ) P1 = P2 [ 0 : int ( L / 2 + 1 ) ] P11 = P1 [ : ] P1 [ 1 : - 1 ] = 2 * P11 [ 1 : - 1 ] f = freq * np . linspace ( 0 , int ( L / 2 ) , num = int ( L / 2 ) + 1 , endpoint = True ) / L plt . figure ( figsize = ( 5 , 3 ) ) plt . plot ( f , P1 ) font = 15 plt . title ( 'Single-sided amplitude spectrum' , fontsize = font ) plt . xlabel ( 'frequncy (Hz)' , fontsize = font ) plt . ylabel ( '|P1(f)|' , fontsize = font ) plt . tight_layout ( ) plt . savefig ( 'FFT_' + str ( round_number ) + '.png' , dpi = 600 , bbox_inches = 'tight' ) return ( acf_lag , pacf_lag )
x: time series data of interests of size Nx1
y: another time series for calculating corss-correlation of size Nx1
plot: flag for plotting
alpha: significance level
freq: sampling frequency of the time series Hz
x : time series data of interests of size Nx1 y : another time series for calculating corss - correlation of size Nx1 plot : flag for plotting alpha : significance level freq : sampling frequency of the time series Hz
"""
    x: time series data of interests of size Nx1
    y: another time series for calculating corss-correlation of size Nx1
    plot: flag for plotting
    alpha: significance level
    freq: sampling frequency of the time series Hz
    
    Output:
        significant lags for ACF, PACF and CCF
    """ #Dickey-Fuller Tests for statinonary, below alpha is staionary
 #ACF
 #Ljung-Box Q-Statistic
 #PACF
 #CCF
 #99% confidence interval
 #99% confidence interval
 #FFT
 #T = 1/freq
 #t = np.linspace(0, L-1, num=L,endpoint=True)*T

x
plot
y
round_number
alpha
freq