<jupyter_start><jupyter_text>Generate ACF plots for different lags p = {1,2,3…} and list down the top 5 optimal lags in sorted order of decreasing ACF. (Autocorrelation function)<jupyter_code>fig = plt.figure(figsize=(12,9))
ax1 = fig.add_subplot(211)
fig = sm.graphics.tsa.plot_acf(data_ABT['High'].values.squeeze(), lags=40, fft=True, ax=ax1)
ax2 = fig.add_subplot(212)
fig = sm.graphics.tsa.plot_pacf(data_ABT['High'], lags=40,ax=ax2)<jupyter_output><empty_output><jupyter_text>From the above autocorrelation fuction plot, we can deduce that p=1, p=2, p=3, p=4, p=5 are suitable lags, q=1, q=2  Case1: p=1, q=1<jupyter_code>arma_mod10 = sm.tsa.ARMA(A, (1,0)).fit(disp=False)
#print(arma_mod10.params)
print(arma_mod10.summary())
sm.stats.durbin_watson(arma_mod10.resid.values) # measure of autocorrelation in the residuals (below 2 indicates auttocorreltaion)
fig = plt.figure(figsize=(12,10))
ax = fig.add_subplot(111)
ax = arma_mod10.resid.plot(ax=ax);
ax.set_ylim([-4,+3])
ax.set_ylim([-4,+3])
#The Ljung–Box test is a type of statistical test of whether any of a 
#group of autocorrelations of a time series are different from zero
resid = arma_mod10.resid
r,q,p = sm.tsa.acf(resid, qstat=True)
data = np.c_[range(1,41), r[1:], q, p]
table = pd.DataFrame(data, columns=['lag', "AC", "Q", "Prob(>Q)"])
print(table.set_index('lag'))<jupyter_output>AC          Q      Prob(>Q)
lag                                    
1.0   0.055614  28.869725  7.741349e-08
2.0  -0.023889  34.197146  3.751321e-08
3.0  -0.014225  36.086194  7.180647e-08
4.0  -0.003444  36.196947  2.635915e-07
5.0  -0.008129  36.813947  6.526476e-07
6.0   0.003611  36.935705  1.812441e-06
7.0  -0.009032  37.697634  3.458746e-06
8.0  -0.001598  37.721492  8.474580e-06
9.0   0.007774  38.286016  1.548753e-05
10.0 -0.004705  38.492849  3.113953e-05
11.0 -0.013874  40.291460  3.189249e-05
12.0  0.006982  40.747039  5.400841e-05
13.0  0.012664  42.245870  5.969900e-05
14.0 -0.001697  42.272782  1.118907e-04
15.0  0.001352  42.289875  2.027885e-04
16.0 -0.011223  43.467497  2.375498e-04
17.0 -0.002893  43.545777  3.994660e-04
18.0  0.018140  46.622787  2.405179e-04
19.0 -0.000092  46.622866  4.043076e-04
20.0 -0.000934  46.631017  6.598450e-04
21.0 -0.016171  49.077189  4.895848e-04
22.0 -0.005910  49.403981  7.055317e-04
23.0 -0.005378  49.674580  1.016277e-03
[...]<jupyter_text>Case1: p=2, q=1<jupyter_code>#arma_mod20 = sm.tsa.ARMA(A, (2,1)).fit(start_params=[-0.5, -0.1,-0.1,0.2,0.1,0.1,0.5]transparams=False, disp=False)
sm.tsa.ARMA._fit_start_params?
print(arma_mod20.summary())
sm.stats.durbin_watson(arma_mod20.resid.values) # measure of autocorrelation in the residuals (below 2 indicates auttocorreltaion)
fig = plt.figure(figsize=(12,10))
ax = fig.add_subplot(111)
ax = arma_mod20.resid.plot(ax=ax);
ax.set_ylim([-4,+3])
ax.set_ylim([-4,+3])
#The Ljung–Box test is a type of statistical test of whether any of a 
#group of autocorrelations of a time series are different from zero
resid = arma_mod20.resid
r,q,p = sm.tsa.acf(resid, qstat=True)
data = np.c_[range(1,41), r[1:], q, p]
table = pd.DataFrame(data, columns=['lag', "AC", "Q", "Prob(>Q)"])
print(table.set_index('lag'))
arma_mod30 = sm.tsa.ARMA(A, (3,0)).fit(disp=False)
print(arma_mod30.summary())
sm.stats.durbin_watson(arma_mod30.resid.values) # measure of autocorrelation in the residuals (below 2 indicates auttocorreltaion)
resid = arma_mod30.resid
r,q,p = sm.tsa.acf(resid, qstat=True)
data = np.c_[range(1,41), r[1:], q, p]
table = pd.DataFrame(data, columns=['lag', "AC", "Q", "Prob(>Q)"])
print(table.set_index('lag'))
arma_mod40 = sm.tsa.ARMA(A, (4,0)).fit(disp=False)
print(arma_mod40.summary())
sm.stats.durbin_watson(arma_mod40.resid.values) # measure of autocorrelation in the residuals (below 2 indicates autocorreltaion)
resid = arma_mod40.resid
r,q,p = sm.tsa.acf(resid, qstat=True)
data = np.c_[range(1,41), r[1:], q, p]
table = pd.DataFrame(data, columns=['lag', "AC", "Q", "Prob(>Q)"])
print(table.set_index('lag'))
arma_mod50 = sm.tsa.ARMA(A, (5,0)).fit(disp=False)
print(arma_mod50.summary())
sm.stats.durbin_watson(arma_mod50.resid.values) # measure of autocorrelation in the residuals (below 2 indicates auttocorreltaion)
resid = arma_mod50.resid
r,q,p = sm.tsa.acf(resid, qstat=True)
data = np.c_[range(1,41), r[1:], q, p]
table = pd.DataFrame(data, columns=['lag', "AC", "Q", "Prob(>Q)"])
print(table.set_index('lag'))
path_AMZN='C:/Users/niharika/Desktop/data sciences/Springboard/Capstone 2/Stock/AMZN.csv'
data_AMZN= dataImport(path_AMZN)
data_AMZN.head()
B=data_AMZN[['High']]
B.head()
path_Apple='C:/Users/niharika/Desktop/data sciences/Springboard/Capstone 2/Stock/appleDaily 1981-2017.csv'
data_Apple= dataImport(path_Apple)
data_Apple.head()
C=data_Apple[['High']]
C.head()
path_BP='C:/Users/niharika/Desktop/data sciences/Springboard/Capstone 2/Stock/BP.csv'
data_BP= dataImport(path_BP)
data_BP.head()
D=data_BP[['High']]
D.head()
path_FORD='C:/Users/niharika/Desktop/data sciences/Springboard/Capstone 2/Stock/fordMotorCompany1972-2017.csv'
data_FORD= dataImport(path_FORD)
data_FORD.head()
E=data_FORD[['High']]
E.head()
data_AMZN['High'].plot(figsize=(12,8))
fig = plt.figure(figsize=(12,9))
ax1 = fig.add_subplot(211)
fig = sm.graphics.tsa.plot_acf(data_AMZN['High'].values.squeeze(), lags=40, ax=ax1)
ax2 = fig.add_subplot(212)
fig = sm.graphics.tsa.plot_pacf(data_AMZN['High'], lags=40, ax=ax2)
data_Apple['High'].plot(figsize=(12,8))
fig = plt.figure(figsize=(12,9))
ax1 = fig.add_subplot(211)
fig = sm.graphics.tsa.plot_acf(data_Apple['High'].values.squeeze(), lags=40, ax=ax1)
ax2 = fig.add_subplot(212)
fig = sm.graphics.tsa.plot_pacf(data_Apple['High'], lags=40, ax=ax2)
data_BP['High'].plot(figsize=(12,8))
fig = plt.figure(figsize=(12,9))
ax1 = fig.add_subplot(211)
fig = sm.graphics.tsa.plot_acf(data_BP['High'], lags=40, ax=ax1)
ax2 = fig.add_subplot(212)
fig = sm.graphics.tsa.plot_pacf(data_BP['High'], lags=40, ax=ax2)
data_FORD['High'].plot(figsize=(12,8))
fig = plt.figure(figsize=(12,9))
ax1 = fig.add_subplot(211)
fig = sm.graphics.tsa.plot_acf(data_FORD['High'].values.squeeze(), lags=40, ax=ax1)
ax2 = fig.add_subplot(212)
fig = sm.graphics.tsa.plot_pacf(data_FORD['High'], lags=40, ax=ax2)
from statsmodels.tsa.seasonal import seasonal_decompose
decomposition = seasonal_decompose(data_ABT.High, freq=12)
fig = plt.figure()  
fig = decomposition.plot()  
fig.set_size_inches(15, 8)
from statsmodels.tsa.stattools import adfuller
def test_stationarity(timeseries):
    
    #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)
test_stationarity(data_ABT.High)
data_ABT.High_log= data_ABT.High.apply(lambda x: np.log(x))  
test_stationarity(data_ABT.High_log)
import statsmodels.api as sm
mod = sm.tsa.statespace.SARIMAX(data_ABT.High, trend='n', order=(1,0,1), seasonal_order=(1,0,1,12), enforce_stationarity=False)
results = mod.fit()
print(results.summary())<jupyter_output>Statespace Model Results                                 
==========================================================================================
Dep. Variable:                               High   No. Observations:                 9331
Model:             SARIMAX(1, 0, 1)x(1, 0, 1, 12)   Log Likelihood                -542.568
Date:                            Tue, 01 Aug 2017   AIC                           1095.136
Time:                                    14:40:30   BIC                           1130.841
Sample:                                07-28-1980   HQIC                          1107.264
                                     - 07-26-2017                                         
Covariance Type:                              opg                                         
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
-----------------------[...]
{'fraction_non_alphanumeric': 0.1780473714615829, 'fraction_numerical': 0.11103408434430964, 'mean_word_length': 3.800887409872435, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '211876', 'license': 'MIT', 'n_tokens_mistral': 4063, 'n_tokens_neox': 3473, 'n_words': 622, 'path': 'Capstone 2.ipynb', 'repo_name': 'niharikabalachandra/Market-Risk-Management-with-Time-Series-Prediction-of-Stock-Market-Trends-', 'size': '8655'}