Spaces:
Runtime error
Runtime error
Create sarima.py
Browse files- algo/sarima.py +28 -0
algo/sarima.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from statsmodels.tsa.statespace.sarimax import SARIMAX
|
3 |
+
|
4 |
+
def sarima_forecast(data, forecast_horizon, order=(1, 1, 1), seasonal_order=(1, 1, 1, 12)):
|
5 |
+
"""
|
6 |
+
Forecast future values using a SARIMA model with a dynamic forecast horizon.
|
7 |
+
|
8 |
+
Parameters:
|
9 |
+
- data: Pandas Series of historical closing prices.
|
10 |
+
- forecast_horizon: Integer specifying the number of days to forecast.
|
11 |
+
- order: The (p, d, q) order of the model for the number of AR parameters, differences, and MA parameters.
|
12 |
+
- seasonal_order: The (P, D, Q, s) seasonal order of the model.
|
13 |
+
|
14 |
+
Returns:
|
15 |
+
- Pandas Series containing the forecasted values with a datetime index.
|
16 |
+
"""
|
17 |
+
# Fit the SARIMA model
|
18 |
+
model = SARIMAX(data, order=order, seasonal_order=seasonal_order, enforce_stationarity=False, enforce_invertibility=False)
|
19 |
+
model_fit = model.fit(disp=False)
|
20 |
+
|
21 |
+
# Forecast for the specified horizon
|
22 |
+
forecast = model_fit.forecast(steps=forecast_horizon)
|
23 |
+
|
24 |
+
# Create a pandas Series for the forecasted values with a date index
|
25 |
+
future_dates = pd.date_range(start=data.index[-1] + pd.Timedelta(days=1), periods=forecast_horizon)
|
26 |
+
forecast_series = pd.Series(forecast, index=future_dates)
|
27 |
+
|
28 |
+
return forecast_series
|