Académique Documents
Professionnel Documents
Culture Documents
Inflation Forecast 1707125827
Inflation Forecast 1707125827
Inflation Forecast 1707125827
import warnings
warnings.filterwarnings('ignore')
df = pd.read_csv('US_inflation_rates.csv')
df.tail()
date value
913 2023-02-01 301.648
914 2023-03-01 301.808
915 2023-04-01 302.918
916 2023-05-01 303.294
917 2023-06-01 303.841
df.rename(columns={'value':'inflation'},inplace=True)
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 918 entries, 0 to 917
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 date 918 non-null object
1 inflation 918 non-null float64
dtypes: float64(1), object(1)
memory usage: 14.5+ KB
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 918 entries, 0 to 917
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 date 918 non-null datetime64[ns]
1 inflation 918 non-null float64
dtypes: datetime64[ns](1), float64(1)
memory usage: 14.5 KB
df.set_index('date',inplace=True)
df
inflation
date
1947-01-01 21.480
1947-02-01 21.620
1947-03-01 22.000
1947-04-01 22.000
1947-05-01 21.950
... ...
2023-02-01 301.648
2023-03-01 301.808
2023-04-01 302.918
2023-05-01 303.294
2023-06-01 303.841
inflation 0
dtype: int64
Identification du modèle
Analyse des donnèes
# affichage du graphique
plt.figure(figsize=(10,6))
plt.plot(df.index,df['inflation'], color='blue')
plt.title('Evolution de inflation')
plt.xlabel('date')
plt.ylabel('IPC')
plt.grid(True)
plt.show
# ACF
plot_acf(df['inflation'], lags=30, zero=True, ax=ax1)
ax1.set_title('ACF - sèrie inflation')
ax1.set_xlabel('Lag')
ax1.set_ylabel('Corrélation')
ax1.grid(True)
ax1.set_xticks(np.arange(0, 31, 1))
# PACF
plot_pacf(df['inflation'], lags=30, zero=True, ax=ax2)
ax2.set_title('PACF - sèrie inflation')
ax2.set_xlabel('Lag')
ax2.set_ylabel('Corrélation partielle')
ax2.grid(True)
ax2.set_xticks(np.arange(0, 31, 1))
plt.subplot(411)
plt.plot(df['inflation'], label='Série originale')
plt.legend(loc='best')
plt.subplot(412)
plt.plot(trend, label='Tendance')
plt.legend(loc='best')
plt.subplot(413)
plt.plot(seasonal, label='Saisonnalité')
plt.legend(loc='best')
plt.subplot(414)
plt.plot(residual, label='Résidus')
plt.legend(loc='best')
plt.tight_layout()
plt.show()
# Différenciation pour rendre la série stationnaire
differenced = df['inflation'].diff().dropna()
# Tracer l'ACF
plot_acf(differenced, lags=30, zero=True, ax=ax1)
ax1.set_title('ACF - sèrie inflation')
ax1.set_xlabel('Lag')
ax1.set_ylabel('Corrélation')
ax1.grid(True)
# Tracer le PACF
plot_pacf(differenced, lags=30, zero=True, ax=ax2)
ax2.set_title('PACF - sèrie inflation')
ax2.set_xlabel('Lag')
ax2.set_ylabel('Corrélation partielle')
ax2.grid(True)
| Métrique | Valeur |
|----------------|---------------------------|
| Valeur de test | -3.5827298316483045 |
| P-valeur | 0.006099910514823511 |
| Conclusion | La série est stationnaire |
# identification de l'ordre p d et q
p= 1
d= 1
q=1,3
# Ajuster le modèle
model_fit = model.fit()
SARIMAX Results
======================================================================
========
Dep. Variable: inflation No. Observations:
817
Model: ARIMA(1, 2, 1) Log Likelihood
-220.011
Date: Mon, 08 Jan 2024 AIC
446.022
Time: 17:01:18 BIC
460.132
Sample: 01-01-1947 HQIC
451.438
- 01-01-2015
======================================================================
========
coef std err z P>|z| [0.025
0.975]
----------------------------------------------------------------------
--------
ar.L1 0.4706 0.014 32.678 0.000 0.442
0.499
ma.L1 -0.9768 0.008 -126.837 0.000 -0.992
-0.962
sigma2 0.1002 0.002 48.348 0.000 0.096
0.104
======================================================================
=============
Ljung-Box (L1) (Q): 4.62 Jarque-Bera (JB):
15605.24
Prob(Q): 0.03 Prob(JB):
0.00
Heteroskedasticity (H): 35.74 Skew:
-1.73
Prob(H) (two-sided): 0.00 Kurtosis:
24.16
======================================================================
=============
Warnings:
[1] Covariance matrix calculated using the outer product of gradients
(complex-step).
plt.xlabel('Date')
plt.ylabel('inflation')
plt.title('Prédictions du modèle ARIMA')
plt.legend()
plt.show()
print(performance_df)
Modèle automatique
import pmdarima as pm
print(model.summary())
SARIMAX Results
======================================================================
========
Dep. Variable: y No. Observations:
718
Model: SARIMAX(2, 2, 1) Log Likelihood
39.909
Date: Mon, 08 Jan 2024 AIC
-71.817
Time: 17:01:24 BIC
-53.523
Sample: 01-01-1947 HQIC
-64.753
- 10-01-2006
======================================================================
========
coef std err z P>|z| [0.025
0.975]
----------------------------------------------------------------------
--------
ar.L1 0.3124 0.023 13.498 0.000 0.267
0.358
ar.L2 -0.2954 0.022 -13.698 0.000 -0.338
-0.253
ma.L1 -0.8716 0.020 -43.165 0.000 -0.911
-0.832
sigma2 0.0523 0.001 53.905 0.000 0.050
0.054
======================================================================
=============
Ljung-Box (L1) (Q): 0.00 Jarque-Bera (JB):
7942.50
Prob(Q): 0.98 Prob(JB):
0.00
Heteroskedasticity (H): 14.88 Skew:
0.24
Prob(H) (two-sided): 0.00 Kurtosis:
19.31
======================================================================
=============
Warnings:
[1] Covariance matrix calculated using the outer product of gradients
(complex-step).
print(performance_df)
Métrique Ensemble d'entraînement Ensemble de test
0 MAE 0.183745 5.485333
1 MSE 0.851302 99.367903
2 RMSE 0.922660 9.968345
3 R² 0.999749 0.833563
comparison_data = pd.DataFrame(index=[pd.to_datetime(target_date)],
columns=['ActualInflation', 'PredictedInflation'])
comparison_data['ActualInflation'] = df.loc[target_date, 'inflation']
comparison_data['PredictedInflation'] = prediction
print(comparison_data)
ActualInflation PredictedInflation
2006-11-01 202.0 202.240288
======================================================================
========
Dep. Variable: y No. Observations:
718
Model: SARIMAX(2, 2, 1) Log Likelihood
39.909
Date: Mon, 08 Jan 2024 AIC
-71.817
Time: 17:01:25 BIC
-53.523
Sample: 01-01-1947 HQIC
-64.753
- 10-01-2006
======================================================================
========
coef std err z P>|z| [0.025
0.975]
----------------------------------------------------------------------
--------
ar.L1 0.3124 0.023 13.498 0.000 0.267
0.358
ar.L2 -0.2954 0.022 -13.698 0.000 -0.338
-0.253
ma.L1 -0.8716 0.020 -43.165 0.000 -0.911
-0.832
sigma2 0.0523 0.001 53.905 0.000 0.050
0.054
======================================================================
=============
Ljung-Box (L1) (Q): 0.00 Jarque-Bera (JB):
7942.50
Prob(Q): 0.98 Prob(JB):
0.00
Heteroskedasticity (H): 14.88 Skew:
0.24
Prob(H) (two-sided): 0.00 Kurtosis:
19.31
======================================================================
=============
Warnings:
[1] Covariance matrix calculated using the outer product of gradients
(complex-step).
result = model.fit(test_data)
print(result.summary())
SARIMAX Results
======================================================================
========
Dep. Variable: y No. Observations:
200
Model: SARIMAX(2, 2, 1) Log Likelihood
-192.689
Date: Mon, 08 Jan 2024 AIC
393.378
Time: 17:01:25 BIC
406.531
Sample: 11-01-2006 HQIC
398.702
- 06-01-2023
======================================================================
========
coef std err z P>|z| [0.025
0.975]
----------------------------------------------------------------------
--------
ar.L1 0.5459 0.055 9.983 0.000 0.439
0.653
ar.L2 -0.1143 0.063 -1.819 0.069 -0.237
0.009
ma.L1 -0.9353 0.024 -38.356 0.000 -0.983
-0.887
sigma2 0.4073 0.027 15.354 0.000 0.355
0.459
======================================================================
=============
Ljung-Box (L1) (Q): 0.01 Jarque-Bera (JB):
193.28
Prob(Q): 0.93 Prob(JB):
0.00
Heteroskedasticity (H): 1.05 Skew:
-1.10
Prob(H) (two-sided): 0.85 Kurtosis:
7.31
======================================================================
=============
Warnings:
[1] Covariance matrix calculated using the outer product of gradients
(complex-step).
print(forecast_df)
date Forecast
2023-07-01 2023-06-30 304.708797
2023-08-01 2023-07-31 305.732176
2023-09-01 2023-08-31 306.803824
2023-10-01 2023-09-30 307.884039
2023-11-01 2023-10-31 308.963415
2023-12-01 2023-11-30 310.041352
2024-01-01 2023-12-31 311.118601
2024-02-01 2024-01-31 312.195638
2024-03-01 2024-02-29 313.272638
2024-04-01 2024-03-31 314.349643
observed_line, = plt.plot(df.index, df['inflation'], label='Observed
Data', color='blue')
forecast_line, = plt.plot(forecast_df['date'],
forecast_df['Forecast'], label='Forecast', linestyle='dashed',
color='red')
plt.xlabel('Date')
plt.ylabel('inflation')
plt.title('Observed Data and Forecast')
plt.show()