Assignment 6
Assignment 6
ipynb - Colab
a.)
1
2
3 import numpy as np
4 import pandas as pd
5 import matplotlib.pyplot as plt
6
7 # Sample data (replace with actual data from the file)
8
9 df = pd.read_excel("Test6.xlsx")
10 print(df.head())
11
12 # Handling 'NA' values (replace with appropriate method)
13 df.fillna(method='bfill', inplace=True)
14
15 # Create time series plots
16 plt.figure(figsize=(12, 8))
17
18 # CPI of Euro area and USA
19 plt.subplot(2, 2, 1)
20 plt.plot(df['YYYY-MM'], df['CPI_EUR'], label='Euro Area')
21 plt.plot(df['YYYY-MM'], df['CPI_USA'], label='USA')
22 plt.title('CPI')
23 plt.legend()
24
25 # Log(CPI) of Euro area and USA
26 plt.subplot(2, 2, 2)
27 plt.plot(df['YYYY-MM'], df['LOGPEUR'], label='Euro Area')
28 plt.plot(df['YYYY-MM'], df['LOGPUSA'], label='USA')
29 plt.title('Log(CPI)')
30 plt.legend()
31
32 # Monthly inflation (DP) of Euro area and USA
33 plt.subplot(2, 2, 3)
34 plt.plot(df['YYYY-MM'], df['DPEUR'], label='Euro Area')
35 plt.plot(df['YYYY-MM'], df['DPUSA'], label='USA')
36 plt.title('Monthly Inflation (DP)')
37 plt.legend()
38
39 plt.tight_layout()
40 plt.show()
41
42 # Conclusions (replace with your own analysis based on the plots)
43 print("Conclusions from the plots:")
44 print("- Both CPI and log(CPI) show similar trends for Euro area and USA.")
45 print("- Monthly inflation (DP) seems to be more volatile.")
46 print("- Further analysis is needed to determine the predictive power of US inflation on Euro ar
47
https://colab.research.google.com/drive/1leKs6__GCNSCY7yCsn-SaZt6j2crI5dr?authuser=0#scrollTo=m0EbDkQf-Vog&printMode=true 1/6
26/07/2024, 08:39 Untitled16.ipynb - Colab
b.)
https://colab.research.google.com/drive/1leKs6__GCNSCY7yCsn-SaZt6j2crI5dr?authuser=0#scrollTo=m0EbDkQf-Vog&printMode=true 2/6
26/07/2024, 08:39 Untitled16.ipynb - Colab
1
2
3 from statsmodels.tsa.stattools import adfuller
4
5 # Perform ADF test for Euro area log(CPI)
6 result_eur = adfuller(df['LOGPEUR'], regression='ct', autolag='AIC', maxlag=3)
7 print("ADF Test for Euro Area Log(CPI):")
8 print("t-value of Coefficient of log(CPIt-1):", result_eur[0])
9 print("Critical t-value:", result_eur[4]['5%'])
10 if result_eur[0] < result_eur[4]['5%']:
11 print("Conclusion: Reject null hypothesis, time series is stationary.")
12 else:
13 print("Conclusion: Fail to reject null hypothesis, time series is non-stationary.")
14
15 print("\n")
16
17 # Perform ADF test for USA log(CPI)
18 result_usa = adfuller(df['LOGPUSA'], regression='ct', autolag='AIC', maxlag=3)
19 print("ADF Test for USA Log(CPI):")
20 print("t-value of Coefficient of log(CPIt-1):", result_usa[0])
21 print("Critical t-value:", result_usa[4]['5%'])
22 if result_usa[0] < result_usa[4]['5%']:
23 print("Conclusion: Reject null hypothesis, time series is stationary.")
24 else:
25 print("Conclusion: Fail to reject null hypothesis, time series is non-stationary.")
26
1
2 import statsmodels.api as sm
3
4 # Perform OLS regression
5 X = sm.add_constant(df['LOGPUSA']) # Add a constant to the independent variable
6 model = sm.OLS(df['LOGPEUR'], X)
7 results = model.fit()
8
9 # Calculate residuals
10 residuals = results.resid
11
12 # Perform ADF test on residuals
13 result_residuals = adfuller(residuals, regression='ct', autolag='AIC', maxlag=3)
14 print("\nADF Test for Residuals:")
15 print("t-value of Coefficient of Residuals(t-1):", result_residuals[0])
16 if result_residuals[0] < -3.8:
17 print("Conclusion: Reject null hypothesis, residuals are stationary. Series are cointegrated.
18 else:
19 print("Conclusion: Fail to reject null hypothesis, residuals are non-stationary. Series are n
20
https://colab.research.google.com/drive/1leKs6__GCNSCY7yCsn-SaZt6j2crI5dr?authuser=0#scrollTo=m0EbDkQf-Vog&printMode=true 3/6
26/07/2024, 08:39 Untitled16.ipynb - Colab
1
2 import pandas as pd
3 import matplotlib.pyplot as plt
4 from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
5
6 # Calculate and plot sample autocorrelations
7 plt.figure(figsize=(12, 4))
8 plot_acf(df['DPEUR'].dropna(), lags=20)
9 plt.title('Sample Autocorrelations of DPEUR')
10 plt.show()
11
12 # Calculate and plot sample partial autocorrelations
13 plt.figure(figsize=(12, 4))
14 plot_pacf(df['DPEUR'].dropna(), lags=20)
15 plt.title('Sample Partial Autocorrelations of DPEUR')
16 plt.show()
17
18 # Fit the AR model
19 X = sm.add_constant(pd.concat([df['DPEUR'].shift(6), df['DPEUR'].shift(12)], axis=1).dropna())
20 y = df['DPEUR'].iloc[12:] # Adjust index to match lagged variables
21 ar_model = sm.OLS(y, X)
22 ar_results = ar_model.fit()
23
24 # Print the model summary
25 print(ar_results.summary())
26
https://colab.research.google.com/drive/1leKs6__GCNSCY7yCsn-SaZt6j2crI5dr?authuser=0#scrollTo=m0EbDkQf-Vog&printMode=true 4/6
26/07/2024, 08:39 Untitled16.ipynb - Colab
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
d.)
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
The p-value of DPUSA with lag 6 is very high, hence we can reject it and fit the model with remaining parameters
https://colab.research.google.com/drive/1leKs6__GCNSCY7yCsn-SaZt6j2crI5dr?authuser=0#scrollTo=m0EbDkQf-Vog&printMode=true 6/6