Skip to content

Commit

Permalink
ENH: avoid weird-looking float approximations when computing alpha(s)…
Browse files Browse the repository at this point in the history
…. Clarify code.
  • Loading branch information
Valentin-Laurent committed Jan 22, 2025
1 parent 7f229b5 commit 7fecc86
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions mapie_v1/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@
from numpy import array
from mapie._typing import ArrayLike, NDArray
from sklearn.model_selection import BaseCrossValidator
from decimal import Decimal


def transform_confidence_level_to_alpha_list(
confidence_level: Union[float, List[float]]
) -> List[float]:
if isinstance(confidence_level, float):
confidence_levels = [confidence_level]
else:
if isinstance(confidence_level, list):
confidence_levels = confidence_level
return [1 - level for level in confidence_levels]
else:
confidence_levels = [confidence_level]

# Using decimals to avoid weird-looking float approximations
# when computing alpha = 1 - confidence_level
# Such approximations arise even with simple confidence levels like 0.9
confidence_levels_d = [Decimal(str(conf_level)) for conf_level in confidence_levels]
alphas_d = [Decimal("1") - conf_level_d for conf_level_d in confidence_levels_d]

return [float(alpha_d) for alpha_d in alphas_d]


def check_if_param_in_allowed_values(
Expand Down

0 comments on commit 7fecc86

Please sign in to comment.