From 1183fbd225cf40f72879dd30448f3795f535a560 Mon Sep 17 00:00:00 2001 From: syropiatovvv Date: Fri, 19 Sep 2025 01:16:30 +0300 Subject: [PATCH] =?UTF-8?q?=D1=83=D0=BB=D1=83=D1=87=D1=88=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20=D0=B0=D0=BB=D0=B3=D0=BE=D1=80=D0=B8=D1=82=D0=BC=20sug?= =?UTF-8?q?gest=5Fbins=5Fnum?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- iis_project/plotting_utils.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/iis_project/plotting_utils.py b/iis_project/plotting_utils.py index 6ca77a5..c00d264 100644 --- a/iis_project/plotting_utils.py +++ b/iis_project/plotting_utils.py @@ -1,4 +1,22 @@ -from math import ceil, log +from math import ceil +from typing import Optional -def suggest_bins_num(n: int) -> int: - return max(int(ceil(log(n))), 10) +from scipy.stats import norm + +SND_QUARTILE: float = 0.674490 + +def suggest_iqr_to_range_to_suggest_bins_num(n: int) -> float: + p = 1 / (n + 1) + return (SND_QUARTILE / norm.ppf(1 - p/2)) + +def suggest_bins_num(n: int, iqr_to_range: Optional[float] = None, scale: float = 1.) -> int: + if n <= 0: + raise ValueError(f'n should be >= 1, got {n}') + if (iqr_to_range is not None) and (iqr_to_range <= 0.): + raise ValueError(f'iqr_to_range should be > 0 or None, got {iqr_to_range}') + if scale <= 0.: + raise ValueError(f'scale should be > 0, got {scale}') + if iqr_to_range is None: + iqr_to_range = suggest_iqr_to_range_to_suggest_bins_num(n) + t = 0.5 * scale * (1. / iqr_to_range) * (n ** (1. / 3)) + return int(ceil(t))