File size: 1,254 Bytes
f852243
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
from statsmodels.tsa.statespace.sarimax import SARIMAX

def sarima_forecast(data, forecast_horizon, order=(1, 1, 1), seasonal_order=(1, 1, 1, 12)):
    """
    Forecast future values using a SARIMA model with a dynamic forecast horizon.

    Parameters:
    - data: Pandas Series of historical closing prices.
    - forecast_horizon: Integer specifying the number of days to forecast.
    - order: The (p, d, q) order of the model for the number of AR parameters, differences, and MA parameters.
    - seasonal_order: The (P, D, Q, s) seasonal order of the model.

    Returns:
    - Pandas Series containing the forecasted values with a datetime index.
    """
    # Fit the SARIMA model
    model = SARIMAX(data, order=order, seasonal_order=seasonal_order, enforce_stationarity=False, enforce_invertibility=False)
    model_fit = model.fit(disp=False)
    
    # Forecast for the specified horizon
    forecast = model_fit.forecast(steps=forecast_horizon)
    
    # Create a pandas Series for the forecasted values with a date index
    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