Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Level domain protection #169

Merged
merged 4 commits into from
Jan 23, 2023
Merged
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
10 changes: 5 additions & 5 deletions hierarchicalforecast/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _reverse_engineer_sigmah(Y_hat_df, y_hat, model_name):
pi_col = pi_model_name[0]
sign = -1 if 'lo' in pi_col else 1
level_col = re.findall('[\d]+[.,\d]+|[\d]*[.][\d]+|[\d]+', pi_col)
level_col = float(level_col[0])
level_col = float(level_col[-1])
z = norm.ppf(0.5 + level_col / 200)
sigmah = Y_hat_df[pi_col].values.reshape(n_series,-1)
sigmah = sign * (sigmah - y_hat) / z
Expand Down Expand Up @@ -127,9 +127,9 @@ def _prepare_fit(self,

# Protect level list
if (level is not None):
level_outside_domain = np.any((np.array(level) <= 0)|(np.array(level) > 100 ))
level_outside_domain = np.any((np.array(level) < 0)|(np.array(level) >= 100 ))
if level_outside_domain and (intervals_method in ['normality', 'permbu']):
raise Exception('Level outside domain, send `level` list in (0,100]')
raise Exception('Level outside domain, send `level` list in [0,100)')

# Declare output names
drop_cols = ['ds', 'y'] if 'y' in Y_hat_df.columns else ['ds']
Expand Down Expand Up @@ -193,7 +193,7 @@ def reconcile(self,
If a class of `self.reconciles` receives `y_hat_insample`, `Y_df` must include them as columns.<br>
`S`: pd.DataFrame with summing matrix of size `(base, bottom)`, see [aggregate method](https://nixtla.github.io/hierarchicalforecast/utils.html#aggregate).<br>
`tags`: Each key is a level and its value contains tags associated to that level.<br>
`level`: positive float list (0-100], confidence levels for prediction intervals.<br>
`level`: positive float list [0,100), confidence levels for prediction intervals.<br>
`intervals_method`: str, method used to calculate prediction intervals, one of `normality`, `bootstrap`, `permbu`.<br>
`num_samples`: int=-1, if positive return that many probabilistic coherent samples.
`seed`: int=0, random seed for numpy generator's replicability.<br>
Expand Down Expand Up @@ -316,7 +316,7 @@ def bootstrap_reconcile(self,
If a class of `self.reconciles` receives `y_hat_insample`, `Y_df` must include them as columns.<br>
`S`: pd.DataFrame with summing matrix of size `(base, bottom)`, see [aggregate method](https://nixtla.github.io/hierarchicalforecast/utils.html#aggregate).<br>
`tags`: Each key is a level and its value contains tags associated to that level.<br>
`level`: float list 0-100, confidence levels for prediction intervals.<br>
`level`: positive float list [0,100), confidence levels for prediction intervals.<br>
`intervals_method`: str, method used to calculate prediction intervals, one of `normality`, `bootstrap`, `permbu`.<br>
`num_samples`: int=-1, if positive return that many probabilistic coherent samples.
`num_seeds`: int=1, random seed for numpy generator's replicability.<br>
Expand Down
12 changes: 6 additions & 6 deletions nbs/core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
" pi_col = pi_model_name[0]\n",
" sign = -1 if 'lo' in pi_col else 1\n",
" level_col = re.findall('[\\d]+[.,\\d]+|[\\d]*[.][\\d]+|[\\d]+', pi_col)\n",
" level_col = float(level_col[0])\n",
" level_col = float(level_col[-1])\n",
" z = norm.ppf(0.5 + level_col / 200)\n",
" sigmah = Y_hat_df[pi_col].values.reshape(n_series,-1)\n",
" sigmah = sign * (sigmah - y_hat) / z\n",
Expand Down Expand Up @@ -227,9 +227,9 @@
" \n",
" # Protect level list\n",
" if (level is not None):\n",
" level_outside_domain = np.any((np.array(level) <= 0)|(np.array(level) > 100 ))\n",
" level_outside_domain = np.any((np.array(level) < 0)|(np.array(level) >= 100 ))\n",
" if level_outside_domain and (intervals_method in ['normality', 'permbu']):\n",
" raise Exception('Level outside domain, send `level` list in (0,100]')\n",
" raise Exception('Level outside domain, send `level` list in [0,100)')\n",
"\n",
" # Declare output names\n",
" drop_cols = ['ds', 'y'] if 'y' in Y_hat_df.columns else ['ds']\n",
Expand Down Expand Up @@ -293,7 +293,7 @@
" If a class of `self.reconciles` receives `y_hat_insample`, `Y_df` must include them as columns.<br>\n",
" `S`: pd.DataFrame with summing matrix of size `(base, bottom)`, see [aggregate method](https://nixtla.github.io/hierarchicalforecast/utils.html#aggregate).<br>\n",
" `tags`: Each key is a level and its value contains tags associated to that level.<br>\n",
" `level`: positive float list (0-100], confidence levels for prediction intervals.<br>\n",
" `level`: positive float list [0,100), confidence levels for prediction intervals.<br>\n",
" `intervals_method`: str, method used to calculate prediction intervals, one of `normality`, `bootstrap`, `permbu`.<br>\n",
" `num_samples`: int=-1, if positive return that many probabilistic coherent samples.\n",
" `seed`: int=0, random seed for numpy generator's replicability.<br>\n",
Expand Down Expand Up @@ -416,7 +416,7 @@
" If a class of `self.reconciles` receives `y_hat_insample`, `Y_df` must include them as columns.<br>\n",
" `S`: pd.DataFrame with summing matrix of size `(base, bottom)`, see [aggregate method](https://nixtla.github.io/hierarchicalforecast/utils.html#aggregate).<br>\n",
" `tags`: Each key is a level and its value contains tags associated to that level.<br>\n",
" `level`: float list 0-100, confidence levels for prediction intervals.<br>\n",
" `level`: positive float list [0,100), confidence levels for prediction intervals.<br>\n",
" `intervals_method`: str, method used to calculate prediction intervals, one of `normality`, `bootstrap`, `permbu`.<br>\n",
" `num_samples`: int=-1, if positive return that many probabilistic coherent samples.\n",
" `num_seeds`: int=1, random seed for numpy generator's replicability.<br>\n",
Expand Down Expand Up @@ -930,7 +930,7 @@
"test_fail(\n",
" hrec.reconcile,\n",
" contains='Level outside domain',\n",
" args=(hier_grouped_hat_df, S_grouped_df, tags_grouped, hier_grouped_df, [0, 80, 90], 'permbu',)\n",
" args=(hier_grouped_hat_df, S_grouped_df, tags_grouped, hier_grouped_df, [-1, 80, 90], 'permbu',)\n",
")\n",
"test_fail(\n",
" hrec.reconcile,\n",
Expand Down