File size: 1,041 Bytes
cf382bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from tbats import TBATS
import pandas as pd

def tbats_forecast(data, forecast_horizon):
    """
    Forecast future values using the TBATS model, with a dynamic forecast horizon.

    Parameters:
    - data: Pandas Series of historical closing prices.
    - forecast_horizon: Integer specifying the number of days to forecast.

    Returns:
    - Pandas Series containing the forecasted values with a datetime index.
    """
    # Instantiate the TBATS estimator with seasonal periods. Adjust these as necessary.
    estimator = TBATS(seasonal_periods=(7, 365.25), use_trend=True, use_box_cox=False)

    # Fit the model to the historical data
    model = estimator.fit(data)

    # Forecast for the specified horizon
    forecast = model.forecast(steps=forecast_horizon)

    # Creating a pandas Series for the forecast, indexed by future dates
    future_dates = pd.date_range(start=data.index[-1] + pd.Timedelta(days=1), periods=forecast_horizon)
    forecast_series = pd.Series(forecast, index=future_dates)

    return forecast_series