18 int last_index; /* optimization */
19 double last_forecast; /* optimization */
24 * forward refs for derived forecaster types
26 int TotalMSEForecast(char *state, double *forecast);
27 int LocalMAEForecast(char *state, double *forecast);
29 int DerivedMethod(char *state);
32 * the MSE forecast chooses the best forecast from the derived
33 * forecasters, one of which is the total MSE forecast
36 MSEForecast(char *i_forcl)
47 l_forcl = (forcl)i_forcl;
50 * first, check to see if there is any data -- the forecaster
51 * routines may be called with an empty series
53 if(F_COUNT(l_forcl->forcs[0]->series) == 0)
55 forecast = FORE_ERROR_VAL;
65 * find the forecaster that has the lowest current
68 for(i=0; i < l_forcl->derived_count; i++)
70 series = l_forcl->derived_forcs[i]->se_series;
71 if(F_COUNT(series) > 0)
74 F_VAL(series,F_FIRST(series)) / F_COUNT(series);
92 (l_forcl->derived_forcs[min_i])->forecast((l_forcl->derived_forcs[min_i])->state,
96 * if an error has occurred, print and error and report
102 * since we know there is at least one data item,
103 * return the last on error
105 forecast = F_VAL(l_forcl->forcs[0]->series,
106 F_FIRST(l_forcl->forcs[0]->series));
111 * update the method that was used
113 l_forcl->total_mse_method =
114 DerivedMethod(l_forcl->derived_forcs[min_i]->state);
120 MSEError(char *state)
127 if(s->total_mse->count > 0.0)
129 val = s->total_mse->se / s->total_mse->count;
140 MSEMethod(char *state)
146 return(s->total_mse_method);
150 * for forecaster interface
153 TotalMSEForecast(char *state, double *forecast)
155 *forecast = MSEForecast(state);
157 if(*forecast == FORE_ERROR_VAL)
168 MAEForecast(char *i_forcl)
179 l_forcl = (forcl)i_forcl;
185 * find the forecaster that has the lowest current
186 * mean absolute error
188 for(i=0; i < l_forcl->derived_count; i++)
190 series = l_forcl->derived_forcs[i]->ae_series;
191 if(F_COUNT(series) > 0)
194 F_VAL(series,F_FIRST(series)) / F_COUNT(series);
212 (l_forcl->derived_forcs[min_i])->forecast((l_forcl->derived_forcs[min_i])->state,
216 * if an error has occurred, print and error and report
221 forecast = F_VAL(l_forcl->forcs[0]->series,
222 F_FIRST(l_forcl->forcs[0]->series));
226 * update the method that was used
228 l_forcl->total_mae_method =
229 DerivedMethod(l_forcl->derived_forcs[min_i]->state);
234 MAEError(char *state)
241 if(s->total_mae->count > 0.0)
243 val = s->total_mae->ae / s->total_mae->count;
254 MAEMethod(char *state)
260 return(s->total_mae_method);
264 TotalMAEForecast(char *state, double *forecast)
266 *forecast = MAEForecast(state);
268 if(*forecast == FORE_ERROR_VAL)
279 InitWinMSE(fbuff series, fbuff time_stamps, char *params)
281 struct mse_state *state;
286 state = (struct mse_state *)malloc(sizeof(struct mse_state));
294 * first parameter is the address of the forcl and the
295 * second is the window size
297 pcount = sscanf(params,"%p %d",
307 state->forc_list = l_forcl;
310 return((char *)state);
314 FreeWinMSE(char *state)
320 InitWinMAE(fbuff series, fbuff time_stamps, char *params)
322 return(InitWinMSE(series,time_stamps,params));
326 FreeWinMAE(char *state)
332 LocalWinMSEForecast(char *state, double *out_f)
347 s = (struct mse_state *)state;
349 l_forcl = s->forc_list;
358 * find the forecaster that has the lowest current
359 * mean square error --
361 * if the win size > 0, look only over the window
362 * using the cumulative series. Otherwise, use the total
365 for(i=0; i < l_forcl->count; i++)
367 if((l_forcl->forcs[i])->count > 0.0)
370 (l_forcl->forcs[i])->se_series;
373 * make sure we only window as much
376 if(win > (F_COUNT(series) - 1))
377 win = F_COUNT(series) - 1;
382 * get the first value which is
383 * the current cumulative total
386 F_VAL(series, F_FIRST(series));
388 (l_forcl->forcs[i])->count;
393 * get the index of the end of the
396 windex = MODPLUS(F_FIRST(series),
400 * the difference is the cumulative
401 * total over the window size
403 temp_err = F_VAL(series, F_FIRST(series)) -
404 F_VAL(series,windex);
405 sq_err = temp_err / win;
424 (l_forcl->forcs[min_i])->forecast((l_forcl->forcs[min_i])->state,&forecast);
427 * if an error has occurred, print and error and report
432 forecast = F_VAL(l_forcl->forcs[0]->series,
433 F_FIRST(l_forcl->forcs[0]->series));
439 * update the method that was used
448 LocalWinMAEForecast(char *state, double *out_f)
463 s = (struct mse_state *)state;
465 l_forcl = s->forc_list;
473 * find the forecaster that has the lowest current
474 * mean square error --
476 * if the win size > 0, look only over the window
477 * using the cumulative series. Otherwise, use the total
480 for(i=0; i < l_forcl->count; i++)
482 if((l_forcl->forcs[i])->count > 0.0)
485 (l_forcl->forcs[i])->ae_series;
488 * make sure we only window as much
491 if(win > (F_COUNT(series) - 1))
492 win = F_COUNT(series) - 1;
497 * get the first value which is
498 * the current cumulative total
501 F_VAL(series, F_FIRST(series));
503 (l_forcl->forcs[i])->count;
508 * get the index of the end of the
511 windex = MODPLUS(F_FIRST(series),
515 * the difference is the cumulative
516 * total over the window size
519 F_VAL(series, F_FIRST(series)) -
520 F_VAL(series,windex);
521 err = temp_err / win;
540 (l_forcl->forcs[min_i])->forecast((l_forcl->forcs[min_i])->state,&forecast);
543 * if an error has occurred, print and error and report
548 forecast = F_VAL(l_forcl->forcs[0]->series,
549 F_FIRST(l_forcl->forcs[0]->series));
556 * update the method that was used
565 DerivedMethod(char *state)
569 s = (struct mse_state *)state;
578 MSELifetimeForecast(char *state)
583 flife = (forclife)state;
584 l_forcl = (forcl)flife->forc_list;
585 if(F_COUNT(l_forcl->forcs[0]->series) == 0)
587 if(flife->count == 0.0)
589 return(flife->total / flife->count);
591 return(MSEForecast((char *)flife->forc_list));
595 MSELifetimeError(char *state)
598 flife = (forclife)state;
599 return(MSEError((char *)flife->forc_list));
603 MSELifetimeMethod(char *state)
606 flife = (forclife)state;
607 return(MSEMethod((char *)flife->forc_list));
611 MSELifetimeEpoch(char *state)
614 flife = (forclife)state;
615 return(flife->epoch_end);
619 MAELifetimeForecast(char *state)
624 flife = (forclife)state;
625 l_forcl = (forcl)flife->forc_list;
627 if(F_COUNT(l_forcl->forcs[0]->series) == 0)
629 if(flife->count == 0.0)
631 return(flife->total / flife->count);
633 return(MAEForecast((char *)flife->forc_list));
637 MAELifetimeError(char *state)
640 flife = (forclife)state;
641 return(MAEError((char *)flife->forc_list));
645 MAELifetimeMethod(char *state)
648 flife = (forclife)state;
649 return(MAEMethod((char *)flife->forc_list));
653 MAELifetimeEpoch(char *state)
656 flife = (forclife)state;
657 return(flife->epoch_end);