I want to create some classes and functions for Time Series Models:
from statsmodels.tsa.ar_model import AutoReg
from statsmodels.tsa.holtwinters import ExponentialSmoothing
from statsmodels.tsa.statespace.sarimax import SARIMAX
from statsmodels.tsa.arima.model import ARIMA
class TimeSeriesModel():
def __init__(self, model_name, model_config):
self.model_name = model_name
self.model = None
self.model_config = model_config
super().__init__(model_name=model_name, model_config=model_config)
@abstractmethod
def get_model(self):
pass
@abstractmethod
def fit(self, X, y):
pass
@abstractmethod
def predict(self, X):
pass
def get_statsmodels_models() -> Dict[str, Any]:
"""
Returns a dictionary, with keys being statsmodels ts estimator
names, and values being the estimator class (to be used for model instantiation).
Returns:
Dictionary of statsmodels time series models.
"""
return {"ARIMA": ARIMA, "AutoReg":AutoReg, "ExponentialSmoothing":ExponentialSmoothing, "SARIMA": SARIMA}
Now I can see a list of models that I included:
get_statsmodels_models()
Next, I want to create methods to describe the model from the list, fit and predict:
I am thinking something like this:
import statsmodels.api as sm
data = sm.datasets.sunspots.load_pandas().data['SUNACTIVITY']
ts_object = TimeSeriesModel(model_name="AutoReg",
model_config={"lags":1,"seasonal":True, period=11})
ts_object.fit(X=data)
ts_object.predict()
Can someone please suggest ways/references how I can initialize a model with parameters (currently, I consider just models that I imported using stats library) and implement fit and predict methods?