Skip to content

[FEAT] add mean and greedy ensemble - #221

Open
jan rathfelder (janrth) wants to merge 4 commits into
Nixtla:mainfrom
janrth:feature/utilsforecast-ensemble
Open

jan rathfelder (janrth) wants to merge 4 commits into
Nixtla:mainfrom
janrth:feature/utilsforecast-ensemble

Conversation

@janrth

@janrth jan rathfelder (janrth) commented Mar 14, 2026 •

Copy link
Copy Markdown
Contributor

Here some first ensemble methods are introduced. A simple mean and a greedy global (same model weight for all unique_ids) and local (different model weights for each unique_id).

Is compatible with statsforecast, mlforecast and neuralforecaster.

I have three open points we need to discuss:

  • Ideally, we have the weights being part of the model objects from the underlying packages like mlforecast. But then the logic needs to be written at the same time over multiple repos (which is possible, but might be a bit unusual). So here I kept all the code inside this repo. People can save the weights now in mlflow and then apply again during inference. I would prefer a solution to have the weights being embedded into mlforecast, statsforecast and neuralforecaster, but would be happy to hear your thoughts.

  • Same issue, different topic: tests. Here I can only run general tests with dependencies inside this repo. Unless we add mlforecast and co as dependencies here, I can't add specific tests around these packages here.

  • When it comes to prediction intervals, I also think it might be better to have them inside utilsforecast, or how would would we now create conformal intervals from the final ensemble column?

Overall, I think this solution is still helpful and the current setup should also allow adding the tabular ensemble (training some more advanced model), but I wonder how we want to continue in the long run.

Solves #220

Usage:

from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor

from mlforecast import MLForecast
from utilsforecast.data import generate_series
from utilsforecast.ensembles import (
    apply_ensemble,
    fit_ensemble,
)
from utilsforecast.losses import rmse

series = generate_series(
    n_series=8,
    freq="D",
    min_length=40,
    max_length=40,
    equal_ends=True,
    with_trend=True,
    static_as_categorical=False,
    seed=0,
)

fcst = MLForecast(
    models=[
        LinearRegression(),
        DecisionTreeRegressor(max_depth=4, random_state=0),
        RandomForestRegressor(n_estimators=32, random_state=0),
    ],
    freq="D",
    lags=[1, 7],
    num_threads=1,
)

cv_df = fcst.cross_validation(series, n_windows=3, h=7)
fcst.fit(series)
preds = fcst.predict(h=7)

model_cols = [c for c in preds.columns if c not in ["unique_id", "ds"]]

global_weights = fit_ensemble(
    cv_df,
    method="greedy_global",
    models=model_cols,
    metric=rmse,
    max_iters=10,
)

cv_global = apply_ensemble(
    cv_df,
    global_weights,
    models=model_cols,
    ensemble_name="GreedyGlobal",
)
preds_global = apply_ensemble(
    preds,
    global_weights,
    models=model_cols,
    ensemble_name="GreedyGlobal",
)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 16c997d911

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "Codex (@codex) review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".

Comment thread utilsforecast/ensembles.py
@janrth

Copy link
Copy Markdown
Contributor Author

Codex (@codex)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2a7d045104

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "Codex (@codex) review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".

Comment thread utilsforecast/ensembles.py
@janrth jan rathfelder (janrth) changed the title add mean and greedy ensemble [FEAT] add mean and greedy ensemble Mar 16, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant