(ts_data, forecast_periods=5)
| 86 | return pd.DataFrame(data) |
| 87 | |
| 88 | def 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 | |
| 126 | def create_map(df, selected_region=None): |
| 127 | region_totals = df.groupby('REGIONAL_OFFICE_NAME')['TOTAL_COST'].sum() |
no outgoing calls
no test coverage detected