6
6
from collections .abc import Collection , Hashable
7
7
from dataclasses import dataclass
8
8
from types import MappingProxyType
9
- from typing import Generic , Literal , Protocol , TypeVar , cast , runtime_checkable , Callable
9
+ from typing import Callable , Generic , Literal , Protocol , TypeVar , cast , runtime_checkable
10
10
11
11
import numpy as np
12
12
import numpy .typing as npt
@@ -1000,21 +1000,23 @@ def pairwise_frame(scores: pd.Series[float]) -> pd.DataFrame:
1000
1000
1001
1001
@dataclass
1002
1002
class BootstrapConfidenceInterval :
1003
- score_method : Literal ['elo' , 'bradley-terry' , 'newman' ] # TODO: more?
1003
+ """Generate confidence interval by bootstrap."""
1004
+
1005
+ score_method : Literal ["elo" , "bradley-terry" , "newman" ] # TODO: more?
1004
1006
num_rounds : int = 100
1005
1007
sample_rate : float = 1.0
1006
1008
with_replace : bool = True
1007
1009
1008
1010
def fit (self ,
1009
1011
df : pd .DataFrame ,
1010
- left_column : str = ' left' ,
1011
- right_column : str = ' right' ,
1012
- winner_colum : str = ' winner' ,
1012
+ left_column : str = " left" ,
1013
+ right_column : str = " right" ,
1014
+ winner_colum : str = " winner" ,
1013
1015
weights : Collection [float ] | None = None ,
1014
1016
win_weight : float = 1.0 ,
1015
1017
tie_weight : float = 0.5 ,
1016
1018
solver : Literal ["naive" , "pyo3" ] = "pyo3" ,
1017
- ** kwargs ,
1019
+ ** kwargs : float , # TODO: change to Unpack?
1018
1020
) -> pd .DataFrame :
1019
1021
"""
1020
1022
Calculate confidence interval by bootstrap.
@@ -1032,15 +1034,15 @@ def fit(self,
1032
1034
1033
1035
Returns:
1034
1036
The dataframe with scores from all bootstrap rounds.
1035
- """
1036
1037
1038
+ """
1037
1039
score_function = self ._get_score_method ()
1038
1040
* _ , index = indexing (
1039
1041
xs = df [left_column ],
1040
1042
ys = df [right_column ],
1041
1043
)
1042
1044
1043
- bootstrap : list [" pd.Series[float]" ] = []
1045
+ bootstrap : list [pd .Series [float ]] = []
1044
1046
for r in range (self .num_rounds ):
1045
1047
df_sample = df .sample (frac = self .sample_rate , replace = self .with_replace , random_state = r )
1046
1048
result_sample = score_function (
@@ -1052,31 +1054,33 @@ def fit(self,
1052
1054
win_weight = win_weight ,
1053
1055
tie_weight = tie_weight ,
1054
1056
solver = solver ,
1055
- ** kwargs
1057
+ ** kwargs ,
1056
1058
)
1057
1059
1058
1060
bootstrap .append (result_sample .scores )
1059
1061
1060
- df_bootstrap = pd .DataFrame (bootstrap )
1061
1062
# TODO: calculate the quantiles in here?
1062
- return df_bootstrap
1063
+ return pd . DataFrame ( bootstrap )
1063
1064
1064
- def plot (self , df : pd .DataFrame ):
1065
- pass
1065
+ def plot (self , df : pd .DataFrame ) -> None :
1066
+ """Plot confidence interval by plotly."""
1067
+ raise NotImplementedError
1066
1068
1067
- def _get_score_method (self ) -> Callable [..., Generic [T ]]:
1069
+ def _get_score_method (self ) -> Callable [..., EloResult [ T ] | BradleyTerryResult [ T ] | NewmanResult [T ]]:
1068
1070
score_method_map = {
1069
- ' elo' : elo ,
1070
- ' bradley-terry' : bradley_terry ,
1071
- ' newman' : newman ,
1071
+ " elo" : elo ,
1072
+ " bradley-terry" : bradley_terry ,
1073
+ " newman" : newman ,
1072
1074
}
1073
1075
if (score_method := score_method_map .get (self .score_method )) is None :
1074
- ValueError (f"{ self .score_method = } , which is not supported!" )
1076
+ error_msg = f"Unsupported score method: { self .score_method } !"
1077
+ raise ValueError (error_msg )
1075
1078
return score_method
1076
1079
1077
1080
1078
1081
__all__ = [
1079
1082
"WINNERS" ,
1083
+ "BootstrapConfidenceInterval" ,
1080
1084
"BradleyTerryResult" ,
1081
1085
"CountingResult" ,
1082
1086
"EigenResult" ,
@@ -1100,5 +1104,4 @@ def _get_score_method(self) -> Callable[..., Generic[T]]:
1100
1104
"pagerank" ,
1101
1105
"pairwise_frame" ,
1102
1106
"pairwise_scores" ,
1103
- "BootstrapConfidenceInterval" ,
1104
1107
]
0 commit comments