Standard high-performance numerical optimization strategies embody Brent MethodologyBrent’s methodology is a root-finding algorithm that mixes varied strategies akin to bisection, secant, and inverse quadratic interpolation. For extra details about its implementation in Statsmodels, see here.
In Python, the implementation appears to be like like this:
def solve_power(self, effect_size=None, nobs1=None, alpha=None, energy=None,
ratio=1., different='two-sided'):
print('--- Arguments: ---')
print('effect_size:', effect_size, 'nobs1:', nobs1, 'alpha:', alpha, 'energy:', energy, 'ratio:', ratio, 'different:', different, 'n')# Verify that solely nobs1 is None
kwds = dict(effect_size=effect_size, nobs1=nobs1, alpha=alpha,
energy=energy, ratio=ratio, different=different)
key = [k for k,v in kwds.items() if v is None]
assert(key == ['nobs1'])
# Verify that the effect_size shouldn't be 0
if kwds['effect_size'] == 0:
increase ValueError('Can't detect an effect-size of 0. Strive altering your effect-size.')
# Initialize the counter
self._counter = 0
# Outline the operate that we need to discover the basis of
# We need to discover nobs1 s.t. present energy = goal energy, i.e. present energy - goal energy = 0
# So func = present energy - goal energy
def func(x):
kwds['nobs1'] = x
target_power = kwds.pop('energy') # at all times the identical goal energy laid out in key phrases, e.g. 0.8
current_power = self.energy(**kwds) # present energy given the present nobs1, notice that self.energy doesn't have energy as an argument
kwds['power'] = target_power # add again energy to kwds
fval = current_power - target_power
print(f'Iteration {self._counter}: nobs1 = {x}, present energy - goal energy = {fval}')
self._counter += 1
return fval
# Get the beginning values for nobs1, given the brentq_expanding algorithm
# Within the authentic code, that is the self.start_bqexp dictionary arrange within the __init__ methodology
bqexp_fit_kwds = {'low': 2., 'start_upp': 50.}
# Remedy for nobs1 utilizing brentq_expanding
print('--- Fixing for optimum nobs1: ---')
val, _ = brentq_expanding(func, full_output=True, **bqexp_fit_kwds)
return val
1.2. Write a simplified model of tt_ind_solve_power that precisely implements the statistical derivation and produces the identical output as the unique operate
Statsmodels supply information can be found hereThe unique operate was written to be extra highly effective, however its generalization makes it tough to intuitively perceive how the code works.
So we went via the supply code line by line and simplified it from 1,600 traces to 160 traces, and from 10+ capabilities to 2, whereas guaranteeing that the implementation remained the identical.
The simplified code accommodates solely two capabilities underneath the TTestIndPower class, precisely following the statistical derivation described partially 1.
- EnergyCalculate statistical energy given pattern measurement
- Answer MeansThe Brent methodology will likely be used to find out the minimal pattern measurement to attain the goal energy.
Right here is the complete code for the simplified model, together with checks to verify if it produces the identical output as the unique operate.

