model = autoarima( y=ytrain, X=Xtrain, seasonal=True, m=2, maxp=10, maxq=10, maxP=7,…
- User
model = auto_arima( y=y_train, X=X_train, seasonal=True, m=2, max_p=10, max_q=10, max_P=7, max_Q=7, d=1, D=1, stepwise=False, with_intercept=True, information_criterion='aic', test='adf', maxiter=10, suppress_warnings=True, trace=True ) how to improve model to use on different non-stationary time series with seasonality
- Assistant
To improve your auto_arima model for handling different non-stationary time series with seasonality, here are several strategies:
Adaptive Parameter Tuning
Instead of fixed parameters, consider making them data-dependent:
# Determine seasonality period dynamically from statsmodels.tsa.seasonal import seasonal_decompose from scipy.signal import periodogram def detect_seasonality(y, max_period=365): frequencies, power = periodogram(y) relevant_frequencies = frequencies[1:max_period//2] relevant_power = power[1:max_period//2] peak_idx = np.argmax(relevant_power) period = int(1 / relevant_frequencies[peak_idx]) return period # Adaptive configuration m = detect_seasonality(y_train) # Instead of fixed m=2 # Scale search space based on data length n = len(y_train) max_p = min(10, int(n/10)) max_q = min(10, int(n/10))Enhanced Preprocessing
Add preprocessing steps to handle various types of non-stationarity:
from sklearn.preprocessing import PowerTransformer, StandardScaler import numpy as np def preprocess_series(y, X=None): # Handle zeros and negative values for log transform if (y > 0).all(): y_transformed = np.log(y) transform_type = 'log' else: # Box-Cox or Yeo-Johnson transformation pt = PowerTransformer(method='yeo-johnson') y_transformed = pt.fit_transform(y.reshape(-1, 1)).flatten() transform_type = 'power' # Outlier detection and treatment Q1 = np.percentile(y_transformed, 25) Q3 = np.percentile(y_transformed, 75) IQR = Q3 - Q1 lower_bound = Q1 - 1.5 * IQR upper_bound = Q3 + 1.5 * IQR y_transformed = np.clip(y_transformed, lower_bound, upper_bound) return y_transformed, transform_typeImproved Model Selection
Use multiple criteria and cross-validation:
from pmdarima.model_selection import RollingForecastCV def fit_robust_arima(y_train, X_train=None, seasonal_period=None): # Try multiple information criteria criteria = ['aic', 'bic', 'hqic'] models = {} for ic in criteria: model = auto_arima( y=y_train, X=X_train, seasonal=True, m=seasonal_period, max_p=10, max_q=10, max_P=3, # Reduced for computational efficiency max_Q=3, d=None, # Let auto_arima determine D=None, # Let auto_arima determine stepwise=True, # Faster for initial screening with_intercept='auto', information_criterion=ic, test='kpss', # More robust for stationarity seasonal_test='ocsb', # Test for seasonality suppress_warnings=True, error_action='ignore', trace=False ) # Cross-validation score cv = RollingForecastCV(h=seasonal_period or 1, step=1, initial=len(y_train)//2) cv_scores = cross_val_score(model, y_train, X_train, cv=cv, scoring='neg_mean_squared_error') models[ic] = {'model': model, 'cv_score': np.mean(cv_scores)} # Select best model based on CV score best_model = min(models.items(), key=lambda x: x[1]['cv_score'])[1]['model'] return best_modelEnsemble Approach
Combine multiple models for robustness:
def ensemble_arima(y_train, X_train=None): models = [] # Different configurations configs = [ {'seasonal': True, 'stepwise': True}, {'seasonal': True, 'stepwise': False, 'approximation': True}, {'seasonal': False, 'trend': 'ct'}, # With trend ] for config in configs: try: model = auto_arima(y_train, X=X_train, **config, suppress_warnings=True, error_action='ignore') models.append(model) except: continue return models # Average predictions from all modelsAdaptive Differencing
Let the model determine optimal differencing:
from pmdarima.arima import ndiffs, nsdiffs # Determine differencing orders d = ndiffs(y_train, test='kpss', max_d=3) D = nsdiffs(y_train, m=seasonal_period, test='ocsb', max_D=2) if seasonal_period > 1 else 0 model = auto_arima( y=y_train, X=X_train, seasonal=True, m=seasonal_period, d=d, D=D, # ... other parameters )Feature Engineering for Exogenous Variables
If using external regressors:
def engineer_features(X, y): # Lag features X_enhanced = X.copy() for lag in [1, seasonal_period, seasonal_period*2]: X_enhanced[f'y_lag_{lag}'] = y.shift(lag) # Rolling statistics for window in [7, 30, seasonal_period]: X_enhanced[f'rolling_mean_{window}'] = y.rolling(window).mean() X_enhanced[f'rolling_std_{window}'] = y.rolling(window).std() # Time-based features if hasattr(y.index, 'dayofweek'): X_enhanced['dayofweek'] = y.index.dayofweek X_enhanced['month'] = y.index.month X_enhanced['quarter'] = y.index.quarter return X_enhanced.fillna(method='bfill').fillna(method='ffill')Complete Robust Pipeline
def robust_auto_arima_pipeline(y_train, X_train=None): # 1. Detect seasonality seasonal_period = detect_seasonality(y_train) # 2. Preprocess y_processed, transform_type = preprocess_series(y_train) # 3. Engineer features if needed if X_train is not None: X_processed = engineer_features(X_train, y_train) else: X_processed = None # 4. Fit model with adaptive parameters model = fit_robust_arima(y_processed, X_processed, seasonal_period) # 5. Store preprocessing info for inverse transform model.transform_type = transform_type model.original_scale = (y_train.mean(), y_train.std()) return modelThese improvements make your model more adaptive to different types of non-stationary time series with varying seasonal patterns, providing better generalization across different datasets.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.