示例#1
0
def estimate_ar_coefficients(r, M):
    '''
    Runs Levinson-Durbin to compute the AR coefficients,
    reflection coefficients, and prediction error powers up to M.
    This function then uses a least squares fit to compute the 
    reflection coefficients from the AR coefficients and compares
    the results.
    '''
    # Levinson Durbin:
    _, ar_coeff, reflec_coeff, _, _ = stattools.levinson_durbin(r, M)
    delta_0 = r.mean()*np.sum(ar_coeff)
    
    print('\nLevinson Durbin Reflection coefficients:', -reflec_coeff)
    print('Levinson-durbin delta:', delta_0)

    # Least squares: (the actual regression is needed for the delta term)
    y = np.transpose(np.array(r[M:], ndmin=2))
    x = np.zeros([r.size-M, M])
    x = np.insert(x, 0, 1, axis=1) # for the delta term (y-intercept)
    for i in range(M, r.size):
        for j in range(1, M+1):
            x[i-M, j] = r[i-j]
        
    beta = np.linalg.inv(np.transpose(x) @ x) @ np.transpose(x) @ y

    ols_pacf = stattools.pacf_ols(r, M)
    print('\nLeast Squares Reflection coefficients:', -1*ols_pacf) 
    print('Least Squares delta:', np.sum(beta[1:])*r.mean(), end='\n\n')

    return np.sum((y - x @ beta)**2)/y.size
示例#2
0
 def test_ols_inefficient(self):
     lag_len = 5
     pacfols = pacf_ols(self.x, nlags=lag_len, efficient=False)
     x = self.x.copy()
     x -= x.mean()
     n = x.shape[0]
     lags = np.zeros((n - 5, 5))
     lead = x[5:]
     direct = np.empty(lag_len + 1)
     direct[0] = 1.0
     for i in range(lag_len):
         lags[:, i] = x[5 - (i + 1):-(i + 1)]
         direct[i + 1] = lstsq(lags[:, :(i + 1)], lead, rcond=None)[0][-1]
     assert_allclose(pacfols, direct, atol=1e-8)
示例#3
0
 def test_ols_inefficient(self):
     lag_len = 5
     pacfols = pacf_ols(self.x, nlags=lag_len, efficient=False)
     x = self.x.copy()
     x -= x.mean()
     n = x.shape[0]
     lags = np.zeros((n - 5, 5))
     lead = x[5:]
     direct = np.empty(lag_len + 1)
     direct[0] = 1.0
     for i in range(lag_len):
         lags[:, i] = x[5 - (i + 1):-(i + 1)]
         direct[i + 1] = lstsq(lags[:, :(i + 1)], lead, rcond=None)[0][-1]
     assert_allclose(pacfols, direct, atol=1e-8)
示例#4
0
def plotACFandPACF(ts_log_diff):
    lag_acf = acf(ts_log_diff, nlags=20)
    lag_pacf = pacf_ols(ts_log_diff, nlags=20)
    
    #Plot ACF: 
    plt.subplot(121) 
    plt.plot(lag_acf)
    plt.axhline(y=0,linestyle='--',color='gray')
    plt.axhline(y=-1.96/np.sqrt(len(ts_log_diff)),linestyle='--',color='gray')
    plt.axhline(y=1.96/np.sqrt(len(ts_log_diff)),linestyle='--',color='gray')
    plt.title('Autocorrelation Function')
    
    #Plot PACF:
    plt.subplot(122)
    plt.plot(lag_pacf)
    plt.axhline(y=0,linestyle='--',color='gray')
    plt.axhline(y=-1.96/np.sqrt(len(ts_log_diff)),linestyle='--',color='gray')
    plt.axhline(y=1.96/np.sqrt(len(ts_log_diff)),linestyle='--',color='gray')
    plt.title('Partial Autocorrelation Function')
    plt.tight_layout()    
    plt.show()
示例#5
0
def test_pacf_ols():
    pacfols = tsa.pacf_ols(x100, 20)
    assert_array_almost_equal(mlpacf.pacf100.ravel(), pacfols, 8)
    pacfols = tsa.pacf_ols(x1000, 20)
    assert_array_almost_equal(mlpacf.pacf1000.ravel(), pacfols, 8)
示例#6
0
clicksPerDay = clicksPerDay.set_index(newIndex)
encountersPerDay = encountersPerDay.set_index(newIndex)

#clicksPerDay.index.name = None
#encountersPerDay.index.name = None

clicksPerDay = pd.Series(data=clicksPerDay["count_clicks"], index=clicksPerDay.index)
encountersPerDay = pd.Series(data=encountersPerDay["count_encounter"], index=encountersPerDay.index)

clicksPerDay = clicksPerDay.fillna(method="ffill")
encountersPerDay = encountersPerDay.fillna(method="ffill")



####################LJUNG-BOX####################
clicksPACF = stattools.pacf_ols(clicksPerDay, nlags=MAX_LAG)
encountersPACF = stattools.pacf_ols(encountersPerDay, nlags=MAX_LAG)

#my implementation
results = _math.ljungBox(encountersPACF, len(encountersPerDay), MAX_LAG)
lag, R, Q, p = zip(*results)
print np.asarray(Q[1:])
print np.asarray(p[1:])

#statsmodels implementation
results = diagnostic.acorr_ljungbox(encountersPerDay, lags=MAX_LAG)
print results

#my copy of the statsmodels implementation
results = _math.ljungBox2(encountersPerDay, maxlag=MAX_LAG)
print results
示例#7
0
df1.head()
df2.head()

import warnings

warnings.filterwarnings('ignore')  #to ignore the warnings which is unnecessary

df = pd.DataFrame({'a': [12, 5, 11, 12, 9]})

acf(df['a'])

pacf_yw(df['a'], nlags=4, method='mle')

pacf_yw(df['a'], nlags=4, method='unbiased')

pacf_ols(df['a'], nlags=4)
'''
Method in the above use different types of calculation corralation itself
'''

from pandas.plotting import lag_plot

lag_plot(df1['Thousands of Passengers'])

lag_plot(df2['Births'])

from statsmodels.graphics.tsaplots import plot_acf, plot_pacf

plot_acf(df1, lags=40)
plot_acf(df2, lags=40)
示例#8
0
def test_pacf_ols():
    pacfols = tsa.pacf_ols(x100, 20)
    assert_array_almost_equal(mlpacf.pacf100.ravel(), pacfols, 8)
    pacfols = tsa.pacf_ols(x1000, 20)
    assert_array_almost_equal(mlpacf.pacf1000.ravel(), pacfols, 8)
示例#9
0
 def test_ols(self):
     pacfols = pacf_ols(self.x, nlags=40)
     assert_almost_equal(pacfols[1:], self.pacfols, DECIMAL_6)