naive.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from typing import Optional
  2. from datetime import timedelta
  3. from statsmodels.api import OLS
  4. from flexmeasures.data.models.forecasting.model_spec_factory import (
  5. create_initial_model_specs,
  6. )
  7. """
  8. Naive model, which simply copies the measurement from which the forecasts is made. Useful as a fallback.
  9. Technically, the model has no regressors and just one lag - made using the horizon.
  10. The value to be copied is this one lag.
  11. This is because we assume the forecast is made for the very reason that the data point at this lag exists - why else would one make
  12. a prediction from there with this horizon?
  13. """
  14. # update this version if small things like parametrisation change
  15. version: int = 1
  16. # if a forecasting job using this model fails, fall back on this one
  17. fallback_model_search_term: Optional[str] = None
  18. class Naive(OLS):
  19. """Naive prediction model for a single input feature that simply throws back the given feature.
  20. Under the hood, it uses linear regression by ordinary least squares, trained with points (0,0) and (1,1).
  21. """
  22. def __init__(self, *args, **kwargs):
  23. super().__init__([0, 1], [0, 1])
  24. def naive_specs_configurator(**kwargs):
  25. """Create and customize initial specs with OLS. See model_spec_factory for param docs."""
  26. kwargs["transform_to_normal"] = False
  27. kwargs["use_regressors"] = False
  28. kwargs["use_periodicity"] = False
  29. kwargs["custom_model_params"] = dict(
  30. training_and_testing_period=timedelta(hours=0), n_lags=1
  31. )
  32. model_specs = create_initial_model_specs(**kwargs)
  33. model_specs.set_model(Naive, library_name="statsmodels")
  34. model_identifier = "naive model v%d" % version
  35. return model_specs, model_identifier, fallback_model_search_term