📚 সমস্ত অধ্যায় দেখুন
অধ্যায়/ফেজ 3 · Phase 3 · Advanced ML
3.6২৫ মিনিট পড়া25 / 68

Time Series Forecasting

Time Series

ARIMA, Prophet, sequence prediction।

Hook — আগামীকালের আবহাওয়া

Stock price, বিদ্যুৎ চাহিদা, sales, traffic — সবই সময়ের সাথে চলে। Time Series Forecasting মানে অতীতের ধরন থেকে ভবিষ্যৎ অনুমান করা।

Components

  • Trend — দীর্ঘমেয়াদি দিক (উপরে/নিচে যাওয়া)।
  • Seasonality — নিয়মিত cycle (প্রতিদিন, প্রতি বছর)।
  • Cyclic — অনিয়মিত long-period swing।
  • Noise — random fluctuation।
y_t = Trend_t + Season_t + Residual_t (additive)

Stationarity — অপরিবর্তনীয় পরিসংখ্যান

Mean ও variance সময়ের সাথে স্থির — তখন stationary। অনেক classical model (ARIMA) এর শর্ত। ADF test দিয়ে check করা হয়, differencing দিয়ে বানানো হয়।

Models — Classical → Modern

  • Naïve / Seasonal Naïve — baseline।
  • Exponential Smoothing (Holt-Winters) — trend + seasonality।
  • ARIMA / SARIMA — AR + I + MA।
  • Prophet (Meta) — easy seasonality + holidays।
  • ML — Lag feature বানিয়ে XGBoost/LightGBM।
  • DL — LSTM, Temporal Fusion Transformer।

Code — Prophet দিয়ে Forecast

prophet_demo.py
import pandas as pd
from prophet import Prophet

# ds = datetime, y = value
df = pd.read_csv("daily_sales.csv")  # columns: ds, y

m = Prophet(yearly_seasonality=True, weekly_seasonality=True)
m.fit(df)

future = m.make_future_dataframe(periods=30)
forecast = m.predict(future)
print(forecast[["ds", "yhat", "yhat_lower", "yhat_upper"]].tail())

m.plot(forecast); m.plot_components(forecast)

Lag Feature + ML

lag_xgb.py
import pandas as pd
from xgboost import XGBRegressor

df = pd.read_csv("daily_sales.csv", parse_dates=["ds"]).sort_values("ds")
for lag in [1, 7, 14, 28]:
    df[f"lag_{lag}"] = df["y"].shift(lag)
df["dow"] = df["ds"].dt.dayofweek
df["month"] = df["ds"].dt.month
df = df.dropna()

X = df.drop(columns=["ds", "y"])
y = df["y"]
split = int(len(df) * 0.8)
model = XGBRegressor(n_estimators=400, learning_rate=0.05, max_depth=5)
model.fit(X.iloc[:split], y.iloc[:split])
print("Test R²:", model.score(X.iloc[split:], y.iloc[split:]))

Evaluation

  • MAE, RMSE — absolute error।
  • MAPE, sMAPE — percentage error।
  • Backtesting — Rolling/Expanding window CV (TimeSeriesSplit)।

Common Mistakes

Leakage

Future এর data দিয়ে past predict করা। Always chronological split।

  • Stationarity check ছাড়া ARIMA fit।
  • Holiday/event ignore করা।
  • Single horizon evaluate করা — multi-horizon দরকার।

Mini Project — Electricity Demand Forecast

একটি hourly electricity dataset নিয়ে Prophet + LightGBM (lag features) compare করো। Next 7-day MAE বের করো।

Summary

এক নজরে

Time Series = সময় respect + লিকেজ-মুক্ত validation + সঠিক model নির্বাচন।