Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

# Copyright 2017-2020 Spotify AB 

# 

# Licensed under the Apache License, Version 2.0 (the "License"); 

# you may not use this file except in compliance with the License. 

# You may obtain a copy of the License at 

# 

# http://www.apache.org/licenses/LICENSE-2.0 

# 

# Unless required by applicable law or agreed to in writing, software 

# distributed under the License is distributed on an "AS IS" BASIS, 

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

# See the License for the specific language governing permissions and 

# limitations under the License. 

 

from abc import ABC, abstractmethod 

from typing import (Union, Iterable, Tuple) 

 

from pandas import DataFrame 

 

from spotify_confidence.chartgrid import ChartGrid 

from .confidence_computer_abc import ConfidenceComputerABC 

from .confidence_grapher_abc import ConfidenceGrapherABC 

from ..constants import NIM_TYPE 

 

 

class ConfidenceABC(ABC): 

 

@property 

def _confidence_computer(self) -> ConfidenceComputerABC: 

return self._computer 

 

@_confidence_computer.setter 

def _confidence_computer(self, computer: ConfidenceComputerABC): 

self._computer = computer 

 

@property 

def _confidence_grapher(self) -> ConfidenceGrapherABC: 

return self._grapher 

 

@_confidence_grapher.setter 

def _confidence_grapher(self, grapher: ConfidenceGrapherABC): 

self._grapher = grapher 

 

@abstractmethod 

def __init__(self, 

data_frame: DataFrame, 

numerator_column: str, 

numerator_sum_squares_column: Union[str, None], 

denominator_column: str, 

categorical_group_columns: Union[str, Iterable], 

ordinal_group_column: Union[str, None], 

interval_size: float, 

correction_method: str): 

pass 

 

@abstractmethod 

def summary(self) -> DataFrame: 

""" 

Returns: 

Dataframe containing summary statistics 

""" 

pass 

 

@abstractmethod 

def difference(self, 

level_1: Union[str, Tuple], 

level_2: Union[str, Tuple], 

absolute: bool, 

groupby: Union[str, Iterable], 

non_inferiority_margins: NIM_TYPE, 

final_expected_sample_size: float 

) -> DataFrame: 

"""Args: 

groupby (str): Name of column. 

If specified, will plot a separate chart for each level of the 

grouping. 

non_inferiority_margins (Union[Tuple[float, str], 

Dict[str, Tuple[float, str]]]): 

Pass tuple(nim, preferred direction) to use the same NIM for all 

comparisons, e.g. (0.01, 'increase'), which means that we want 

level_2 to be grater than the average of level_1 times (1-0.01), 

or (0.05, 'decrease') which means that we want 

level_2 to be smaller than the average 

of level_1 times (1+0.01). 

Pass dictionary {{group:tuple(nim, preferred direction}} to use 

different non-inferiority margins for different values of 

groupby column. 

To performe a one-sided test without nim, use 

(None, preffered direction). 

final_expected_sample_size (float): Expected number of observations 

at end of experiment. 

Use in combination with ordinal groupby to perform a 

sequential test. See https://cran.r-project.org/web/packages/ldbounds/index.html for details. 

 

Returns: 

Dataframe containing the difference in means between 

group 1 and 2, p-values and confidence intervals for each value 

in the groupby column 

""" 

pass 

 

@abstractmethod 

def multiple_difference(self, 

level: Union[str, Tuple], 

absolute: bool, 

groupby: Union[str, Iterable], 

level_as_reference: bool, 

non_inferiority_margins: NIM_TYPE, 

final_expected_sample_size: float 

) -> DataFrame: 

"""Args: 

groupby (str): Name of column. 

If specified, will plot a separate chart for each level of the 

grouping. 

level_as_reference (bool): 

If false (default), compare level to all other 

groups. If true, compare all other groups to level. 

non_inferiority_margins (Union[Tuple[float, str], 

Dict[str, Tuple[float, str]]]): 

Pass tuple(nim, preferred direction) to use the same NIM for all 

comparisons, e.g. (0.01, 'increase'), which means that we want 

level_2 to be grater than the average of level_1 times (1-0.01), 

or (0.05, 'decrease') which means that we want 

level_2 to be smaller than the average 

of level_1 times (1+0.01). 

Pass dictionary {{group:tuple(nim, preferred direction}} to use 

different non-inferiority margins for different values of 

groupby column. 

To performe a one-sided test without nim, use 

(None, preffered direction). 

final_expected_sample_size (float): Expected number of observations 

at end of experiment. 

Use in combination with ordinal groupby to perform a 

sequential test. See https://cran.r-project.org/web/packages/ldbounds/index.html for details. 

 

Returns: 

Dataframe containing the difference in means between 

group 1 and 2, p-values and confidence intervals for each value 

in the groupby column 

""" 

pass 

 

@abstractmethod 

def summary_plot(self, 

groupby: Union[str, Iterable]) -> ChartGrid: 

"""Plot for each group in the data_frame: 

 

if ordinal level exists: 

line graph with area to represent confidence interval 

if categorical levels: 

Interval plots of confidence intervals by group 

 

Args: 

groupby (str): Name of column. 

If specified, will plot a separate chart for each level of the 

grouping. 

 

Returns: 

ChartGrid object and a DataFrame with numerical results. 

""" 

pass 

 

@abstractmethod 

def difference_plot(self, 

level_1: Union[str, Tuple], 

level_2: Union[str, Tuple], 

absolute: bool, 

groupby: Union[str, Iterable], 

non_inferiority_margins: NIM_TYPE, 

use_adjusted_intervals: bool, 

final_expected_sample_size: float 

) -> ChartGrid: 

"""Plot representing the difference between group 1 and 2. 

- Difference in means or proportions, depending 

on the response variable type. 

 

- Plot interval plot with confidence interval of the 

difference between groups 

 

Args: 

level_1 (str, tuple of str): Name of first level. 

level_2 (str, tuple of str): Name of second level. 

absolute (bool): If True then return the absolute 

difference (level2 - level1) 

otherwise return the relative difference (level2 / level1 - 1) 

groupby (str): Name of column, or list of columns. 

If specified, will return an interval for each level 

of the grouped dimension, or a confidence band if the 

grouped dimension is ordinal 

non_inferiority_margins (Union[Tuple[float, str], 

Dict[str, Tuple[float, str]]]): 

Pass tuple(nim, preferred direction) to use the same NIM for all 

comparisons, e.g. (0.01, 'increase'), which means that we want 

level_2 to be grater than the average of level_1 times (1-0.01), 

or (0.05, 'decrease') which means that we want 

level_2 to be smaller than the average 

of level_1 times (1+0.01). 

Pass dictionary {{group:tuple(nim, preferred direction}} to use 

different non-inferiority margins for different values of 

groupby column. 

To performe a one-sided test without nim, use 

(None, preffered direction). 

use_adjusted_intervals (bool): 

If true, use e.g. bon-ferroni corrected 

(or other method provided) confidence intervals 

final_expected_sample_size (float): Expected number of observations 

at end of experiment. 

Use in combination with ordinal groupby to perform a 

sequential test. See https://cran.r-project.org/web/packages/ldbounds/index.html for details. 

 

Returns: 

Chartify Chart object and a DataFrame with numerical results. 

""" 

 

@abstractmethod 

def multiple_difference_plot(self, 

level: Union[str, Tuple], 

absolute: bool, 

groupby: Union[str, Iterable], 

level_as_reference: bool, 

non_inferiority_margins: NIM_TYPE, 

use_adjusted_intervals: bool, 

final_expected_sample_size: float 

) -> ChartGrid: 

"""Compare level to all other groups or, if level_as_reference = True, 

all other groups to level. 

 

Args: 

level (str, tuple of str): Name of level. 

absolute (bool): If True then return the absolute 

difference (level2 - level1) 

otherwise return the relative difference (level2 / level1 - 1) 

groupby (str): Name of column, or list of columns. 

If specified, will return an interval for each level 

of the grouped dimension, or a confidence band if the 

grouped dimension is ordinal 

level_as_reference: If false (default), compare level to all other 

groups. If true, compare all other groups to level. 

non_inferiority_margins (Union[Tuple[float, str], 

Dict[str, Tuple[float, str]]]): 

Pass tuple(nim, preferred direction) to use the same NIM for all 

comparisons, e.g. (0.01, 'increase'), which means that we want 

level_2 to be grater than the average of level_1 times (1-0.01), 

or (0.05, 'decrease') which means that we want 

level_2 to be smaller than the average 

of level_1 times (1+0.01). 

Pass dictionary {{group:tuple(nim, preferred direction}} to use 

different non-inferiority margins for different values of 

groupby column. 

To performe a one-sided test without nim, use 

(None, preffered direction). 

use_adjusted_intervals (bool): 

If true, use e.g. bon-ferroni corrected 

(or other method provided) confidence intervals 

final_expected_sample_size (float): Expected number of observations 

at end of experiment. 

Use in combination with ordinal groupby to perform a 

sequential test. See https://cran.r-project.org/web/packages/ldbounds/index.html for details. 

 

Returns: 

ChartGrid object and a DataFrame with numerical results. 

"""