Files
OpenBB/openbb_terminal/forecast/regr_model.py
martinb-bb e1bba247e2 Forecast docstring fixes and other things (#3564)
* init commit for doc string fixes

* quick fix

* Forecast Guide Draft 1

* grammar

* sentence edit

* missing word

* target column is a string, not a list. ooops

* clarification on default target column

* minor mods

* more fixes

* Fixed dashboard

* val instead of tickerseries

* Added fixes

* $ --> <>

* Fixed pylint

* one quick change

Co-authored-by: Danglewood <85772166+deeleeramone@users.noreply.github.com>
Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com>
Co-authored-by: colin99d <colin99delahunty@gmail.com>
2022-11-25 17:13:38 -05:00

105 lines
2.8 KiB
Python

# pylint: disable=too-many-arguments
"""Regression Model"""
__docformat__ = "numpy"
import logging
from typing import Tuple, Union, List
import warnings
import pandas as pd
from darts import TimeSeries
from darts.models import RegressionModel
from openbb_terminal.decorators import log_start_end
from openbb_terminal.forecast import helpers
logger = logging.getLogger(__name__)
@log_start_end(log=logger)
def get_regression_data(
data: Union[pd.Series, pd.DataFrame],
target_column: str = "close",
n_predict: int = 5,
past_covariates: str = None,
train_split: float = 0.85,
forecast_horizon: int = 5,
output_chunk_length: int = 5,
lags: Union[int, List[int]] = 14,
) -> Tuple[
List[TimeSeries], List[TimeSeries], List[TimeSeries], float, type[RegressionModel]
]:
"""Perform Regression Forecasting
Parameters
----------
data: Union[pd.Series, pd.DataFrame]
Input Data
n_predict: int
Days to predict. Defaults to 5.
target_column: str
Target column to forecast. Defaults to "close".
train_split: float
Train/val split. Defaults to 0.85.
past_covariates: str
Multiple secondary columns to factor in when forecasting. Defaults to None.
forecast_horizon: int
Forecast horizon when performing historical forecasting. Defaults to 5.
output_chunk_length: int
The length of the forecast of the model. Defaults to 1.
lags: Union[int, List[int]]
lagged target values to predict the next time step
Returns
-------
Tuple[List[TimeSeries], List[TimeSeries], List[TimeSeries], float, type[RegressionModel]]
Adjusted Data series,
Historical forecast by best RNN model,
list of Predictions,
Mean average precision error,
Best Regression Model.
"""
use_scalers = False
probabilistic = False
scaler, ticker_series = helpers.get_series(
data, target_column, is_scaler=use_scalers
)
past_covariate_whole, _, _ = helpers.past_covs(
past_covariates, data, train_split, use_scalers
)
if past_covariates is not None:
lags_past_covariates = lags
else:
lags_past_covariates = None
reg_model = RegressionModel(
output_chunk_length=output_chunk_length,
lags=lags,
lags_past_covariates=lags_past_covariates,
)
with warnings.catch_warnings():
warnings.simplefilter(action="ignore", category=FutureWarning)
reg_model.fit(ticker_series, past_covariate_whole)
# Showing historical backtesting without retraining model (too slow)
return helpers.get_prediction(
"Regression",
probabilistic,
use_scalers,
scaler,
past_covariates,
reg_model,
ticker_series,
past_covariate_whole,
train_split,
forecast_horizon,
n_predict,
)