# -*- coding: utf-8 -*- """ Created on Thu Feb 27 10:37:59 2025 @author: Abdellah El Fallahi """ import pandas as pd import matplotlib.pyplot as plt filepath=r"D:/Enseignement/cours/AutomaticLearning/TP_2025/Exo1.xlsx" data=pd.read_excel(filepath,sheet_name="ARMA(2,2)",usecols=["Value"]) data # check stationary from statsmodels.tsa.stattools import adfuller stat=adfuller(data) stat # plot acf and pacf from statsmodels.graphics.tsaplots import plot_acf, plot_pacf plot_acf(data,title="Autocorrelation of data") plot_pacf(data) plt.show() from statsmodels.tsa.arima.model import ARIMA # fiting the model model MA(1) model_MA1=ARIMA(data, order=(0,0,1)) resultMA1=model_MA1.fit() print(resultMA1.summary()) # Print log-likelihood and AIC/BIC of MA(1) print("Log-Likelihood:", resultMA1.llf) print("AIC:", resultMA1.aic) print("BIC:", resultMA1.bic) # fiting the model AR(2) model_AR2=ARIMA(data, order=(2,0,0)) resultAR2=model_AR2.fit() print(resultAR2.summary()) # Print log-likelihood and AIC/BIC of AR(2) print("Log-Likelihood:", resultAR2.llf) print("AIC:", resultAR2.aic) print("BIC:", resultAR2.bic) # another way to display aic and bic values print(f"AIC: {resultAR2.aic}") print(f"BIC: {resultAR2.bic}") # Ft arma(2,1) mpdel model_ARMA=ARIMA(data, order=(2,0,1)) resultARMA=model_ARMA.fit() print(resultARMA.summary()) # Fit arima model with parameters (1,1,1) model_ARiMA=ARIMA(data, order=(2,1,1)) resultARMA=model_ARiMA.fit() print(resultARMA.summary()) # Print log-likelihood and AIC/BIC of AR(2) print("Log-Likelihood:", resultARMA.llf) print("AIC:", resultARMA.aic) print("BIC:", resultARMA.bic) # write the AIC and BIC as a table from tabulate import tabulate data = [ ["MA(1)", resultMA1.aic, resultMA1.bic], ["RA(2)", resultAR2.aic, resultAR2.bic], ["ARMA(2,1)", resultARMA.aic, resultARMA.bic] ] headers = ["Model", "AIC", "BIC"] print(tabulate(data, headers=headers, tablefmt="grid"))