theta metrics

This commit is contained in:
martinb-bb
2023-02-08 14:45:15 -05:00
parent 39ab07d9a3
commit fe074dd3c0
2 changed files with 25 additions and 6 deletions

View File

@@ -8,7 +8,7 @@ from typing import List, Optional, Tuple, Union
import numpy as np
import pandas as pd
from darts import TimeSeries
from darts.metrics import mape
from darts.metrics import mape, rmse, mse
from darts.models import Theta
from darts.utils.utils import SeasonalityMode
from statsmodels.tools.sm_exceptions import ConvergenceWarning
@@ -34,6 +34,7 @@ def get_theta_data(
n_predict: int = 5,
start_window: float = 0.85,
forecast_horizon: int = 5,
metric: str = "mape",
) -> Tuple[
Optional[List[TimeSeries]],
Optional[List[TimeSeries]],
@@ -64,7 +65,9 @@ def get_theta_data(
start_window: float
Size of sliding window from start of timeseries and onwards
forecast_horizon: int
Number of days to forecast when backtesting and retraining historical
Number of days to forecast when backtesting and retraining historical data
metric: str
Metric to use when backtesting and retraining historical data. Defaults to "mape".
Returns
-------
@@ -135,10 +138,19 @@ def get_theta_data(
# fit model on entire series for final prediction
best_theta_model_final.fit(ticker_series)
prediction = best_theta_model_final.predict(int(n_predict))
precision = mape(
actual_series=ticker_series, pred_series=historical_fcast_theta
) # mape = mean average precision error
console.print(f"Theta Model obtains MAPE: {precision:.2f}% \n")
if metric == "rmse":
precision = rmse(
actual_series=ticker_series, pred_series=historical_fcast_theta
)
elif metric == "mse":
precision = mse(actual_series=ticker_series, pred_series=historical_fcast_theta)
elif metric == "mape":
precision = mape(
actual_series=ticker_series, pred_series=historical_fcast_theta
)
console.print(f"Theta Model obtains {metric.upper()}: {precision:.2f}% \n")
return (
ticker_series,

View File

@@ -34,6 +34,7 @@ def display_theta_forecast(
end_date: Optional[datetime] = None,
naive: bool = False,
export_pred_raw: bool = False,
metric: str = "mape",
external_axes: Optional[List[plt.axes]] = None,
):
"""Display Theta forecast
@@ -73,6 +74,10 @@ def display_theta_forecast(
naive: bool
Whether to show the naive baseline. This just assumes the closing price will be the same
as the previous day's closing price. Defaults to False.
export_pred_raw: bool
Whether to export the raw predicted values. Defaults to False.
metric: str
The metric to use for backtesting. Defaults to "mape".
external_axes:Optional[List[plt.axes]]
External axes to plot on
"""
@@ -95,6 +100,7 @@ def display_theta_forecast(
target_column=target_column,
start_window=start_window,
forecast_horizon=forecast_horizon,
metric=metric,
)
if ticker_series == []:
return
@@ -119,6 +125,7 @@ def display_theta_forecast(
naive=naive,
export_pred_raw=export_pred_raw,
external_axes=external_axes,
metric=metric,
)
if residuals: