-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
274 lines (216 loc) · 9.2 KB
/
main.py
File metadata and controls
274 lines (216 loc) · 9.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import os
import time
from tqdm import tqdm
import pandas as pd
import numpy as np
from src.preprocessing import load_and_clean_data
from src.arima_baseline import run_arima
from src.regression_ols import run_ols, run_ols_feature_selection
from src.ml_models import run_rf, run_xgboost, run_mlp
from src.bayesian_dlm_mcmc import run_dlm_pymc
from src.bayesian_dlm_smc import run_dlm_numpyro
from src.metrics import plot_model_comparison
from src.bayesian_dlm_reg import run_dlm_dynamic_regression
from src.regression_ols_in import run_ols_in, run_ols_feature_selection_in
from src.ml_models_in import run_rf_in, run_xgboost_in, run_mlp_in
from src.bayesian_dlm_mcmc_in import run_dlm_pymc_in
from src.bayesian_dlm_smc_in import run_dlm_numpyro_in
from src.bayesian_dlm_reg_in import run_dlm_dynamic_regression_in
from src.arima_baseline_in import run_arima_in
# 📁 Create folders if they don't exist
os.makedirs("results", exist_ok=True)
# ⚙️ Dataset and variables
data_path = "dataset.csv"
target_col = "CPI - YoY"
print("🚀 Starting Inflation Forecasting Model Comparison")
print("=" * 60)
# 📊 1. Preprocessing
print("📊 Loading and preprocessing data...")
df = load_and_clean_data(data_path)
print(f"✅ Data loaded: {len(df)} observations")
print()
# Define models to run
models = [
("ARIMA", "📈 Classical Time Series"),
("OLS", "📈 Linear Regression"),
("OLS + RFE", "📈 Feature Selection"),
("Random Forest", "🤖 Ensemble Learning"),
("XGBoost", "🤖 Gradient Boosting"),
("MLP", "🤖 Neural Network"),
("Bayesian DLM (MCMC)", "🧠 Bayesian MCMC"),
("Bayesian DLM (SMC)", "🧠 Sequential Monte Carlo"),
("Bayesian DLM (DR)", "🧠 Dynamic Coefficients")
]
# Initialize results dictionary
rmse_results_out = {}
# Create main progress bar
main_pbar = tqdm(models, desc="🔄 Running Models", unit="model",
bar_format='{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}]')
train_start="2001-12-31"
train_end="2019-12-31"
test_start="2020-01-01"
test_end="2023-12-31"
for model_name, model_desc in main_pbar:
main_pbar.set_description(f"🔄 {model_desc}")
try:
start_time = time.time()
if model_name == "ARIMA":
forecast, rmse, best_order = run_arima(
df, target_col,
train_start=train_start,
train_end=train_end,
test_start=test_start,
test_end=test_end
)
elif model_name == "OLS":
model, rmse = run_ols(
df, target_col,
train_start=train_start,
train_end=train_end,
test_start=test_start,
test_end=test_end
)
elif model_name == "OLS + RFE":
model, selected_features, rmse = run_ols_feature_selection(
df, target_col,
train_start=train_start,
train_end=train_end,
test_start=test_start,
test_end=test_end,
n_features=5
)
elif model_name == "Random Forest":
_, rmse = run_rf(df, target_col,
train_start=train_start,
train_end=train_end,
test_start=test_start,
test_end=test_end
)
elif model_name == "XGBoost":
_, rmse = run_xgboost(df, target_col,
train_start=train_start,
train_end=train_end,
test_start=test_start,
test_end=test_end
)
elif model_name == "MLP":
_, rmse = run_mlp(df, target_col,
train_start=train_start,
train_end=train_end,
test_start=test_start,
test_end=test_end
)
elif model_name == "Bayesian DLM (MCMC)":
_, rmse = run_dlm_pymc(
df,
target_col,
train_start=train_start,
train_end=train_end,
test_start=test_start,
test_end=test_end
)
elif model_name == "Bayesian DLM (SMC)":
_, rmse = run_dlm_numpyro(
df,
target_col,
train_start=train_start,
train_end=train_end,
test_start=test_start,
test_end=test_end
)
elif model_name == "Bayesian DLM (DR)":
trace, rmse = run_dlm_dynamic_regression(
df,
target_col,
train_start=train_start,
train_end=train_end,
test_start=test_start,
test_end=test_end,
)
# Store result
rmse_results_out[model_name] = rmse
# Calculate execution time
exec_time = time.time() - start_time
# Update progress bar with result
main_pbar.set_postfix({
'RMSE': f'{rmse:.4f}',
'Time': f'{exec_time:.1f}s'
})
print(f"\n✅ {model_name}: RMSE = {rmse:.4f} (took {exec_time:.1f}s)")
except Exception as e:
print(f"\n❌ Error running {model_name}: {str(e)}")
rmse_results_out[model_name] = float('inf') # Mark as failed
continue
# 📊 2. In-sample models
rmse_results_in = {}
# Create main progress bar
main_pbar = tqdm(models, desc="🔄 Running Models", unit="model",
bar_format='{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}]')
for model_name, model_desc in main_pbar:
main_pbar.set_description(f"🔄 {model_desc}")
try:
start_time = time.time()
if model_name == "ARIMA":
forecast, rmse, best_order = run_arima_in(df, target_col)
elif model_name == "OLS":
model, rmse = run_ols_in(df, target_col)
elif model_name == "OLS + RFE":
model, selected_features, rmse = run_ols_feature_selection_in(df, target_col, n_features=5)
elif model_name == "Random Forest":
_, rmse = run_rf_in(df, target_col)
elif model_name == "XGBoost":
_, rmse = run_xgboost_in(df, target_col)
elif model_name == "MLP":
_, rmse = run_mlp_in(df, target_col)
elif model_name == "Bayesian DLM (MCMC)":
_, rmse = run_dlm_pymc_in(df, target_col)
elif model_name == "Bayesian DLM (SMC)":
_, rmse = run_dlm_numpyro_in(df, target_col)
elif model_name == "Bayesian DLM (DR)":
trace, rmse = run_dlm_dynamic_regression_in(df, target_col)
# Store result
rmse_results_in[model_name] = rmse
# Calculate execution time
exec_time = time.time() - start_time
# Update progress bar with result
main_pbar.set_postfix({
'RMSE': f'{rmse:.4f}',
'Time': f'{exec_time:.1f}s'
})
print(f"\n✅ {model_name}: RMSE = {rmse:.4f} (took {exec_time:.1f}s)")
except Exception as e:
print(f"\n❌ Error running {model_name}: {str(e)}")
rmse_results_in[model_name] = float('inf') # Mark as failed
continue
print("\n" + "=" * 60)
print("📊 Generating comparison plots and saving results...")
# 📊 5. Comparison
plot_model_comparison(rmse_results_out, title="Out-of-Sample RMSE Comparison", filename="rmse_comparison_out.png")
plot_model_comparison(rmse_results_in, title="In-Sample RMSE Comparison", filename="rmse_comparison_in.png")
# 📝 Save RMSE to CSV
results_df = pd.DataFrame.from_dict(rmse_results_out, orient='index', columns=['RMSE'])
results_df = results_df.sort_values('RMSE') # Sort by best performance
results_df.to_csv("results/rmse_summary.csv")
results_df_in = pd.DataFrame.from_dict(rmse_results_in, orient='index', columns=['RMSE'])
results_df_in = results_df_in.sort_values('RMSE') # Sort by best performance
results_df_in.to_csv("results/rmse_summary_in.csv")
print("\n🏆 FINAL RESULTS RANKING:")
print("-" * 40)
for i, (model, rmse) in enumerate(results_df.iterrows(), 1):
status = "🥇" if i == 1 else "🥈" if i == 2 else "🥉" if i == 3 else f"{i:2d}."
print(f"{status} {model:<25} RMSE: {rmse['RMSE']:.4f}")
print("\n🏆 FINAL RESULTS RANKING (In-sample):")
print("-" * 40)
for i, (model, rmse) in enumerate(results_df_in.iterrows(), 1):
status = "🥇" if i == 1 else "🥈" if i == 2 else "🥉" if i == 3 else f"{i:2d}."
print(f"{status} {model:<25} RMSE: {rmse['RMSE']:.4f}")
print(f"\n✅ All models completed! Results saved in /results")
print("📁 Files generated:")
print(" • rmse_summary.csv - Detailed results")
print(" • model_comparison.png - Visual comparison")
print(" • Individual model plots in /results folder")
# Optional: Show execution summary
total_successful = sum(1 for rmse in rmse_results_out.values() if rmse != float('inf'))
print(f"\n📈 Execution Summary: {total_successful}/{len(models)} models completed successfully")
total_successful_in = sum(1 for rmse in rmse_results_in.values() if rmse != float('inf'))
print(f"📈 In-sample Execution Summary: {total_successful_in}/{len(models)} models completed succesfully")