MCPcopy Create free account
hub / github.com/Hrishikesh332/HCDS-EPD / train_arima

Function train_arima

utils.py:88–124  ·  view source on GitHub ↗
(ts_data, forecast_periods=5)

Source from the content-addressed store, hash-verified

86 return pd.DataFrame(data)
87
88def train_arima(ts_data, forecast_periods=5):
89 if len(ts_data) < 3:
90 raise ValueError("Insufficient data for ARIMA modeling. Need at least 3 data points.")
91
92 orders = [(1,1,1), (2,1,1), (1,0,1), (0,1,1), (1,1,0)]
93 best_model = None
94 best_aic = float('inf')
95
96 for order in orders:
97 try:
98 model = ARIMA(ts_data['TOTAL_COST'], order=order)
99 fitted_model = model.fit()
100 if fitted_model.aic < best_aic:
101 best_aic = fitted_model.aic
102 best_model = fitted_model
103 except:
104 continue
105
106 if best_model is None:
107 raise ValueError("All ARIMA models failed to converge. Cannot generate forecast.")
108
109 forecast_result = best_model.get_forecast(steps=forecast_periods)
110 forecast = forecast_result.predicted_mean
111 conf_int = forecast_result.conf_int()
112
113 forecast_dates = pd.date_range(
114 start=ts_data['YEAR_MONTH'].iloc[-1] + pd.DateOffset(months=1),
115 periods=forecast_periods,
116 freq='MS'
117 )
118
119 return pd.DataFrame({
120 'YEAR_MONTH': forecast_dates,
121 'FORECAST': forecast.values,
122 'CONFIDENCE_LOWER': conf_int.iloc[:, 0].values,
123 'CONFIDENCE_UPPER': conf_int.iloc[:, 1].values
124 })
125
126def create_map(df, selected_region=None):
127 region_totals = df.groupby('REGIONAL_OFFICE_NAME')['TOTAL_COST'].sum()

Callers 4

forecast_insightsFunction · 0.90
gen_real_pred_errorsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected