Generate time-series forecasts with confidence intervals using Google TimesFM and Nixtla StatsForecast for environmental parameters
This skill enables implementation of probabilistic time-series forecasting that generates prediction intervals rather than single point estimates. Essential for regulatory decision-making where uncertainty quantification matters.
import timesfm
# Initialize the model
tfm = timesfm.TimesFm(
context_len=512,
horizon_len=128,
input_patch_len=32,
output_patch_len=128,
num_layers=20,
model_dims=1280,
)
tfm.load_from_checkpoint(repo_id="google/timesfm-1.0-200m")
# Generate forecasts with confidence intervals
point_forecast, quantile_forecast = tfm.forecast(
inputs=historical_data,
freq=[0], # 0 for high-frequency data
)
from statsforecast import StatsForecast
from statsforecast.models import AutoARIMA, AutoETS
# Initialize with multiple models
sf = StatsForecast(
models=[
AutoARIMA(season_length=24),
AutoETS(season_length=24)
],
freq='H', # Hourly data
n_jobs=-1
)
# Fit and predict with confidence intervals
sf.fit(historical_df)
forecasts = sf.predict(
h=24, # 24-hour horizon
level=[90, 95] # 90% and 95% confidence intervals
)
# Result columns: AutoARIMA, AutoARIMA-lo-90, AutoARIMA-hi-90, etc.
from fastapi import APIRouter
from typing import List
router = APIRouter()
@router.post("/forecast/air-quality")
async def forecast_air_quality(
station_id: str,
parameter: str,
horizon_hours: int,
confidence_levels: List[float] = [0.90, 0.95]
):
# Fetch historical data from ClickHouse
historical_data = await get_station_history(station_id, parameter)
# Generate probabilistic forecast
forecast = generate_probabilistic_forecast(
data=historical_data,
horizon=horizon_hours,
levels=confidence_levels
)
return {
"point_forecast": forecast.mean.tolist(),
"confidence_intervals": {
f"{int(level*100)}%": {
"lower": forecast[f'lo-{int(level*100)}'].tolist(),
"upper": forecast[f'hi-{int(level*100)}'].tolist()
}
for level in confidence_levels
}
}
# Multi-seasonal decomposition for environmental data
from statsforecast.models import MSTL
model = MSTL(
season_length=[24, 168], # Daily and weekly patterns
trend_forecaster=AutoARIMA()
)