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

ENH: avoid weird-looking float approximations when computing alpha(s). Clarify code. #603

Merged
merged 1 commit into from
Jan 22, 2025
Merged
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
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
Loading