Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions nbs/core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1678,19 +1678,20 @@
" raise AttributeError('Please rerun the `fit` method passing a valid prediction_interval setting to compute conformity scores')\n",
" \n",
" min_size = ufp.counts_by_id(df, id_col)['counts'].min()\n",
" min_samples = self.h * self.prediction_intervals.n_windows + 1\n",
" min_samples = self.h + self.prediction_intervals.n_windows * self.prediction_intervals.step_size\n",
" if min_size < min_samples:\n",
" raise ValueError(\n",
" \"Minimum required samples in each serie for the prediction intervals \"\n",
" f\"settings are: {min_samples}, shortest serie has: {min_size}. \"\n",
" \"Please reduce the number of windows, horizon or remove those series.\"\n",
" \"Please reduce the number of windows, horizon, step_size or remove those series.\"\n",
" )\n",
" \n",
" self._add_level = True\n",
" cv_results = self.cross_validation(\n",
" df=df,\n",
" static_df=static_df,\n",
" n_windows=self.prediction_intervals.n_windows,\n",
" step_size=self.prediction_intervals.step_size,\n",
" id_col=id_col,\n",
" time_col=time_col,\n",
" target_col=target_col,\n",
Expand Down Expand Up @@ -3676,6 +3677,30 @@
" 'LSTM', 'LSTM1', 'LSTM1-median', 'LSTM2_ql0.5', 'TSMixer', 'TSMixer1',\n",
" 'TSMixer1-median', 'TSMixer2_ql0.5']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e03292d2",
"metadata": {},
"outputs": [],
"source": [
"#| hide\n",
"# test conformal prediction with step_size argument\n",
"\n",
"models = []\n",
"for nf_model in [NHITS, RNN, TSMixer]:\n",
" params = {\"h\": 12, \"input_size\": 24, \"max_steps\": 1}\n",
" if nf_model.__name__ == \"TSMixer\":\n",
" params.update({\"n_series\": 2})\n",
" models.append(nf_model(**params))\n",
"\n",
"# Case 1: step_size = None\n",
"prediction_intervals = PredictionIntervals(method=\"conformal_error\", step_size=2)\n",
"nf = NeuralForecast(models=models, freq='M')\n",
"nf.fit(AirPassengersPanel_train, prediction_intervals=prediction_intervals)\n",
"preds = nf.predict(futr_df=AirPassengersPanel_test, level=[90])"
]
}
],
"metadata": {
Expand Down
6 changes: 5 additions & 1 deletion nbs/utils.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@
" def __init__(\n",
" self,\n",
" n_windows: int = 2,\n",
" step_size: int = 1,\n",
" method: str = \"conformal_distribution\",\n",
" ):\n",
" \"\"\" \n",
Expand All @@ -585,6 +586,8 @@
" method : str, default is conformal_distribution\n",
" One of the supported methods for the computation of prediction intervals:\n",
" conformal_error or conformal_distribution\n",
" step_size : int, default is 1\n",
" Step size for the prediction intervals. \n",
" \"\"\"\n",
" if n_windows < 2:\n",
" raise ValueError(\n",
Expand All @@ -595,9 +598,10 @@
" raise ValueError(f\"method must be one of {allowed_methods}\")\n",
" self.n_windows = n_windows\n",
" self.method = method\n",
" self.step_size = step_size\n",
"\n",
" def __repr__(self):\n",
" return f\"PredictionIntervals(n_windows={self.n_windows}, method='{self.method}')\""
" return f\"PredictionIntervals(n_windows={self.n_windows}, method='{self.method}', step_size={self.step_size})\""
]
},
{
Expand Down
8 changes: 6 additions & 2 deletions neuralforecast/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1688,19 +1688,23 @@ def _conformity_scores(
)

min_size = ufp.counts_by_id(df, id_col)["counts"].min()
min_samples = self.h * self.prediction_intervals.n_windows + 1
min_samples = (
self.h
+ self.prediction_intervals.n_windows * self.prediction_intervals.step_size
)
if min_size < min_samples:
raise ValueError(
"Minimum required samples in each serie for the prediction intervals "
f"settings are: {min_samples}, shortest serie has: {min_size}. "
"Please reduce the number of windows, horizon or remove those series."
"Please reduce the number of windows, horizon, step_size or remove those series."
)

self._add_level = True
cv_results = self.cross_validation(
df=df,
static_df=static_df,
n_windows=self.prediction_intervals.n_windows,
step_size=self.prediction_intervals.step_size,
id_col=id_col,
time_col=time_col,
target_col=target_col,
Expand Down
8 changes: 5 additions & 3 deletions neuralforecast/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ class PredictionIntervals:
def __init__(
self,
n_windows: int = 2,
step_size: int = 1,
method: str = "conformal_distribution",
):
"""
Expand All @@ -465,6 +466,8 @@ def __init__(
method : str, default is conformal_distribution
One of the supported methods for the computation of prediction intervals:
conformal_error or conformal_distribution
step_size : int, default is 1
Step size for the prediction intervals.
"""
if n_windows < 2:
raise ValueError(
Expand All @@ -475,11 +478,10 @@ def __init__(
raise ValueError(f"method must be one of {allowed_methods}")
self.n_windows = n_windows
self.method = method
self.step_size = step_size

def __repr__(self):
return (
f"PredictionIntervals(n_windows={self.n_windows}, method='{self.method}')"
)
return f"PredictionIntervals(n_windows={self.n_windows}, method='{self.method}', step_size={self.step_size})"

# %% ../nbs/utils.ipynb 32
def add_conformal_distribution_intervals(
Expand Down