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

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

from typing import Tuple, Union 

 

import numpy as np 

from pandas import DataFrame, Series 

from scipy import stats as st 

from scipy import optimize 

from statsmodels.stats.weightstats import _zconfint_generic, _zstat_generic 

 

from spotify_confidence.analysis.confidence_utils import power_calculation 

from spotify_confidence.analysis.constants import POINT_ESTIMATE, CI_LOWER, CI_UPPER, VARIANCE, TWO_SIDED, SFX2, SFX1, \ 

STD_ERR, PREFERENCE_TEST, NULL_HYPOTHESIS, DIFFERENCE, ALPHA, IS_SIGNIFICANT, HOLM, SPOT_1_HOLM, HOMMEL, \ 

SIMES_HOCHBERG, SPOT_1_HOMMEL, SPOT_1_SIMES_HOCHBERG, NIM, ADJUSTED_ALPHA, ADJUSTED_POWER, MDE,\ 

ALTERNATIVE_HYPOTHESIS 

from spotify_confidence.analysis.frequentist.sequential_bound_solver import bounds 

 

 

def sequential_bounds(t: np.array, alpha: float, sides: int): 

return bounds(t, alpha, rho=2, ztrun=8, sides=sides, max_nints=1000) 

 

 

class ZTestComputer(object): 

def __init__(self, numerator, numerator_sumsq, denominator, ordinal_group_column, interval_size): 

self._numerator = numerator 

self._numerator_sumsq = numerator_sumsq 

self._denominator = denominator 

self._ordinal_group_column = ordinal_group_column 

self._interval_size = interval_size 

 

def _point_estimate(self, row: Series) -> float: 

if row[self._denominator] == 0: 

raise ValueError('''Can't compute point estimate: denominator is 0''') 

return row[self._numerator] / row[self._denominator] 

 

def _variance(self, row: Series) -> float: 

variance = ( 

row[self._numerator_sumsq] / row[self._denominator] - 

row[POINT_ESTIMATE] ** 2) 

if variance < 0: 

raise ValueError('Computed variance is negative. ' 

'Please check your inputs.') 

return variance 

 

def _std_err(self, row: Series) -> float: 

return np.sqrt(row[VARIANCE + SFX1] / row[self._denominator + SFX1] + 

row[VARIANCE + SFX2] / row[self._denominator + SFX2]) 

 

def _add_point_estimate_ci(self, row: Series) -> Series: 

row[CI_LOWER], row[CI_UPPER] = _zconfint_generic( 

mean=row[POINT_ESTIMATE], 

std_mean=np.sqrt(row[VARIANCE] / row[self._denominator]), 

alpha=1-self._interval_size, 

alternative=TWO_SIDED 

) 

return row 

 

def _p_value(self, row: Series) -> float: 

_, p_value = _zstat_generic(value1=row[POINT_ESTIMATE + SFX2], 

value2=row[POINT_ESTIMATE + SFX1], 

std_diff=row[STD_ERR], 

alternative=row[PREFERENCE_TEST], 

diff=row[NULL_HYPOTHESIS]) 

return p_value 

 

def _ci(self, row: Series, alpha_column: str) -> Tuple[float, float]: 

return _zconfint_generic( 

mean=row[DIFFERENCE], 

std_mean=row[STD_ERR], 

alpha=row[alpha_column], 

alternative=row[PREFERENCE_TEST]) 

 

def _achieved_power(self, 

df: DataFrame, 

mde: float, 

alpha: float) -> DataFrame: 

v1, v2 = df[VARIANCE + SFX1], df[VARIANCE + SFX2] 

n1, n2 = df[self._denominator + SFX1], df[self._denominator + SFX2] 

 

var_pooled = ((n1 - 1) * v1 + (n2 - 1) * v2) / (n1 + n2 - 2) 

 

return power_calculation(mde, var_pooled, alpha, n1, n2) 

 

def _compute_sequential_adjusted_alpha(self, 

df: DataFrame, 

final_expected_sample_size_column: str, 

filtered_sufficient_statistics: DataFrame, 

num_comparisons: int): 

total_sample_size = ( 

filtered_sufficient_statistics.groupby(df.index.names) 

.agg({self._denominator: sum, final_expected_sample_size_column: np.mean}) 

.rename(columns={self._denominator: f'total_{self._denominator}'}) 

) 

groups_except_ordinal = [ 

column for column in df.index.names if column != self._ordinal_group_column] 

max_sample_size_by_group = ( 

total_sample_size[f'total_{self._denominator}'].max() if len(groups_except_ordinal) == 0 

else total_sample_size.groupby(groups_except_ordinal)[f'total_{self._denominator}'].max()) 

 

if type(max_sample_size_by_group) is not Series: 

total_sample_size = total_sample_size.assign(**{f'total_{self._denominator}_max': max_sample_size_by_group}) 

else: 

total_sample_size = total_sample_size.merge(right=max_sample_size_by_group, 

left_index=True, 

right_index=True, 

suffixes=('', '_max')) 

 

total_sample_size = ( 

total_sample_size 

.assign(final_expected_sample_size=lambda df: df[[f'total_{self._denominator}_max', 

final_expected_sample_size_column]].max(axis=1)) 

.assign( 

sample_size_proportions=lambda df: df['total_' + self._denominator]/df['final_expected_sample_size'] 

) 

) 

 

def adjusted_alphas_for_group(grp: DataFrame) -> Series: 

return ( 

sequential_bounds( 

t=grp['sample_size_proportions'].values, 

alpha=grp[ALPHA].values[0]/num_comparisons, 

sides=2 if (grp[PREFERENCE_TEST] == TWO_SIDED).all() else 1 

).df 

.set_index(grp.index) 

.assign(adjusted_alpha=lambda df: df.apply( 

lambda row: 2 * (1 - st.norm.cdf(row['zb'])) if (grp[PREFERENCE_TEST] == TWO_SIDED).all() 

else 1 - st.norm.cdf(row['zb']), axis=1)) 

)[['zb', 'adjusted_alpha']] 

 

return ( 

df.merge(total_sample_size, left_index=True, right_index=True) 

.groupby(groups_except_ordinal + ['level_1', 'level_2'])[['sample_size_proportions', 

PREFERENCE_TEST, 

ALPHA]] 

.apply(adjusted_alphas_for_group) 

.reset_index().set_index(df.index.names) 

)['adjusted_alpha'] 

 

def _ci_for_multiple_comparison_methods( 

self, 

df: DataFrame, 

correction_method: str, 

alpha: float, 

w: float = 1.0, 

) -> Tuple[Union[Series, float], Union[Series, float]]: 

if TWO_SIDED in df[PREFERENCE_TEST]: 

raise ValueError( 

"CIs can only be produced for one-sided tests when other multiple test corrections " 

"methods than bonferroni are applied" 

) 

m_scal = len(df) 

num_significant = sum(df[IS_SIGNIFICANT]) 

r = m_scal - num_significant 

 

def _aw(W: float, alpha: float, m_scal: float, r: int): 

return alpha * (1 - (1 - W) * (m_scal - r) / m_scal) 

 

def _bw(W: float, alpha: float, m_scal: float, r: int): 

return 1 - (1 - alpha) / np.power((1 - (1 - W) * (1 - np.power((1 - alpha), (1 / m_scal)))), (m_scal - r)) 

 

if correction_method in [HOLM, SPOT_1_HOLM]: 

adjusted_alpha_rej_equal_m = 1 - alpha / m_scal 

adjusted_alpha_rej_less_m = 1 - (1 - w) * (alpha / m_scal) 

adjusted_alpha_accept = 1 - _aw(w, alpha, m_scal, r) / r if r != 0 else 0 

elif correction_method in [HOMMEL, SIMES_HOCHBERG, SPOT_1_HOMMEL, SPOT_1_SIMES_HOCHBERG]: 

adjusted_alpha_rej_equal_m = np.power((1 - alpha), (1 / m_scal)) 

adjusted_alpha_rej_less_m = 1 - (1 - w) * (1 - np.power((1 - alpha), (1 / m_scal))) 

adjusted_alpha_accept = 1 - _bw(w, alpha, m_scal, r) / r if r != 0 else 0 

else: 

raise ValueError("CIs not supported for correction method. " 

f"Supported methods: {HOMMEL}, {HOLM}, {SIMES_HOCHBERG}," 

f"{SPOT_1_HOLM}, {SPOT_1_HOMMEL} and {SPOT_1_SIMES_HOCHBERG}") 

 

def _compute_ci_for_row(row: Series) -> Tuple[float, float]: 

if row[IS_SIGNIFICANT] and num_significant == m_scal: 

alpha_adj = adjusted_alpha_rej_equal_m 

elif row[IS_SIGNIFICANT] and num_significant < m_scal: 

alpha_adj = adjusted_alpha_rej_less_m 

else: 

alpha_adj = adjusted_alpha_accept 

 

ci_sign = -1 if row[PREFERENCE_TEST] == "larger" else 1 

bound1 = row[DIFFERENCE] + ci_sign * st.norm.ppf(alpha_adj) * row[STD_ERR] 

if ci_sign == -1: 

bound2 = max(row[NULL_HYPOTHESIS], bound1) 

else: 

bound2 = min(row[NULL_HYPOTHESIS], bound1) 

 

bound = bound2 if row[IS_SIGNIFICANT] else bound1 

 

lower = bound if row[PREFERENCE_TEST] == "larger" else -np.inf 

upper = bound if row[PREFERENCE_TEST] == "smaller" else np.inf 

 

return lower, upper 

 

return df.apply(_compute_ci_for_row, axis=1) 

 

def _powered_effect(self, 

df: Series, 

kappa: float, 

proportion_of_total: float, 

z_alpha: float, 

z_power: float, 

binary: bool, 

current_number_of_units: int, 

non_inferiority: bool 

) -> Series: 

 

 

if binary and not non_inferiority: 

effect = self._search_MDE_binary_local_search( 

control_avg=df[POINT_ESTIMATE + SFX1], 

control_var=df[VARIANCE + SFX1], 

non_inferiority=False, 

kappa=kappa, 

proportion_of_total=proportion_of_total, 

current_number_of_units=current_number_of_units, 

z_alpha=z_alpha, 

z_power=z_power, 

)[0] 

else: 

treatment_var = self._get_hypothetical_treatment_var( 

binary_metric=binary, non_inferiority=df[NIM] is not None, 

control_avg=df[POINT_ESTIMATE + SFX1], control_var=df[VARIANCE + SFX1], 

hypothetical_effect=0 

) 

n2_partial = np.power((z_alpha + z_power), 2) * ( 

df[VARIANCE + SFX1] / kappa + treatment_var) 

effect = np.sqrt((1 / (current_number_of_units * proportion_of_total)) * ( 

n2_partial + kappa * n2_partial)) 

 

return effect 

 

def _required_sample_size(self, 

binary: Union[Series, bool], 

non_inferiority: Union[Series, bool], 

hypothetical_effect: Union[Series, float], 

control_avg: Union[Series, float], 

control_var: Union[Series, float], 

z_alpha: float = None, 

kappa: float = None, 

proportion_of_total: Union[Series, float] = None, 

z_power: float = None, 

) -> Union[Series, float]: 

 

if kappa is None: 

raise ValueError('kappa is None, must be postive float') 

if proportion_of_total is None: 

raise ValueError('proportion_of_total is None, must be between 0 and 1') 

 

treatment_var = np.vectorize(self._get_hypothetical_treatment_var)( 

binary, non_inferiority, control_avg, control_var, hypothetical_effect 

) 

 

n2 = self._treatment_group_sample_size( 

z_alpha=z_alpha, 

z_power=z_power, 

hypothetical_effect=hypothetical_effect, 

control_var=control_var, 

treatment_var=treatment_var, 

kappa=kappa, 

) 

required_sample_size = np.ceil((n2 + n2 * kappa) / proportion_of_total) 

return required_sample_size 

 

 

 

def _powered_effect_and_required_sample_size(self, 

df: Series, 

) -> Series: 

proportion_of_total = 1 # TODO 

z_alpha = st.norm.ppf(1 - df[ADJUSTED_ALPHA]) 

z_power = st.norm.ppf(df[ADJUSTED_POWER]) 

n1, n2 = df[self._denominator + SFX1], df[self._denominator + SFX2] 

kappa = n2 / n1 

binary = df[self._numerator_sumsq + SFX1] == df[self._numerator + SFX1] 

current_number_of_units = n1 + n2 

 

if isinstance(df[NIM], float): 

non_inferiority = not np.isnan(df[NIM]) 

elif type(df[NIM]) is type(None): 

non_inferiority = df[NIM] is not None 

else: 

raise ValueError('NIM has to be type float or None.') 

 

 

df['powered_effect'] = self._powered_effect(df=df, 

kappa=kappa, 

proportion_of_total=proportion_of_total, 

z_alpha=z_alpha, 

z_power=z_power, 

binary=binary, 

current_number_of_units=current_number_of_units, 

non_inferiority=non_inferiority) 

 

 

if (ALTERNATIVE_HYPOTHESIS + SFX1) in df and NULL_HYPOTHESIS in df: 

df['required_sample_size'] = self._required_sample_size(proportion_of_total=proportion_of_total, 

z_alpha=z_alpha, 

z_power=z_power, 

binary=binary, 

non_inferiority=non_inferiority, 

hypothetical_effect = df[ALTERNATIVE_HYPOTHESIS + SFX1] - df[NULL_HYPOTHESIS], 

control_avg=df[POINT_ESTIMATE + SFX1], 

control_var=df[VARIANCE + SFX1], 

kappa=kappa 

) 

else: 

df['required_sample_size'] = None 

 

return df 

 

 

 

def _currently_powered_effect(self, 

control_avg: float, 

control_var: float, 

binary_metric: bool, 

non_inferiority: bool = False, 

power: float = None, 

alpha: float = None, 

kappa: float = None, 

proportion_of_total: float = None, 

current_number_of_units: float = None, 

): 

z_alpha = st.norm.ppf(1 - alpha) 

z_power = st.norm.ppf(power) 

 

if binary_metric and not non_inferiority: 

effect = self._search_MDE_binary_local_search( 

control_avg=control_avg, 

control_var=control_var, 

non_inferiority=non_inferiority, 

kappa=kappa, 

proportion_of_total=proportion_of_total, 

current_number_of_units=current_number_of_units, 

z_alpha=z_alpha, 

z_power=z_power, 

)[0] 

else: 

treatment_var = self._get_hypothetical_treatment_var( 

binary_metric, non_inferiority, control_avg, control_var, hypothetical_effect=0 

) 

n2_partial = np.power((z_alpha + z_power), 2) * ( 

control_var / kappa + treatment_var) 

effect = np.sqrt((1 / (current_number_of_units * proportion_of_total)) * ( 

n2_partial + kappa * n2_partial)) 

 

return effect 

 

def _search_MDE_binary_local_search(self, 

control_avg: float, 

control_var: float, 

non_inferiority: bool, 

kappa: float, 

proportion_of_total: float, 

current_number_of_units: float, 

z_alpha: float = None, 

z_power: float = None, 

): 

def f(x): 

return self.find_current_powered_effect( 

hypothetical_effect=x, 

control_avg=control_avg, 

control_var=control_var, 

binary=True, 

non_inferiority=non_inferiority, 

kappa=kappa, 

proportion_of_total=proportion_of_total, 

current_number_of_units=current_number_of_units, 

z_alpha=z_alpha, 

z_power=z_power, 

) 

 

max_val = 1 - control_avg 

min_val = min(10e-9, max_val) 

 

if min_val == max_val: 

# corner case that crashes the optimizer 

return min_val, f(min_val) 

 

max_iter = 100 # max number of iterations before falling back to slow grid search 

 

# we stop immediately if a solution was found that is "good enough". A threshold of 

# 1 indicates that 

# the approximated number of units (based on the current effect candidate) is off by 

# at most 1.0 

goodness_threshold = 1.0 

 

curr_iter = 0 

best_x = None 

best_fun = float("inf") 

 

bounds_queue = [(min_val, max_val)] 

 

while curr_iter < max_iter and best_fun > goodness_threshold: 

 

# take next value from queue 

interval = bounds_queue.pop(0) 

 

# conduct a bounded local search, using a very small tol value improved 

# performance during tests 

# result = optimize.minimize_scalar(f, bounds=(interval[0], interval[1]), 

# method='bounded', tol=10e-14) 

result = optimize.minimize_scalar( 

f, bounds=(interval[0], interval[1]), method="bounded", 

options={"xatol": 10e-14, "maxiter": 50} 

) 

 

if result.fun < best_fun: 

best_x = result.x 

best_fun = result.fun 

 

curr_iter += 1 

 

# add new bounds to the queue 

interval_split = (interval[0] + interval[1]) / 2 

bounds_queue.append((interval[0], interval_split)) 

bounds_queue.append((interval_split, interval[1])) 

 

if best_fun <= goodness_threshold: 

return best_x, best_fun 

else: # check if grid search finds a better solution 

alt_result_x, alt_result_fun = self.search_MDE_binary( 

control_avg, 

control_var, 

non_inferiority, 

kappa, 

proportion_of_total, 

current_number_of_units, 

z_alpha, 

z_power, 

return_cost_val=True, 

) 

 

return (alt_result_x, alt_result_fun) if alt_result_fun < best_fun else ( 

best_x, best_fun) 

 

def search_MDE_binary(self, 

control_avg: float, 

control_var: float, 

non_inferiority: bool, 

kappa: float, 

proportion_of_total: float, 

current_number_of_units: float, 

z_alpha: float = None, 

z_power: float = None, 

return_cost_val=False, 

): 

candidate_effects = np.linspace(10e-9, 1 - control_avg, num=2000) 

for i in range(2): 

test = [] 

for effect in candidate_effects: 

test.append( 

self.find_current_powered_effect( 

hypothetical_effect=effect, 

control_avg=control_avg, 

control_var=control_var, 

binary=True, 

non_inferiority=non_inferiority, 

kappa=kappa, 

proportion_of_total=proportion_of_total, 

current_number_of_units=current_number_of_units, 

z_alpha=z_alpha, 

z_power=z_power, 

) 

) 

 

test = np.array(test) 

index = [idx for idx, element in enumerate(test) if element == test.min()] 

if len(index) != 1: 

index = [index[int(np.ceil(len(index) / 2))]] 

if i == 0: 

if index[0] == 9999: 

return np.inf 

lower_effect_bound = 10e-9 if index[0] == 0 else candidate_effects[ 

index[0] - 1] 

candidate_effects = np.linspace(lower_effect_bound, 

candidate_effects[index[0]], num=10000) 

 

index = [idx for idx, element in enumerate(test) if element == test.min()] 

 

return candidate_effects[index[0]], test[index[0]] if return_cost_val else \ 

candidate_effects[index[0]] 

 

def _treatment_group_sample_size(self, 

z_alpha: float, z_power: float, hypothetical_effect: float, control_var: float, 

treatment_var: float, kappa: float, 

) -> float: 

return np.ceil(np.power((z_alpha + z_power) / abs(hypothetical_effect), 2) * ( 

control_var / kappa + treatment_var)) 

 

def find_current_powered_effect(self, 

hypothetical_effect: float, 

control_avg: float, 

control_var: float, 

binary: bool, 

non_inferiority: bool, 

kappa: float, 

proportion_of_total: float, 

current_number_of_units: float, 

z_power: float = None, 

z_alpha: float = None, 

) -> float: 

 

treatment_var = self._get_hypothetical_treatment_var( 

binary_metric=binary, non_inferiority=non_inferiority, control_avg=control_avg, 

control_var=control_var, hypothetical_effect=hypothetical_effect 

) 

n2 = self._treatment_group_sample_size(z_alpha, z_power, hypothetical_effect, 

control_var, 

treatment_var, kappa, ) 

 

return np.power( 

current_number_of_units - ((n2 + n2 * kappa) / proportion_of_total), 2) 

 

def _get_hypothetical_treatment_var(self, 

binary_metric: bool, non_inferiority: bool, control_avg: float, control_var: float, 

hypothetical_effect: float, 

) -> float: 

if binary_metric and not non_inferiority: 

# For binary metrics, the variance can be derived from the average. However, 

# we do *not* do this for 

# non-inferiority tests because for non-inferiority tests, the basic assumption 

# is that the 

# mean of the control group and treatment group are identical. 

return (control_avg + hypothetical_effect) * (1 - (control_avg + hypothetical_effect)) 

else: 

return control_var