Repository navigation
Open
Labels
Milestone
Description
sm.stats.zt_ind_solve_power returns an array if nobs1 is less than 197. I'm sure 197 is not special; it's just the result of the other settings in this specific case. The documentation says that a float is returned, not an array. If an array is returned, should it be assumed that there is no answer for the ratio?
es = 0.20
alpha = 0.05
power = 0.80
for n1 in range(1, 401, 10):
n1, sm.stats.zt_ind_solve_power(es, n1, alpha, power, ratio=None, alternative='two-sided')
(1, array([ 12926.]))
(11, array([ 7233.36610346]))
(21, array([ 14302.63047839]))
(31, array([ 294141.47907722]))
(41, array([ 424723.9626395]))
(51, array([ 653547.47554599]))
(61, array([ 1047796.24848788]))
(71, array([ 864465.36172393]))
(81, array([ 1457060.60164413]))
(91, array([ 1248130.6972059]))
(101, array([ 2165978.03108565]))
(111, array([ 1886292.83657938]))
(121, array([ 3004670.74975744]))
(131, array([ 4855495.1619719]))
(141, array([ 3969905.79322159]))
(151, array([ 6555037.82106867]))
(161, array([ 5454723.46003231]))
(171, array([ 9135790.240248]))
(181, array([ 15376233.87906924]))
(191, array([ 41926014.47320025]))
(201, 41.06351974728969)
(211, 13.277510022299376)
(221, 7.919027208755753)
(231, 5.642037224648649)
(241, 4.382048715544689)
(251, 3.5820930501426567)
(261, 3.029115166586838)
(271, 2.6240369376713617)
(281, 2.314520102141116)
(291, 2.070316800463505)
(301, 1.8727279560623094)
(311, 1.709566984615799)
(321, 1.5725585411403584)
(331, 1.4558815605038435)
(341, 1.3553226926244626)
(351, 1.267757015041292)
(361, 1.1908199627958316)
(371, 1.122686872917464)
(381, 1.0619267276423723)
(391, 1.0074094489317402)
Just for reference, with R using rpy2 pwr library, if I attempt this same thing, an error is thrown with the following:
>>> for i in range(2, 1000):
pwr.pwr_2p2n_test(h=0.20, n1=i, sig_level=alpha, power=power, alternative="two.sided")[2][0]
Traceback (most recent call last):
File "<pyshell#45>", line 2, in <module>
pwr.pwr_2p2n_test(h=0.20, n1=i, sig_level=alpha, power=power, alternative="two.sided")[2][0]
File "C:\Python35\lib\site-packages\rpy2\robjects\functions.py", line 178, in __call__
return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
File "C:\Python35\lib\site-packages\rpy2\robjects\functions.py", line 106, in __call__
res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in uniroot(function(n2) eval(p.body) - power, c(2 + 1e-10, 1e+09)) :
f() values at end points not of opposite sign
>>> es = 0.20
>>> for i in range(196, 1000):
pwr.pwr_2p2n_test(es, n1=i, sig_level=alpha, power=power, alternative="two.sided")[2][0]
Traceback (most recent call last):
File "<pyshell#48>", line 2, in <module>
pwr.pwr_2p2n_test(es, n1=i, sig_level=alpha, power=power, alternative="two.sided")[2][0]
File "C:\Python35\lib\site-packages\rpy2\robjects\functions.py", line 178, in __call__
return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
File "C:\Python35\lib\site-packages\rpy2\robjects\functions.py", line 106, in __call__
res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in uniroot(function(n2) eval(p.body) - power, c(2 + 1e-10, 1e+09)) :
f() values at end points not of opposite sign
>>> for i in range(197, 1000):
pwr.pwr_2p2n_test(es, n1=i, sig_level=alpha, power=power, alternative="two.sided")[2][0]
49654.8108846919
21845.45271925996
14053.719626671102
Activity
josef-pkt commentedon Oct 11, 2017
I need to check what's going on with these numbers the array numbers look too large.
What is your scipy version for this?
In general, we pay attention in some function, but overall we don't have enough checks about python scalar, numpy scalar or 1-element array.
In this case, I think the problem is that there are different rootfinders used below and above 200. The way it is written the root solving has several backups, i.e. if the first fails to find a solution, then we try a second and maybe a third solver. It could also be that the scipy rootfinders don't consistently return scalars.
running your example I get the same results, but it prints ConvergenceWarnings, e.g.
If I read the example correctly (without having thought about all the details yet):
There might or will be cases like this where a solution just doesn't exist. i.e. with only 1 observation in the first groups it is not possible to achieve the desired power and alpha, even with an infinite number of observations in the other group.
(I try to check the details later.)
jaradc commentedon Oct 11, 2017
Scipy Version:
scipy.version : 0.19.1
Python Version: 3.5 64-bit
Operating system: Windows 7 Professional
You know what, you're right. I did have
warnings.filterwarnings("ignore")set to ignore another warning (clearly not a good idea to do this I know :) ) so I didn't see the convergence warning. I uncommented that line and saw the same convergence warning as you.I saw on your blog that you mentioned root-finding in the power calculations is based on scipy.optimize. Would you venture to go out on a limb and say if the returned value is a float instance then it converged, else no solution is possible?
josef-pkt commentedon Oct 11, 2017
a quick check of the achievable power
computing power uses an explicit formula and doesn't need rootfinding
fixing ratio at 1 million (as something very large), and checking power for different nobs1:
Based on this we need at least nobs=197 to have a power=0.8. For smaller nobs1, there will be no ratio to achieve that power.
I think the numbers is your list up to 191, are just the large numbers where the rootfinder "gave up"
edit to add import, I'm using
from statsmodels.stats import power as pwranother check based on nobs1=197
So from this it looks like the results are as expected.
I didn't try to figure out where the array is created. (postponed to 0.10)
jaradc commentedon Oct 12, 2017
I looked at the source code and tracked it back to the Power class in /stats/power.py. I believe the array is being returned by
optimize.fsolve. I hope this helps.Also, I just want to clarify that there was nothing special about iteration 197; it was just the point where arrays stopped printing and floats started with those specific alphas, effect size, etc. I assume floats starting to print is synonymous with convergence starting to happen.
The take-away that I'm trying to convey is that the point at which an array is returned (did not converge) and a float is returned is dependent on the other settings of alpha, power, effect size, etc. And no-doubt you already know that.
This function is so useful! My intent of reaching out was just to figure out:
1a. if it did not converge, is it safe to assume it's either an infinite loop or there's no solution?
I don't need an answer, these questions are more rhetorical. I love this function, it's so valuable and useful!
josef-pkt commentedon Oct 12, 2017
just replying to two parts
No, all we can infer is that it uses a different code path, in this case rootfinder. Whether that one converged is independent of the return type.
If printing the warning is the only indication of non-convergence, then we might need to provide more information to the user.
Actually, I didn't know that there are impossible cases like this. For equivalence test I explicitly checked cases where no solution that satisfies the requirements exists.
Unfortunately not. Non-convergence is a strong indication that there is a problem with the "question", but all numerical optimizers and rootfinders can fail for many reasons. This was my initial motivation to layer several rootfinders in the hope that at least one works. But there is no guarantee that the solution is found even if one exists.
The "non-rhetorical" parts are
Issues and feedback like this is helpful for both.
Just as background:
I spent most of the time when implementing power on getting a good generic solution that we can use for many different cases. There is currently very little in terms of handling specific cases or exploiting computational shortcuts that apply in specific cases, or safeguards for special cases when the generic setting breaks down in specific cases.
On the other hand it covers many different cases without having to write much code for each of those cases.
For example. in this case:
I think we could compute the minimum nobs1 that is required to satisfy the requirements and for which a solution for
ratioor nobs2 exists. The extreme case when ratio and nobs2 is very large is when the second mean has zero sampling error.e.g. using the current implementation
solve for nobs1 given a large ratio, gives an answer close to 197 and similar in the example cases
This looks fine, and it even works, once we know what the specific problem and question are.