Skip to content
TheChatArchiveThe Chat Archive
Googlegemma-3n-e4b-it· GoogleHugging Face LMArena Human Preference 140K

I thin that the daily data is difficult let us group it by weeks and calculate again

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with grok-3-preview-02-24’s answer.
  1. User

    I thin that the daily data is difficult let us group it by weeks and calculate again

  2. Google Assistant
    import pandas as pd
    import numpy as np
    from prophet import Prophet
    import optuna
    
    # ------------------------------------------------------------------
    # 1. Load your data  (ds = date, y = daily contract count)
    # ------------------------------------------------------------------
    df = pd.read_csv('your_file.csv', parse_dates=['ds'])
    
    # ------------------------------------------------------------------
    # 2. Keep only business days (drop Sat/Sun)
    # ------------------------------------------------------------------
    df = df[df['ds'].dt.weekday < 5].reset_index(drop=True)
    
    # ------------------------------------------------------------------
    # 3. Group by week and calculate weekly sum
    # ------------------------------------------------------------------
    df_weekly = df.groupby(pd.Grouper(key='ds', freq='W')).sum().reset_index()
    df_weekly['ds'] = df_weekly['ds'].dt.date  # Keep only the date part
    df_weekly = df_weekly.rename(columns={'y': 'y'})
    
    # ------------------------------------------------------------------
    # 4. Train / test split  -> last 365 BUSINESS weeks as hold-out
    # ------------------------------------------------------------------
    HOLDOUT_WEEKS = 365
    train_df = df_weekly.iloc[:-HOLDOUT_WEEKS]
    test_df  = df_weekly.iloc[-HOLDOUT_WEEKS:]
    
    # ------------------------------------------------------------------
    # 5. SMAPE helper
    # ------------------------------------------------------------------
    def smape(y_true, y_pred):
        y_true, y_pred = np.array(y_true), np.array(y_pred)
        return 100 * np.mean(2 * np.abs(y_pred - y_true) /
                             (np.abs(y_true) + np.abs(y_pred) + 1e-8))
    
    # ------------------------------------------------------------------
    # 6. Optuna objective
    # ------------------------------------------------------------------
    def objective(trial):
        params = {
            'changepoint_prior_scale': trial.suggest_float('changepoint_prior_scale', 0.001, 0.5, log=True),
            'seasonality_prior_scale': trial.suggest_float('seasonality_prior_scale', 0.01, 10, log=True),
            'changepoint_range'      : trial.suggest_float('changepoint_range', 0.8, 0.95),
            'n_changepoints'         : trial.suggest_int('n_changepoints', 15, 50),
            'seasonality_mode'       : trial.suggest_categorical('seasonality_mode', ['additive', 'multiplicative']),
            'interval_width'         : trial.suggest_float('interval_width', 0.90, 0.95)
        }
    
        m = Prophet(
            daily_seasonality=False,
            weekly_seasonality=True,
            yearly_seasonality=True,
            **params
        )
        m.fit(train_df)
    
        # Forecast exactly the test period
        future = pd.DataFrame({'ds': test_df['ds']})
        fcst   = m.predict(future)
    
        # Align and compute SMAPE
        merged = pd.merge(test_df, fcst[['ds', 'yhat']], on='ds', how='inner')
        return smape(merged['y'], merged['yhat'])
    
    # ------------------------------------------------------------------
    # 7. Run Bayesian optimisation
    # ------------------------------------------------------------------
    study = optuna.create_study(direction='minimize')
    study.optimize(objective, n_trials=30, show_progress_bar=True)
    
    print("Best SMAPE :", round(study.best_value, 2))
    print("Best params:", study.best_params)
    
    # ------------------------------------------------------------------
    # 8. Final model on best parameters
    # ------------------------------------------------------------------
    best = study.best_params
    m_final = Prophet(
        daily_seasonality=False,
        weekly_seasonality=True,
        yearly_seasonality=True,
        **best
    )
    m_final.fit(train_df)
    
    future = pd.DataFrame({'ds': test_df['ds']})
    fcst   = m_final.predict(future)
    
    merged = pd.merge(test_df, fcst[['ds', 'yhat', 'yhat_lower', 'yhat_upper']], on='ds')
    
    final_smape = smape(merged['y'], merged['yhat'])
    print("Final back-test SMAPE:", round(final_smape, 2))
    
    # ------------------------------------------------------------------
    # 9. Quick plot
    # ------------------------------------------------------------------
    import matplotlib.pyplot as plt
    plt.figure(figsize=(14,4))
    plt.plot(merged['ds'], merged['y'], label='actual')
    plt.plot(merged['ds'], merged['yhat'], label='forecast')
    plt.fill_between(merged['ds'], merged['yhat_lower'], merged['yhat_upper'], alpha=.2)
    plt.axvline(train_df['ds'].iloc[-1], color='k', ls='--')
    plt.legend(); plt.title(f"Back-test SMAPE = {final_smape:.2f}%"); plt.show()
    

    The key change is grouping by week using pd.Grouper(key='ds', freq='W') and then summing the values within each week. This provides a more stable time series for Prophet to learn from, especially when dealing with sparse data like weekly counts.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.